3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.api
.application
;
12 import java
.lang
.reflect
.InvocationTargetException
;
13 import java
.lang
.reflect
.Method
;
14 import java
.lang
.reflect
.Type
;
16 import org
.springframework
.security
.core
.context
.SecurityContext
;
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
;
30 public class CdmApplicationState
{
32 private static CdmApplicationState cdmApplicationState
;
34 private ICdmApplicationConfiguration appConfig
;
36 private ICdmDataChangeService dataChangeService
;
38 private SecurityContext securityContext
;
40 private static CdmServiceCacher cdmServiceCacher
;
43 public static CdmApplicationState
getInstance() {
44 if(cdmApplicationState
== null) {
45 cdmApplicationState
= new CdmApplicationState();
48 return cdmApplicationState
;
51 public void setAppConfig(ICdmApplicationConfiguration appConfig
) {
52 this.appConfig
= appConfig
;
55 public ICdmApplicationConfiguration
getAppConfig() {
59 public static void setCurrentAppConfig(ICdmApplicationConfiguration appConfig
) {
60 getInstance().setAppConfig(appConfig
);
63 public static ICdmApplicationConfiguration
getCurrentAppConfig() {
64 return getInstance().getAppConfig();
68 * @return the dataChangeService
70 public ICdmDataChangeService
getDataChangeService() {
71 return dataChangeService
;
75 * @param dataChangeService the dataChangeService to set
77 public void setDataChangeService(ICdmDataChangeService dataChangeService
) {
78 this.dataChangeService
= dataChangeService
;
81 public static ICdmDataChangeService
getCurrentDataChangeService() {
82 return getInstance().getDataChangeService();
85 public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService
) {
86 getInstance().setDataChangeService(dataChangeService
);
92 * @return the securityContext
94 public SecurityContext
getSecurityContext() {
95 return securityContext
;
99 * @param securityContext the securityContext to set
101 public void setSecurityContext(SecurityContext securityContext
) {
102 this.securityContext
= securityContext
;
106 * @return the securityContext
108 public static SecurityContext
getCurrentSecurityContext() {
109 return getInstance().getSecurityContext();
113 * @param securityContext the securityContext to set
115 public static void setCurrentSecurityContext(SecurityContext securityContext
) {
116 getInstance().setSecurityContext(securityContext
);
119 public static void dispose() {
120 getInstance().setCurrentDataChangeService(null);
121 getInstance().setAppConfig(null);
122 getInstance().setSecurityContext(null);
123 cdmApplicationState
= null;
124 cdmServiceCacher
= null;
129 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
130 * interface. If a matching getter is found the according service implementation is returned by
131 * invoking the getter otherwise the method returns <code>null</code>.
134 * @param serviceClass
135 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
136 * @throws CdmApplicationException
138 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) throws CdmApplicationException
{
139 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
141 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
145 for (Method method
: methods
) {
146 Type type
= method
.getGenericReturnType();
148 if (type
.equals(serviceClass
)) {
150 service
= (T
) method
.invoke(configuration
, null);
152 } catch (IllegalArgumentException iae
) {
153 throw new CdmApplicationException(iae
);
154 } catch (IllegalAccessException iae
) {
155 throw new CdmApplicationException(iae
);
156 } catch (InvocationTargetException ite
) {
157 throw new CdmApplicationException(ite
);
167 * @see #getService(Class)
168 * As ICommonService is not extending IService we need a specific request here
170 public static ICommonService
getCommonService() {
171 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
173 return configuration
.getCommonService();
177 public static IIOService
getIOService() {
178 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
180 return ((CdmApplicationRemoteController
)configuration
).getIOService();
185 public static ITestService
getTestService() {
186 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
188 return ((CdmApplicationRemoteController
)configuration
).getTestService();
192 public static ICachedCommonService
getCachedCommonService() {
193 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
195 return ((CdmApplicationRemoteController
)configuration
).getCachedCommonService();
199 public static CdmServiceCacher
getCdmServiceCacher() {
200 return cdmServiceCacher
;
203 public static void setCdmServiceCacher(CdmServiceCacher cacher
) {
204 cdmServiceCacher
= cacher
;