1
|
/**
|
2
|
* Copyright (C) 2015 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.api.application;
|
10
|
|
11
|
import java.io.File;
|
12
|
import java.io.IOException;
|
13
|
import java.lang.reflect.InvocationTargetException;
|
14
|
import java.lang.reflect.Method;
|
15
|
import java.lang.reflect.Type;
|
16
|
import java.net.URI;
|
17
|
import java.net.URISyntaxException;
|
18
|
import java.net.URL;
|
19
|
import java.util.Dictionary;
|
20
|
import java.util.jar.Attributes;
|
21
|
import java.util.jar.JarFile;
|
22
|
import java.util.jar.Manifest;
|
23
|
|
24
|
import org.eclipse.core.runtime.FileLocator;
|
25
|
import org.eclipse.core.runtime.Platform;
|
26
|
import org.eclipse.osgi.util.ManifestElement;
|
27
|
import org.osgi.framework.Bundle;
|
28
|
import org.osgi.framework.BundleException;
|
29
|
import org.osgi.framework.Constants;
|
30
|
import org.springframework.security.core.context.SecurityContext;
|
31
|
|
32
|
import eu.etaxonomy.cdm.api.cache.CdmServiceCacher;
|
33
|
import eu.etaxonomy.cdm.api.service.ICommonService;
|
34
|
import eu.etaxonomy.cdm.api.service.IService;
|
35
|
import eu.etaxonomy.cdm.api.service.ITestService;
|
36
|
import eu.etaxonomy.cdm.api.service.longrunningService.ILongRunningTasksService;
|
37
|
import eu.etaxonomy.cdm.io.service.IIOService;
|
38
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
39
|
import eu.etaxonomy.taxeditor.service.ICachedCommonService;
|
40
|
import eu.etaxonomy.taxeditor.session.DefaultNewEntityListener;
|
41
|
|
42
|
/**
|
43
|
* @author cmathew
|
44
|
* @date 17 Jun 2015
|
45
|
*
|
46
|
*/
|
47
|
public class CdmApplicationState {
|
48
|
|
49
|
private static CdmApplicationState cdmApplicationState;
|
50
|
|
51
|
private ICdmRepository appConfig;
|
52
|
|
53
|
private ICdmDataChangeService dataChangeService;
|
54
|
|
55
|
//FIXME SecurityContextHolder.getContext()
|
56
|
private SecurityContext securityContext;
|
57
|
|
58
|
private static CdmServiceCacher cdmServiceCacher;
|
59
|
|
60
|
private static String cdmlibVersion = null;
|
61
|
private static String cdmlibLastModified = null;
|
62
|
|
63
|
public static CdmApplicationState getInstance() {
|
64
|
if(cdmApplicationState == null) {
|
65
|
cdmApplicationState = new CdmApplicationState();
|
66
|
}
|
67
|
|
68
|
return cdmApplicationState;
|
69
|
}
|
70
|
|
71
|
public void setAppConfig(ICdmRepository appConfig) {
|
72
|
this.appConfig = appConfig;
|
73
|
if(appConfig instanceof CdmApplicationRemoteController) {
|
74
|
CdmBase.setNewEntityListener(new DefaultNewEntityListener());
|
75
|
} else {
|
76
|
CdmBase.setNewEntityListener(null);
|
77
|
}
|
78
|
}
|
79
|
|
80
|
public ICdmRepository getAppConfig() {
|
81
|
return appConfig;
|
82
|
}
|
83
|
|
84
|
public static void setCurrentAppConfig(ICdmRepository appConfig) {
|
85
|
getInstance().setAppConfig(appConfig);
|
86
|
}
|
87
|
|
88
|
public static ICdmRepository getCurrentAppConfig() {
|
89
|
return getInstance().getAppConfig();
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* @return the dataChangeService
|
94
|
*/
|
95
|
public ICdmDataChangeService getDataChangeService() {
|
96
|
return dataChangeService;
|
97
|
}
|
98
|
|
99
|
/**
|
100
|
* @param dataChangeService the dataChangeService to set
|
101
|
*/
|
102
|
public void setDataChangeService(ICdmDataChangeService dataChangeService) {
|
103
|
this.dataChangeService = dataChangeService;
|
104
|
}
|
105
|
|
106
|
public static ICdmDataChangeService getCurrentDataChangeService() {
|
107
|
return getInstance().getDataChangeService();
|
108
|
}
|
109
|
|
110
|
public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService) {
|
111
|
getInstance().setDataChangeService(dataChangeService);
|
112
|
}
|
113
|
|
114
|
|
115
|
|
116
|
/**
|
117
|
* @return the securityContext
|
118
|
*/
|
119
|
public SecurityContext getSecurityContext() {
|
120
|
return securityContext;
|
121
|
}
|
122
|
|
123
|
/**
|
124
|
* @param securityContext the securityContext to set
|
125
|
*/
|
126
|
public void setSecurityContext(SecurityContext securityContext) {
|
127
|
this.securityContext = securityContext;
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* @return the securityContext
|
132
|
*/
|
133
|
public static SecurityContext getCurrentSecurityContext() {
|
134
|
return getInstance().getSecurityContext();
|
135
|
}
|
136
|
|
137
|
/**
|
138
|
* @param securityContext the securityContext to set
|
139
|
*/
|
140
|
public static void setCurrentSecurityContext(SecurityContext securityContext) {
|
141
|
getInstance().setSecurityContext(securityContext);
|
142
|
}
|
143
|
|
144
|
public static void dispose() {
|
145
|
getInstance().setCurrentDataChangeService(null);
|
146
|
getInstance().setAppConfig(null);
|
147
|
getInstance().setSecurityContext(null);
|
148
|
cdmApplicationState = null;
|
149
|
cdmServiceCacher = null;
|
150
|
cdmlibVersion = null;
|
151
|
cdmlibLastModified = null;
|
152
|
}
|
153
|
|
154
|
|
155
|
/**
|
156
|
* Generic method that will scan the getters of {@link ICdmRepository} for the given service
|
157
|
* interface. If a matching getter is found the according service implementation is returned by
|
158
|
* invoking the getter otherwise the method returns <code>null</code>.
|
159
|
*
|
160
|
* @param <T>
|
161
|
* @param serviceClass
|
162
|
* @return the configured implementation of <code>serviceClass</code> or <code>null</code>
|
163
|
* @throws CdmApplicationException
|
164
|
*/
|
165
|
public static <T extends IService> T getService(Class<T> serviceClass) throws CdmApplicationException {
|
166
|
ICdmRepository configuration = getCurrentAppConfig();
|
167
|
|
168
|
Method[] methods = ICdmRepository.class.getDeclaredMethods();
|
169
|
|
170
|
T service = null;
|
171
|
|
172
|
for (Method method : methods) {
|
173
|
Type type = method.getGenericReturnType();
|
174
|
|
175
|
if (type.equals(serviceClass)) {
|
176
|
try {
|
177
|
service = (T) method.invoke(configuration, null);
|
178
|
break;
|
179
|
} catch (IllegalArgumentException iae) {
|
180
|
throw new CdmApplicationException(iae);
|
181
|
} catch (IllegalAccessException iae) {
|
182
|
throw new CdmApplicationException(iae);
|
183
|
} catch (InvocationTargetException ite) {
|
184
|
throw new CdmApplicationException(ite);
|
185
|
}
|
186
|
}
|
187
|
}
|
188
|
|
189
|
return service;
|
190
|
}
|
191
|
|
192
|
|
193
|
/**
|
194
|
* @see #getService(Class)
|
195
|
* As ICommonService is not extending IService we need a specific request here
|
196
|
*/
|
197
|
public static ICommonService getCommonService() {
|
198
|
ICdmRepository configuration = getCurrentAppConfig();
|
199
|
|
200
|
return configuration.getCommonService();
|
201
|
|
202
|
}
|
203
|
|
204
|
public static IIOService getIOService() {
|
205
|
ICdmRepository configuration = getCurrentAppConfig();
|
206
|
|
207
|
return ((CdmApplicationRemoteController)configuration).getIOService();
|
208
|
|
209
|
}
|
210
|
|
211
|
public static ILongRunningTasksService getLongRunningTasksService() {
|
212
|
ICdmRepository configuration = getCurrentAppConfig();
|
213
|
|
214
|
return ((CdmApplicationRemoteController)configuration).getLongRunningTasksService();
|
215
|
|
216
|
}
|
217
|
|
218
|
|
219
|
|
220
|
public static ITestService getTestService() {
|
221
|
ICdmRepository configuration = getCurrentAppConfig();
|
222
|
|
223
|
return ((CdmApplicationRemoteController)configuration).getTestService();
|
224
|
|
225
|
}
|
226
|
|
227
|
public static ICachedCommonService getCachedCommonService() {
|
228
|
ICdmRepository configuration = getCurrentAppConfig();
|
229
|
|
230
|
return ((CdmApplicationRemoteController)configuration).getCachedCommonService();
|
231
|
|
232
|
}
|
233
|
|
234
|
public static CdmServiceCacher getCdmServiceCacher() {
|
235
|
return cdmServiceCacher;
|
236
|
}
|
237
|
|
238
|
public static void setCdmServiceCacher(CdmServiceCacher cacher) {
|
239
|
cdmServiceCacher = cacher;
|
240
|
}
|
241
|
|
242
|
public static void updateCdmlibManifestInfo() {
|
243
|
cdmlibVersion = null;
|
244
|
cdmlibLastModified = null;
|
245
|
String cdmlibPathPrefix = "lib/cdmlib-services-";
|
246
|
String jarSuffix = ".jar";
|
247
|
Bundle bundle = Platform.getBundle("eu.etaxonomy.taxeditor.cdmlib");
|
248
|
Dictionary<String, String> headers = bundle.getHeaders();
|
249
|
String bundleClasspath = headers.get(Constants.BUNDLE_CLASSPATH);
|
250
|
try {
|
251
|
ManifestElement[] elements = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, bundleClasspath);
|
252
|
for (ManifestElement manifestElement : elements) {
|
253
|
String jar = manifestElement.getValue();
|
254
|
if(jar.startsWith(cdmlibPathPrefix) && jar.endsWith(jarSuffix)) {
|
255
|
URL fileURL = bundle.getEntry(jar);
|
256
|
File file = null;
|
257
|
try {
|
258
|
String urlString = FileLocator.resolve(fileURL).toExternalForm().replace(" ", "%20");
|
259
|
file = new File(new URI(urlString));
|
260
|
JarFile jarFile = new JarFile(file);
|
261
|
Manifest manifest = jarFile.getManifest();
|
262
|
Attributes attributes = manifest.getMainAttributes();
|
263
|
// from the OSGI spec the LastModified value is " the number of milliseconds
|
264
|
// since midnight Jan. 1, 1970 UTC with the condition that a change must
|
265
|
// always result in a higher value than the previous last modified time
|
266
|
// of any bundle"
|
267
|
cdmlibVersion = attributes.getValue("Bundle-Version");
|
268
|
cdmlibLastModified = attributes.getValue("Bnd-LastModified");
|
269
|
|
270
|
jarFile.close();
|
271
|
if(cdmlibVersion == null || cdmlibLastModified == null) {
|
272
|
throw new IllegalStateException("Invalid cdmlib manifest info");
|
273
|
}
|
274
|
} catch (URISyntaxException urise) {
|
275
|
throw new IllegalStateException(urise);
|
276
|
} catch (IOException ioe) {
|
277
|
throw new IllegalStateException(ioe);
|
278
|
}
|
279
|
}
|
280
|
}
|
281
|
} catch (BundleException e) {
|
282
|
throw new IllegalStateException(e);
|
283
|
}
|
284
|
}
|
285
|
|
286
|
public static String getCdmlibVersion() {
|
287
|
if(cdmlibVersion == null) {
|
288
|
updateCdmlibManifestInfo();
|
289
|
}
|
290
|
return cdmlibVersion;
|
291
|
}
|
292
|
|
293
|
public static String getCdmlibLastModified() {
|
294
|
if(cdmlibLastModified == null) {
|
295
|
updateCdmlibManifestInfo();
|
296
|
}
|
297
|
return cdmlibLastModified;
|
298
|
}
|
299
|
}
|