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();
50 return cdmApplicationState
;
53 public void setAppConfig(ICdmApplicationConfiguration appConfig
) {
54 this.appConfig
= appConfig
;
55 if(appConfig
instanceof CdmApplicationRemoteController
) {
56 CdmBase
.setNewEntityListener(new DefaultNewEntityListener());
58 CdmBase
.setNewEntityListener(null);
62 public ICdmApplicationConfiguration
getAppConfig() {
66 public static void setCurrentAppConfig(ICdmApplicationConfiguration appConfig
) {
67 getInstance().setAppConfig(appConfig
);
70 public static ICdmApplicationConfiguration
getCurrentAppConfig() {
71 return getInstance().getAppConfig();
75 * @return the dataChangeService
77 public ICdmDataChangeService
getDataChangeService() {
78 return dataChangeService
;
82 * @param dataChangeService the dataChangeService to set
84 public void setDataChangeService(ICdmDataChangeService dataChangeService
) {
85 this.dataChangeService
= dataChangeService
;
88 public static ICdmDataChangeService
getCurrentDataChangeService() {
89 return getInstance().getDataChangeService();
92 public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService
) {
93 getInstance().setDataChangeService(dataChangeService
);
99 * @return the securityContext
101 public SecurityContext
getSecurityContext() {
102 return securityContext
;
106 * @param securityContext the securityContext to set
108 public void setSecurityContext(SecurityContext securityContext
) {
109 this.securityContext
= securityContext
;
113 * @return the securityContext
115 public static SecurityContext
getCurrentSecurityContext() {
116 return getInstance().getSecurityContext();
120 * @param securityContext the securityContext to set
122 public static void setCurrentSecurityContext(SecurityContext securityContext
) {
123 getInstance().setSecurityContext(securityContext
);
126 public static void dispose() {
127 getInstance().setCurrentDataChangeService(null);
128 getInstance().setAppConfig(null);
129 getInstance().setSecurityContext(null);
130 cdmApplicationState
= null;
131 cdmServiceCacher
= null;
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>.
141 * @param serviceClass
142 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
143 * @throws CdmApplicationException
145 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) throws CdmApplicationException
{
146 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
148 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
152 for (Method method
: methods
) {
153 Type type
= method
.getGenericReturnType();
155 if (type
.equals(serviceClass
)) {
157 service
= (T
) method
.invoke(configuration
, null);
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
);
174 * @see #getService(Class)
175 * As ICommonService is not extending IService we need a specific request here
177 public static ICommonService
getCommonService() {
178 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
180 return configuration
.getCommonService();
184 public static IIOService
getIOService() {
185 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
187 return ((CdmApplicationRemoteController
)configuration
).getIOService();
192 public static ITestService
getTestService() {
193 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
195 return ((CdmApplicationRemoteController
)configuration
).getTestService();
199 public static ICachedCommonService
getCachedCommonService() {
200 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
202 return ((CdmApplicationRemoteController
)configuration
).getCachedCommonService();
206 public static CdmServiceCacher
getCdmServiceCacher() {
207 return cdmServiceCacher
;
210 public static void setCdmServiceCacher(CdmServiceCacher cacher
) {
211 cdmServiceCacher
= cacher
;