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