|
|||||||||||||||||||
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 | |||||||||||||||
XmlModuleDescriptorProvider.java | 100% | 95.3% | 100% | 96.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.io.IOException;
|
|
18 |
import java.net.URL;
|
|
19 |
import java.util.ArrayList;
|
|
20 |
import java.util.Enumeration;
|
|
21 |
import java.util.Iterator;
|
|
22 |
import java.util.List;
|
|
23 |
|
|
24 |
import org.apache.commons.logging.Log;
|
|
25 |
import org.apache.commons.logging.LogFactory;
|
|
26 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
27 |
import org.apache.hivemind.ClassResolver;
|
|
28 |
import org.apache.hivemind.ErrorHandler;
|
|
29 |
import org.apache.hivemind.HiveMind;
|
|
30 |
import org.apache.hivemind.ModuleDescriptorProvider;
|
|
31 |
import org.apache.hivemind.Resource;
|
|
32 |
import org.apache.hivemind.parse.ModuleDescriptor;
|
|
33 |
import org.apache.hivemind.parse.SubModuleDescriptor;
|
|
34 |
import org.apache.hivemind.parse.XmlResourceProcessor;
|
|
35 |
import org.apache.hivemind.util.URLResource;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* Implementation of the {@link ModuleDescriptorProvider}interface which uses the
|
|
39 |
* {@link org.apache.hivemind.parse.DescriptorParser}to provide module descriptors defined in XML.
|
|
40 |
* The module descriptors are loaded from files or resources on the classpath.
|
|
41 |
*
|
|
42 |
* @author Knut Wannheden
|
|
43 |
* @since 1.1
|
|
44 |
*/
|
|
45 |
public class XmlModuleDescriptorProvider implements ModuleDescriptorProvider |
|
46 |
{ |
|
47 |
private static final Log LOG = LogFactory.getLog(XmlModuleDescriptorProvider.class); |
|
48 |
|
|
49 |
/**
|
|
50 |
* The default path, within a JAR or the classpath, to the XML HiveMind module deployment
|
|
51 |
* descriptor: <code>META-INF/hivemodule.xml</code>. Use this constant with the
|
|
52 |
* {@link XmlModuleDescriptorProvider(ClassResolver, String)}constructor.
|
|
53 |
*/
|
|
54 |
public static final String HIVE_MODULE_XML = "META-INF/hivemodule.xml"; |
|
55 |
|
|
56 |
/**
|
|
57 |
* Set of all specified resources processed by this ModuleDescriptorProvider. Descriptors of
|
|
58 |
* sub-modules are not included.
|
|
59 |
*/
|
|
60 |
private List _resources = new ArrayList(); |
|
61 |
|
|
62 |
/**
|
|
63 |
* List of parsed {@link ModuleDescriptor}instances. Also includes referenced sub-modules.
|
|
64 |
*/
|
|
65 |
private List _moduleDescriptors = new ArrayList(); |
|
66 |
|
|
67 |
private ClassResolver _resolver;
|
|
68 |
|
|
69 |
private ErrorHandler _errorHandler;
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Parser instance used by all parsing of module descriptors.
|
|
73 |
*/
|
|
74 |
private XmlResourceProcessor _processor;
|
|
75 |
|
|
76 |
/**
|
|
77 |
* Convenience constructor. Equivalent to using
|
|
78 |
* {@link #XmlModuleDescriptorProvider(ClassResolver, String)}with {@link #HIVE_MODULE_XML}as
|
|
79 |
* the second argument.
|
|
80 |
*/
|
|
81 | 102 |
public XmlModuleDescriptorProvider(ClassResolver resolver)
|
82 |
{ |
|
83 | 102 |
this(resolver, HIVE_MODULE_XML);
|
84 |
} |
|
85 |
|
|
86 |
/**
|
|
87 |
* Loads all XML module descriptors found on the classpath (using the given
|
|
88 |
* {@link org.apache.hivemind.ClassResolver}. Only module descriptors matching the specified
|
|
89 |
* path are loaded. Use the {@link XmlModuleDescriptorProvider#HIVE_MODULE_XML}constant to load
|
|
90 |
* all descriptors in the default location.
|
|
91 |
*/
|
|
92 | 102 |
public XmlModuleDescriptorProvider(ClassResolver resolver, String resourcePath)
|
93 |
{ |
|
94 | 102 |
_resolver = resolver; |
95 | 102 |
_resources.addAll(getDescriptorResources(resourcePath, _resolver)); |
96 |
} |
|
97 |
|
|
98 |
/**
|
|
99 |
* Constructs an XmlModuleDescriptorProvider only loading the ModuleDescriptor identified by the
|
|
100 |
* given {@link org.apache.hivemind.Resource}.
|
|
101 |
*/
|
|
102 | 2 |
public XmlModuleDescriptorProvider(ClassResolver resolver, Resource resource)
|
103 |
{ |
|
104 | 2 |
_resolver = resolver; |
105 | 2 |
_resources.add(resource); |
106 |
} |
|
107 |
|
|
108 |
/**
|
|
109 |
* Constructs an XmlModuleDescriptorProvider loading all ModuleDescriptor identified by the
|
|
110 |
* given List of {@link org.apache.hivemind.Resource}objects.
|
|
111 |
*/
|
|
112 | 93 |
public XmlModuleDescriptorProvider(ClassResolver resolver, List resources)
|
113 |
{ |
|
114 | 93 |
_resolver = resolver; |
115 | 93 |
_resources.addAll(resources); |
116 |
} |
|
117 |
|
|
118 | 102 |
private List getDescriptorResources(String resourcePath, ClassResolver resolver)
|
119 |
{ |
|
120 | 102 |
if (LOG.isDebugEnabled())
|
121 | 12 |
LOG.debug("Processing modules visible to " + resolver);
|
122 |
|
|
123 | 102 |
List descriptors = new ArrayList();
|
124 |
|
|
125 | 102 |
ClassLoader loader = resolver.getClassLoader(); |
126 | 102 |
Enumeration e = null;
|
127 |
|
|
128 | 102 |
try
|
129 |
{ |
|
130 | 102 |
e = loader.getResources(resourcePath); |
131 |
} |
|
132 |
catch (IOException ex)
|
|
133 |
{ |
|
134 | 0 |
throw new ApplicationRuntimeException(ImplMessages.unableToFindModules(resolver, ex), |
135 |
ex); |
|
136 |
} |
|
137 |
|
|
138 | 102 |
while (e.hasMoreElements())
|
139 |
{ |
|
140 | 103 |
URL descriptorURL = (URL) e.nextElement(); |
141 |
|
|
142 | 103 |
descriptors.add(new URLResource(descriptorURL));
|
143 |
} |
|
144 |
|
|
145 | 102 |
return descriptors;
|
146 |
} |
|
147 |
|
|
148 | 197 |
public List getModuleDescriptors(ErrorHandler handler)
|
149 |
{ |
|
150 | 197 |
_errorHandler = handler; |
151 |
|
|
152 | 197 |
_processor = getResourceProcessor(_resolver, handler); |
153 |
|
|
154 | 197 |
for (Iterator i = _resources.iterator(); i.hasNext();)
|
155 |
{ |
|
156 | 207 |
Resource resource = (Resource) i.next(); |
157 |
|
|
158 | 207 |
processResource(resource); |
159 |
} |
|
160 |
|
|
161 | 197 |
_processor = null;
|
162 |
|
|
163 | 197 |
_errorHandler = null;
|
164 |
|
|
165 | 197 |
return _moduleDescriptors;
|
166 |
} |
|
167 |
|
|
168 | 209 |
private void processResource(Resource resource) |
169 |
{ |
|
170 | 209 |
try
|
171 |
{ |
|
172 | 209 |
ModuleDescriptor md = _processor.processResource(resource); |
173 |
|
|
174 | 209 |
_moduleDescriptors.add(md); |
175 |
|
|
176 |
// After parsing a module, parse any additional modules identified
|
|
177 |
// within the module (using the <sub-module> element) recursively.
|
|
178 | 209 |
processSubModules(md); |
179 |
} |
|
180 |
catch (RuntimeException ex)
|
|
181 |
{ |
|
182 | 0 |
_errorHandler.error(LOG, ex.getMessage(), HiveMind.getLocation(ex), ex); |
183 |
} |
|
184 |
} |
|
185 |
|
|
186 | 209 |
private void processSubModules(ModuleDescriptor moduleDescriptor) |
187 |
{ |
|
188 | 209 |
List subModules = moduleDescriptor.getSubModules(); |
189 |
|
|
190 | 209 |
if (subModules == null) |
191 | 206 |
return;
|
192 |
|
|
193 | 3 |
for (Iterator i = subModules.iterator(); i.hasNext();)
|
194 |
{ |
|
195 | 3 |
SubModuleDescriptor smd = (SubModuleDescriptor) i.next(); |
196 |
|
|
197 | 3 |
Resource descriptorResource = smd.getDescriptor(); |
198 |
|
|
199 | 3 |
if (descriptorResource.getResourceURL() == null) |
200 |
{ |
|
201 | 1 |
_errorHandler.error( |
202 |
LOG, |
|
203 |
ImplMessages.subModuleDoesNotExist(descriptorResource), |
|
204 |
smd.getLocation(), |
|
205 |
null);
|
|
206 | 1 |
continue;
|
207 |
} |
|
208 |
|
|
209 | 2 |
processResource(smd.getDescriptor()); |
210 |
} |
|
211 |
} |
|
212 |
|
|
213 | 197 |
protected XmlResourceProcessor getResourceProcessor(ClassResolver resolver, ErrorHandler handler)
|
214 |
{ |
|
215 | 197 |
return new XmlResourceProcessor(resolver, handler); |
216 |
} |
|
217 |
} |
|