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
.service
.ICommonService
;
34 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
35 import eu
.etaxonomy
.cdm
.api
.service
.ITestService
;
36 import eu
.etaxonomy
.cdm
.io
.service
.IIOService
;
37 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
38 import eu
.etaxonomy
.taxeditor
.service
.ICachedCommonService
;
39 import eu
.etaxonomy
.taxeditor
.session
.DefaultNewEntityListener
;
46 public class CdmApplicationState
{
48 private static CdmApplicationState cdmApplicationState
;
50 private ICdmApplicationConfiguration appConfig
;
52 private ICdmDataChangeService dataChangeService
;
54 private SecurityContext securityContext
;
56 private static CdmServiceCacher cdmServiceCacher
;
58 private static String cdmlibVersion
= null;
59 private static String cdmlibLastModified
= null;
61 public static CdmApplicationState
getInstance() {
62 if(cdmApplicationState
== null) {
63 cdmApplicationState
= new CdmApplicationState();
66 return cdmApplicationState
;
69 public void setAppConfig(ICdmApplicationConfiguration appConfig
) {
70 this.appConfig
= appConfig
;
71 if(appConfig
instanceof CdmApplicationRemoteController
) {
72 CdmBase
.setNewEntityListener(new DefaultNewEntityListener());
74 CdmBase
.setNewEntityListener(null);
78 public ICdmApplicationConfiguration
getAppConfig() {
82 public static void setCurrentAppConfig(ICdmApplicationConfiguration appConfig
) {
83 getInstance().setAppConfig(appConfig
);
86 public static ICdmApplicationConfiguration
getCurrentAppConfig() {
87 return getInstance().getAppConfig();
91 * @return the dataChangeService
93 public ICdmDataChangeService
getDataChangeService() {
94 return dataChangeService
;
98 * @param dataChangeService the dataChangeService to set
100 public void setDataChangeService(ICdmDataChangeService dataChangeService
) {
101 this.dataChangeService
= dataChangeService
;
104 public static ICdmDataChangeService
getCurrentDataChangeService() {
105 return getInstance().getDataChangeService();
108 public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService
) {
109 getInstance().setDataChangeService(dataChangeService
);
115 * @return the securityContext
117 public SecurityContext
getSecurityContext() {
118 return securityContext
;
122 * @param securityContext the securityContext to set
124 public void setSecurityContext(SecurityContext securityContext
) {
125 this.securityContext
= securityContext
;
129 * @return the securityContext
131 public static SecurityContext
getCurrentSecurityContext() {
132 return getInstance().getSecurityContext();
136 * @param securityContext the securityContext to set
138 public static void setCurrentSecurityContext(SecurityContext securityContext
) {
139 getInstance().setSecurityContext(securityContext
);
142 public static void dispose() {
143 getInstance().setCurrentDataChangeService(null);
144 getInstance().setAppConfig(null);
145 getInstance().setSecurityContext(null);
146 cdmApplicationState
= null;
147 cdmServiceCacher
= null;
148 cdmlibVersion
= null;
149 cdmlibLastModified
= null;
154 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
155 * interface. If a matching getter is found the according service implementation is returned by
156 * invoking the getter otherwise the method returns <code>null</code>.
159 * @param serviceClass
160 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
161 * @throws CdmApplicationException
163 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) throws CdmApplicationException
{
164 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
166 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
170 for (Method method
: methods
) {
171 Type type
= method
.getGenericReturnType();
173 if (type
.equals(serviceClass
)) {
175 service
= (T
) method
.invoke(configuration
, null);
177 } catch (IllegalArgumentException iae
) {
178 throw new CdmApplicationException(iae
);
179 } catch (IllegalAccessException iae
) {
180 throw new CdmApplicationException(iae
);
181 } catch (InvocationTargetException ite
) {
182 throw new CdmApplicationException(ite
);
192 * @see #getService(Class)
193 * As ICommonService is not extending IService we need a specific request here
195 public static ICommonService
getCommonService() {
196 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
198 return configuration
.getCommonService();
202 public static IIOService
getIOService() {
203 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
205 return ((CdmApplicationRemoteController
)configuration
).getIOService();
210 public static ITestService
getTestService() {
211 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
213 return ((CdmApplicationRemoteController
)configuration
).getTestService();
217 public static ICachedCommonService
getCachedCommonService() {
218 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
220 return ((CdmApplicationRemoteController
)configuration
).getCachedCommonService();
224 public static CdmServiceCacher
getCdmServiceCacher() {
225 return cdmServiceCacher
;
228 public static void setCdmServiceCacher(CdmServiceCacher cacher
) {
229 cdmServiceCacher
= cacher
;
232 public static void updateCdmlibManifestInfo() {
233 cdmlibVersion
= null;
234 cdmlibLastModified
= null;
235 String cdmlibPathPrefix
= "lib/cdmlib-services-";
236 String jarSuffix
= ".jar";
237 Bundle bundle
= Platform
.getBundle("eu.etaxonomy.taxeditor.cdmlib");
238 Dictionary
<String
, String
> headers
= bundle
.getHeaders();
239 String bundleClasspath
= headers
.get(Constants
.BUNDLE_CLASSPATH
);
241 ManifestElement
[] elements
= ManifestElement
.parseHeader(Constants
.BUNDLE_CLASSPATH
, bundleClasspath
);
242 for (ManifestElement manifestElement
: elements
) {
243 String jar
= manifestElement
.getValue();
244 if(jar
.startsWith(cdmlibPathPrefix
) && jar
.endsWith(jarSuffix
)) {
245 URL fileURL
= bundle
.getEntry(jar
);
248 String urlString
= FileLocator
.resolve(fileURL
).toExternalForm().replace(" ", "%20");
249 file
= new File(new URI(urlString
));
250 JarFile jarFile
= new JarFile(file
);
251 Manifest manifest
= jarFile
.getManifest();
252 Attributes attributes
= manifest
.getMainAttributes();
253 // from the OSGI spec the LastModified value is " the number of milliseconds
254 // since midnight Jan. 1, 1970 UTC with the condition that a change must
255 // always result in a higher value than the previous last modified time
257 cdmlibVersion
= attributes
.getValue("Bundle-Version");
258 cdmlibLastModified
= attributes
.getValue("Bnd-LastModified");
261 if(cdmlibVersion
== null || cdmlibLastModified
== null) {
262 throw new IllegalStateException("Invalid cdmlib manifest info");
264 } catch (URISyntaxException urise
) {
265 throw new IllegalStateException(urise
);
266 } catch (IOException ioe
) {
267 throw new IllegalStateException(ioe
);
271 } catch (BundleException e
) {
272 throw new IllegalStateException(e
);
276 public static String
getCdmlibVersion() {
277 if(cdmlibVersion
== null) {
278 updateCdmlibManifestInfo();
280 return cdmlibVersion
;
283 public static String
getCdmlibLastModified() {
284 if(cdmlibLastModified
== null) {
285 updateCdmlibManifestInfo();
287 return cdmlibLastModified
;