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
.taxeditor
.service
.ICachedCommonService
;
29 public class CdmApplicationState
{
31 private static CdmApplicationState cdmApplicationState
;
33 private ICdmApplicationConfiguration appConfig
;
35 private ICdmDataChangeService dataChangeService
;
37 private SecurityContext securityContext
;
40 public static CdmApplicationState
getInstance() {
41 if(cdmApplicationState
== null) {
42 cdmApplicationState
= new CdmApplicationState();
43 new CdmServiceCacher();
45 return cdmApplicationState
;
48 public void setAppConfig(ICdmApplicationConfiguration appConfig
) {
49 this.appConfig
= appConfig
;
52 public ICdmApplicationConfiguration
getAppConfig() {
56 public static void setCurrentAppConfig(ICdmApplicationConfiguration appConfig
) {
57 getInstance().setAppConfig(appConfig
);
60 public static ICdmApplicationConfiguration
getCurrentAppConfig() {
61 return getInstance().getAppConfig();
65 * @return the dataChangeService
67 public ICdmDataChangeService
getDataChangeService() {
68 return dataChangeService
;
72 * @param dataChangeService the dataChangeService to set
74 public void setDataChangeService(ICdmDataChangeService dataChangeService
) {
75 this.dataChangeService
= dataChangeService
;
78 public static ICdmDataChangeService
getCurrentDataChangeService() {
79 return getInstance().getDataChangeService();
82 public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService
) {
83 getInstance().setDataChangeService(dataChangeService
);
89 * @return the securityContext
91 public SecurityContext
getSecurityContext() {
92 return securityContext
;
96 * @param securityContext the securityContext to set
98 public void setSecurityContext(SecurityContext securityContext
) {
99 this.securityContext
= securityContext
;
103 * @return the securityContext
105 public static SecurityContext
getCurrentSecurityContext() {
106 return getInstance().getSecurityContext();
110 * @param securityContext the securityContext to set
112 public static void setCurrentSecurityContext(SecurityContext securityContext
) {
113 getInstance().setSecurityContext(securityContext
);
116 public static void dispose() {
117 getInstance().setCurrentDataChangeService(null);
118 getInstance().setAppConfig(null);
119 getInstance().setSecurityContext(null);
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>.
129 * @param serviceClass
130 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
131 * @throws CdmApplicationException
133 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) throws CdmApplicationException
{
134 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
136 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
140 for (Method method
: methods
) {
141 Type type
= method
.getGenericReturnType();
143 if (type
.equals(serviceClass
)) {
145 service
= (T
) method
.invoke(configuration
, null);
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
);
162 * @see #getService(Class)
163 * As ICommonService is not extending IService we need a specific request here
165 public static ICommonService
getCommonService() {
166 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
168 return configuration
.getCommonService();
172 public static ITestService
getTestService() {
173 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
175 return ((CdmApplicationRemoteController
)configuration
).getTestService();
179 public static ICachedCommonService
getCachedCommonService() {
180 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
182 return ((CdmApplicationRemoteController
)configuration
).getCachedCommonService();