Project

General

Profile

Download (9.46 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.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
        T service = null;
145

    
146
        for (Method method : methods) {
147
            Type type = method.getGenericReturnType();
148

    
149
            if (type.equals(serviceClass)) {
150
                try {
151
                    service = (T) method.invoke(configuration, null);
152
                    break;
153
                } catch (IllegalArgumentException iae) {
154
                    throw new CdmApplicationException(iae);
155
                } catch (IllegalAccessException iae) {
156
                    throw new CdmApplicationException(iae);
157
                } catch (InvocationTargetException ite) {
158
                    throw new CdmApplicationException(ite);
159
                }
160
            }
161
        }
162
        return service;
163
    }
164

    
165
    /**
166
     * @see #getService(Class)
167
     * As ICommonService is not extending IService we need a specific request here
168
     */
169
    public static ICommonService getCommonService() {
170
        ICdmRepository configuration = getCurrentAppConfig();
171

    
172
        return configuration.getCommonService();
173
    }
174

    
175
    public static IIOService getIOService() {
176
        ICdmRepository configuration = getCurrentAppConfig();
177

    
178
        return ((CdmApplicationRemoteController)configuration).getIOService();
179
    }
180

    
181
    public static ILongRunningTasksService getLongRunningTasksService() {
182
        ICdmRepository configuration = getCurrentAppConfig();
183

    
184
        return ((CdmApplicationRemoteController)configuration).getLongRunningTasksService();
185
    }
186

    
187
    public static ITestService getTestService() {
188
        ICdmRepository configuration = getCurrentAppConfig();
189
        return ((CdmApplicationRemoteController)configuration).getTestService();
190
    }
191

    
192
    public static ICachedCommonService getCachedCommonService() {
193
        ICdmRepository configuration = getCurrentAppConfig();
194
        return ((CdmApplicationRemoteController)configuration).getCachedCommonService();
195
    }
196

    
197
    public static CdmServiceCacher getCdmServiceCacher() {
198
        return cdmServiceCacher;
199
    }
200

    
201
    public static void setCdmServiceCacher(CdmServiceCacher cacher) {
202
        cdmServiceCacher = cacher;
203
    }
204

    
205
    public static void updateCdmlibManifestInfo() {
206
        cdmlibVersion = null;
207
        cdmlibLastModified = null;
208
        String cdmlibPathPrefix = "lib/cdmlib-services-";
209
        String jarSuffix = ".jar";
210
        Bundle bundle = Platform.getBundle("eu.etaxonomy.taxeditor.cdmlib");
211
        Dictionary<String, String> headers = bundle.getHeaders();
212
        String bundleClasspath = headers.get(Constants.BUNDLE_CLASSPATH);
213
        try {
214
            ManifestElement[] elements = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, bundleClasspath);
215
            for (ManifestElement manifestElement : elements) {
216
                String jar =  manifestElement.getValue();
217
                if(jar.startsWith(cdmlibPathPrefix) && jar.endsWith(jarSuffix)) {
218
                    URL fileURL = bundle.getEntry(jar);
219
                    File file = null;
220
                    try {
221
                        String urlString = FileLocator.resolve(fileURL).toExternalForm().replace(" ", "%20");
222
                        file = new File(new URI(urlString));
223
                        JarFile jarFile = new JarFile(file);
224
                        Manifest manifest = jarFile.getManifest();
225
                        Attributes attributes = manifest.getMainAttributes();
226
                        // from the OSGI spec the LastModified value is " the number of milliseconds
227
                        // since midnight Jan. 1, 1970 UTC with the condition that a change must
228
                        // always result in a higher value than the previous last modified time
229
                        // of any bundle"
230
                        cdmlibVersion = attributes.getValue("Bundle-Version");
231
                        cdmlibLastModified = attributes.getValue("Bnd-LastModified");
232

    
233
                        jarFile.close();
234
                        if(cdmlibVersion == null || cdmlibLastModified == null) {
235
                            throw new IllegalStateException("Invalid cdmlib manifest info");
236
                        }
237
                    } catch (URISyntaxException urise) {
238
                        throw new IllegalStateException(urise);
239
                    } catch (IOException ioe) {
240
                        throw new IllegalStateException(ioe);
241
                    }
242
                }
243
            }
244
        } catch (BundleException e) {
245
            throw new IllegalStateException(e);
246
        }
247
    }
248

    
249
    public static String getCdmlibVersion() {
250
        if(cdmlibVersion == null) {
251
            updateCdmlibManifestInfo();
252
        }
253
        return cdmlibVersion;
254
    }
255

    
256
    public static String getCdmlibLastModified() {
257
        if(cdmlibLastModified == null) {
258
            updateCdmlibManifestInfo();
259
        }
260
        return cdmlibLastModified;
261
    }
262
}
(4-4/8)