|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ServicePointImpl.java | 89.3% | 97.2% | 100% | 95.9% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
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.ClassResolver;
|
|
25 |
import org.apache.hivemind.HiveMind;
|
|
26 |
import org.apache.hivemind.Occurances;
|
|
27 |
import org.apache.hivemind.ShutdownCoordinator;
|
|
28 |
import org.apache.hivemind.internal.ServiceImplementationConstructor;
|
|
29 |
import org.apache.hivemind.internal.ServiceInterceptorContribution;
|
|
30 |
import org.apache.hivemind.internal.ServiceModel;
|
|
31 |
import org.apache.hivemind.internal.ServiceModelFactory;
|
|
32 |
import org.apache.hivemind.order.Orderer;
|
|
33 |
import org.apache.hivemind.schema.Schema;
|
|
34 |
import org.apache.hivemind.util.ToStringBuilder;
|
|
35 |
|
|
36 |
/**
|
|
37 |
* Abstract implementation of {@link org.apache.hivemind.internal.ServicePoint}. Provides some of
|
|
38 |
* the machinery for creating new service instances, delegating most of it to the
|
|
39 |
* {@link org.apache.hivemind.internal.ServiceModel}instace for the service.
|
|
40 |
*
|
|
41 |
* @author Howard Lewis Ship
|
|
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 ServiceImplementationConstructor _defaultServiceConstructor;
|
|
55 |
|
|
56 |
private ServiceImplementationConstructor _serviceConstructor;
|
|
57 |
|
|
58 |
private List _interceptorContributions;
|
|
59 |
|
|
60 |
private boolean _interceptorsOrdered; |
|
61 |
|
|
62 |
private Schema _parametersSchema;
|
|
63 |
|
|
64 |
private Occurances _parametersCount;
|
|
65 |
|
|
66 |
private String _serviceModel;
|
|
67 |
|
|
68 |
private ShutdownCoordinator _shutdownCoordinator;
|
|
69 |
|
|
70 |
private ServiceModel _serviceModelObject;
|
|
71 |
|
|
72 | 1 |
protected void extendDescription(ToStringBuilder builder) |
73 |
{ |
|
74 | 1 |
if (_service != null) |
75 | 0 |
builder.append("service", _service);
|
76 |
|
|
77 | 1 |
builder.append("serviceInterfaceName", _serviceInterfaceName);
|
78 | 1 |
builder.append("defaultServiceConstructor", _defaultServiceConstructor);
|
79 | 1 |
builder.append("serviceConstructor", _serviceConstructor);
|
80 | 1 |
builder.append("interceptorContributions", _interceptorContributions);
|
81 | 1 |
builder.append("parametersSchema", _parametersSchema);
|
82 | 1 |
builder.append("parametersCount", _parametersCount);
|
83 | 1 |
builder.append("serviceModel", _serviceModel);
|
84 |
|
|
85 | 1 |
if (_building)
|
86 | 0 |
builder.append("building", _building);
|
87 |
} |
|
88 |
|
|
89 | 30 |
public void addInterceptorContribution(ServiceInterceptorContribution contribution) |
90 |
{ |
|
91 | 30 |
if (_interceptorContributions == null) |
92 | 28 |
_interceptorContributions = new ArrayList();
|
93 |
|
|
94 | 30 |
_interceptorContributions.add(contribution); |
95 |
} |
|
96 |
|
|
97 | 3635 |
public synchronized Class getServiceInterface() |
98 |
{ |
|
99 | 3635 |
if (_serviceInterface == null) |
100 | 1046 |
_serviceInterface = lookupServiceInterface(); |
101 |
|
|
102 | 3633 |
return _serviceInterface;
|
103 |
} |
|
104 |
|
|
105 |
/** @since 1.1 */
|
|
106 |
|
|
107 | 1644 |
public String getServiceInterfaceClassName()
|
108 |
{ |
|
109 | 1644 |
return _serviceInterfaceName;
|
110 |
} |
|
111 |
|
|
112 | 1046 |
private Class lookupServiceInterface()
|
113 |
{ |
|
114 | 1046 |
ClassResolver resolver = getModule().getClassResolver(); |
115 | 1046 |
Class result = null;
|
116 |
|
|
117 | 1046 |
try
|
118 |
{ |
|
119 | 1046 |
result = resolver.findClass(_serviceInterfaceName); |
120 |
} |
|
121 |
catch (Exception ex)
|
|
122 |
{ |
|
123 | 1 |
throw new ApplicationRuntimeException(ImplMessages.badInterface( |
124 |
_serviceInterfaceName, |
|
125 |
getExtensionPointId()), getLocation(), ex); |
|
126 |
} |
|
127 |
|
|
128 | 1045 |
if (!result.isInterface())
|
129 | 1 |
throw new ApplicationRuntimeException(ImplMessages.interfaceRequired( |
130 |
_serviceInterfaceName, |
|
131 |
getExtensionPointId()), getLocation(), null);
|
|
132 |
|
|
133 | 1044 |
return result;
|
134 |
} |
|
135 |
|
|
136 | 1642 |
public void setServiceConstructor(ServiceImplementationConstructor contribution, |
137 |
boolean defaultConstructor)
|
|
138 |
{ |
|
139 | 1642 |
if (defaultConstructor)
|
140 | 1641 |
_defaultServiceConstructor = contribution; |
141 |
else
|
|
142 | 1 |
_serviceConstructor = contribution; |
143 |
} |
|
144 |
|
|
145 | 1647 |
public void setServiceInterfaceName(String string) |
146 |
{ |
|
147 | 1647 |
_serviceInterfaceName = string; |
148 |
} |
|
149 |
|
|
150 | 204 |
public void setParametersSchema(Schema schema) |
151 |
{ |
|
152 | 204 |
_parametersSchema = schema; |
153 |
} |
|
154 |
|
|
155 | 404 |
public Schema getParametersSchema()
|
156 |
{ |
|
157 | 404 |
return _parametersSchema;
|
158 |
} |
|
159 |
|
|
160 | 1642 |
public ServiceImplementationConstructor getServiceConstructor(boolean defaultConstructor) |
161 |
{ |
|
162 | 1642 |
return defaultConstructor ? _defaultServiceConstructor : _serviceConstructor;
|
163 |
} |
|
164 |
|
|
165 |
/**
|
|
166 |
* Invoked by {@link #getService(Class)}to get a service implementation from the
|
|
167 |
* {@link ServiceModel}.
|
|
168 |
* <p>
|
|
169 |
* TODO: I'm concerned that this synchronized method could cause a deadlock. It would take a LOT
|
|
170 |
* (mutually dependent services in multiple threads being realized at the same time).
|
|
171 |
*/
|
|
172 | 2823 |
private synchronized Object getService() |
173 |
{ |
|
174 | 2823 |
if (_service == null) |
175 |
{ |
|
176 |
|
|
177 | 1043 |
if (_building)
|
178 | 1 |
throw new ApplicationRuntimeException(ImplMessages.recursiveServiceBuild(this)); |
179 |
|
|
180 | 1042 |
_building = true;
|
181 |
|
|
182 | 1042 |
try
|
183 |
{ |
|
184 |
|
|
185 | 1042 |
ServiceModelFactory factory = getModule().getServiceModelFactory(getServiceModel()); |
186 |
|
|
187 | 1042 |
_serviceModelObject = factory.createServiceModelForService(this);
|
188 |
|
|
189 | 1042 |
_service = _serviceModelObject.getService(); |
190 |
} |
|
191 |
finally
|
|
192 |
{ |
|
193 | 1042 |
_building = false;
|
194 |
} |
|
195 |
} |
|
196 |
|
|
197 | 2819 |
return _service;
|
198 |
} |
|
199 |
|
|
200 | 2819 |
public Object getService(Class serviceInterface)
|
201 |
{ |
|
202 | 2819 |
Object result = getService(); |
203 |
|
|
204 | 2815 |
if (!serviceInterface.isAssignableFrom(result.getClass()))
|
205 |
{ |
|
206 | 1 |
throw new ApplicationRuntimeException(ImplMessages.serviceWrongInterface( |
207 |
this,
|
|
208 |
serviceInterface), getLocation(), null);
|
|
209 |
} |
|
210 |
|
|
211 | 2814 |
return result;
|
212 |
} |
|
213 |
|
|
214 | 1042 |
public String getServiceModel()
|
215 |
{ |
|
216 | 1042 |
return _serviceModel;
|
217 |
} |
|
218 |
|
|
219 | 1643 |
public void setServiceModel(String model) |
220 |
{ |
|
221 | 1643 |
_serviceModel = model; |
222 |
} |
|
223 |
|
|
224 | 710 |
public void clearConstructorInformation() |
225 |
{ |
|
226 | 710 |
_serviceConstructor = null;
|
227 | 710 |
_interceptorContributions = null;
|
228 |
} |
|
229 |
|
|
230 |
// Hm. Does this need to be synchronized?
|
|
231 |
|
|
232 | 724 |
public List getOrderedInterceptorContributions()
|
233 |
{ |
|
234 | 724 |
if (!_interceptorsOrdered)
|
235 |
{ |
|
236 | 724 |
_interceptorContributions = orderInterceptors(); |
237 | 724 |
_interceptorsOrdered = true;
|
238 |
} |
|
239 |
|
|
240 | 724 |
return _interceptorContributions;
|
241 |
} |
|
242 |
|
|
243 | 724 |
private List orderInterceptors()
|
244 |
{ |
|
245 | 724 |
if (HiveMind.isEmpty(_interceptorContributions))
|
246 | 708 |
return null; |
247 |
|
|
248 |
// Any error logging should go to the extension point
|
|
249 |
// we're constructing.
|
|
250 |
|
|
251 | 16 |
Log log = LogFactory.getLog(getExtensionPointId()); |
252 |
|
|
253 | 16 |
Orderer orderer = new Orderer(log, getModule().getErrorHandler(), ImplMessages
|
254 |
.interceptorContribution()); |
|
255 |
|
|
256 | 16 |
Iterator i = _interceptorContributions.iterator(); |
257 | 16 |
while (i.hasNext())
|
258 |
{ |
|
259 | 18 |
ServiceInterceptorContribution sic = (ServiceInterceptorContribution) i.next(); |
260 |
|
|
261 |
// Sort them into runtime excecution order. When we build
|
|
262 |
// the interceptor stack we'll apply them in reverse order,
|
|
263 |
// building outward from the core service implementation.
|
|
264 |
|
|
265 | 18 |
orderer.add(sic, sic.getFactoryServiceId(), sic.getPrecedingInterceptorIds(), sic |
266 |
.getFollowingInterceptorIds()); |
|
267 |
} |
|
268 |
|
|
269 | 16 |
return orderer.getOrderedObjects();
|
270 |
} |
|
271 |
|
|
272 | 718 |
public ShutdownCoordinator getShutdownCoordinator()
|
273 |
{ |
|
274 | 718 |
return _shutdownCoordinator;
|
275 |
} |
|
276 |
|
|
277 | 1644 |
public void setShutdownCoordinator(ShutdownCoordinator coordinator) |
278 |
{ |
|
279 | 1644 |
_shutdownCoordinator = coordinator; |
280 |
} |
|
281 |
|
|
282 |
/**
|
|
283 |
* Forces the service into existence.
|
|
284 |
*/
|
|
285 | 4 |
public void forceServiceInstantiation() |
286 |
{ |
|
287 | 4 |
getService(); |
288 |
|
|
289 | 4 |
_serviceModelObject.instantiateService(); |
290 |
} |
|
291 |
|
|
292 | 389 |
public Occurances getParametersCount()
|
293 |
{ |
|
294 | 389 |
return _parametersCount;
|
295 |
} |
|
296 |
|
|
297 | 1644 |
public void setParametersCount(Occurances occurances) |
298 |
{ |
|
299 | 1644 |
_parametersCount = occurances; |
300 |
} |
|
301 |
|
|
302 |
/**
|
|
303 |
* Returns the service constructor, if defined, or the default service constructor. The default
|
|
304 |
* service constructor comes from the <service-point> itself; other modules can override
|
|
305 |
* this default using an <implementation> element.
|
|
306 |
*/
|
|
307 |
|
|
308 | 2408 |
public ServiceImplementationConstructor getServiceConstructor()
|
309 |
{ |
|
310 | 2408 |
return _serviceConstructor == null ? _defaultServiceConstructor : _serviceConstructor; |
311 |
} |
|
312 |
} |
|