1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
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 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| public class XmlModuleDescriptorProvider implements ModuleDescriptorProvider |
46 |
| { |
47 |
| private static final Log LOG = LogFactory.getLog(XmlModuleDescriptorProvider.class); |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| public static final String HIVE_MODULE_XML = "META-INF/hivemodule.xml"; |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| private List _resources = new ArrayList(); |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| private List _moduleDescriptors = new ArrayList(); |
66 |
| |
67 |
| private ClassResolver _resolver; |
68 |
| |
69 |
| private ErrorHandler _errorHandler; |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| private XmlResourceProcessor _processor; |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
122
| public XmlModuleDescriptorProvider(ClassResolver resolver)
|
82 |
| { |
83 |
122
| this(resolver, HIVE_MODULE_XML);
|
84 |
| } |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
122
| public XmlModuleDescriptorProvider(ClassResolver resolver, String resourcePath)
|
93 |
| { |
94 |
122
| _resolver = resolver;
|
95 |
122
| _resources.addAll(getDescriptorResources(resourcePath, _resolver));
|
96 |
| } |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
2
| public XmlModuleDescriptorProvider(ClassResolver resolver, Resource resource)
|
103 |
| { |
104 |
2
| _resolver = resolver;
|
105 |
2
| _resources.add(resource);
|
106 |
| } |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
111
| public XmlModuleDescriptorProvider(ClassResolver resolver, List resources)
|
113 |
| { |
114 |
111
| _resolver = resolver;
|
115 |
111
| _resources.addAll(resources);
|
116 |
| } |
117 |
| |
118 |
122
| private List getDescriptorResources(String resourcePath, ClassResolver resolver)
|
119 |
| { |
120 |
122
| if (LOG.isDebugEnabled())
|
121 |
12
| LOG.debug("Processing modules visible to " + resolver);
|
122 |
| |
123 |
122
| List descriptors = new ArrayList();
|
124 |
| |
125 |
122
| ClassLoader loader = resolver.getClassLoader();
|
126 |
122
| Enumeration e = null;
|
127 |
| |
128 |
122
| try
|
129 |
| { |
130 |
122
| e = loader.getResources(resourcePath);
|
131 |
| } |
132 |
| catch (IOException ex) |
133 |
| { |
134 |
0
| throw new ApplicationRuntimeException(ImplMessages.unableToFindModules(resolver, ex),
|
135 |
| ex); |
136 |
| } |
137 |
| |
138 |
122
| while (e.hasMoreElements())
|
139 |
| { |
140 |
123
| URL descriptorURL = (URL) e.nextElement();
|
141 |
| |
142 |
123
| descriptors.add(new URLResource(descriptorURL));
|
143 |
| } |
144 |
| |
145 |
122
| return descriptors;
|
146 |
| } |
147 |
| |
148 |
235
| public List getModuleDescriptors(ErrorHandler handler)
|
149 |
| { |
150 |
235
| _errorHandler = handler;
|
151 |
| |
152 |
235
| _processor = getResourceProcessor(_resolver, handler);
|
153 |
| |
154 |
235
| for (Iterator i = _resources.iterator(); i.hasNext();)
|
155 |
| { |
156 |
245
| Resource resource = (Resource) i.next();
|
157 |
| |
158 |
245
| processResource(resource);
|
159 |
| } |
160 |
| |
161 |
235
| _processor = null;
|
162 |
| |
163 |
235
| _errorHandler = null;
|
164 |
| |
165 |
235
| return _moduleDescriptors;
|
166 |
| } |
167 |
| |
168 |
247
| private void processResource(Resource resource)
|
169 |
| { |
170 |
247
| try
|
171 |
| { |
172 |
247
| ModuleDescriptor md = _processor.processResource(resource);
|
173 |
| |
174 |
247
| _moduleDescriptors.add(md);
|
175 |
| |
176 |
| |
177 |
| |
178 |
247
| processSubModules(md);
|
179 |
| } |
180 |
| catch (RuntimeException ex) |
181 |
| { |
182 |
0
| _errorHandler.error(LOG, ex.getMessage(), HiveMind.getLocation(ex), ex);
|
183 |
| } |
184 |
| } |
185 |
| |
186 |
247
| private void processSubModules(ModuleDescriptor moduleDescriptor)
|
187 |
| { |
188 |
247
| List subModules = moduleDescriptor.getSubModules();
|
189 |
| |
190 |
247
| if (subModules == null)
|
191 |
244
| 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 |
235
| protected XmlResourceProcessor getResourceProcessor(ClassResolver resolver, ErrorHandler handler)
|
214 |
| { |
215 |
235
| return new XmlResourceProcessor(resolver, handler);
|
216 |
| } |
217 |
| } |