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