Project

General

Profile

Download (9.79 KB) Statistics
| Branch: | Tag: | Revision:
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.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;
41

    
42
/**
43
 * @author cmathew
44
 * @date 17 Jun 2015
45
 *
46
 */
47
public class CdmApplicationState {
48

    
49
    private static CdmApplicationState cdmApplicationState;
50

    
51
    private ICdmRepository appConfig;
52

    
53
    private ICdmDataChangeService dataChangeService;
54

    
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

    
67
        return cdmApplicationState;
68
    }
69

    
70
    public void setAppConfig(ICdmRepository appConfig) {
71
        this.appConfig = appConfig;
72
        if(appConfig instanceof CdmApplicationRemoteController) {
73
            CdmBase.setNewEntityListener(new DefaultNewEntityListener());
74
        } else {
75
            CdmBase.setNewEntityListener(null);
76
        }
77
    }
78

    
79
    public ICdmRepository getAppConfig() {
80
        return appConfig;
81
    }
82

    
83
    public static void setCurrentAppConfig(ICdmRepository appConfig) {
84
        getInstance().setAppConfig(appConfig);
85
    }
86

    
87
    public static ICdmRepository getCurrentAppConfig() {
88
        return getInstance().getAppConfig();
89
    }
90

    
91
    /**
92
     * @return the dataChangeService
93
     */
94
    public ICdmDataChangeService getDataChangeService() {
95
        return dataChangeService;
96
    }
97

    
98
    /**
99
     * @param dataChangeService the dataChangeService to set
100
     */
101
    public void setDataChangeService(ICdmDataChangeService dataChangeService) {
102
        this.dataChangeService = dataChangeService;
103
    }
104

    
105
    public static ICdmDataChangeService getCurrentDataChangeService() {
106
        return getInstance().getDataChangeService();
107
    }
108

    
109
    public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService) {
110
        getInstance().setDataChangeService(dataChangeService);
111
    }
112

    
113

    
114

    
115
    /**
116
     * @return the securityContext
117
     */
118
    public SecurityContext getSecurityContext() {
119
        return securityContext;
120
    }
121

    
122
    /**
123
     * @param securityContext the securityContext to set
124
     */
125
    public void setSecurityContext(SecurityContext securityContext) {
126
        this.securityContext = securityContext;
127
    }
128

    
129
    /**
130
     * @return the securityContext
131
     */
132
    public static SecurityContext getCurrentSecurityContext() {
133
        return getInstance().getSecurityContext();
134
    }
135

    
136
    /**
137
     * @param securityContext the securityContext to set
138
     */
139
    public static void setCurrentSecurityContext(SecurityContext securityContext) {
140
        getInstance().setSecurityContext(securityContext);
141
    }
142

    
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;
151
    }
152

    
153

    
154
    /**
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>.
158
     *
159
     * @param <T>
160
     * @param serviceClass
161
     * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
162
     * @throws CdmApplicationException
163
     */
164
    public static <T extends IService> T getService(Class<T> serviceClass) throws CdmApplicationException {
165
        ICdmRepository configuration = getCurrentAppConfig();
166

    
167
        Method[] methods = ICdmRepository.class.getDeclaredMethods();
168

    
169
        T service = null;
170

    
171
        for (Method method : methods) {
172
            Type type = method.getGenericReturnType();
173

    
174
            if (type.equals(serviceClass)) {
175
                try {
176
                    service = (T) method.invoke(configuration, null);
177
                    break;
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);
184
                }
185
            }
186
        }
187

    
188
        return service;
189
    }
190

    
191

    
192
    /**
193
     * @see #getService(Class)
194
     * As ICommonService is not extending IService we need a specific request here
195
     */
196
    public static ICommonService getCommonService() {
197
        ICdmRepository configuration = getCurrentAppConfig();
198

    
199
        return configuration.getCommonService();
200

    
201
    }
202

    
203
    public static IIOService getIOService() {
204
        ICdmRepository configuration = getCurrentAppConfig();
205

    
206
        return ((CdmApplicationRemoteController)configuration).getIOService();
207

    
208
    }
209

    
210
    public static ILongRunningTasksService getLongRunningTasksService() {
211
        ICdmRepository configuration = getCurrentAppConfig();
212

    
213
        return ((CdmApplicationRemoteController)configuration).getLongRunningTasksService();
214

    
215
    }
216

    
217

    
218

    
219
    public static ITestService getTestService() {
220
        ICdmRepository configuration = getCurrentAppConfig();
221

    
222
        return ((CdmApplicationRemoteController)configuration).getTestService();
223

    
224
    }
225

    
226
    public static ICachedCommonService getCachedCommonService() {
227
        ICdmRepository configuration = getCurrentAppConfig();
228

    
229
        return ((CdmApplicationRemoteController)configuration).getCachedCommonService();
230

    
231
    }
232

    
233
    public static CdmServiceCacher getCdmServiceCacher() {
234
        return cdmServiceCacher;
235
    }
236

    
237
    public static void setCdmServiceCacher(CdmServiceCacher cacher) {
238
        cdmServiceCacher = cacher;
239
    }
240

    
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);
249
        try {
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);
255
                    File file = null;
256
                    try {
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
265
                        // of any bundle"
266
                        cdmlibVersion = attributes.getValue("Bundle-Version");
267
                        cdmlibLastModified = attributes.getValue("Bnd-LastModified");
268

    
269
                        jarFile.close();
270
                        if(cdmlibVersion == null || cdmlibLastModified == null) {
271
                            throw new IllegalStateException("Invalid cdmlib manifest info");
272
                        }
273
                    } catch (URISyntaxException urise) {
274
                        throw new IllegalStateException(urise);
275
                    } catch (IOException ioe) {
276
                        throw new IllegalStateException(ioe);
277
                    }
278
                }
279
            }
280
        } catch (BundleException e) {
281
            throw new IllegalStateException(e);
282
        }
283
    }
284

    
285
    public static String getCdmlibVersion() {
286
        if(cdmlibVersion == null) {
287
            updateCdmlibManifestInfo();
288
        }
289
        return cdmlibVersion;
290
    }
291

    
292
    public static String getCdmlibLastModified() {
293
        if(cdmlibLastModified == null) {
294
            updateCdmlibManifestInfo();
295
        }
296
        return cdmlibLastModified;
297
    }
298
}
(4-4/8)