Updated version in pom / project files to taxeditor version : 5.16.0 and cdmlib versi...
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / cdm / api / application / CdmApplicationState.java
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.URI;
17 import java.net.URISyntaxException;
18 import java.net.URL;
19 import java.util.Dictionary;
20 import java.util.jar.Attributes;
21 import java.util.jar.JarFile;
22 import java.util.jar.Manifest;
23
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;
31
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;
41
42 /**
43 * @author cmathew
44 * @date 17 Jun 2015
45 */
46 public class CdmApplicationState {
47
48 private static CdmApplicationState cdmApplicationState;
49
50 private ICdmRepository 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(ICdmRepository appConfig) {
70 this.appConfig = appConfig;
71 if(appConfig instanceof CdmApplicationRemoteController) {
72 CdmBase.setNewEntityListener(new DefaultNewEntityListener());
73 } else {
74 CdmBase.setNewEntityListener(null);
75 }
76 }
77
78 public ICdmRepository getAppConfig() {
79 return appConfig;
80 }
81
82 public static void setCurrentAppConfig(ICdmRepository appConfig) {
83 getInstance().setAppConfig(appConfig);
84 }
85
86 public static ICdmRepository getCurrentAppConfig() {
87 return getInstance().getAppConfig();
88 }
89
90 public ICdmDataChangeService getDataChangeService() {
91 return dataChangeService;
92 }
93 public void setDataChangeService(ICdmDataChangeService dataChangeService) {
94 this.dataChangeService = dataChangeService;
95 }
96
97 public static ICdmDataChangeService getCurrentDataChangeService() {
98 return getInstance().getDataChangeService();
99 }
100
101 public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService) {
102 getInstance().setDataChangeService(dataChangeService);
103 }
104
105 public SecurityContext getSecurityContext() {
106 return securityContext;
107 }
108 public void setSecurityContext(SecurityContext securityContext) {
109 this.securityContext = securityContext;
110 }
111
112 public static SecurityContext getCurrentSecurityContext() {
113 return getInstance().getSecurityContext();
114 }
115 public static void setCurrentSecurityContext(SecurityContext securityContext) {
116 getInstance().setSecurityContext(securityContext);
117 }
118
119 public static void dispose() {
120 getInstance().setCurrentDataChangeService(null);
121 getInstance().setAppConfig(null);
122 getInstance().setSecurityContext(null);
123 cdmApplicationState = null;
124 cdmServiceCacher = null;
125 cdmlibVersion = null;
126 cdmlibLastModified = null;
127 }
128
129 /**
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>.
133 *
134 * @param <T>
135 * @param serviceClass
136 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
137 * @throws CdmApplicationException
138 */
139 public static <T extends IService> T getService(Class<T> serviceClass) throws CdmApplicationException {
140 ICdmRepository configuration = getCurrentAppConfig();
141
142 Method[] methods = ICdmRepository.class.getDeclaredMethods();
143
144 for (Method method : methods) {
145 Type type = method.getGenericReturnType();
146
147 if (type.equals(serviceClass)) {
148 try {
149 @SuppressWarnings("unchecked")
150 T service = (T) method.invoke(configuration);
151 return service;
152 } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException iae) {
153 throw new CdmApplicationException(iae);
154 }
155 }
156 }
157 return null;
158 }
159
160 /**
161 * @see #getService(Class)
162 * As ICommonService is not extending IService we need a specific request here
163 */
164 public static ICommonService getCommonService() {
165 ICdmRepository configuration = getCurrentAppConfig();
166
167 return configuration.getCommonService();
168 }
169
170 public static IIOService getIOService() {
171 ICdmRepository configuration = getCurrentAppConfig();
172
173 return ((CdmApplicationRemoteController)configuration).getIOService();
174 }
175
176 public static ILongRunningTasksService getLongRunningTasksService() {
177 ICdmRepository configuration = getCurrentAppConfig();
178
179 return ((CdmApplicationRemoteController)configuration).getLongRunningTasksService();
180 }
181
182 public static ITestService getTestService() {
183 ICdmRepository configuration = getCurrentAppConfig();
184 return ((CdmApplicationRemoteController)configuration).getTestService();
185 }
186
187 public static ICachedCommonService getCachedCommonService() {
188 ICdmRepository configuration = getCurrentAppConfig();
189 return ((CdmApplicationRemoteController)configuration).getCachedCommonService();
190 }
191
192 public static CdmServiceCacher getCdmServiceCacher() {
193 return cdmServiceCacher;
194 }
195
196 public static void setCdmServiceCacher(CdmServiceCacher cacher) {
197 cdmServiceCacher = cacher;
198 }
199
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);
208 try {
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);
214 File file = null;
215 try {
216 String urlString = FileLocator.resolve(fileURL).toExternalForm().replace(" ", "%20");
217 file = new File(new URI(urlString));
218 JarFile jarFile = new JarFile(file);
219 Manifest manifest = jarFile.getManifest();
220 Attributes attributes = manifest.getMainAttributes();
221 // from the OSGI spec the LastModified value is " the number of milliseconds
222 // since midnight Jan. 1, 1970 UTC with the condition that a change must
223 // always result in a higher value than the previous last modified time
224 // of any bundle"
225 cdmlibVersion = attributes.getValue("Bundle-Version");
226 cdmlibLastModified = attributes.getValue("Bnd-LastModified");
227
228 jarFile.close();
229 if(cdmlibVersion == null || cdmlibLastModified == null) {
230 throw new IllegalStateException("Invalid cdmlib manifest info");
231 }
232 } catch (URISyntaxException urise) {
233 throw new IllegalStateException(urise);
234 } catch (IOException ioe) {
235 throw new IllegalStateException(ioe);
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 }