Merge branch 'develop' into remoting-4.0
[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.cache.CdmServiceCacher;
19 import eu.etaxonomy.cdm.api.service.ICommonService;
20 import eu.etaxonomy.cdm.api.service.IService;
21 import eu.etaxonomy.cdm.api.service.ITestService;
22 import eu.etaxonomy.cdm.io.service.IIOService;
23 import eu.etaxonomy.taxeditor.service.ICachedCommonService;
24
25 /**
26 * @author cmathew
27 * @date 17 Jun 2015
28 *
29 */
30 public class CdmApplicationState {
31
32 private static CdmApplicationState cdmApplicationState;
33
34 private ICdmApplicationConfiguration appConfig;
35
36 private ICdmDataChangeService dataChangeService;
37
38 private SecurityContext securityContext;
39
40 private static CdmServiceCacher cdmServiceCacher;
41
42
43 public static CdmApplicationState getInstance() {
44 if(cdmApplicationState == null) {
45 cdmApplicationState = new CdmApplicationState();
46 cdmServiceCacher = new CdmServiceCacher();
47 }
48
49 return cdmApplicationState;
50 }
51
52 public void setAppConfig(ICdmApplicationConfiguration appConfig) {
53 this.appConfig = appConfig;
54 }
55
56 public ICdmApplicationConfiguration getAppConfig() {
57 return appConfig;
58 }
59
60 public static void setCurrentAppConfig(ICdmApplicationConfiguration appConfig) {
61 getInstance().setAppConfig(appConfig);
62 }
63
64 public static ICdmApplicationConfiguration getCurrentAppConfig() {
65 return getInstance().getAppConfig();
66 }
67
68 /**
69 * @return the dataChangeService
70 */
71 public ICdmDataChangeService getDataChangeService() {
72 return dataChangeService;
73 }
74
75 /**
76 * @param dataChangeService the dataChangeService to set
77 */
78 public void setDataChangeService(ICdmDataChangeService dataChangeService) {
79 this.dataChangeService = dataChangeService;
80 }
81
82 public static ICdmDataChangeService getCurrentDataChangeService() {
83 return getInstance().getDataChangeService();
84 }
85
86 public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService) {
87 getInstance().setDataChangeService(dataChangeService);
88 }
89
90
91
92 /**
93 * @return the securityContext
94 */
95 public SecurityContext getSecurityContext() {
96 return securityContext;
97 }
98
99 /**
100 * @param securityContext the securityContext to set
101 */
102 public void setSecurityContext(SecurityContext securityContext) {
103 this.securityContext = securityContext;
104 }
105
106 /**
107 * @return the securityContext
108 */
109 public static SecurityContext getCurrentSecurityContext() {
110 return getInstance().getSecurityContext();
111 }
112
113 /**
114 * @param securityContext the securityContext to set
115 */
116 public static void setCurrentSecurityContext(SecurityContext securityContext) {
117 getInstance().setSecurityContext(securityContext);
118 }
119
120 public static void dispose() {
121 getInstance().setCurrentDataChangeService(null);
122 getInstance().setAppConfig(null);
123 getInstance().setSecurityContext(null);
124 cdmApplicationState = null;
125 cdmServiceCacher = null;
126 }
127
128
129 /**
130 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
131 * interface. If a matching getter is found the according service implementation is returned by
132 * invoking the getter otherwise the method returns <code>null</code>.
133 *
134 * @param <T>
135 * @param serviceClass
136 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
137 * @throws CdmApplicationException
138 */
139 public static <T extends IService> T getService(Class<T> serviceClass) throws CdmApplicationException {
140 ICdmApplicationConfiguration configuration = getCurrentAppConfig();
141
142 Method[] methods = ICdmApplicationConfiguration.class.getDeclaredMethods();
143
144 T service = null;
145
146 for (Method method : methods) {
147 Type type = method.getGenericReturnType();
148
149 if (type.equals(serviceClass)) {
150 try {
151 service = (T) method.invoke(configuration, null);
152 break;
153 } catch (IllegalArgumentException iae) {
154 throw new CdmApplicationException(iae);
155 } catch (IllegalAccessException iae) {
156 throw new CdmApplicationException(iae);
157 } catch (InvocationTargetException ite) {
158 throw new CdmApplicationException(ite);
159 }
160 }
161 }
162
163 return service;
164 }
165
166
167 /**
168 * @see #getService(Class)
169 * As ICommonService is not extending IService we need a specific request here
170 */
171 public static ICommonService getCommonService() {
172 ICdmApplicationConfiguration configuration = getCurrentAppConfig();
173
174 return configuration.getCommonService();
175
176 }
177
178 public static IIOService getIOService() {
179 ICdmApplicationConfiguration configuration = getCurrentAppConfig();
180
181 return ((CdmApplicationRemoteController)configuration).getIOService();
182
183 }
184
185
186 public static ITestService getTestService() {
187 ICdmApplicationConfiguration configuration = getCurrentAppConfig();
188
189 return ((CdmApplicationRemoteController)configuration).getTestService();
190
191 }
192
193 public static ICachedCommonService getCachedCommonService() {
194 ICdmApplicationConfiguration configuration = getCurrentAppConfig();
195
196 return ((CdmApplicationRemoteController)configuration).getCachedCommonService();
197
198 }
199
200 public static CdmServiceCacher getCdmServiceCacher() {
201 return cdmServiceCacher;
202 }
203
204
205 }