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 eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
17 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
18 import eu
.etaxonomy
.cdm
.api
.service
.ITestService
;
25 public class CdmApplicationState
{
27 private static CdmApplicationState cdmApplicationState
;
29 private ICdmApplicationConfiguration appConfig
;
31 private ICdmDataChangeService dataChangeService
;
33 public static CdmApplicationState
getInstance() {
34 if(cdmApplicationState
== null) {
35 cdmApplicationState
= new CdmApplicationState();
37 return cdmApplicationState
;
40 public void setAppConfig(ICdmApplicationConfiguration appConfig
) {
41 this.appConfig
= appConfig
;
44 public ICdmApplicationConfiguration
getAppConfig() {
48 public static void setCurrentAppConfig(ICdmApplicationConfiguration appConfig
) {
49 getInstance().setAppConfig(appConfig
);
52 public static ICdmApplicationConfiguration
getCurrentAppConfig() {
53 return getInstance().getAppConfig();
57 * @return the dataChangeService
59 public ICdmDataChangeService
getDataChangeService() {
60 return dataChangeService
;
64 * @param dataChangeService the dataChangeService to set
66 public void setDataChangeService(ICdmDataChangeService dataChangeService
) {
67 this.dataChangeService
= dataChangeService
;
70 public static ICdmDataChangeService
getCurrentDataChangeService() {
71 return getInstance().getDataChangeService();
74 public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService
) {
75 getInstance().setDataChangeService(dataChangeService
);
78 public static void dispose() {
79 getInstance().setCurrentDataChangeService(null);
80 getInstance().setAppConfig(null);
84 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
85 * interface. If a matching getter is found the according service implementation is returned by
86 * invoking the getter otherwise the method returns <code>null</code>.
90 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
91 * @throws CdmApplicationException
93 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) throws CdmApplicationException
{
94 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
96 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
100 for (Method method
: methods
) {
101 Type type
= method
.getGenericReturnType();
103 if (type
.equals(serviceClass
)) {
105 service
= (T
) method
.invoke(configuration
, null);
107 } catch (IllegalArgumentException iae
) {
108 throw new CdmApplicationException(iae
);
109 } catch (IllegalAccessException iae
) {
110 throw new CdmApplicationException(iae
);
111 } catch (InvocationTargetException ite
) {
112 throw new CdmApplicationException(ite
);
121 * @see #getService(Class)
122 * As ICommonService is not extending IService we need a specific request here
124 public static ICommonService
getCommonService() {
125 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
127 return configuration
.getCommonService();
131 public static ITestService
getTestService() {
132 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
134 return ((CdmApplicationRemoteController
)configuration
).getTestService();