41f33d8196fd7a420fb9933f4897179e99a3620d
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / cdm / api / application / CdmApplicationState.java
1 // $Id$
2 /**
3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.api.application;
11
12 import java.lang.reflect.InvocationTargetException;
13 import java.lang.reflect.Method;
14 import java.lang.reflect.Type;
15
16 import org.springframework.security.core.context.SecurityContext;
17
18 import eu.etaxonomy.cdm.api.service.ICommonService;
19 import eu.etaxonomy.cdm.api.service.IService;
20 import eu.etaxonomy.cdm.api.service.ITestService;
21 import eu.etaxonomy.taxeditor.service.ICachedCommonService;
22
23 /**
24 * @author cmathew
25 * @date 17 Jun 2015
26 *
27 */
28 public class CdmApplicationState {
29
30 private static CdmApplicationState cdmApplicationState;
31
32 private ICdmApplicationConfiguration appConfig;
33
34 private ICdmDataChangeService dataChangeService;
35
36 private SecurityContext securityContext;
37
38
39 public static CdmApplicationState getInstance() {
40 if(cdmApplicationState == null) {
41 cdmApplicationState = new CdmApplicationState();
42 }
43 return cdmApplicationState;
44 }
45
46 public void setAppConfig(ICdmApplicationConfiguration appConfig) {
47 this.appConfig = appConfig;
48 }
49
50 public ICdmApplicationConfiguration getAppConfig() {
51 return appConfig;
52 }
53
54 public static void setCurrentAppConfig(ICdmApplicationConfiguration appConfig) {
55 getInstance().setAppConfig(appConfig);
56 }
57
58 public static ICdmApplicationConfiguration getCurrentAppConfig() {
59 return getInstance().getAppConfig();
60 }
61
62 /**
63 * @return the dataChangeService
64 */
65 public ICdmDataChangeService getDataChangeService() {
66 return dataChangeService;
67 }
68
69 /**
70 * @param dataChangeService the dataChangeService to set
71 */
72 public void setDataChangeService(ICdmDataChangeService dataChangeService) {
73 this.dataChangeService = dataChangeService;
74 }
75
76 public static ICdmDataChangeService getCurrentDataChangeService() {
77 return getInstance().getDataChangeService();
78 }
79
80 public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService) {
81 getInstance().setDataChangeService(dataChangeService);
82 }
83
84
85
86 /**
87 * @return the securityContext
88 */
89 public SecurityContext getSecurityContext() {
90 return securityContext;
91 }
92
93 /**
94 * @param securityContext the securityContext to set
95 */
96 public void setSecurityContext(SecurityContext securityContext) {
97 this.securityContext = securityContext;
98 }
99
100 /**
101 * @return the securityContext
102 */
103 public static SecurityContext getCurrentSecurityContext() {
104 return getInstance().getSecurityContext();
105 }
106
107 /**
108 * @param securityContext the securityContext to set
109 */
110 public static void setCurrentSecurityContext(SecurityContext securityContext) {
111 getInstance().setSecurityContext(securityContext);
112 }
113
114 public static void dispose() {
115 getInstance().setCurrentDataChangeService(null);
116 getInstance().setAppConfig(null);
117 getInstance().setSecurityContext(null);
118 }
119
120
121 /**
122 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
123 * interface. If a matching getter is found the according service implementation is returned by
124 * invoking the getter otherwise the method returns <code>null</code>.
125 *
126 * @param <T>
127 * @param serviceClass
128 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
129 * @throws CdmApplicationException
130 */
131 public static <T extends IService> T getService(Class<T> serviceClass) throws CdmApplicationException {
132 ICdmApplicationConfiguration configuration = getCurrentAppConfig();
133
134 Method[] methods = ICdmApplicationConfiguration.class.getDeclaredMethods();
135
136 T service = null;
137
138 for (Method method : methods) {
139 Type type = method.getGenericReturnType();
140
141 if (type.equals(serviceClass)) {
142 try {
143 service = (T) method.invoke(configuration, null);
144 break;
145 } catch (IllegalArgumentException iae) {
146 throw new CdmApplicationException(iae);
147 } catch (IllegalAccessException iae) {
148 throw new CdmApplicationException(iae);
149 } catch (InvocationTargetException ite) {
150 throw new CdmApplicationException(ite);
151 }
152 }
153 }
154
155 return service;
156 }
157
158
159 /**
160 * @see #getService(Class)
161 * As ICommonService is not extending IService we need a specific request here
162 */
163 public static ICommonService getCommonService() {
164 ICdmApplicationConfiguration configuration = getCurrentAppConfig();
165
166 return configuration.getCommonService();
167
168 }
169
170 public static ITestService getTestService() {
171 ICdmApplicationConfiguration configuration = getCurrentAppConfig();
172
173 return ((CdmApplicationRemoteController)configuration).getTestService();
174
175 }
176
177 public static ICachedCommonService getCachedCommonService() {
178 ICdmApplicationConfiguration configuration = getCurrentAppConfig();
179
180 return ((CdmApplicationRemoteController)configuration).getCachedCommonService();
181
182 }
183
184 }