|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| package org.objectledge.configuration; |
|
30 |
| |
|
31 |
| import java.io.IOException; |
|
32 |
| import java.net.URL; |
|
33 |
| |
|
34 |
| import javax.xml.parsers.ParserConfigurationException; |
|
35 |
| import javax.xml.parsers.SAXParserFactory; |
|
36 |
| |
|
37 |
| import org.jcontainer.dna.Configuration; |
|
38 |
| import org.jcontainer.dna.impl.SAXConfigurationHandler; |
|
39 |
| import org.jcontainer.dna.impl.SAXConfigurationSerializer; |
|
40 |
| import org.objectledge.ComponentInitializationError; |
|
41 |
| import org.objectledge.filesystem.FileSystem; |
|
42 |
| import org.objectledge.xml.XMLValidator; |
|
43 |
| import org.xml.sax.InputSource; |
|
44 |
| import org.xml.sax.SAXException; |
|
45 |
| import org.xml.sax.SAXParseException; |
|
46 |
| import org.xml.sax.XMLReader; |
|
47 |
| |
|
48 |
| import com.sun.msv.verifier.Verifier; |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| public class ConfigurationFactory |
|
78 |
| { |
|
79 |
| private FileSystem fileSystem; |
|
80 |
| |
|
81 |
| private String directory; |
|
82 |
| |
|
83 |
| private XMLValidator xmlValidator; |
|
84 |
| |
|
85 |
| private URL relaxngUrl; |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
414
| public ConfigurationFactory(FileSystem fileSystem, XMLValidator xmlValidator, String directory)
|
|
96 |
| throws IOException |
|
97 |
| { |
|
98 |
414
| this.fileSystem = fileSystem;
|
|
99 |
414
| this.xmlValidator = xmlValidator;
|
|
100 |
414
| this.directory = directory;
|
|
101 |
414
| this.relaxngUrl = fileSystem.getResource(XMLValidator.RELAXNG_SCHEMA);
|
|
102 |
| } |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
414
| public Configuration getConfig(String componentName, Class componentClass)
|
|
112 |
| { |
|
113 |
414
| String path = getComponentConfigurationPath(componentName);
|
|
114 |
414
| InputSource source = getConfigurationSource(componentName, componentClass);
|
|
115 |
414
| Configuration configuration;
|
|
116 |
414
| try
|
|
117 |
| { |
|
118 |
414
| SAXParserFactory parserFactory = SAXParserFactory.newInstance();
|
|
119 |
414
| XMLReader reader = parserFactory.newSAXParser().getXMLReader();
|
|
120 |
414
| SAXConfigurationHandler handler = new SAXConfigurationHandler();
|
|
121 |
414
| reader.setContentHandler(handler);
|
|
122 |
414
| reader.setErrorHandler(handler);
|
|
123 |
414
| reader.parse(source);
|
|
124 |
414
| configuration = handler.getConfiguration();
|
|
125 |
| } |
|
126 |
| catch(SAXParseException e) |
|
127 |
| { |
|
128 |
0
| throw new ComponentInitializationError("parse error in configuration of component "+
|
|
129 |
| componentName+": "+e.getMessage()+" in "+e.getSystemId()+" at line "+ |
|
130 |
| e.getLineNumber(), e); |
|
131 |
| } |
|
132 |
| catch(Exception e) |
|
133 |
| { |
|
134 |
0
| throw new ComponentInitializationError("configuration file "+
|
|
135 |
| path+" for component "+componentName+" is malformed", e); |
|
136 |
| } |
|
137 |
414
| return configuration;
|
|
138 |
| } |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
0
| public Configuration getConfig(Class componentRole, Class componentImplementation)
|
|
148 |
| { |
|
149 |
0
| return getConfig(componentRole.getName(), componentImplementation);
|
|
150 |
| } |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
414
| public InputSource getConfigurationSource(String componentName, Class componentClass)
|
|
160 |
| { |
|
161 |
414
| String path = getComponentConfigurationPath(componentName);
|
|
162 |
414
| InputSource source = getRawConfigurationSource(componentName);
|
|
163 |
414
| String schema = getComponentConfigurationSchemaPath(componentClass);
|
|
164 |
414
| if(!fileSystem.exists(schema))
|
|
165 |
| { |
|
166 |
0
| throw new ComponentInitializationError("schema file "+schema+" for component "+
|
|
167 |
| componentName+" not found"); |
|
168 |
| } |
|
169 |
414
| try
|
|
170 |
| { |
|
171 |
414
| checkSchema(path, schema);
|
|
172 |
| } |
|
173 |
| catch(SAXParseException e) |
|
174 |
| { |
|
175 |
0
| throw new ComponentInitializationError("parse error in configuration of component "+
|
|
176 |
| componentName+": "+e.getMessage()+" in "+e.getSystemId()+" at line "+ |
|
177 |
| e.getLineNumber(), e); |
|
178 |
| } |
|
179 |
| catch(Exception e) |
|
180 |
| { |
|
181 |
0
| throw new ComponentInitializationError("configuration file "+
|
|
182 |
| path+" for component "+componentName+" is malformed: '" + e.getMessage() + "'", e); |
|
183 |
| } |
|
184 |
414
| return source;
|
|
185 |
| } |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
414
| public InputSource getRawConfigurationSource(String componentName)
|
|
194 |
| { |
|
195 |
414
| String path = getComponentConfigurationPath(componentName);
|
|
196 |
414
| if(!fileSystem.exists(path))
|
|
197 |
| { |
|
198 |
0
| throw new ComponentInitializationError("configuration file "+path+" for component "+
|
|
199 |
| componentName+" not found"); |
|
200 |
| } |
|
201 |
414
| return new InputSource(fileSystem.getInputStream(path));
|
|
202 |
| } |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
0
| public boolean hasConfig(String componentName)
|
|
211 |
| { |
|
212 |
0
| String path = getComponentConfigurationPath(componentName);
|
|
213 |
0
| return fileSystem.exists(path);
|
|
214 |
| } |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
0
| public InputSource getConfigurationSource(Class componentRole, Class componentClass)
|
|
224 |
| { |
|
225 |
0
| return getConfigurationSource(componentRole.getName(), componentClass);
|
|
226 |
| } |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
0
| public InputSource getCompositionSource()
|
|
234 |
| { |
|
235 |
0
| String path = directory + "/container.xml";
|
|
236 |
0
| if(!fileSystem.exists(path))
|
|
237 |
| { |
|
238 |
0
| throw new ComponentInitializationError("composition file " + path + " not found");
|
|
239 |
| } |
|
240 |
0
| return new InputSource(fileSystem.getInputStream(path));
|
|
241 |
| } |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
1242
| protected String getComponentConfigurationPath(String componentName)
|
|
252 |
| { |
|
253 |
1242
| return directory+"/"+componentName+".xml";
|
|
254 |
| } |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
414
| protected String getComponentConfigurationSchemaPath(Class componentImplementation)
|
|
263 |
| { |
|
264 |
414
| return ((Class)componentImplementation).getName().replace('.','/')+".rng";
|
|
265 |
| } |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
0
| protected void checkSchema(Configuration configuration, String schemaPath)
|
|
277 |
| throws SAXException, IOException, ParserConfigurationException |
|
278 |
| { |
|
279 |
0
| URL schemaUrl = fileSystem.getResource(schemaPath);
|
|
280 |
0
| xmlValidator.validate(schemaUrl, relaxngUrl);
|
|
281 |
0
| Verifier verifier= xmlValidator.getVerifier(schemaUrl);
|
|
282 |
0
| SAXConfigurationSerializer serializer = new SAXConfigurationSerializer();
|
|
283 |
0
| serializer.serialize(configuration, verifier);
|
|
284 |
| } |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
414
| protected void checkSchema(String configuration, String schemaPath)
|
|
296 |
| throws SAXException, IOException, ParserConfigurationException |
|
297 |
| { |
|
298 |
414
| URL schemaUrl = fileSystem.getResource(schemaPath);
|
|
299 |
414
| xmlValidator.validate(schemaUrl, relaxngUrl);
|
|
300 |
414
| xmlValidator.validate(fileSystem.getResource(configuration), schemaUrl);
|
|
301 |
| } |
|
302 |
| } |