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
;
16 import eu
.etaxonomy
.cdm
.common
.URI
;
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
.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
;
46 public class CdmApplicationState
{
48 private static CdmApplicationState cdmApplicationState
;
50 private ICdmRepository appConfig
;
52 private ICdmDataChangeService dataChangeService
;
54 //FIXME SecurityContextHolder.getContext()
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();
66 return cdmApplicationState
;
69 public void setAppConfig(ICdmRepository appConfig
) {
70 this.appConfig
= appConfig
;
71 if(appConfig
instanceof CdmApplicationRemoteController
) {
72 CdmBase
.setNewEntityListener(new DefaultNewEntityListener());
74 CdmBase
.setNewEntityListener(null);
78 public ICdmRepository
getAppConfig() {
82 public static void setCurrentAppConfig(ICdmRepository appConfig
) {
83 getInstance().setAppConfig(appConfig
);
86 public static ICdmRepository
getCurrentAppConfig() {
87 return getInstance().getAppConfig();
90 public ICdmDataChangeService
getDataChangeService() {
91 return dataChangeService
;
93 public void setDataChangeService(ICdmDataChangeService dataChangeService
) {
94 this.dataChangeService
= dataChangeService
;
97 public static ICdmDataChangeService
getCurrentDataChangeService() {
98 return getInstance().getDataChangeService();
101 public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService
) {
102 getInstance().setDataChangeService(dataChangeService
);
105 public SecurityContext
getSecurityContext() {
106 return securityContext
;
108 public void setSecurityContext(SecurityContext securityContext
) {
109 this.securityContext
= securityContext
;
112 public static SecurityContext
getCurrentSecurityContext() {
113 return getInstance().getSecurityContext();
115 public static void setCurrentSecurityContext(SecurityContext securityContext
) {
116 getInstance().setSecurityContext(securityContext
);
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;
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>.
135 * @param serviceClass
136 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
137 * @throws CdmApplicationException
139 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) throws CdmApplicationException
{
140 ICdmRepository configuration
= getCurrentAppConfig();
142 Method
[] methods
= ICdmRepository
.class.getDeclaredMethods();
144 for (Method method
: methods
) {
145 Type type
= method
.getGenericReturnType();
147 if (type
.equals(serviceClass
)) {
149 @SuppressWarnings("unchecked")
150 T service
= (T
) method
.invoke(configuration
);
152 } catch (IllegalArgumentException
| IllegalAccessException
| InvocationTargetException iae
) {
153 throw new CdmApplicationException(iae
);
161 * @see #getService(Class)
162 * As ICommonService is not extending IService we need a specific request here
164 public static ICommonService
getCommonService() {
165 ICdmRepository configuration
= getCurrentAppConfig();
167 return configuration
.getCommonService();
170 public static IIOService
getIOService() {
171 ICdmRepository configuration
= getCurrentAppConfig();
173 return ((CdmApplicationRemoteController
)configuration
).getIOService();
176 public static ILongRunningTasksService
getLongRunningTasksService() {
177 ICdmRepository configuration
= getCurrentAppConfig();
179 return ((CdmApplicationRemoteController
)configuration
).getLongRunningTasksService();
182 public static ITestService
getTestService() {
183 ICdmRepository configuration
= getCurrentAppConfig();
184 return ((CdmApplicationRemoteController
)configuration
).getTestService();
187 public static ICachedCommonService
getCachedCommonService() {
188 ICdmRepository configuration
= getCurrentAppConfig();
189 return ((CdmApplicationRemoteController
)configuration
).getCachedCommonService();
192 public static CdmServiceCacher
getCdmServiceCacher() {
193 return cdmServiceCacher
;
196 public static void setCdmServiceCacher(CdmServiceCacher cacher
) {
197 cdmServiceCacher
= cacher
;
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
);
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
);
216 String urlString
= FileLocator
.resolve(fileURL
).toExternalForm().replace(" ", "%20");
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
226 cdmlibVersion
= attributes
.getValue("Bundle-Version");
227 cdmlibLastModified
= attributes
.getValue("Bnd-LastModified");
230 if(cdmlibVersion
== null || cdmlibLastModified
== null) {
231 throw new IllegalStateException("Invalid cdmlib manifest info");
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
);
243 } catch (BundleException e
) {
244 throw new IllegalStateException(e
);
248 public static String
getCdmlibVersion() {
249 if(cdmlibVersion
== null) {
250 updateCdmlibManifestInfo();
252 return cdmlibVersion
;
255 public static String
getCdmlibLastModified() {
256 if(cdmlibLastModified
== null) {
257 updateCdmlibManifestInfo();
259 return cdmlibLastModified
;