Project

General

Profile

Download (6.02 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2015 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
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.
9
*/
10
package eu.etaxonomy.cdm.api.application;
11

    
12
import java.lang.reflect.InvocationTargetException;
13
import java.lang.reflect.Method;
14
import java.lang.reflect.Type;
15

    
16
import org.springframework.security.core.context.SecurityContext;
17

    
18
import eu.etaxonomy.cdm.api.cache.CdmServiceCacher;
19
import eu.etaxonomy.cdm.api.service.ICommonService;
20
import eu.etaxonomy.cdm.api.service.IService;
21
import eu.etaxonomy.cdm.api.service.ITestService;
22
import eu.etaxonomy.cdm.io.service.IIOService;
23
import eu.etaxonomy.taxeditor.service.ICachedCommonService;
24

    
25
/**
26
 * @author cmathew
27
 * @date 17 Jun 2015
28
 *
29
 */
30
public class CdmApplicationState {
31

    
32
    private static CdmApplicationState cdmApplicationState;
33

    
34
    private ICdmApplicationConfiguration appConfig;
35

    
36
    private ICdmDataChangeService dataChangeService;
37

    
38
    private SecurityContext securityContext;
39

    
40
    private static CdmServiceCacher cdmServiceCacher;
41

    
42

    
43
    public static CdmApplicationState getInstance() {
44
        if(cdmApplicationState == null) {
45
            cdmApplicationState = new CdmApplicationState();
46
        }
47

    
48
        return cdmApplicationState;
49
    }
50

    
51
    public void setAppConfig(ICdmApplicationConfiguration appConfig) {
52
        this.appConfig = appConfig;
53
    }
54

    
55
    public ICdmApplicationConfiguration getAppConfig() {
56
        return appConfig;
57
    }
58

    
59
    public static void setCurrentAppConfig(ICdmApplicationConfiguration appConfig) {
60
        getInstance().setAppConfig(appConfig);
61
    }
62

    
63
    public static ICdmApplicationConfiguration getCurrentAppConfig() {
64
        return getInstance().getAppConfig();
65
    }
66

    
67
    /**
68
     * @return the dataChangeService
69
     */
70
    public ICdmDataChangeService getDataChangeService() {
71
        return dataChangeService;
72
    }
73

    
74
    /**
75
     * @param dataChangeService the dataChangeService to set
76
     */
77
    public void setDataChangeService(ICdmDataChangeService dataChangeService) {
78
        this.dataChangeService = dataChangeService;
79
    }
80

    
81
    public static ICdmDataChangeService getCurrentDataChangeService() {
82
        return getInstance().getDataChangeService();
83
    }
84

    
85
    public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService) {
86
        getInstance().setDataChangeService(dataChangeService);
87
    }
88

    
89

    
90

    
91
    /**
92
     * @return the securityContext
93
     */
94
    public SecurityContext getSecurityContext() {
95
        return securityContext;
96
    }
97

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

    
105
    /**
106
     * @return the securityContext
107
     */
108
    public static SecurityContext getCurrentSecurityContext() {
109
        return getInstance().getSecurityContext();
110
    }
111

    
112
    /**
113
     * @param securityContext the securityContext to set
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
    }
126

    
127

    
128
    /**
129
     * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
130
     * interface. If a matching getter is found the according service implementation is returned by
131
     * invoking the getter otherwise the method returns <code>null</code>.
132
     *
133
     * @param <T>
134
     * @param serviceClass
135
     * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
136
     * @throws CdmApplicationException
137
     */
138
    public static <T extends IService> T getService(Class<T> serviceClass) throws CdmApplicationException {
139
        ICdmApplicationConfiguration configuration = getCurrentAppConfig();
140

    
141
        Method[] methods = ICdmApplicationConfiguration.class.getDeclaredMethods();
142

    
143
        T service = null;
144

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

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

    
162
        return service;
163
    }
164

    
165

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

    
173
        return configuration.getCommonService();
174

    
175
    }
176

    
177
    public static IIOService getIOService() {
178
        ICdmApplicationConfiguration configuration = getCurrentAppConfig();
179

    
180
        return ((CdmApplicationRemoteController)configuration).getIOService();
181

    
182
    }
183

    
184

    
185
    public static ITestService getTestService() {
186
        ICdmApplicationConfiguration configuration = getCurrentAppConfig();
187

    
188
        return ((CdmApplicationRemoteController)configuration).getTestService();
189

    
190
    }
191

    
192
    public static ICachedCommonService getCachedCommonService() {
193
        ICdmApplicationConfiguration configuration = getCurrentAppConfig();
194

    
195
        return ((CdmApplicationRemoteController)configuration).getCachedCommonService();
196

    
197
    }
198

    
199
    public static CdmServiceCacher getCdmServiceCacher() {
200
        return cdmServiceCacher;
201
    }
202

    
203
    public static void setCdmServiceCacher(CdmServiceCacher cacher) {
204
        cdmServiceCacher = cacher;
205
    }
206

    
207

    
208
}
(4-4/8)