cf47b3cfce64fce2bf7da00713e2f6b4bbbac9e3
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / cdm / api / application / CdmApplicationState.java
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 import eu.etaxonomy.cdm.api.service.ITestService;
19
20 /**
21 * @author cmathew
22 * @date 17 Jun 2015
23 *
24 */
25 public class CdmApplicationState {
26
27 private static CdmApplicationState cdmApplicationState;
28
29 private ICdmApplicationConfiguration appConfig;
30
31 private ICdmDataChangeService dataChangeService;
32
33 public static CdmApplicationState getInstance() {
34 if(cdmApplicationState == null) {
35 cdmApplicationState = new CdmApplicationState();
36 }
37 return cdmApplicationState;
38 }
39
40 public void setAppConfig(ICdmApplicationConfiguration appConfig) {
41 this.appConfig = appConfig;
42 }
43
44 public ICdmApplicationConfiguration getAppConfig() {
45 return appConfig;
46 }
47
48 public static void setCurrentAppConfig(ICdmApplicationConfiguration appConfig) {
49 getInstance().setAppConfig(appConfig);
50 }
51
52 public static ICdmApplicationConfiguration getCurrentAppConfig() {
53 return getInstance().getAppConfig();
54 }
55
56 /**
57 * @return the dataChangeService
58 */
59 public ICdmDataChangeService getDataChangeService() {
60 return dataChangeService;
61 }
62
63 /**
64 * @param dataChangeService the dataChangeService to set
65 */
66 public void setDataChangeService(ICdmDataChangeService dataChangeService) {
67 this.dataChangeService = dataChangeService;
68 }
69
70 public static ICdmDataChangeService getCurrentDataChangeService() {
71 return getInstance().getDataChangeService();
72 }
73
74 public static void setCurrentDataChangeService(ICdmDataChangeService dataChangeService) {
75 getInstance().setDataChangeService(dataChangeService);
76 }
77
78 public static void dispose() {
79 getInstance().setCurrentDataChangeService(null);
80 getInstance().setAppConfig(null);
81 }
82
83 /**
84 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
85 * interface. If a matching getter is found the according service implementation is returned by
86 * invoking the getter otherwise the method returns <code>null</code>.
87 *
88 * @param <T>
89 * @param serviceClass
90 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
91 * @throws CdmApplicationException
92 */
93 public static <T extends IService> T getService(Class<T> serviceClass) throws CdmApplicationException {
94 ICdmApplicationConfiguration configuration = getCurrentAppConfig();
95
96 Method[] methods = ICdmApplicationConfiguration.class.getDeclaredMethods();
97
98 T service = null;
99
100 for (Method method : methods) {
101 Type type = method.getGenericReturnType();
102
103 if (type.equals(serviceClass)) {
104 try {
105 service = (T) method.invoke(configuration, null);
106 break;
107 } catch (IllegalArgumentException iae) {
108 throw new CdmApplicationException(iae);
109 } catch (IllegalAccessException iae) {
110 throw new CdmApplicationException(iae);
111 } catch (InvocationTargetException ite) {
112 throw new CdmApplicationException(ite);
113 }
114 }
115 }
116
117 return service;
118 }
119
120 /**
121 * @see #getService(Class)
122 * As ICommonService is not extending IService we need a specific request here
123 */
124 public static ICommonService getCommonService() {
125 ICdmApplicationConfiguration configuration = getCurrentAppConfig();
126
127 return configuration.getCommonService();
128
129 }
130
131 public static ITestService getTestService() {
132 ICdmApplicationConfiguration configuration = getCurrentAppConfig();
133
134 return ((CdmApplicationRemoteController)configuration).getTestService();
135
136 }
137
138 }