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 eu.etaxonomy.cdm.common.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
|
public class CdmApplicationState {
|
47
|
|
48
|
private static CdmApplicationState cdmApplicationState;
|
49
|
|
50
|
private ICdmRepository appConfig;
|
51
|
|
52
|
private ICdmDataChangeService dataChangeService;
|
53
|
|
54
|
//FIXME SecurityContextHolder.getContext()
|
55
|
private SecurityContext securityContext;
|
56
|
|
57
|
private static CdmServiceCacher cdmServiceCacher;
|
58
|
|
59
|
private static String cdmlibVersion = null;
|
60
|
private static String cdmlibLastModified = null;
|
61
|
|
62
|
public static CdmApplicationState getInstance() {
|
63
|
if(cdmApplicationState == null) {
|
64
|
cdmApplicationState = new CdmApplicationState();
|
65
|
}
|
66
|
return cdmApplicationState;
|
67
|
}
|
68
|
|
69
|
public void setAppConfig(ICdmRepository appConfig) {
|
70
|
this.appConfig = appConfig;
|
71
|
if(appConfig instanceof CdmApplicationRemoteController) {
|
72
|
CdmBase.setNewEntityListener(new DefaultNewEntityListener());
|
73
|
} else {
|
74
|
CdmBase.setNewEntityListener(null);
|
75
|
}
|
76
|
}
|
77
|
|
78
|
public ICdmRepository getAppConfig() {
|
79
|
return appConfig;
|
80
|
}
|
81
|
|
82
|
public static void setCurrentAppConfig(ICdmRepository appConfig) {
|
83
|
getInstance().setAppConfig(appConfig);
|
84
|
}
|
85
|
|
86
|
public static ICdmRepository getCurrentAppConfig() {
|
87
|
return getInstance().getAppConfig();
|
88
|
}
|
89
|
|
90
|
public ICdmDataChangeService getDataChangeService() {
|
91
|
return dataChangeService;
|
92
|
}
|
93
|
public void setDataChangeService(ICdmDataChangeService dataChangeService) {
|
94
|
this.dataChangeService = dataChangeService;
|
95
|
}
|
96
|
|
97
|
public static ICdmDataChangeService getCurrentDataChangeService() {
|
98
|
return getInstance().getDataChangeService();
|
99
|
}
|
100
|
|
101
|
public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService) {
|
102
|
getInstance().setDataChangeService(dataChangeService);
|
103
|
}
|
104
|
|
105
|
public SecurityContext getSecurityContext() {
|
106
|
return securityContext;
|
107
|
}
|
108
|
public void setSecurityContext(SecurityContext securityContext) {
|
109
|
this.securityContext = securityContext;
|
110
|
}
|
111
|
|
112
|
public static SecurityContext getCurrentSecurityContext() {
|
113
|
return getInstance().getSecurityContext();
|
114
|
}
|
115
|
public static void setCurrentSecurityContext(SecurityContext securityContext) {
|
116
|
getInstance().setSecurityContext(securityContext);
|
117
|
}
|
118
|
|
119
|
public static void dispose() {
|
120
|
setCurrentDataChangeService(null);
|
121
|
getInstance().setAppConfig(null);
|
122
|
getInstance().setSecurityContext(null);
|
123
|
cdmApplicationState = null;
|
124
|
cdmServiceCacher = null;
|
125
|
cdmlibVersion = null;
|
126
|
cdmlibLastModified = null;
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* Generic method that will scan the getters of {@link ICdmRepository} for the given service
|
131
|
* interface. If a matching getter is found the according service implementation is returned by
|
132
|
* invoking the getter otherwise the method returns <code>null</code>.
|
133
|
*
|
134
|
* @param <T>
|
135
|
* @param serviceClass
|
136
|
* @return the configured implementation of <code>serviceClass</code> or <code>null</code>
|
137
|
* @throws CdmApplicationException
|
138
|
*/
|
139
|
public static <T extends IService> T getService(Class<T> serviceClass) throws CdmApplicationException {
|
140
|
ICdmRepository configuration = getCurrentAppConfig();
|
141
|
|
142
|
Method[] methods = ICdmRepository.class.getDeclaredMethods();
|
143
|
|
144
|
for (Method method : methods) {
|
145
|
Type type = method.getGenericReturnType();
|
146
|
|
147
|
if (type.equals(serviceClass)) {
|
148
|
try {
|
149
|
@SuppressWarnings("unchecked")
|
150
|
T service = (T) method.invoke(configuration);
|
151
|
return service;
|
152
|
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException iae) {
|
153
|
throw new CdmApplicationException(iae);
|
154
|
}
|
155
|
}
|
156
|
}
|
157
|
return null;
|
158
|
}
|
159
|
|
160
|
/**
|
161
|
* @see #getService(Class)
|
162
|
* As ICommonService is not extending IService we need a specific request here
|
163
|
*/
|
164
|
public static ICommonService getCommonService() {
|
165
|
ICdmRepository configuration = getCurrentAppConfig();
|
166
|
|
167
|
return configuration.getCommonService();
|
168
|
}
|
169
|
|
170
|
public static IIOService getIOService() {
|
171
|
ICdmRepository configuration = getCurrentAppConfig();
|
172
|
|
173
|
return ((CdmApplicationRemoteController)configuration).getIOService();
|
174
|
}
|
175
|
|
176
|
public static ILongRunningTasksService getLongRunningTasksService() {
|
177
|
ICdmRepository configuration = getCurrentAppConfig();
|
178
|
|
179
|
return ((CdmApplicationRemoteController)configuration).getLongRunningTasksService();
|
180
|
}
|
181
|
|
182
|
public static ITestService getTestService() {
|
183
|
ICdmRepository configuration = getCurrentAppConfig();
|
184
|
return ((CdmApplicationRemoteController)configuration).getTestService();
|
185
|
}
|
186
|
|
187
|
public static ICachedCommonService getCachedCommonService() {
|
188
|
ICdmRepository configuration = getCurrentAppConfig();
|
189
|
return ((CdmApplicationRemoteController)configuration).getCachedCommonService();
|
190
|
}
|
191
|
|
192
|
public static CdmServiceCacher getCdmServiceCacher() {
|
193
|
return cdmServiceCacher;
|
194
|
}
|
195
|
|
196
|
public static void setCdmServiceCacher(CdmServiceCacher cacher) {
|
197
|
cdmServiceCacher = cacher;
|
198
|
}
|
199
|
|
200
|
public static void updateCdmlibManifestInfo() {
|
201
|
cdmlibVersion = null;
|
202
|
cdmlibLastModified = null;
|
203
|
String cdmlibPathPrefix = "lib/cdmlib-services-";
|
204
|
String jarSuffix = ".jar";
|
205
|
Bundle bundle = Platform.getBundle("eu.etaxonomy.taxeditor.cdmlib");
|
206
|
Dictionary<String, String> headers = bundle.getHeaders();
|
207
|
String bundleClasspath = headers.get(Constants.BUNDLE_CLASSPATH);
|
208
|
try {
|
209
|
ManifestElement[] elements = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, bundleClasspath);
|
210
|
for (ManifestElement manifestElement : elements) {
|
211
|
String jar = manifestElement.getValue();
|
212
|
if(jar.startsWith(cdmlibPathPrefix) && jar.endsWith(jarSuffix)) {
|
213
|
URL fileURL = bundle.getEntry(jar);
|
214
|
File file = null;
|
215
|
try {
|
216
|
String urlString = FileLocator.resolve(fileURL).toExternalForm().replace(" ", "%20");
|
217
|
|
218
|
file = new File(new URI(urlString).getJavaUri());
|
219
|
JarFile jarFile = new JarFile(file);
|
220
|
Manifest manifest = jarFile.getManifest();
|
221
|
Attributes attributes = manifest.getMainAttributes();
|
222
|
// from the OSGI spec the LastModified value is " the number of milliseconds
|
223
|
// since midnight Jan. 1, 1970 UTC with the condition that a change must
|
224
|
// always result in a higher value than the previous last modified time
|
225
|
// of any bundle"
|
226
|
cdmlibVersion = attributes.getValue("Bundle-Version");
|
227
|
cdmlibLastModified = attributes.getValue("Bnd-LastModified");
|
228
|
|
229
|
jarFile.close();
|
230
|
if(cdmlibVersion == null || cdmlibLastModified == null) {
|
231
|
throw new IllegalStateException("Invalid cdmlib manifest info");
|
232
|
}
|
233
|
} catch (URISyntaxException urise) {
|
234
|
throw new IllegalStateException(urise);
|
235
|
} catch (IOException ioe) {
|
236
|
throw new IllegalStateException(ioe);
|
237
|
} catch (IllegalArgumentException iae) {
|
238
|
String message = iae.getMessage().concat("uri" + jar);
|
239
|
throw new IllegalStateException(message);
|
240
|
}
|
241
|
}
|
242
|
}
|
243
|
} catch (BundleException e) {
|
244
|
throw new IllegalStateException(e);
|
245
|
}
|
246
|
}
|
247
|
|
248
|
public static String getCdmlibVersion() {
|
249
|
if(cdmlibVersion == null) {
|
250
|
updateCdmlibManifestInfo();
|
251
|
}
|
252
|
return cdmlibVersion;
|
253
|
}
|
254
|
|
255
|
public static String getCdmlibLastModified() {
|
256
|
if(cdmlibLastModified == null) {
|
257
|
updateCdmlibManifestInfo();
|
258
|
}
|
259
|
return cdmlibLastModified;
|
260
|
}
|
261
|
}
|