1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2015 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
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.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.api.application;
|
11
|
|
12
|
import java.lang.reflect.InvocationTargetException;
|
13
|
import java.lang.reflect.Method;
|
14
|
import java.lang.reflect.Type;
|
15
|
|
16
|
import eu.etaxonomy.cdm.api.service.ICommonService;
|
17
|
import eu.etaxonomy.cdm.api.service.IService;
|
18
|
import eu.etaxonomy.cdm.api.service.ITestService;
|
19
|
import eu.etaxonomy.taxeditor.service.ICachedCommonService;
|
20
|
|
21
|
/**
|
22
|
* @author cmathew
|
23
|
* @date 17 Jun 2015
|
24
|
*
|
25
|
*/
|
26
|
public class CdmApplicationState {
|
27
|
|
28
|
private static CdmApplicationState cdmApplicationState;
|
29
|
|
30
|
private ICdmApplicationConfiguration appConfig;
|
31
|
|
32
|
private ICdmDataChangeService dataChangeService;
|
33
|
|
34
|
public static CdmApplicationState getInstance() {
|
35
|
if(cdmApplicationState == null) {
|
36
|
cdmApplicationState = new CdmApplicationState();
|
37
|
}
|
38
|
return cdmApplicationState;
|
39
|
}
|
40
|
|
41
|
public void setAppConfig(ICdmApplicationConfiguration appConfig) {
|
42
|
this.appConfig = appConfig;
|
43
|
}
|
44
|
|
45
|
public ICdmApplicationConfiguration getAppConfig() {
|
46
|
return appConfig;
|
47
|
}
|
48
|
|
49
|
public static void setCurrentAppConfig(ICdmApplicationConfiguration appConfig) {
|
50
|
getInstance().setAppConfig(appConfig);
|
51
|
}
|
52
|
|
53
|
public static ICdmApplicationConfiguration getCurrentAppConfig() {
|
54
|
return getInstance().getAppConfig();
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* @return the dataChangeService
|
59
|
*/
|
60
|
public ICdmDataChangeService getDataChangeService() {
|
61
|
return dataChangeService;
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* @param dataChangeService the dataChangeService to set
|
66
|
*/
|
67
|
public void setDataChangeService(ICdmDataChangeService dataChangeService) {
|
68
|
this.dataChangeService = dataChangeService;
|
69
|
}
|
70
|
|
71
|
public static ICdmDataChangeService getCurrentDataChangeService() {
|
72
|
return getInstance().getDataChangeService();
|
73
|
}
|
74
|
|
75
|
public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService) {
|
76
|
getInstance().setDataChangeService(dataChangeService);
|
77
|
}
|
78
|
|
79
|
public static void dispose() {
|
80
|
getInstance().setCurrentDataChangeService(null);
|
81
|
getInstance().setAppConfig(null);
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
|
86
|
* interface. If a matching getter is found the according service implementation is returned by
|
87
|
* invoking the getter otherwise the method returns <code>null</code>.
|
88
|
*
|
89
|
* @param <T>
|
90
|
* @param serviceClass
|
91
|
* @return the configured implementation of <code>serviceClass</code> or <code>null</code>
|
92
|
* @throws CdmApplicationException
|
93
|
*/
|
94
|
public static <T extends IService> T getService(Class<T> serviceClass) throws CdmApplicationException {
|
95
|
ICdmApplicationConfiguration configuration = getCurrentAppConfig();
|
96
|
|
97
|
Method[] methods = ICdmApplicationConfiguration.class.getDeclaredMethods();
|
98
|
|
99
|
T service = null;
|
100
|
|
101
|
for (Method method : methods) {
|
102
|
Type type = method.getGenericReturnType();
|
103
|
|
104
|
if (type.equals(serviceClass)) {
|
105
|
try {
|
106
|
service = (T) method.invoke(configuration, null);
|
107
|
break;
|
108
|
} catch (IllegalArgumentException iae) {
|
109
|
throw new CdmApplicationException(iae);
|
110
|
} catch (IllegalAccessException iae) {
|
111
|
throw new CdmApplicationException(iae);
|
112
|
} catch (InvocationTargetException ite) {
|
113
|
throw new CdmApplicationException(ite);
|
114
|
}
|
115
|
}
|
116
|
}
|
117
|
|
118
|
return service;
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* @see #getService(Class)
|
123
|
* As ICommonService is not extending IService we need a specific request here
|
124
|
*/
|
125
|
public static ICommonService getCommonService() {
|
126
|
ICdmApplicationConfiguration configuration = getCurrentAppConfig();
|
127
|
|
128
|
return configuration.getCommonService();
|
129
|
|
130
|
}
|
131
|
|
132
|
public static ITestService getTestService() {
|
133
|
ICdmApplicationConfiguration configuration = getCurrentAppConfig();
|
134
|
|
135
|
return ((CdmApplicationRemoteController)configuration).getTestService();
|
136
|
|
137
|
}
|
138
|
|
139
|
public static ICachedCommonService getCachedCommonService() {
|
140
|
ICdmApplicationConfiguration configuration = getCurrentAppConfig();
|
141
|
|
142
|
return ((CdmApplicationRemoteController)configuration).getCachedCommonService();
|
143
|
|
144
|
}
|
145
|
|
146
|
}
|