2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.api
.application
;
12 import java
.io
.IOException
;
13 import java
.lang
.reflect
.InvocationTargetException
;
14 import java
.lang
.reflect
.Method
;
15 import java
.lang
.reflect
.Type
;
17 import java
.net
.URISyntaxException
;
19 import java
.util
.Dictionary
;
20 import java
.util
.jar
.Attributes
;
21 import java
.util
.jar
.JarFile
;
22 import java
.util
.jar
.Manifest
;
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
;
32 import eu
.etaxonomy
.cdm
.api
.cache
.CdmServiceCacher
;
33 import eu
.etaxonomy
.cdm
.api
.longrunningService
.ILongRunningTasksService
;
34 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
35 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
36 import eu
.etaxonomy
.cdm
.api
.service
.ITestService
;
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
;
47 public class CdmApplicationState
{
49 private static CdmApplicationState cdmApplicationState
;
51 private ICdmRepository appConfig
;
53 private ICdmDataChangeService dataChangeService
;
55 private SecurityContext securityContext
;
57 private static CdmServiceCacher cdmServiceCacher
;
59 private static String cdmlibVersion
= null;
60 private static String cdmlibLastModified
= null;
62 public static CdmApplicationState
getInstance() {
63 if(cdmApplicationState
== null) {
64 cdmApplicationState
= new CdmApplicationState();
67 return cdmApplicationState
;
70 public void setAppConfig(ICdmRepository appConfig
) {
71 this.appConfig
= appConfig
;
72 if(appConfig
instanceof CdmApplicationRemoteController
) {
73 CdmBase
.setNewEntityListener(new DefaultNewEntityListener());
75 CdmBase
.setNewEntityListener(null);
79 public ICdmRepository
getAppConfig() {
83 public static void setCurrentAppConfig(ICdmRepository appConfig
) {
84 getInstance().setAppConfig(appConfig
);
87 public static ICdmRepository
getCurrentAppConfig() {
88 return getInstance().getAppConfig();
92 * @return the dataChangeService
94 public ICdmDataChangeService
getDataChangeService() {
95 return dataChangeService
;
99 * @param dataChangeService the dataChangeService to set
101 public void setDataChangeService(ICdmDataChangeService dataChangeService
) {
102 this.dataChangeService
= dataChangeService
;
105 public static ICdmDataChangeService
getCurrentDataChangeService() {
106 return getInstance().getDataChangeService();
109 public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService
) {
110 getInstance().setDataChangeService(dataChangeService
);
116 * @return the securityContext
118 public SecurityContext
getSecurityContext() {
119 return securityContext
;
123 * @param securityContext the securityContext to set
125 public void setSecurityContext(SecurityContext securityContext
) {
126 this.securityContext
= securityContext
;
130 * @return the securityContext
132 public static SecurityContext
getCurrentSecurityContext() {
133 return getInstance().getSecurityContext();
137 * @param securityContext the securityContext to set
139 public static void setCurrentSecurityContext(SecurityContext securityContext
) {
140 getInstance().setSecurityContext(securityContext
);
143 public static void dispose() {
144 getInstance().setCurrentDataChangeService(null);
145 getInstance().setAppConfig(null);
146 getInstance().setSecurityContext(null);
147 cdmApplicationState
= null;
148 cdmServiceCacher
= null;
149 cdmlibVersion
= null;
150 cdmlibLastModified
= null;
155 * Generic method that will scan the getters of {@link ICdmRepository} for the given service
156 * interface. If a matching getter is found the according service implementation is returned by
157 * invoking the getter otherwise the method returns <code>null</code>.
160 * @param serviceClass
161 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
162 * @throws CdmApplicationException
164 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) throws CdmApplicationException
{
165 ICdmRepository configuration
= getCurrentAppConfig();
167 Method
[] methods
= ICdmRepository
.class.getDeclaredMethods();
171 for (Method method
: methods
) {
172 Type type
= method
.getGenericReturnType();
174 if (type
.equals(serviceClass
)) {
176 service
= (T
) method
.invoke(configuration
, null);
178 } catch (IllegalArgumentException iae
) {
179 throw new CdmApplicationException(iae
);
180 } catch (IllegalAccessException iae
) {
181 throw new CdmApplicationException(iae
);
182 } catch (InvocationTargetException ite
) {
183 throw new CdmApplicationException(ite
);
193 * @see #getService(Class)
194 * As ICommonService is not extending IService we need a specific request here
196 public static ICommonService
getCommonService() {
197 ICdmRepository configuration
= getCurrentAppConfig();
199 return configuration
.getCommonService();
203 public static IIOService
getIOService() {
204 ICdmRepository configuration
= getCurrentAppConfig();
206 return ((CdmApplicationRemoteController
)configuration
).getIOService();
210 public static ILongRunningTasksService
getLongRunningTasksService() {
211 ICdmRepository configuration
= getCurrentAppConfig();
213 return ((CdmApplicationRemoteController
)configuration
).getLongRunningTasksService();
219 public static ITestService
getTestService() {
220 ICdmRepository configuration
= getCurrentAppConfig();
222 return ((CdmApplicationRemoteController
)configuration
).getTestService();
226 public static ICachedCommonService
getCachedCommonService() {
227 ICdmRepository configuration
= getCurrentAppConfig();
229 return ((CdmApplicationRemoteController
)configuration
).getCachedCommonService();
233 public static CdmServiceCacher
getCdmServiceCacher() {
234 return cdmServiceCacher
;
237 public static void setCdmServiceCacher(CdmServiceCacher cacher
) {
238 cdmServiceCacher
= cacher
;
241 public static void updateCdmlibManifestInfo() {
242 cdmlibVersion
= null;
243 cdmlibLastModified
= null;
244 String cdmlibPathPrefix
= "lib/cdmlib-services-";
245 String jarSuffix
= ".jar";
246 Bundle bundle
= Platform
.getBundle("eu.etaxonomy.taxeditor.cdmlib");
247 Dictionary
<String
, String
> headers
= bundle
.getHeaders();
248 String bundleClasspath
= headers
.get(Constants
.BUNDLE_CLASSPATH
);
250 ManifestElement
[] elements
= ManifestElement
.parseHeader(Constants
.BUNDLE_CLASSPATH
, bundleClasspath
);
251 for (ManifestElement manifestElement
: elements
) {
252 String jar
= manifestElement
.getValue();
253 if(jar
.startsWith(cdmlibPathPrefix
) && jar
.endsWith(jarSuffix
)) {
254 URL fileURL
= bundle
.getEntry(jar
);
257 String urlString
= FileLocator
.resolve(fileURL
).toExternalForm().replace(" ", "%20");
258 file
= new File(new URI(urlString
));
259 JarFile jarFile
= new JarFile(file
);
260 Manifest manifest
= jarFile
.getManifest();
261 Attributes attributes
= manifest
.getMainAttributes();
262 // from the OSGI spec the LastModified value is " the number of milliseconds
263 // since midnight Jan. 1, 1970 UTC with the condition that a change must
264 // always result in a higher value than the previous last modified time
266 cdmlibVersion
= attributes
.getValue("Bundle-Version");
267 cdmlibLastModified
= attributes
.getValue("Bnd-LastModified");
270 if(cdmlibVersion
== null || cdmlibLastModified
== null) {
271 throw new IllegalStateException("Invalid cdmlib manifest info");
273 } catch (URISyntaxException urise
) {
274 throw new IllegalStateException(urise
);
275 } catch (IOException ioe
) {
276 throw new IllegalStateException(ioe
);
280 } catch (BundleException e
) {
281 throw new IllegalStateException(e
);
285 public static String
getCdmlibVersion() {
286 if(cdmlibVersion
== null) {
287 updateCdmlibManifestInfo();
289 return cdmlibVersion
;
292 public static String
getCdmlibLastModified() {
293 if(cdmlibLastModified
== null) {
294 updateCdmlibManifestInfo();
296 return cdmlibLastModified
;