1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.impl; |
16 |
| |
17 |
| import java.util.ArrayList; |
18 |
| import java.util.Iterator; |
19 |
| import java.util.List; |
20 |
| |
21 |
| import org.apache.commons.logging.Log; |
22 |
| import org.apache.commons.logging.LogFactory; |
23 |
| import org.apache.hivemind.ApplicationRuntimeException; |
24 |
| import org.apache.hivemind.HiveMind; |
25 |
| import org.apache.hivemind.Occurances; |
26 |
| import org.apache.hivemind.ShutdownCoordinator; |
27 |
| import org.apache.hivemind.internal.ServiceImplementationConstructor; |
28 |
| import org.apache.hivemind.internal.ServiceInterceptorContribution; |
29 |
| import org.apache.hivemind.internal.ServiceModel; |
30 |
| import org.apache.hivemind.internal.ServiceModelFactory; |
31 |
| import org.apache.hivemind.order.Orderer; |
32 |
| import org.apache.hivemind.schema.Schema; |
33 |
| import org.apache.hivemind.service.InterfaceSynthesizer; |
34 |
| import org.apache.hivemind.util.ToStringBuilder; |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| public final class ServicePointImpl extends AbstractExtensionPoint implements |
44 |
| ConstructableServicePoint |
45 |
| { |
46 |
| private Object _service; |
47 |
| |
48 |
| private boolean _building; |
49 |
| |
50 |
| private String _serviceInterfaceName; |
51 |
| |
52 |
| private Class _serviceInterface; |
53 |
| |
54 |
| private Class _declaredInterface; |
55 |
| |
56 |
| private ServiceImplementationConstructor _defaultServiceConstructor; |
57 |
| |
58 |
| private ServiceImplementationConstructor _serviceConstructor; |
59 |
| |
60 |
| private List _interceptorContributions; |
61 |
| |
62 |
| private boolean _interceptorsOrdered; |
63 |
| |
64 |
| private Schema _parametersSchema; |
65 |
| |
66 |
| private Occurances _parametersCount; |
67 |
| |
68 |
| private String _serviceModel; |
69 |
| |
70 |
| private ShutdownCoordinator _shutdownCoordinator; |
71 |
| |
72 |
| private ServiceModel _serviceModelObject; |
73 |
| |
74 |
1
| protected void extendDescription(ToStringBuilder builder)
|
75 |
| { |
76 |
1
| if (_service != null)
|
77 |
0
| builder.append("service", _service);
|
78 |
| |
79 |
1
| builder.append("serviceInterfaceName", _serviceInterfaceName);
|
80 |
1
| builder.append("defaultServiceConstructor", _defaultServiceConstructor);
|
81 |
1
| builder.append("serviceConstructor", _serviceConstructor);
|
82 |
1
| builder.append("interceptorContributions", _interceptorContributions);
|
83 |
1
| builder.append("parametersSchema", _parametersSchema);
|
84 |
1
| builder.append("parametersCount", _parametersCount);
|
85 |
1
| builder.append("serviceModel", _serviceModel);
|
86 |
| |
87 |
1
| if (_building)
|
88 |
0
| builder.append("building", _building);
|
89 |
| } |
90 |
| |
91 |
36
| public void addInterceptorContribution(ServiceInterceptorContribution contribution)
|
92 |
| { |
93 |
36
| if (_interceptorContributions == null)
|
94 |
31
| _interceptorContributions = new ArrayList();
|
95 |
| |
96 |
36
| _interceptorContributions.add(contribution);
|
97 |
| } |
98 |
| |
99 |
5579
| public synchronized Class getServiceInterface()
|
100 |
| { |
101 |
5579
| if (_serviceInterface == null)
|
102 |
1410
| _serviceInterface = lookupServiceInterface();
|
103 |
| |
104 |
5578
| return _serviceInterface;
|
105 |
| } |
106 |
| |
107 |
4402
| public synchronized Class getDeclaredInterface()
|
108 |
| { |
109 |
4402
| if (_declaredInterface == null)
|
110 |
1410
| _declaredInterface = lookupDeclaredInterface();
|
111 |
| |
112 |
4401
| return _declaredInterface;
|
113 |
| } |
114 |
| |
115 |
| |
116 |
| |
117 |
2550
| public String getServiceInterfaceClassName()
|
118 |
| { |
119 |
2550
| return _serviceInterfaceName;
|
120 |
| } |
121 |
| |
122 |
1410
| private Class lookupDeclaredInterface()
|
123 |
| { |
124 |
1410
| Class result = null;
|
125 |
| |
126 |
1410
| try
|
127 |
| { |
128 |
1410
| result = getModule().resolveType(_serviceInterfaceName);
|
129 |
| } |
130 |
| catch (Exception ex) |
131 |
| { |
132 |
1
| throw new ApplicationRuntimeException(ImplMessages.badInterface(
|
133 |
| _serviceInterfaceName, |
134 |
| getExtensionPointId()), getLocation(), ex); |
135 |
| } |
136 |
| |
137 |
1409
| return result;
|
138 |
| } |
139 |
| |
140 |
1410
| private Class lookupServiceInterface()
|
141 |
| { |
142 |
1410
| Class declaredInterface = getDeclaredInterface();
|
143 |
| |
144 |
1409
| if (declaredInterface.isInterface())
|
145 |
1398
| return declaredInterface;
|
146 |
| |
147 |
| |
148 |
| |
149 |
11
| InterfaceSynthesizer is = (InterfaceSynthesizer) getModule().getService(
|
150 |
| HiveMind.INTERFACE_SYNTHESIZER_SERVICE, |
151 |
| InterfaceSynthesizer.class); |
152 |
| |
153 |
11
| return is.synthesizeInterface(declaredInterface);
|
154 |
| } |
155 |
| |
156 |
2542
| public void setServiceConstructor(ServiceImplementationConstructor contribution,
|
157 |
| boolean defaultConstructor) |
158 |
| { |
159 |
2542
| if (defaultConstructor)
|
160 |
2541
| _defaultServiceConstructor = contribution;
|
161 |
| else |
162 |
1
| _serviceConstructor = contribution;
|
163 |
| } |
164 |
| |
165 |
2555
| public void setServiceInterfaceName(String string)
|
166 |
| { |
167 |
2555
| _serviceInterfaceName = string;
|
168 |
| } |
169 |
| |
170 |
2545
| public void setParametersSchema(Schema schema)
|
171 |
| { |
172 |
2545
| _parametersSchema = schema;
|
173 |
| } |
174 |
| |
175 |
504
| public Schema getParametersSchema()
|
176 |
| { |
177 |
504
| return _parametersSchema;
|
178 |
| } |
179 |
| |
180 |
2542
| public ServiceImplementationConstructor getServiceConstructor(boolean defaultConstructor)
|
181 |
| { |
182 |
2542
| return defaultConstructor ? _defaultServiceConstructor : _serviceConstructor;
|
183 |
| } |
184 |
| |
185 |
| |
186 |
| |
187 |
| |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
3885
| private synchronized Object getService()
|
193 |
| { |
194 |
3885
| if (_service == null)
|
195 |
| { |
196 |
| |
197 |
1405
| if (_building)
|
198 |
1
| throw new ApplicationRuntimeException(ImplMessages.recursiveServiceBuild(this));
|
199 |
| |
200 |
1404
| _building = true;
|
201 |
| |
202 |
1404
| try
|
203 |
| { |
204 |
| |
205 |
1404
| ServiceModelFactory factory = getModule().getServiceModelFactory(getServiceModel());
|
206 |
| |
207 |
1404
| _serviceModelObject = factory.createServiceModelForService(this);
|
208 |
| |
209 |
1404
| _service = _serviceModelObject.getService();
|
210 |
| } |
211 |
| finally |
212 |
| { |
213 |
1404
| _building = false;
|
214 |
| } |
215 |
| } |
216 |
| |
217 |
3881
| return _service;
|
218 |
| } |
219 |
| |
220 |
3881
| public Object getService(Class serviceInterface)
|
221 |
| { |
222 |
3881
| Object result = getService();
|
223 |
| |
224 |
3877
| if (!serviceInterface.isAssignableFrom(result.getClass()))
|
225 |
| { |
226 |
1
| throw new ApplicationRuntimeException(ImplMessages.serviceWrongInterface(
|
227 |
| this, |
228 |
| serviceInterface), getLocation(), null); |
229 |
| } |
230 |
| |
231 |
3876
| return result;
|
232 |
| } |
233 |
| |
234 |
1404
| public String getServiceModel()
|
235 |
| { |
236 |
1404
| return _serviceModel;
|
237 |
| } |
238 |
| |
239 |
2543
| public void setServiceModel(String model)
|
240 |
| { |
241 |
2543
| _serviceModel = model;
|
242 |
| } |
243 |
| |
244 |
876
| public void clearConstructorInformation()
|
245 |
| { |
246 |
876
| _serviceConstructor = null;
|
247 |
876
| _interceptorContributions = null;
|
248 |
| } |
249 |
| |
250 |
| |
251 |
| |
252 |
910
| public List getOrderedInterceptorContributions()
|
253 |
| { |
254 |
910
| if (!_interceptorsOrdered)
|
255 |
| { |
256 |
910
| _interceptorContributions = orderInterceptors();
|
257 |
910
| _interceptorsOrdered = true;
|
258 |
| } |
259 |
| |
260 |
910
| return _interceptorContributions;
|
261 |
| } |
262 |
| |
263 |
910
| private List orderInterceptors()
|
264 |
| { |
265 |
910
| if (HiveMind.isEmpty(_interceptorContributions))
|
266 |
891
| return null;
|
267 |
| |
268 |
| |
269 |
| |
270 |
| |
271 |
19
| Log log = LogFactory.getLog(getExtensionPointId());
|
272 |
| |
273 |
19
| Orderer orderer = new Orderer(log, getModule().getErrorHandler(), ImplMessages
|
274 |
| .interceptorContribution()); |
275 |
| |
276 |
19
| Iterator i = _interceptorContributions.iterator();
|
277 |
19
| while (i.hasNext())
|
278 |
| { |
279 |
24
| ServiceInterceptorContribution sic = (ServiceInterceptorContribution) i.next();
|
280 |
| |
281 |
| |
282 |
| |
283 |
| |
284 |
| |
285 |
24
| orderer.add(sic, sic.getName(), sic.getPrecedingInterceptorIds(), sic
|
286 |
| .getFollowingInterceptorIds()); |
287 |
| } |
288 |
| |
289 |
19
| return orderer.getOrderedObjects();
|
290 |
| } |
291 |
| |
292 |
1020
| public ShutdownCoordinator getShutdownCoordinator()
|
293 |
| { |
294 |
1020
| return _shutdownCoordinator;
|
295 |
| } |
296 |
| |
297 |
2545
| public void setShutdownCoordinator(ShutdownCoordinator coordinator)
|
298 |
| { |
299 |
2545
| _shutdownCoordinator = coordinator;
|
300 |
| } |
301 |
| |
302 |
| |
303 |
| |
304 |
| |
305 |
4
| public void forceServiceInstantiation()
|
306 |
| { |
307 |
4
| getService();
|
308 |
| |
309 |
4
| _serviceModelObject.instantiateService();
|
310 |
| } |
311 |
| |
312 |
489
| public Occurances getParametersCount()
|
313 |
| { |
314 |
489
| return _parametersCount;
|
315 |
| } |
316 |
| |
317 |
2545
| public void setParametersCount(Occurances occurances)
|
318 |
| { |
319 |
2545
| _parametersCount = occurances;
|
320 |
| } |
321 |
| |
322 |
| |
323 |
| |
324 |
| |
325 |
| |
326 |
| |
327 |
| |
328 |
3487
| public ServiceImplementationConstructor getServiceConstructor()
|
329 |
| { |
330 |
3487
| return _serviceConstructor == null ? _defaultServiceConstructor : _serviceConstructor;
|
331 |
| } |
332 |
| } |