Project

General

Profile

Download (3.96 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 eu.etaxonomy.cdm.api.service.ICommonService;
17
import eu.etaxonomy.cdm.api.service.IService;
18

    
19
/**
20
 * @author cmathew
21
 * @date 17 Jun 2015
22
 *
23
 */
24
public class CdmApplicationState {
25

    
26
    private static CdmApplicationState cdmApplicationState;
27

    
28
    private ICdmApplicationConfiguration appConfig;
29

    
30
    private ICdmDataChangeService dataChangeService;
31

    
32
    public static CdmApplicationState getInstance() {
33
        if(cdmApplicationState == null) {
34
            cdmApplicationState = new CdmApplicationState();
35
        }
36
        return cdmApplicationState;
37
    }
38

    
39
    public void setAppConfig(ICdmApplicationConfiguration appConfig) {
40
        this.appConfig = appConfig;
41
    }
42

    
43
    public ICdmApplicationConfiguration getAppConfig() {
44
        return appConfig;
45
    }
46

    
47
    public static void setCurrentAppConfig(ICdmApplicationConfiguration appConfig) {
48
        getInstance().setAppConfig(appConfig);
49
    }
50

    
51
    public static ICdmApplicationConfiguration getCurrentAppConfig() {
52
        return getInstance().getAppConfig();
53
    }
54

    
55
    /**
56
     * @return the dataChangeService
57
     */
58
    public ICdmDataChangeService getDataChangeService() {
59
        return dataChangeService;
60
    }
61

    
62
    /**
63
     * @param dataChangeService the dataChangeService to set
64
     */
65
    public void setDataChangeService(ICdmDataChangeService dataChangeService) {
66
        this.dataChangeService = dataChangeService;
67
    }
68

    
69
    public static ICdmDataChangeService getCurrentDataChangeService() {
70
        return getInstance().getDataChangeService();
71
    }
72

    
73
    public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService) {
74
        getInstance().setDataChangeService(dataChangeService);
75
    }
76

    
77
    public static void dispose() {
78
        getInstance().setCurrentDataChangeService(null);
79
        getInstance().setAppConfig(null);
80
    }
81

    
82
    /**
83
     * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
84
     * interface. If a matching getter is found the according service implementation is returned by
85
     * invoking the getter otherwise the method returns <code>null</code>.
86
     *
87
     * @param <T>
88
     * @param serviceClass
89
     * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
90
     * @throws CdmApplicationException
91
     */
92
    public static <T extends IService> T getService(Class<T> serviceClass) throws CdmApplicationException {
93
        ICdmApplicationConfiguration configuration = getCurrentAppConfig();
94

    
95
        Method[] methods = ICdmApplicationConfiguration.class.getDeclaredMethods();
96

    
97
        T service = null;
98

    
99
        for (Method method : methods) {
100
            Type type = method.getGenericReturnType();
101

    
102
            if (type.equals(serviceClass)) {
103
                try {
104
                    service = (T) method.invoke(configuration, null);
105
                    break;
106
                } catch (IllegalArgumentException iae) {
107
                    throw new CdmApplicationException(iae);
108
                } catch (IllegalAccessException iae) {
109
                    throw new CdmApplicationException(iae);
110
                } catch (InvocationTargetException ite) {
111
                    throw new CdmApplicationException(ite);
112
                }
113
            }
114
        }
115

    
116
        return service;
117
    }
118

    
119
    /**
120
     * @see #getService(Class)
121
     * As ICommonService is not extending IService we need a specific request here
122
     */
123
    public static ICommonService getCommonService() {
124
        ICdmApplicationConfiguration configuration = getCurrentAppConfig();
125

    
126
        return configuration.getCommonService();
127

    
128
    }
129

    
130
}
(4-4/8)