1 package fr.ifremer.quadrige3.core.service;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import fr.ifremer.quadrige3.core.security.SecurityContext;
27 import fr.ifremer.quadrige3.core.service.technical.CacheService;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.springframework.beans.factory.access.BeanFactoryLocator;
31 import org.springframework.beans.factory.access.BeanFactoryReference;
32 import org.springframework.context.ApplicationContext;
33 import org.springframework.context.access.ContextSingletonBeanFactoryLocator;
34 import org.springframework.context.support.AbstractApplicationContext;
35
36 import java.io.Closeable;
37
38
39
40
41 public class ServiceLocator implements Closeable {
42
43
44 private static final Log log = LogFactory.getLog(ServiceLocator.class);
45
46
47
48
49 private static final String DEFAULT_BEAN_REFERENCE_LOCATION = "beanRefFactory.xml";
50
51
52
53
54 private static final String DEFAULT_BEAN_REFERENCE_ID = "beanRefFactory";
55
56
57
58
59 private static ServiceLocator instance = new ServiceLocator();
60
61
62
63
64 private boolean open = false;
65
66
67
68
69 protected ServiceLocator() {
70
71 init(null, null);
72 }
73
74
75
76
77
78
79
80 protected ServiceLocator(String beanFactoryReferenceLocation,
81 String beanRefFactoryReferenceId) {
82 init(beanFactoryReferenceLocation, beanRefFactoryReferenceId);
83 }
84
85
86
87
88
89
90 public static void setInstance(ServiceLocator newInstance) {
91 instance = newInstance;
92 }
93
94
95
96
97
98
99 public static ServiceLocator instance() {
100 return instance;
101 }
102
103
104
105
106 private BeanFactoryReference beanFactoryReference;
107
108
109
110
111 private String beanFactoryReferenceLocation;
112
113
114
115
116 private String beanRefFactoryReferenceId;
117
118
119
120
121 public static void initQuadrigeDefault() {
122 instance.init(null, null);
123 ServiceLocator.setInstance(instance);
124 }
125
126
127
128
129
130
131
132
133
134 public synchronized void init(String beanFactoryReferenceLocation,
135 String beanRefFactoryReferenceId) {
136
137 if (log.isDebugEnabled() && beanFactoryReferenceLocation != null && beanRefFactoryReferenceId != null) {
138 log.debug(String.format("Initializing ServiceLocator to use Spring bean factory [%s] at: %s", beanRefFactoryReferenceId,
139 beanFactoryReferenceLocation));
140 }
141
142 this.beanFactoryReferenceLocation =
143 beanFactoryReferenceLocation == null ?
144 DEFAULT_BEAN_REFERENCE_LOCATION :
145 beanFactoryReferenceLocation;
146 this.beanRefFactoryReferenceId = beanRefFactoryReferenceId == null ?
147 DEFAULT_BEAN_REFERENCE_ID :
148 beanRefFactoryReferenceId;
149 this.beanFactoryReference = null;
150 }
151
152
153
154
155
156
157
158
159 public synchronized void init(String beanFactoryReferenceLocation) {
160 this.beanFactoryReferenceLocation = beanFactoryReferenceLocation == null ?
161 DEFAULT_BEAN_REFERENCE_LOCATION :
162 beanFactoryReferenceLocation;
163 this.beanFactoryReference = null;
164 }
165
166
167
168
169 public synchronized void shutdown() {
170
171 if (!open) {
172 return;
173 }
174 if (log.isDebugEnabled()) {
175 log.debug("Close Spring application context");
176 }
177
178 ((AbstractApplicationContext) getContext()).close();
179 if (beanFactoryReference != null) {
180 beanFactoryReference.release();
181 beanFactoryReference = null;
182 }
183 open = false;
184 }
185
186
187
188
189
190
191
192
193
194 public <S> S getService(String name, Class<S> serviceType) {
195 return getContext().getBean(name, serviceType);
196 }
197
198
199
200
201
202
203 public final CacheService getCacheService() {
204 return getService("cacheService", CacheService.class);
205 }
206
207
208
209
210
211
212
213 public boolean isOpen() {
214 return open;
215 }
216
217
218
219
220
221
222 protected synchronized ApplicationContext getContext() {
223 if (beanFactoryReference == null) {
224 if (log.isDebugEnabled() && beanFactoryReferenceLocation != null && beanRefFactoryReferenceId != null) {
225 log.debug(String.format("Starting Spring application context using bean factory [%s] from file: %s", beanRefFactoryReferenceId,
226 beanFactoryReferenceLocation));
227 }
228 BeanFactoryLocator beanFactoryLocator =
229 ContextSingletonBeanFactoryLocator.getInstance(
230 beanFactoryReferenceLocation);
231 beanFactoryReference = beanFactoryLocator
232 .useBeanFactory(beanRefFactoryReferenceId);
233
234 open = true;
235 }
236 return (ApplicationContext) beanFactoryReference.getFactory();
237 }
238
239
240
241
242 @Override
243 public void close() {
244 shutdown();
245 }
246
247
248
249
250
251
252 public DatabaseSchemaService getDatabaseSchemaService() {
253 return getService("databaseSchemaService", DatabaseSchemaService.class);
254 }
255
256
257
258
259
260
261 public SecurityContext getSecurityContext() {
262 return getService("securityContext", SecurityContext.class);
263 }
264
265 }