|
|||||||||||||||||||
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.3% | 71.9% | 100% | 77.8% |
|
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.HashSet;
|
|
18 |
import java.util.Iterator;
|
|
19 |
import java.util.List;
|
|
20 |
import java.util.Locale;
|
|
21 |
import java.util.Set;
|
|
22 |
|
|
23 |
import org.apache.commons.logging.Log;
|
|
24 |
import org.apache.commons.logging.LogFactory;
|
|
25 |
import org.apache.hivemind.ErrorHandler;
|
|
26 |
import org.apache.hivemind.ModuleDescriptorProvider;
|
|
27 |
import org.apache.hivemind.Registry;
|
|
28 |
import org.apache.hivemind.internal.RegistryInfrastructure;
|
|
29 |
import org.apache.hivemind.parse.ModuleDescriptor;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* Class used to build a {@link org.apache.hivemind.Registry}from individual
|
|
33 |
* {@link org.apache.hivemind.parse.ModuleDescriptor}. The descriptors are provided by the
|
|
34 |
* {@link ModuleDescriptorProvider}parameter passed to {@link #constructRegistry(Locale)}method.
|
|
35 |
* <p>
|
|
36 |
* A note about threadsafety: The assumption is that a single thread will access the RegistryBuilder
|
|
37 |
* at one time (typically, a startup class within some form of server or application). Code here and
|
|
38 |
* in many of the related classes is divided into construction-time logic and runtime logic. Runtime
|
|
39 |
* logic is synchronized and threadsafe. Construction-time logic is not threadsafe. Once the
|
|
40 |
* registry is fully constructed, it is not allowed to invoke those methods (though, at this time,
|
|
41 |
* no checks occur).
|
|
42 |
* <p>
|
|
43 |
* Runtime methods, such as {@link org.apache.hivemind.impl.ModuleImpl#getService(String, Class)}
|
|
44 |
* are fully threadsafe.
|
|
45 |
*
|
|
46 |
* @author Howard Lewis Ship
|
|
47 |
*/
|
|
48 |
public final class RegistryBuilder |
|
49 |
{ |
|
50 |
private static final Log LOG = LogFactory.getLog(RegistryBuilder.class); |
|
51 |
|
|
52 |
static
|
|
53 |
{ |
|
54 | 1 |
if (!LOG.isErrorEnabled())
|
55 |
{ |
|
56 | 0 |
System.err |
57 |
.println("********************************************************************************");
|
|
58 | 0 |
System.err |
59 |
.println("* 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 *");
|
|
60 | 0 |
System.err |
61 |
.println("* ---------------------------------------------------------------------------- *");
|
|
62 | 0 |
System.err |
63 |
.println("* Logging is not enabled for org.apache.hivemind.impl.RegistryBuilder. *");
|
|
64 | 0 |
System.err |
65 |
.println("* Errors during HiveMind module descriptor parsing and validation may not be *");
|
|
66 | 0 |
System.err |
67 |
.println("* logged. This may result in difficult-to-trace runtime exceptions, if there *");
|
|
68 | 0 |
System.err |
69 |
.println("* are errors in any of your module descriptors. You should enable error *");
|
|
70 | 0 |
System.err |
71 |
.println("* logging for the org.apache.hivemind and hivemind loggers. *");
|
|
72 | 0 |
System.err |
73 |
.println("********************************************************************************");
|
|
74 |
} |
|
75 |
} |
|
76 |
|
|
77 |
/**
|
|
78 |
* Delegate used for handling errors.
|
|
79 |
*/
|
|
80 |
|
|
81 |
private ErrorHandler _errorHandler;
|
|
82 |
|
|
83 |
/**
|
|
84 |
* RegistryAssembly used by the module descriptor parser(s).
|
|
85 |
*/
|
|
86 |
|
|
87 |
private RegistryAssemblyImpl _registryAssembly;
|
|
88 |
|
|
89 |
/**
|
|
90 |
* A set of all {@link ModuleDescriptorProvider}objects used to construct the Registry.
|
|
91 |
*
|
|
92 |
* @since 1.1
|
|
93 |
*/
|
|
94 |
|
|
95 |
private Set _moduleDescriptorProviders;
|
|
96 |
|
|
97 |
/**
|
|
98 |
* Contains most of the logic for actually creating the registry.
|
|
99 |
*
|
|
100 |
* @since 1.1
|
|
101 |
*/
|
|
102 |
|
|
103 |
private RegistryInfrastructureConstructor _constructor;
|
|
104 |
|
|
105 | 115 |
public RegistryBuilder()
|
106 |
{ |
|
107 | 115 |
this(new DefaultErrorHandler()); |
108 |
} |
|
109 |
|
|
110 | 115 |
public RegistryBuilder(ErrorHandler handler)
|
111 |
{ |
|
112 | 115 |
_errorHandler = handler; |
113 |
|
|
114 | 115 |
_registryAssembly = new RegistryAssemblyImpl();
|
115 |
|
|
116 | 115 |
_moduleDescriptorProviders = new HashSet();
|
117 |
|
|
118 | 115 |
_constructor = new RegistryInfrastructureConstructor(handler, LOG, _registryAssembly);
|
119 |
} |
|
120 |
|
|
121 |
/**
|
|
122 |
* Adds a {@link ModuleDescriptorProvider}as a source for
|
|
123 |
* {@link ModuleDescriptor module descriptors}to this RegistryBuilder. Adding the same provider
|
|
124 |
* instance multiple times has no effect.
|
|
125 |
*
|
|
126 |
* @since 1.1
|
|
127 |
*/
|
|
128 | 222 |
public void addModuleDescriptorProvider(ModuleDescriptorProvider provider) |
129 |
{ |
|
130 | 222 |
_moduleDescriptorProviders.add(provider); |
131 |
} |
|
132 |
|
|
133 |
/**
|
|
134 |
* This first loads all modules provided by the ModuleDescriptorProvider, then resolves all the
|
|
135 |
* contributions, then constructs and returns the Registry.
|
|
136 |
*/
|
|
137 | 115 |
public Registry constructRegistry(Locale locale)
|
138 |
{ |
|
139 | 115 |
for (Iterator i = _moduleDescriptorProviders.iterator(); i.hasNext();)
|
140 |
{ |
|
141 | 222 |
ModuleDescriptorProvider provider = (ModuleDescriptorProvider) i.next(); |
142 |
|
|
143 | 222 |
processModuleDescriptorProvider(provider); |
144 |
} |
|
145 |
|
|
146 |
// Process any deferred operations. Post processing is added by
|
|
147 |
// both the parser and the registry constructor.
|
|
148 |
|
|
149 | 115 |
_registryAssembly.performPostProcessing(); |
150 |
|
|
151 | 115 |
RegistryInfrastructure infrastructure = _constructor |
152 |
.constructRegistryInfrastructure(locale); |
|
153 |
|
|
154 | 115 |
infrastructure.startup(); |
155 |
|
|
156 | 115 |
return new RegistryImpl(infrastructure); |
157 |
} |
|
158 |
|
|
159 | 222 |
private void processModuleDescriptorProvider(ModuleDescriptorProvider provider) |
160 |
{ |
|
161 | 222 |
List descriptors = provider.getModuleDescriptors(_errorHandler); |
162 |
|
|
163 | 222 |
Iterator i = descriptors.iterator(); |
164 | 222 |
while (i.hasNext())
|
165 |
{ |
|
166 | 234 |
ModuleDescriptor md = (ModuleDescriptor) i.next(); |
167 |
|
|
168 | 234 |
_constructor.addModuleDescriptor(md); |
169 |
} |
|
170 |
} |
|
171 |
|
|
172 |
/**
|
|
173 |
* Adds a default module descriptor provider to this <code>RegistryBuilder</code>. A
|
|
174 |
* default module descriptor provider is merely a {@link XmlModuleDescriptorProvider}
|
|
175 |
* constructed with a {@link DefaultClassResolver}.
|
|
176 |
* @since 1.1
|
|
177 |
*/
|
|
178 | 4 |
public void addDefaultModuleDescriptorProvider() |
179 |
{ |
|
180 | 4 |
addModuleDescriptorProvider(new XmlModuleDescriptorProvider(new DefaultClassResolver())); |
181 |
} |
|
182 |
|
|
183 |
/**
|
|
184 |
* Constructs a default registry based on just the modules visible to the thread context class
|
|
185 |
* loader (this is sufficient is the majority of cases), and using the default locale. If you
|
|
186 |
* have different error handling needs, or wish to pick up HiveMind module deployment
|
|
187 |
* descriptors for non-standard locations, you must create a RegistryBuilder instance yourself.
|
|
188 |
* @see #addDefaultModuleDescriptorProvider()
|
|
189 |
*/
|
|
190 | 4 |
public static Registry constructDefaultRegistry() |
191 |
{ |
|
192 | 4 |
RegistryBuilder builder = new RegistryBuilder();
|
193 | 4 |
builder.addDefaultModuleDescriptorProvider(); |
194 | 4 |
return builder.constructRegistry(Locale.getDefault());
|
195 |
} |
|
196 |
|
|
197 |
} |
|