|
|||||||||||||||||||
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 | |||||||||||||||
RegistryBuilder.java | 88.2% | 88.7% | 100% | 89.4% |
|
1 |
// Copyright 2004 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.io.IOException;
|
|
18 |
import java.net.URL;
|
|
19 |
import java.util.ArrayList;
|
|
20 |
import java.util.Collection;
|
|
21 |
import java.util.Enumeration;
|
|
22 |
import java.util.HashMap;
|
|
23 |
import java.util.Iterator;
|
|
24 |
import java.util.List;
|
|
25 |
import java.util.Locale;
|
|
26 |
import java.util.Map;
|
|
27 |
|
|
28 |
import org.apache.commons.logging.Log;
|
|
29 |
import org.apache.commons.logging.LogFactory;
|
|
30 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
31 |
import org.apache.hivemind.ClassResolver;
|
|
32 |
import org.apache.hivemind.ErrorHandler;
|
|
33 |
import org.apache.hivemind.HiveMind;
|
|
34 |
import org.apache.hivemind.Occurances;
|
|
35 |
import org.apache.hivemind.Registry;
|
|
36 |
import org.apache.hivemind.Resource;
|
|
37 |
import org.apache.hivemind.ShutdownCoordinator;
|
|
38 |
import org.apache.hivemind.internal.Module;
|
|
39 |
import org.apache.hivemind.parse.ConfigurationPointDescriptor;
|
|
40 |
import org.apache.hivemind.parse.ContributionDescriptor;
|
|
41 |
import org.apache.hivemind.parse.DescriptorParser;
|
|
42 |
import org.apache.hivemind.parse.ImplementationDescriptor;
|
|
43 |
import org.apache.hivemind.parse.InstanceBuilder;
|
|
44 |
import org.apache.hivemind.parse.InterceptorDescriptor;
|
|
45 |
import org.apache.hivemind.parse.ModuleDescriptor;
|
|
46 |
import org.apache.hivemind.parse.ServicePointDescriptor;
|
|
47 |
import org.apache.hivemind.util.IdUtils;
|
|
48 |
import org.apache.hivemind.util.URLResource;
|
|
49 |
|
|
50 |
/**
|
|
51 |
* Class used to build a {@link org.apache.hivemind.Registry} from individual
|
|
52 |
* {@link org.apache.hivemind.parse.ModuleDescriptor}. The descriptors
|
|
53 |
* are processed one at a time and the registry is constructed using a single call
|
|
54 |
* to {@link #constructRegistry(Locale)} at the end.
|
|
55 |
*
|
|
56 |
* <p>
|
|
57 |
* A note about threadsafety: The assumption is that a single thread will access the RegistryBuilder
|
|
58 |
* at one time (typically, a startup class within some form of server or application). Code
|
|
59 |
* here and in many of the related classes is divided into construction-time logic
|
|
60 |
* and runtime logic. Runtime logic is synchronized and threadsafe. Construction-time logic
|
|
61 |
* is not threadsafe. Methods such as {@link org.apache.hivemind.impl.RegistryImpl#addModule(Module)},
|
|
62 |
* {@link org.apache.hivemind.impl.ModuleImpl#addConfigurationPoint(ConfigurationPoint)},
|
|
63 |
* {@link org.apache.hivemind.impl.ConfigurationPointImpl#addContribution(Contribution)}
|
|
64 |
* and the like are construction-time. Once the registry is fully constructed, it is not
|
|
65 |
* allowed to invoke those methods (though, at this time, no checks occur).
|
|
66 |
*
|
|
67 |
* <p>Runtime methods, such as {@link org.apache.hivemind.impl.ModuleImpl#getService(String, Class)}
|
|
68 |
* are fully threadsafe.
|
|
69 |
*
|
|
70 |
* @author Howard Lewis Ship
|
|
71 |
*/
|
|
72 |
public final class RegistryBuilder |
|
73 |
{ |
|
74 |
private static final Log LOG = LogFactory.getLog(RegistryBuilder.class); |
|
75 |
|
|
76 |
static {
|
|
77 | 1 |
if (!LOG.isErrorEnabled())
|
78 |
{ |
|
79 | 0 |
System.err.println( |
80 |
"********************************************************************************");
|
|
81 | 0 |
System.err.println( |
82 |
"* L O G G I N G C O N F I G U R A T I O N E R R O R *");
|
|
83 | 0 |
System.err.println( |
84 |
"* ---------------------------------------------------------------------------- *");
|
|
85 | 0 |
System.err.println( |
86 |
"* Logging is not enabled for org.apache.hivemind.impl.RegistryBuilder. *");
|
|
87 | 0 |
System.err.println( |
88 |
"* Errors during HiveMind module descriptor parsing and validation may not be *");
|
|
89 | 0 |
System.err.println( |
90 |
"* logged. This may result in difficult-to-trace runtime exceptions, if there *");
|
|
91 | 0 |
System.err.println( |
92 |
"* are errors in any of your module descriptors. You should enable error *");
|
|
93 | 0 |
System.err.println( |
94 |
"* logging for the org.apache.hivemind and hivemind loggers. *");
|
|
95 | 0 |
System.err.println( |
96 |
"********************************************************************************");
|
|
97 |
} |
|
98 |
} |
|
99 |
|
|
100 |
/**
|
|
101 |
* The path, within a JAR or the classpath, to the XML HiveMind module
|
|
102 |
* deployment descriptor: <code>META-INF/hivemodule.xml</code>.
|
|
103 |
*/
|
|
104 |
public static final String HIVE_MODULE_XML = "META-INF/hivemodule.xml"; |
|
105 |
|
|
106 |
/**
|
|
107 |
* List of {@link ModuleDescriptor}.
|
|
108 |
*/
|
|
109 |
|
|
110 |
private List _moduleDescriptors = new ArrayList(); |
|
111 |
|
|
112 |
/**
|
|
113 |
* Map of {@link ModuleImpl} keyed on fully qualified module id.
|
|
114 |
*/
|
|
115 |
private Map _modules = new HashMap(); |
|
116 |
|
|
117 |
/**
|
|
118 |
* Map of {@link PrimitiveServiceModel} (or subclass) keyed on fully qualified id.
|
|
119 |
*/
|
|
120 |
|
|
121 |
private Map _servicePoints = new HashMap(); |
|
122 |
|
|
123 |
/**
|
|
124 |
* Map of {@link ConfigurationPointImpl} keyed on fully qualified id.
|
|
125 |
*/
|
|
126 |
|
|
127 |
private Map _configurationPoints = new HashMap(); |
|
128 |
|
|
129 |
/**
|
|
130 |
* Delegate used for handling errors.
|
|
131 |
*/
|
|
132 |
|
|
133 |
private ErrorHandler _errorHandler;
|
|
134 |
|
|
135 |
/**
|
|
136 |
* Parser instance used by all parsing for this builder.
|
|
137 |
*/
|
|
138 |
private DescriptorParser _parser;
|
|
139 |
|
|
140 |
/**
|
|
141 |
* Shutdown coordinator shared by all objects.
|
|
142 |
*/
|
|
143 |
|
|
144 |
private ShutdownCoordinator _shutdownCoordinator = new ShutdownCoordinatorImpl(); |
|
145 |
|
|
146 |
/**
|
|
147 |
* RegistryAssembly used by the module descriptor parser(s).
|
|
148 |
*/
|
|
149 |
|
|
150 |
private RegistryAssemblyImpl _registryAssembly;
|
|
151 |
|
|
152 | 96 |
public RegistryBuilder()
|
153 |
{ |
|
154 | 96 |
this(new DefaultErrorHandler()); |
155 |
} |
|
156 |
|
|
157 | 96 |
public RegistryBuilder(ErrorHandler handler)
|
158 |
{ |
|
159 | 96 |
_errorHandler = handler; |
160 |
|
|
161 | 96 |
_registryAssembly = new RegistryAssemblyImpl(handler);
|
162 |
} |
|
163 |
|
|
164 |
/**
|
|
165 |
* Processes all modules that can be found using the resolver.
|
|
166 |
*/
|
|
167 | 96 |
public void processModules(ClassResolver resolver) |
168 |
{ |
|
169 | 96 |
if (LOG.isDebugEnabled())
|
170 | 9 |
LOG.debug("Processing modules visible to " + resolver);
|
171 |
|
|
172 | 96 |
processModulesResources(resolver, HIVE_MODULE_XML); |
173 |
} |
|
174 |
|
|
175 |
/**
|
|
176 |
* Locates module deployment descriptors in the "standard" location (META-INF).
|
|
177 |
*/
|
|
178 | 96 |
private void processModulesResources(ClassResolver resolver, String resourcePath) |
179 |
{ |
|
180 | 96 |
ClassLoader loader = resolver.getClassLoader(); |
181 | 96 |
Enumeration e = null;
|
182 |
|
|
183 | 96 |
try
|
184 |
{ |
|
185 | 96 |
e = loader.getResources(resourcePath); |
186 |
} |
|
187 |
catch (IOException ex)
|
|
188 |
{ |
|
189 | 0 |
throw new ApplicationRuntimeException( |
190 |
ImplMessages.unableToFindModules(resolver, ex), |
|
191 |
ex); |
|
192 |
} |
|
193 |
|
|
194 | 96 |
while (e.hasMoreElements())
|
195 |
{ |
|
196 | 97 |
URL descriptorURL = (URL) e.nextElement(); |
197 | 97 |
Resource descriptorResource = new URLResource(descriptorURL);
|
198 |
|
|
199 | 97 |
processModule(resolver, descriptorResource); |
200 |
} |
|
201 |
} |
|
202 |
|
|
203 |
/**
|
|
204 |
* Parses a module and processes its contents. This is often used
|
|
205 |
* in conjunction with {@link #processModules(ClassResolver)} to
|
|
206 |
* parse additional modules that are not in the standard location
|
|
207 |
* (for whatever reason).
|
|
208 |
*/
|
|
209 | 193 |
public void processModule(ClassResolver resolver, Resource moduleDescriptorResource) |
210 |
{ |
|
211 | 193 |
if (_parser == null) |
212 | 96 |
_parser = new DescriptorParser(_errorHandler, _registryAssembly);
|
213 |
|
|
214 | 193 |
try
|
215 |
{ |
|
216 | 193 |
ModuleDescriptor md = _parser.parse(moduleDescriptorResource, resolver); |
217 |
|
|
218 | 193 |
processModule(md); |
219 |
|
|
220 |
// After parsing a module, parse any additional modules identified
|
|
221 |
// within the module (using the <sub-module> element.
|
|
222 |
|
|
223 | 193 |
while (_registryAssembly.moreQueuedModules())
|
224 |
{ |
|
225 | 1 |
md = _registryAssembly.parseNextQueued(_parser); |
226 |
|
|
227 | 1 |
processModule(md); |
228 |
} |
|
229 |
} |
|
230 |
catch (RuntimeException ex)
|
|
231 |
{ |
|
232 |
// An exception may leave the parser in an unknown state, so
|
|
233 |
// give up on that instance and start with a fresh one.
|
|
234 |
|
|
235 | 0 |
_parser = null;
|
236 |
|
|
237 | 0 |
_errorHandler.error(LOG, ex.getMessage(), HiveMind.getLocation(ex), ex); |
238 |
} |
|
239 |
} |
|
240 |
|
|
241 |
/**
|
|
242 |
* Processes a parsed HiveMind module descriptor. This may be called
|
|
243 |
* repeatedly before invoking {@link #constructRegistry(Locale)}.
|
|
244 |
*
|
|
245 |
* @param md the parsed module descriptor
|
|
246 |
*/
|
|
247 | 194 |
public void processModule(ModuleDescriptor md) |
248 |
{ |
|
249 | 194 |
String id = md.getModuleId(); |
250 |
|
|
251 | 194 |
if (LOG.isDebugEnabled())
|
252 | 18 |
LOG.debug("Processing module " + id);
|
253 |
|
|
254 | 194 |
if (_modules.containsKey(id))
|
255 |
{ |
|
256 | 0 |
Module existing = (Module) _modules.get(id); |
257 |
|
|
258 | 0 |
_errorHandler.error(LOG, ImplMessages.duplicateModuleId(existing, md), null, null); |
259 |
|
|
260 |
// Ignore the duplicate module descriptor.
|
|
261 | 0 |
return;
|
262 |
} |
|
263 |
|
|
264 | 194 |
ModuleImpl module = new ModuleImpl();
|
265 |
|
|
266 | 194 |
module.setLocation(md.getLocation()); |
267 | 194 |
module.setModuleId(id); |
268 | 194 |
module.setClassResolver(md.getClassResolver()); |
269 |
|
|
270 | 194 |
_modules.put(id, module); |
271 |
|
|
272 | 194 |
_moduleDescriptors.add(md); |
273 |
} |
|
274 |
|
|
275 | 96 |
private void addServiceAndConfigurationPoints(RegistryImpl registry) |
276 |
{ |
|
277 | 96 |
int count = _moduleDescriptors.size();
|
278 |
|
|
279 | 96 |
for (int i = 0; i < count; i++) |
280 |
{ |
|
281 | 194 |
ModuleDescriptor md = (ModuleDescriptor) _moduleDescriptors.get(i); |
282 |
|
|
283 | 194 |
String id = md.getModuleId(); |
284 |
|
|
285 | 194 |
ModuleImpl module = (ModuleImpl) _modules.get(id); |
286 |
|
|
287 | 194 |
addServicePoints(registry, module, md); |
288 |
|
|
289 | 194 |
addConfigurationPoints(registry, module, md); |
290 |
} |
|
291 |
} |
|
292 |
|
|
293 | 194 |
private void addServicePoints(RegistryImpl registry, ModuleImpl module, ModuleDescriptor md) |
294 |
{ |
|
295 | 194 |
String moduleId = md.getModuleId(); |
296 | 194 |
List services = md.getServicePoints(); |
297 | 194 |
int count = size(services);
|
298 |
|
|
299 | 194 |
for (int i = 0; i < count; i++) |
300 |
{ |
|
301 | 1544 |
ServicePointDescriptor sd = (ServicePointDescriptor) services.get(i); |
302 |
|
|
303 | 1544 |
String pointId = moduleId + "." + sd.getId();
|
304 |
|
|
305 | 1544 |
if (LOG.isDebugEnabled())
|
306 | 135 |
LOG.debug("Creating service extension point: " + pointId);
|
307 |
|
|
308 |
// Choose which class to instantiate based on
|
|
309 |
// whether the service is create-on-first-reference
|
|
310 |
// or create-on-first-use (deferred).
|
|
311 |
|
|
312 | 1544 |
ServicePointImpl point = new ServicePointImpl();
|
313 |
|
|
314 | 1544 |
point.setExtensionPointId(pointId); |
315 | 1544 |
point.setLocation(sd.getLocation()); |
316 | 1544 |
point.setModule(module); |
317 |
|
|
318 | 1544 |
point.setServiceInterfaceName(sd.getInterfaceClassName()); |
319 | 1544 |
point.setParametersSchema(sd.getParametersSchema()); |
320 | 1544 |
point.setParametersCount(sd.getParametersCount()); |
321 |
|
|
322 | 1544 |
point.setShutdownCoordinator(_shutdownCoordinator); |
323 |
|
|
324 | 1544 |
registry.addServicePoint(point); |
325 |
|
|
326 |
// Save this for the second phase, where contributions
|
|
327 |
// from other modules are applied.
|
|
328 |
|
|
329 | 1544 |
_servicePoints.put(pointId, point); |
330 |
|
|
331 | 1544 |
addInternalImplementations(module, pointId, sd); |
332 |
} |
|
333 |
} |
|
334 |
|
|
335 | 194 |
private void addConfigurationPoints( |
336 |
RegistryImpl registry, |
|
337 |
ModuleImpl module, |
|
338 |
ModuleDescriptor md) |
|
339 |
{ |
|
340 | 194 |
String moduleId = md.getModuleId(); |
341 | 194 |
List points = md.getConfigurationPoints(); |
342 | 194 |
int count = size(points);
|
343 |
|
|
344 | 194 |
for (int i = 0; i < count; i++) |
345 |
{ |
|
346 | 819 |
ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) points.get(i); |
347 |
|
|
348 | 819 |
String pointId = moduleId + "." + cpd.getId();
|
349 |
|
|
350 | 819 |
if (LOG.isDebugEnabled())
|
351 | 80 |
LOG.debug("Creating extension point " + pointId);
|
352 |
|
|
353 | 819 |
ConfigurationPointImpl point = new ConfigurationPointImpl();
|
354 |
|
|
355 | 819 |
point.setExtensionPointId(pointId); |
356 | 819 |
point.setLocation(cpd.getLocation()); |
357 | 819 |
point.setModule(module); |
358 | 819 |
point.setExpectedCount(cpd.getCount()); |
359 | 819 |
point.setContributionsSchema(cpd.getContributionsSchema()); |
360 |
|
|
361 | 819 |
point.setShutdownCoordinator(_shutdownCoordinator); |
362 |
|
|
363 | 819 |
registry.addConfigurationPoint(point); |
364 |
|
|
365 |
// Needed later when we reconcile the rest
|
|
366 |
// of the configuration contributions.
|
|
367 |
|
|
368 | 819 |
_configurationPoints.put(pointId, point); |
369 |
} |
|
370 |
} |
|
371 |
|
|
372 | 545 |
private void addContributionElements( |
373 |
Module sourceModule, |
|
374 |
ConfigurationPointImpl point, |
|
375 |
List elements) |
|
376 |
{ |
|
377 | 545 |
if (size(elements) == 0)
|
378 | 0 |
return;
|
379 |
|
|
380 | 545 |
if (LOG.isDebugEnabled())
|
381 | 54 |
LOG.debug("Adding extensions to configuration point " + point.getExtensionPointId());
|
382 |
|
|
383 | 545 |
ContributionImpl c = new ContributionImpl();
|
384 | 545 |
c.setContributingModule(sourceModule); |
385 | 545 |
c.addElements(elements); |
386 |
|
|
387 | 545 |
point.addContribution(c); |
388 |
} |
|
389 |
|
|
390 |
/**
|
|
391 |
* Invoked after all modules have been added with
|
|
392 |
* {@link #processModule(ClassResolver, Resource)}.
|
|
393 |
* This first resolves all the contributions, then constructs and returns
|
|
394 |
* the registry.
|
|
395 |
*/
|
|
396 | 96 |
public Registry constructRegistry(Locale locale)
|
397 |
{ |
|
398 |
|
|
399 |
// Process any deferred operations
|
|
400 |
|
|
401 | 96 |
_registryAssembly.performPostProcessing(); |
402 |
|
|
403 | 96 |
RegistryImpl result = new RegistryImpl(_errorHandler, locale);
|
404 |
|
|
405 | 96 |
addServiceAndConfigurationPoints(result); |
406 |
|
|
407 | 96 |
addImplementationsAndContributions(); |
408 |
|
|
409 | 96 |
checkForMissingServices(); |
410 |
|
|
411 | 96 |
checkContributionCounts(); |
412 |
|
|
413 | 96 |
result.setShutdownCoordinator(_shutdownCoordinator); |
414 |
|
|
415 | 96 |
addModulesToRegistry(result); |
416 |
|
|
417 | 96 |
result.startup(); |
418 |
|
|
419 | 96 |
return result;
|
420 |
} |
|
421 |
|
|
422 | 96 |
private void addModulesToRegistry(RegistryImpl registry) |
423 |
{ |
|
424 |
// Add each module to the registry.
|
|
425 |
|
|
426 | 96 |
Iterator i = _modules.values().iterator(); |
427 | 96 |
while (i.hasNext())
|
428 |
{ |
|
429 | 194 |
ModuleImpl module = (ModuleImpl) i.next(); |
430 |
|
|
431 | 194 |
if (LOG.isDebugEnabled())
|
432 | 18 |
LOG.debug("Adding module " + module.getModuleId() + " to registry"); |
433 |
|
|
434 | 194 |
module.setRegistry(registry); |
435 |
} |
|
436 |
} |
|
437 |
|
|
438 | 96 |
private void addImplementationsAndContributions() |
439 |
{ |
|
440 | 96 |
int count = _moduleDescriptors.size();
|
441 |
|
|
442 | 96 |
for (int i = 0; i < count; i++) |
443 |
{ |
|
444 | 194 |
ModuleDescriptor md = (ModuleDescriptor) _moduleDescriptors.get(i); |
445 |
|
|
446 | 194 |
if (LOG.isDebugEnabled())
|
447 | 18 |
LOG.debug("Adding contributions from module " + md.getModuleId());
|
448 |
|
|
449 | 194 |
addImplementations(md); |
450 | 194 |
addContributions(md); |
451 |
} |
|
452 |
} |
|
453 |
|
|
454 | 194 |
private void addImplementations(ModuleDescriptor md) |
455 |
{ |
|
456 | 194 |
String moduleId = md.getModuleId(); |
457 | 194 |
ModuleImpl sourceModule = (ModuleImpl) _modules.get(moduleId); |
458 |
|
|
459 | 194 |
List implementations = md.getImplementations(); |
460 | 194 |
int count = size(implementations);
|
461 |
|
|
462 | 194 |
for (int i = 0; i < count; i++) |
463 |
{ |
|
464 | 3 |
ImplementationDescriptor impl = (ImplementationDescriptor) implementations.get(i); |
465 |
|
|
466 | 3 |
String pointId = impl.getServiceId(); |
467 | 3 |
String qualifiedId = qualify(moduleId, pointId); |
468 |
|
|
469 | 3 |
addImplementations(sourceModule, qualifiedId, impl); |
470 |
} |
|
471 |
|
|
472 |
} |
|
473 |
|
|
474 | 194 |
private void addContributions(ModuleDescriptor md) |
475 |
{ |
|
476 | 194 |
String moduleId = md.getModuleId(); |
477 | 194 |
ModuleImpl sourceModule = (ModuleImpl) _modules.get(moduleId); |
478 |
|
|
479 | 194 |
List contributions = md.getContributions(); |
480 | 194 |
int count = size(contributions);
|
481 |
|
|
482 | 194 |
for (int i = 0; i < count; i++) |
483 |
{ |
|
484 | 546 |
ContributionDescriptor cd = (ContributionDescriptor) contributions.get(i); |
485 |
|
|
486 | 546 |
String pointId = cd.getConfigurationId(); |
487 | 546 |
String qualifiedId = qualify(moduleId, pointId); |
488 |
|
|
489 | 546 |
ConfigurationPointImpl point = |
490 |
(ConfigurationPointImpl) _configurationPoints.get(qualifiedId); |
|
491 |
|
|
492 | 546 |
if (point == null) |
493 |
{ |
|
494 | 1 |
_errorHandler.error( |
495 |
LOG, |
|
496 |
ImplMessages.unknownConfigurationPoint(moduleId, cd), |
|
497 |
cd.getLocation(), |
|
498 |
null);
|
|
499 |
|
|
500 | 1 |
continue;
|
501 |
} |
|
502 |
|
|
503 | 545 |
addContributionElements(sourceModule, point, cd.getElements()); |
504 |
} |
|
505 |
} |
|
506 |
|
|
507 |
/**
|
|
508 |
* Qualifies the artifact id with the module id, if necessary.
|
|
509 |
*/
|
|
510 | 579 |
private static String qualify(String moduleId, String artifactId) |
511 |
{ |
|
512 | 579 |
if (artifactId.indexOf('.') >= 0)
|
513 | 48 |
return artifactId;
|
514 |
|
|
515 | 531 |
return moduleId + "." + artifactId; |
516 |
} |
|
517 |
|
|
518 |
/**
|
|
519 |
* Adds internal service contributions; the contributions provided inplace
|
|
520 |
* with the service definition.
|
|
521 |
*/
|
|
522 | 1544 |
private void addInternalImplementations( |
523 |
ModuleImpl sourceModule, |
|
524 |
String pointId, |
|
525 |
ServicePointDescriptor spd) |
|
526 |
{ |
|
527 | 1544 |
InstanceBuilder builder = spd.getInstanceBuilder(); |
528 | 1544 |
List interceptors = spd.getInterceptors(); |
529 |
|
|
530 | 1544 |
if (builder == null && interceptors == null) |
531 | 1 |
return;
|
532 |
|
|
533 | 1543 |
if (builder != null) |
534 | 1543 |
addServiceInstanceBuilder(sourceModule, pointId, builder); |
535 |
|
|
536 | 1543 |
if (interceptors == null) |
537 | 1516 |
return;
|
538 |
|
|
539 | 27 |
int count = size(interceptors);
|
540 |
|
|
541 | 27 |
for (int i = 0; i < count; i++) |
542 |
{ |
|
543 | 27 |
InterceptorDescriptor id = (InterceptorDescriptor) interceptors.get(i); |
544 | 27 |
addInterceptor(sourceModule, pointId, id); |
545 |
} |
|
546 |
} |
|
547 |
|
|
548 |
/**
|
|
549 |
* Adds ordinary service contributions.
|
|
550 |
*/
|
|
551 |
|
|
552 | 3 |
private void addImplementations( |
553 |
ModuleImpl sourceModule, |
|
554 |
String pointId, |
|
555 |
ImplementationDescriptor id) |
|
556 |
{ |
|
557 | 3 |
InstanceBuilder builder = id.getInstanceBuilder(); |
558 | 3 |
List interceptors = id.getInterceptors(); |
559 |
|
|
560 | 3 |
if (builder != null) |
561 | 1 |
addServiceInstanceBuilder(sourceModule, pointId, builder); |
562 |
|
|
563 | 3 |
int count = size(interceptors);
|
564 | 3 |
for (int i = 0; i < count; i++) |
565 |
{ |
|
566 | 3 |
InterceptorDescriptor ind = (InterceptorDescriptor) interceptors.get(i); |
567 | 3 |
addInterceptor(sourceModule, pointId, ind); |
568 |
} |
|
569 |
} |
|
570 |
|
|
571 |
/**
|
|
572 |
* Adds an {@link InstanceBuilder}
|
|
573 |
* to a service extension point.
|
|
574 |
*
|
|
575 |
*
|
|
576 |
*/
|
|
577 |
|
|
578 | 1544 |
private void addServiceInstanceBuilder( |
579 |
ModuleImpl sourceModule, |
|
580 |
String pointId, |
|
581 |
InstanceBuilder builder) |
|
582 |
{ |
|
583 | 1544 |
if (LOG.isDebugEnabled())
|
584 | 135 |
LOG.debug("Adding " + builder + " to service extension point " + pointId); |
585 |
|
|
586 | 1544 |
ServicePointImpl sep = (ServicePointImpl) _servicePoints.get(pointId); |
587 |
|
|
588 | 1544 |
if (sep == null) |
589 |
{ |
|
590 | 0 |
_errorHandler.error( |
591 |
LOG, |
|
592 |
ImplMessages.unknownServicePoint(sourceModule, pointId), |
|
593 |
builder.getLocation(), |
|
594 |
null);
|
|
595 | 0 |
return;
|
596 |
} |
|
597 |
|
|
598 | 1544 |
if (sep.getServiceConstructor() != null) |
599 |
{ |
|
600 | 0 |
LOG.error(ImplMessages.duplicateFactory(sourceModule, pointId, sep)); |
601 |
|
|
602 | 0 |
return;
|
603 |
} |
|
604 |
|
|
605 | 1544 |
sep.setServiceModel(builder.getServiceModel()); |
606 | 1544 |
sep.setServiceConstructor(builder.createConstructor(sep, sourceModule)); |
607 |
} |
|
608 |
|
|
609 | 30 |
private void addInterceptor(ModuleImpl sourceModule, String pointId, InterceptorDescriptor id) |
610 |
{ |
|
611 | 30 |
if (LOG.isDebugEnabled())
|
612 | 0 |
LOG.debug("Adding " + id + " to service extension point " + pointId); |
613 |
|
|
614 | 30 |
ServicePointImpl sep = (ServicePointImpl) _servicePoints.get(pointId); |
615 |
|
|
616 | 30 |
String sourceModuleId = sourceModule.getModuleId(); |
617 |
|
|
618 | 30 |
if (sep == null) |
619 |
{ |
|
620 | 0 |
_errorHandler.error( |
621 |
LOG, |
|
622 |
ImplMessages.unknownServicePoint(sourceModule, pointId), |
|
623 |
id.getLocation(), |
|
624 |
null);
|
|
625 |
|
|
626 | 0 |
return;
|
627 |
} |
|
628 |
|
|
629 | 30 |
ServiceInterceptorContributionImpl sic = new ServiceInterceptorContributionImpl();
|
630 |
|
|
631 |
// Allow the factory id to be unqualified, to refer to an interceptor factory
|
|
632 |
// service from within the same module.
|
|
633 |
|
|
634 | 30 |
sic.setFactoryServiceId(qualify(sourceModuleId, id.getFactoryServiceId())); |
635 | 30 |
sic.setLocation(id.getLocation()); |
636 |
|
|
637 | 30 |
sic.setFollowingInterceptorIds(IdUtils.qualifyList(sourceModuleId, id.getBefore())); |
638 | 30 |
sic.setPrecedingInterceptorIds(IdUtils.qualifyList(sourceModuleId, id.getAfter())); |
639 |
|
|
640 | 30 |
sic.setContributingModule(sourceModule); |
641 | 30 |
sic.setParameters(id.getParameters()); |
642 |
|
|
643 | 30 |
sep.addInterceptorContribution(sic); |
644 |
} |
|
645 |
|
|
646 |
/**
|
|
647 |
* Checks that each service has at service constructor.
|
|
648 |
*/
|
|
649 | 96 |
private void checkForMissingServices() |
650 |
{ |
|
651 | 96 |
Iterator i = _servicePoints.values().iterator(); |
652 | 96 |
while (i.hasNext())
|
653 |
{ |
|
654 | 1543 |
ServicePointImpl point = (ServicePointImpl) i.next(); |
655 |
|
|
656 | 1543 |
if (point.getServiceConstructor() != null) |
657 | 1543 |
continue;
|
658 |
|
|
659 | 0 |
_errorHandler.error(LOG, ImplMessages.missingService(point), null, null); |
660 |
} |
|
661 |
} |
|
662 |
|
|
663 |
/**
|
|
664 |
* Checks that each configuration extension point has the right number of contributions.
|
|
665 |
*/
|
|
666 |
|
|
667 | 96 |
private void checkContributionCounts() |
668 |
{ |
|
669 | 96 |
Iterator i = _configurationPoints.values().iterator(); |
670 |
|
|
671 | 96 |
while (i.hasNext())
|
672 |
{ |
|
673 | 819 |
ConfigurationPointImpl point = (ConfigurationPointImpl) i.next(); |
674 |
|
|
675 | 819 |
Occurances expected = point.getExpectedCount(); |
676 |
|
|
677 | 819 |
int actual = point.getContributionCount();
|
678 |
|
|
679 | 819 |
if (expected.inRange(actual))
|
680 | 817 |
continue;
|
681 |
|
|
682 | 2 |
_errorHandler.error( |
683 |
LOG, |
|
684 |
ImplMessages.wrongNumberOfContributions(point, actual, expected), |
|
685 |
point.getLocation(), |
|
686 |
null);
|
|
687 |
} |
|
688 |
|
|
689 |
} |
|
690 |
|
|
691 | 1351 |
private static int size(Collection c) |
692 |
{ |
|
693 | 1351 |
return c == null ? 0 : c.size(); |
694 |
} |
|
695 |
|
|
696 |
/**
|
|
697 |
* Constructs a default registry based on just the modules
|
|
698 |
* visible to the thread context class loader (this is sufficient
|
|
699 |
* is the majority of cases), and using the default locale. If you have different
|
|
700 |
* error handling needs, or wish to pick up HiveMind module deployment
|
|
701 |
* descriptors for non-standard locations, you must create
|
|
702 |
* a RegistryBuilder instance yourself.
|
|
703 |
*/
|
|
704 | 2 |
public static Registry constructDefaultRegistry() |
705 |
{ |
|
706 | 2 |
ClassResolver resolver = new DefaultClassResolver();
|
707 | 2 |
RegistryBuilder builder = new RegistryBuilder();
|
708 |
|
|
709 | 2 |
builder.processModules(resolver); |
710 |
|
|
711 | 2 |
return builder.constructRegistry(Locale.getDefault());
|
712 |
} |
|
713 |
|
|
714 |
} |
|
715 |
|
|