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