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
.cdm
.model
.common
.CdmBase
;
24 import eu
.etaxonomy
.taxeditor
.service
.ICachedCommonService
;
25 import eu
.etaxonomy
.taxeditor
.session
.DefaultNewEntityListener
;
32 public class CdmApplicationState
{
34 private static CdmApplicationState cdmApplicationState
;
36 private ICdmApplicationConfiguration appConfig
;
38 private ICdmDataChangeService dataChangeService
;
40 private SecurityContext securityContext
;
42 private static CdmServiceCacher cdmServiceCacher
;
45 public static CdmApplicationState
getInstance() {
46 if(cdmApplicationState
== null) {
47 cdmApplicationState
= new CdmApplicationState();
48 CdmBase
.setNewEntityListener(new DefaultNewEntityListener());
51 return cdmApplicationState
;
54 public void setAppConfig(ICdmApplicationConfiguration appConfig
) {
55 this.appConfig
= appConfig
;
58 public ICdmApplicationConfiguration
getAppConfig() {
62 public static void setCurrentAppConfig(ICdmApplicationConfiguration appConfig
) {
63 getInstance().setAppConfig(appConfig
);
66 public static ICdmApplicationConfiguration
getCurrentAppConfig() {
67 return getInstance().getAppConfig();
71 * @return the dataChangeService
73 public ICdmDataChangeService
getDataChangeService() {
74 return dataChangeService
;
78 * @param dataChangeService the dataChangeService to set
80 public void setDataChangeService(ICdmDataChangeService dataChangeService
) {
81 this.dataChangeService
= dataChangeService
;
84 public static ICdmDataChangeService
getCurrentDataChangeService() {
85 return getInstance().getDataChangeService();
88 public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService
) {
89 getInstance().setDataChangeService(dataChangeService
);
95 * @return the securityContext
97 public SecurityContext
getSecurityContext() {
98 return securityContext
;
102 * @param securityContext the securityContext to set
104 public void setSecurityContext(SecurityContext securityContext
) {
105 this.securityContext
= securityContext
;
109 * @return the securityContext
111 public static SecurityContext
getCurrentSecurityContext() {
112 return getInstance().getSecurityContext();
116 * @param securityContext the securityContext to set
118 public static void setCurrentSecurityContext(SecurityContext securityContext
) {
119 getInstance().setSecurityContext(securityContext
);
122 public static void dispose() {
123 getInstance().setCurrentDataChangeService(null);
124 getInstance().setAppConfig(null);
125 getInstance().setSecurityContext(null);
126 cdmApplicationState
= null;
127 cdmServiceCacher
= null;
132 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
133 * interface. If a matching getter is found the according service implementation is returned by
134 * invoking the getter otherwise the method returns <code>null</code>.
137 * @param serviceClass
138 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
139 * @throws CdmApplicationException
141 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) throws CdmApplicationException
{
142 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
144 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
148 for (Method method
: methods
) {
149 Type type
= method
.getGenericReturnType();
151 if (type
.equals(serviceClass
)) {
153 service
= (T
) method
.invoke(configuration
, null);
155 } catch (IllegalArgumentException iae
) {
156 throw new CdmApplicationException(iae
);
157 } catch (IllegalAccessException iae
) {
158 throw new CdmApplicationException(iae
);
159 } catch (InvocationTargetException ite
) {
160 throw new CdmApplicationException(ite
);
170 * @see #getService(Class)
171 * As ICommonService is not extending IService we need a specific request here
173 public static ICommonService
getCommonService() {
174 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
176 return configuration
.getCommonService();
180 public static IIOService
getIOService() {
181 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
183 return ((CdmApplicationRemoteController
)configuration
).getIOService();
188 public static ITestService
getTestService() {
189 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
191 return ((CdmApplicationRemoteController
)configuration
).getTestService();
195 public static ICachedCommonService
getCachedCommonService() {
196 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
198 return ((CdmApplicationRemoteController
)configuration
).getCachedCommonService();
202 public static CdmServiceCacher
getCdmServiceCacher() {
203 return cdmServiceCacher
;
206 public static void setCdmServiceCacher(CdmServiceCacher cacher
) {
207 cdmServiceCacher
= cacher
;