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