3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.api
.application
;
13 import java
.io
.IOException
;
14 import java
.lang
.reflect
.InvocationTargetException
;
15 import java
.lang
.reflect
.Method
;
16 import java
.lang
.reflect
.Type
;
18 import java
.net
.URISyntaxException
;
20 import java
.util
.Dictionary
;
21 import java
.util
.jar
.Attributes
;
22 import java
.util
.jar
.JarFile
;
23 import java
.util
.jar
.Manifest
;
25 import org
.eclipse
.core
.runtime
.FileLocator
;
26 import org
.eclipse
.core
.runtime
.Platform
;
27 import org
.eclipse
.osgi
.util
.ManifestElement
;
28 import org
.osgi
.framework
.Bundle
;
29 import org
.osgi
.framework
.BundleException
;
30 import org
.osgi
.framework
.Constants
;
31 import org
.springframework
.security
.core
.context
.SecurityContext
;
33 import eu
.etaxonomy
.cdm
.api
.cache
.CdmServiceCacher
;
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 ICdmApplicationConfiguration 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(ICdmApplicationConfiguration appConfig
) {
71 this.appConfig
= appConfig
;
72 if(appConfig
instanceof CdmApplicationRemoteController
) {
73 CdmBase
.setNewEntityListener(new DefaultNewEntityListener());
75 CdmBase
.setNewEntityListener(null);
79 public ICdmApplicationConfiguration
getAppConfig() {
83 public static void setCurrentAppConfig(ICdmApplicationConfiguration appConfig
) {
84 getInstance().setAppConfig(appConfig
);
87 public static ICdmApplicationConfiguration
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 ICdmApplicationConfiguration} 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 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
167 Method
[] methods
= ICdmApplicationConfiguration
.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 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
199 return configuration
.getCommonService();
203 public static IIOService
getIOService() {
204 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
206 return ((CdmApplicationRemoteController
)configuration
).getIOService();
211 public static ITestService
getTestService() {
212 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
214 return ((CdmApplicationRemoteController
)configuration
).getTestService();
218 public static ICachedCommonService
getCachedCommonService() {
219 ICdmApplicationConfiguration configuration
= getCurrentAppConfig();
221 return ((CdmApplicationRemoteController
)configuration
).getCachedCommonService();
225 public static CdmServiceCacher
getCdmServiceCacher() {
226 return cdmServiceCacher
;
229 public static void setCdmServiceCacher(CdmServiceCacher cacher
) {
230 cdmServiceCacher
= cacher
;
233 public static void updateCdmlibManifestInfo() {
234 cdmlibVersion
= null;
235 cdmlibLastModified
= null;
236 String cdmlibPathPrefix
= "lib/cdmlib-services-";
237 String jarSuffix
= ".jar";
238 Bundle bundle
= Platform
.getBundle("eu.etaxonomy.taxeditor.cdmlib");
239 Dictionary
<String
, String
> headers
= bundle
.getHeaders();
240 String bundleClasspath
= headers
.get(Constants
.BUNDLE_CLASSPATH
);
242 ManifestElement
[] elements
= ManifestElement
.parseHeader(Constants
.BUNDLE_CLASSPATH
, bundleClasspath
);
243 for (ManifestElement manifestElement
: elements
) {
244 String jar
= manifestElement
.getValue();
245 if(jar
.startsWith(cdmlibPathPrefix
) && jar
.endsWith(jarSuffix
)) {
246 URL fileURL
= bundle
.getEntry(jar
);
249 String urlString
= FileLocator
.resolve(fileURL
).toExternalForm().replace(" ", "%20");
250 file
= new File(new URI(urlString
));
251 JarFile jarFile
= new JarFile(file
);
252 Manifest manifest
= jarFile
.getManifest();
253 Attributes attributes
= manifest
.getMainAttributes();
254 // from the OSGI spec the LastModified value is " the number of milliseconds
255 // since midnight Jan. 1, 1970 UTC with the condition that a change must
256 // always result in a higher value than the previous last modified time
258 cdmlibVersion
= attributes
.getValue("Bundle-Version");
259 cdmlibLastModified
= attributes
.getValue("Bnd-LastModified");
262 if(cdmlibVersion
== null || cdmlibLastModified
== null) {
263 throw new IllegalStateException("Invalid cdmlib manifest info");
265 } catch (URISyntaxException urise
) {
266 throw new IllegalStateException(urise
);
267 } catch (IOException ioe
) {
268 throw new IllegalStateException(ioe
);
272 } catch (BundleException e
) {
273 throw new IllegalStateException(e
);
277 public static String
getCdmlibVersion() {
278 if(cdmlibVersion
== null) {
279 updateCdmlibManifestInfo();
281 return cdmlibVersion
;
284 public static String
getCdmlibLastModified() {
285 if(cdmlibLastModified
== null) {
286 updateCdmlibManifestInfo();
288 return cdmlibLastModified
;