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();
46 cdmServiceCacher
= new CdmServiceCacher();
49 return cdmApplicationState
;
52 public void setAppConfig(ICdmApplicationConfiguration appConfig
) {
53 this.appConfig
= appConfig
;
56 public ICdmApplicationConfiguration
getAppConfig() {
60 public static void setCurrentAppConfig(ICdmApplicationConfiguration appConfig
) {
61 getInstance().setAppConfig(appConfig
);
64 public static ICdmApplicationConfiguration
getCurrentAppConfig() {
65 return getInstance().getAppConfig();
69 * @return the dataChangeService
71 public ICdmDataChangeService
getDataChangeService() {
72 return dataChangeService
;
76 * @param dataChangeService the dataChangeService to set
78 public void setDataChangeService(ICdmDataChangeService dataChangeService
) {
79 this.dataChangeService
= dataChangeService
;
82 public static ICdmDataChangeService
getCurrentDataChangeService() {
83 return getInstance().getDataChangeService();
86 public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService
) {
87 getInstance().setDataChangeService(dataChangeService
);
93 * @return the securityContext
95 public SecurityContext
getSecurityContext() {
96 return securityContext
;
100 * @param securityContext the securityContext to set
102 public void setSecurityContext(SecurityContext securityContext
) {
103 this.securityContext
= securityContext
;
107 * @return the securityContext
109 public static SecurityContext
getCurrentSecurityContext() {
110 return getInstance().getSecurityContext();
114 * @param securityContext the securityContext to set
116 public static void setCurrentSecurityContext(SecurityContext securityContext
) {
117 getInstance().setSecurityContext(securityContext
);
120 public static void dispose() {
121 getInstance().setCurrentDataChangeService(null);
122 getInstance().setAppConfig(null);
123 getInstance().setSecurityContext(null);
124 cdmApplicationState
= null;
125 cdmServiceCacher
= null;
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>.
135 * @param serviceClass
136 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
137 * @throws CdmApplicationException
139 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) throws CdmApplicationException
{
140 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
142 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
146 for (Method method
: methods
) {
147 Type type
= method
.getGenericReturnType();
149 if (type
.equals(serviceClass
)) {
151 service
= (T
) method
.invoke(configuration
, null);
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
);
168 * @see #getService(Class)
169 * As ICommonService is not extending IService we need a specific request here
171 public static ICommonService
getCommonService() {
172 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
174 return configuration
.getCommonService();
178 public static IIOService
getIOService() {
179 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
181 return ((CdmApplicationRemoteController
)configuration
).getIOService();
186 public static ITestService
getTestService() {
187 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
189 return ((CdmApplicationRemoteController
)configuration
).getTestService();
193 public static ICachedCommonService
getCachedCommonService() {
194 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
196 return ((CdmApplicationRemoteController
)configuration
).getCachedCommonService();
200 public static CdmServiceCacher
getCdmServiceCacher() {
201 return cdmServiceCacher
;