1 | /* |
2 | * Copyright (C) Christian Schulte, 2005-206 |
3 | * All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * o Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * |
12 | * o Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in |
14 | * the documentation and/or other materials provided with the |
15 | * distribution. |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
19 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | * |
28 | * $JOMC: ModelContext.java 4843 2014-01-06 01:29:56Z schulte $ |
29 | * |
30 | */ |
31 | package org.jomc.modlet; |
32 | |
33 | import java.io.IOException; |
34 | import java.lang.reflect.Constructor; |
35 | import java.lang.reflect.InvocationTargetException; |
36 | import java.net.URI; |
37 | import java.net.URL; |
38 | import java.text.MessageFormat; |
39 | import java.util.Collections; |
40 | import java.util.Enumeration; |
41 | import java.util.HashMap; |
42 | import java.util.LinkedList; |
43 | import java.util.List; |
44 | import java.util.Locale; |
45 | import java.util.Map; |
46 | import java.util.ResourceBundle; |
47 | import java.util.Set; |
48 | import java.util.logging.Level; |
49 | import javax.xml.bind.JAXBContext; |
50 | import javax.xml.bind.JAXBException; |
51 | import javax.xml.bind.Marshaller; |
52 | import javax.xml.bind.Unmarshaller; |
53 | import javax.xml.bind.util.JAXBSource; |
54 | import javax.xml.transform.Source; |
55 | import javax.xml.validation.Validator; |
56 | import org.w3c.dom.ls.LSResourceResolver; |
57 | import org.xml.sax.EntityResolver; |
58 | import org.xml.sax.SAXException; |
59 | |
60 | /** |
61 | * Model context interface. |
62 | * <p><b>Use Cases:</b><br/><ul> |
63 | * <li>{@link #createContext(java.lang.String) }</li> |
64 | * <li>{@link #createContext(java.net.URI) }</li> |
65 | * <li>{@link #createEntityResolver(java.lang.String) }</li> |
66 | * <li>{@link #createEntityResolver(java.net.URI) }</li> |
67 | * <li>{@link #createMarshaller(java.lang.String) }</li> |
68 | * <li>{@link #createMarshaller(java.net.URI) }</li> |
69 | * <li>{@link #createResourceResolver(java.lang.String) }</li> |
70 | * <li>{@link #createResourceResolver(java.net.URI) }</li> |
71 | * <li>{@link #createSchema(java.lang.String) }</li> |
72 | * <li>{@link #createSchema(java.net.URI) }</li> |
73 | * <li>{@link #createUnmarshaller(java.lang.String) }</li> |
74 | * <li>{@link #createUnmarshaller(java.net.URI) }</li> |
75 | * <li>{@link #findModel(java.lang.String) }</li> |
76 | * <li>{@link #findModel(org.jomc.modlet.Model) }</li> |
77 | * <li>{@link #processModel(org.jomc.modlet.Model) }</li> |
78 | * <li>{@link #validateModel(org.jomc.modlet.Model) }</li> |
79 | * <li>{@link #validateModel(java.lang.String, javax.xml.transform.Source) }</li> |
80 | * </ul> |
81 | * |
82 | * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> |
83 | * @version $JOMC: ModelContext.java 4843 2014-01-06 01:29:56Z schulte $ |
84 | * |
85 | * @see ModelContextFactory |
86 | */ |
87 | public abstract class ModelContext |
88 | { |
89 | |
90 | /** Listener interface. */ |
91 | public abstract static class Listener |
92 | { |
93 | |
94 | /** Creates a new {@code Listener} instance. */ |
95 | public Listener() |
96 | { |
97 | super(); |
98 | } |
99 | |
100 | /** |
101 | * Gets called on logging. |
102 | * |
103 | * @param level The level of the event. |
104 | * @param message The message of the event or {@code null}. |
105 | * @param t The throwable of the event or {@code null}. |
106 | * |
107 | * @throws NullPointerException if {@code level} is {@code null}. |
108 | */ |
109 | public void onLog( final Level level, final String message, final Throwable t ) |
110 | { |
111 | if ( level == null ) |
112 | { |
113 | throw new NullPointerException( "level" ); |
114 | } |
115 | } |
116 | |
117 | } |
118 | |
119 | /** |
120 | * Default {@code http://jomc.org/modlet} namespace schema system id. |
121 | * @see #getDefaultModletSchemaSystemId() |
122 | */ |
123 | private static final String DEFAULT_MODLET_SCHEMA_SYSTEM_ID = |
124 | "http://xml.jomc.org/modlet/jomc-modlet-1.3.xsd"; |
125 | |
126 | /** |
127 | * Log level events are logged at by default. |
128 | * @see #getDefaultLogLevel() |
129 | */ |
130 | private static final Level DEFAULT_LOG_LEVEL = Level.WARNING; |
131 | |
132 | /** Default log level. */ |
133 | private static volatile Level defaultLogLevel; |
134 | |
135 | /** Default {@code http://jomc.org/model/modlet} namespace schema system id. */ |
136 | private static volatile String defaultModletSchemaSystemId; |
137 | |
138 | /** Class name of the {@code ModelContext} implementation. */ |
139 | @Deprecated |
140 | private static volatile String modelContextClassName; |
141 | |
142 | /** The attributes of the instance. */ |
143 | private final Map<String, Object> attributes = new HashMap<String, Object>(); |
144 | |
145 | /** The class loader of the instance. */ |
146 | private ClassLoader classLoader; |
147 | |
148 | /** |
149 | * Flag indicating the {@code classLoader} field is initialized. |
150 | * @since 1.2 |
151 | */ |
152 | private boolean classLoaderSet; |
153 | |
154 | /** The listeners of the instance. */ |
155 | private List<Listener> listeners; |
156 | |
157 | /** Log level of the instance. */ |
158 | private Level logLevel; |
159 | |
160 | /** The {@code Modlets} of the instance. */ |
161 | private Modlets modlets; |
162 | |
163 | /** Modlet namespace schema system id of the instance. */ |
164 | private String modletSchemaSystemId; |
165 | |
166 | /** |
167 | * Creates a new {@code ModelContext} instance. |
168 | * @since 1.2 |
169 | */ |
170 | public ModelContext() |
171 | { |
172 | super(); |
173 | this.classLoader = null; |
174 | this.classLoaderSet = false; |
175 | } |
176 | |
177 | /** |
178 | * Creates a new {@code ModelContext} instance taking a class loader. |
179 | * |
180 | * @param classLoader The class loader of the context. |
181 | * |
182 | * @see #getClassLoader() |
183 | */ |
184 | public ModelContext( final ClassLoader classLoader ) |
185 | { |
186 | super(); |
187 | this.classLoader = classLoader; |
188 | this.classLoaderSet = true; |
189 | } |
190 | |
191 | /** |
192 | * Gets a set holding the names of all attributes of the context. |
193 | * |
194 | * @return An unmodifiable set holding the names of all attributes of the context. |
195 | * |
196 | * @see #clearAttribute(java.lang.String) |
197 | * @see #getAttribute(java.lang.String) |
198 | * @see #getAttribute(java.lang.String, java.lang.Object) |
199 | * @see #setAttribute(java.lang.String, java.lang.Object) |
200 | * @since 1.2 |
201 | */ |
202 | public Set<String> getAttributeNames() |
203 | { |
204 | return Collections.unmodifiableSet( this.attributes.keySet() ); |
205 | } |
206 | |
207 | /** |
208 | * Gets an attribute of the context. |
209 | * |
210 | * @param name The name of the attribute to get. |
211 | * |
212 | * @return The value of the attribute with name {@code name}; {@code null} if no attribute matching {@code name} is |
213 | * found. |
214 | * |
215 | * @throws NullPointerException if {@code name} is {@code null}. |
216 | * |
217 | * @see #getAttribute(java.lang.String, java.lang.Object) |
218 | * @see #setAttribute(java.lang.String, java.lang.Object) |
219 | * @see #clearAttribute(java.lang.String) |
220 | */ |
221 | public Object getAttribute( final String name ) |
222 | { |
223 | if ( name == null ) |
224 | { |
225 | throw new NullPointerException( "name" ); |
226 | } |
227 | |
228 | return this.attributes.get( name ); |
229 | } |
230 | |
231 | /** |
232 | * Gets an attribute of the context. |
233 | * |
234 | * @param name The name of the attribute to get. |
235 | * @param def The value to return if no attribute matching {@code name} is found. |
236 | * |
237 | * @return The value of the attribute with name {@code name}; {@code def} if no such attribute is found. |
238 | * |
239 | * @throws NullPointerException if {@code name} is {@code null}. |
240 | * |
241 | * @see #getAttribute(java.lang.String) |
242 | * @see #setAttribute(java.lang.String, java.lang.Object) |
243 | * @see #clearAttribute(java.lang.String) |
244 | */ |
245 | public Object getAttribute( final String name, final Object def ) |
246 | { |
247 | if ( name == null ) |
248 | { |
249 | throw new NullPointerException( "name" ); |
250 | } |
251 | |
252 | Object value = this.getAttribute( name ); |
253 | |
254 | if ( value == null ) |
255 | { |
256 | value = def; |
257 | } |
258 | |
259 | return value; |
260 | } |
261 | |
262 | /** |
263 | * Sets an attribute in the context. |
264 | * |
265 | * @param name The name of the attribute to set. |
266 | * @param value The value of the attribute to set. |
267 | * |
268 | * @return The previous value of the attribute with name {@code name}; {@code null} if no such value is found. |
269 | * |
270 | * @throws NullPointerException if {@code name} or {@code value} is {@code null}. |
271 | * |
272 | * @see #getAttribute(java.lang.String) |
273 | * @see #getAttribute(java.lang.String, java.lang.Object) |
274 | * @see #clearAttribute(java.lang.String) |
275 | */ |
276 | public Object setAttribute( final String name, final Object value ) |
277 | { |
278 | if ( name == null ) |
279 | { |
280 | throw new NullPointerException( "name" ); |
281 | } |
282 | if ( value == null ) |
283 | { |
284 | throw new NullPointerException( "value" ); |
285 | } |
286 | |
287 | return this.attributes.put( name, value ); |
288 | } |
289 | |
290 | /** |
291 | * Removes an attribute from the context. |
292 | * |
293 | * @param name The name of the attribute to remove. |
294 | * |
295 | * @throws NullPointerException if {@code name} is {@code null}. |
296 | * |
297 | * @see #getAttribute(java.lang.String) |
298 | * @see #getAttribute(java.lang.String, java.lang.Object) |
299 | * @see #setAttribute(java.lang.String, java.lang.Object) |
300 | */ |
301 | public void clearAttribute( final String name ) |
302 | { |
303 | if ( name == null ) |
304 | { |
305 | throw new NullPointerException( "name" ); |
306 | } |
307 | |
308 | this.attributes.remove( name ); |
309 | } |
310 | |
311 | /** |
312 | * Gets the class loader of the context. |
313 | * |
314 | * @return The class loader of the context or {@code null}, indicating the bootstrap class loader. |
315 | * |
316 | * @see #findClass(java.lang.String) |
317 | * @see #findResource(java.lang.String) |
318 | * @see #findResources(java.lang.String) |
319 | */ |
320 | public ClassLoader getClassLoader() |
321 | { |
322 | if ( !this.classLoaderSet ) |
323 | { |
324 | this.classLoader = this.getClass().getClassLoader(); |
325 | this.classLoaderSet = true; |
326 | } |
327 | |
328 | return this.classLoader; |
329 | } |
330 | |
331 | /** |
332 | * Gets the listeners of the context. |
333 | * <p>This accessor method returns a reference to the live list, not a snapshot. Therefore any modification you make |
334 | * to the returned list will be present inside the object. This is why there is no {@code set} method for the |
335 | * listeners property.</p> |
336 | * |
337 | * @return The list of listeners of the context. |
338 | * |
339 | * @see #log(java.util.logging.Level, java.lang.String, java.lang.Throwable) |
340 | */ |
341 | public List<Listener> getListeners() |
342 | { |
343 | if ( this.listeners == null ) |
344 | { |
345 | this.listeners = new LinkedList<Listener>(); |
346 | } |
347 | |
348 | return this.listeners; |
349 | } |
350 | |
351 | /** |
352 | * Gets the default {@code http://jomc.org/modlet} namespace schema system id. |
353 | * <p>The default {@code http://jomc.org/modlet} namespace schema system id is controlled by system property |
354 | * {@code org.jomc.modlet.ModelContext.defaultModletSchemaSystemId} holding a system id URI. |
355 | * If that property is not set, the {@code http://xml.jomc.org/modlet/jomc-modlet-1.3.xsd} default is |
356 | * returned.</p> |
357 | * |
358 | * @return The default system id of the {@code http://jomc.org/modlet} namespace schema. |
359 | * |
360 | * @see #setDefaultModletSchemaSystemId(java.lang.String) |
361 | */ |
362 | public static String getDefaultModletSchemaSystemId() |
363 | { |
364 | if ( defaultModletSchemaSystemId == null ) |
365 | { |
366 | defaultModletSchemaSystemId = System.getProperty( |
367 | "org.jomc.modlet.ModelContext.defaultModletSchemaSystemId", DEFAULT_MODLET_SCHEMA_SYSTEM_ID ); |
368 | |
369 | } |
370 | |
371 | return defaultModletSchemaSystemId; |
372 | } |
373 | |
374 | /** |
375 | * Sets the default {@code http://jomc.org/modlet} namespace schema system id. |
376 | * |
377 | * @param value The new default {@code http://jomc.org/modlet} namespace schema system id or {@code null}. |
378 | * |
379 | * @see #getDefaultModletSchemaSystemId() |
380 | */ |
381 | public static void setDefaultModletSchemaSystemId( final String value ) |
382 | { |
383 | defaultModletSchemaSystemId = value; |
384 | } |
385 | |
386 | /** |
387 | * Gets the {@code http://jomc.org/modlet} namespace schema system id of the context. |
388 | * |
389 | * @return The {@code http://jomc.org/modlet} namespace schema system id of the context. |
390 | * |
391 | * @see #getDefaultModletSchemaSystemId() |
392 | * @see #setModletSchemaSystemId(java.lang.String) |
393 | */ |
394 | public final String getModletSchemaSystemId() |
395 | { |
396 | if ( this.modletSchemaSystemId == null ) |
397 | { |
398 | this.modletSchemaSystemId = getDefaultModletSchemaSystemId(); |
399 | |
400 | if ( this.isLoggable( Level.CONFIG ) ) |
401 | { |
402 | this.log( Level.CONFIG, |
403 | getMessage( "defaultModletSchemaSystemIdInfo", this.modletSchemaSystemId ), null ); |
404 | |
405 | } |
406 | } |
407 | |
408 | return this.modletSchemaSystemId; |
409 | } |
410 | |
411 | /** |
412 | * Sets the {@code http://jomc.org/modlet} namespace schema system id of the context. |
413 | * |
414 | * @param value The new {@code http://jomc.org/modlet} namespace schema system id or {@code null}. |
415 | * |
416 | * @see #getModletSchemaSystemId() |
417 | */ |
418 | public final void setModletSchemaSystemId( final String value ) |
419 | { |
420 | final String oldModletSchemaSystemId = this.getModletSchemaSystemId(); |
421 | this.modletSchemaSystemId = value; |
422 | |
423 | if ( this.modlets != null ) |
424 | { |
425 | for ( int i = 0, s0 = this.modlets.getModlet().size(); i < s0; i++ ) |
426 | { |
427 | final Modlet m = this.modlets.getModlet().get( i ); |
428 | |
429 | if ( m.getSchemas() != null ) |
430 | { |
431 | final Schema s = m.getSchemas().getSchemaBySystemId( oldModletSchemaSystemId ); |
432 | |
433 | if ( s != null ) |
434 | { |
435 | s.setSystemId( value ); |
436 | } |
437 | } |
438 | } |
439 | } |
440 | } |
441 | |
442 | /** |
443 | * Gets the default log level events are logged at. |
444 | * <p>The default log level is controlled by system property |
445 | * {@code org.jomc.modlet.ModelContext.defaultLogLevel} holding the log level to log events at by default. |
446 | * If that property is not set, the {@code WARNING} default is returned.</p> |
447 | * |
448 | * @return The log level events are logged at by default. |
449 | * |
450 | * @see #getLogLevel() |
451 | * @see Level#parse(java.lang.String) |
452 | */ |
453 | public static Level getDefaultLogLevel() |
454 | { |
455 | if ( defaultLogLevel == null ) |
456 | { |
457 | defaultLogLevel = Level.parse( System.getProperty( |
458 | "org.jomc.modlet.ModelContext.defaultLogLevel", DEFAULT_LOG_LEVEL.getName() ) ); |
459 | |
460 | } |
461 | |
462 | return defaultLogLevel; |
463 | } |
464 | |
465 | /** |
466 | * Sets the default log level events are logged at. |
467 | * |
468 | * @param value The new default level events are logged at or {@code null}. |
469 | * |
470 | * @see #getDefaultLogLevel() |
471 | */ |
472 | public static void setDefaultLogLevel( final Level value ) |
473 | { |
474 | defaultLogLevel = value; |
475 | } |
476 | |
477 | /** |
478 | * Gets the log level of the context. |
479 | * |
480 | * @return The log level of the context. |
481 | * |
482 | * @see #getDefaultLogLevel() |
483 | * @see #setLogLevel(java.util.logging.Level) |
484 | * @see #isLoggable(java.util.logging.Level) |
485 | */ |
486 | public final Level getLogLevel() |
487 | { |
488 | if ( this.logLevel == null ) |
489 | { |
490 | this.logLevel = getDefaultLogLevel(); |
491 | |
492 | if ( this.isLoggable( Level.CONFIG ) ) |
493 | { |
494 | this.log( Level.CONFIG, getMessage( "defaultLogLevelInfo", this.logLevel.getLocalizedName() ), null ); |
495 | } |
496 | } |
497 | |
498 | return this.logLevel; |
499 | } |
500 | |
501 | /** |
502 | * Sets the log level of the context. |
503 | * |
504 | * @param value The new log level of the context or {@code null}. |
505 | * |
506 | * @see #getLogLevel() |
507 | * @see #isLoggable(java.util.logging.Level) |
508 | */ |
509 | public final void setLogLevel( final Level value ) |
510 | { |
511 | this.logLevel = value; |
512 | } |
513 | |
514 | /** |
515 | * Checks if a message at a given level is provided to the listeners of the context. |
516 | * |
517 | * @param level The level to test. |
518 | * |
519 | * @return {@code true}, if messages at {@code level} are provided to the listeners of the context; {@code false}, |
520 | * if messages at {@code level} are not provided to the listeners of the context. |
521 | * |
522 | * @throws NullPointerException if {@code level} is {@code null}. |
523 | * |
524 | * @see #getLogLevel() |
525 | * @see #setLogLevel(java.util.logging.Level) |
526 | */ |
527 | public boolean isLoggable( final Level level ) |
528 | { |
529 | if ( level == null ) |
530 | { |
531 | throw new NullPointerException( "level" ); |
532 | } |
533 | |
534 | return level.intValue() >= this.getLogLevel().intValue(); |
535 | } |
536 | |
537 | /** |
538 | * Notifies all listeners of the context. |
539 | * |
540 | * @param level The level of the event. |
541 | * @param message The message of the event or {@code null}. |
542 | * @param throwable The throwable of the event {@code null}. |
543 | * |
544 | * @throws NullPointerException if {@code level} is {@code null}. |
545 | * |
546 | * @see #getListeners() |
547 | * @see #isLoggable(java.util.logging.Level) |
548 | */ |
549 | public void log( final Level level, final String message, final Throwable throwable ) |
550 | { |
551 | if ( level == null ) |
552 | { |
553 | throw new NullPointerException( "level" ); |
554 | } |
555 | |
556 | if ( this.isLoggable( level ) ) |
557 | { |
558 | for ( Listener l : this.getListeners() ) |
559 | { |
560 | l.onLog( level, message, throwable ); |
561 | } |
562 | } |
563 | } |
564 | |
565 | /** |
566 | * Gets the {@code Modlets} of the context. |
567 | * <p>If no {@code Modlets} have been set using the {@code setModlets} method, this method calls the |
568 | * {@code findModlets} method and the {@code processModlets} method to initialize the {@code Modlets} of the |
569 | * context.</p> |
570 | * <p>This accessor method returns a reference to the live list, not a snapshot. Therefore any modification you make |
571 | * to the returned list will be present inside the object.</p> |
572 | * |
573 | * @return The {@code Modlets} of the context. |
574 | * |
575 | * @throws ModelException if getting the {@code Modlets} of the context fails. |
576 | * |
577 | * @see #setModlets(org.jomc.modlet.Modlets) |
578 | * @see #findModlets(org.jomc.modlet.Modlets) |
579 | * @see #processModlets(org.jomc.modlet.Modlets) |
580 | */ |
581 | public final Modlets getModlets() throws ModelException |
582 | { |
583 | try |
584 | { |
585 | if ( this.modlets == null ) |
586 | { |
587 | final Modlet modlet = new Modlet(); |
588 | modlet.setModel( ModletObject.MODEL_PUBLIC_ID ); |
589 | modlet.setName( getMessage( "projectName" ) ); |
590 | modlet.setVendor( getMessage( "projectVendor" ) ); |
591 | modlet.setVersion( getMessage( "projectVersion" ) ); |
592 | modlet.setSchemas( new Schemas() ); |
593 | |
594 | final Schema schema = new Schema(); |
595 | schema.setPublicId( ModletObject.MODEL_PUBLIC_ID ); |
596 | schema.setSystemId( this.getModletSchemaSystemId() ); |
597 | schema.setContextId( ModletObject.class.getPackage().getName() ); |
598 | schema.setClasspathId( ModletObject.class.getPackage().getName().replace( '.', '/' ) |
599 | + "/jomc-modlet-1.3.xsd" ); |
600 | |
601 | modlet.getSchemas().getSchema().add( schema ); |
602 | |
603 | this.modlets = new Modlets(); |
604 | this.modlets.getModlet().add( modlet ); |
605 | |
606 | final Modlets provided = this.findModlets( this.modlets ); |
607 | |
608 | if ( provided != null ) |
609 | { |
610 | this.modlets = provided; |
611 | } |
612 | |
613 | final Modlets processed = this.processModlets( this.modlets ); |
614 | |
615 | if ( processed != null ) |
616 | { |
617 | this.modlets = processed; |
618 | } |
619 | |
620 | final javax.xml.validation.Schema modletSchema = this.createSchema( ModletObject.MODEL_PUBLIC_ID ); |
621 | final Validator validator = modletSchema.newValidator(); |
622 | validator.validate( new JAXBSource( this.createContext( ModletObject.MODEL_PUBLIC_ID ), |
623 | new ObjectFactory().createModlets( this.modlets ) ) ); |
624 | |
625 | } |
626 | |
627 | return this.modlets; |
628 | } |
629 | catch ( final IOException e ) |
630 | { |
631 | this.modlets = null; |
632 | throw new ModelException( getMessage( e ), e ); |
633 | } |
634 | catch ( final JAXBException e ) |
635 | { |
636 | this.modlets = null; |
637 | String message = getMessage( e ); |
638 | if ( message == null && e.getLinkedException() != null ) |
639 | { |
640 | message = getMessage( e.getLinkedException() ); |
641 | } |
642 | |
643 | throw new ModelException( message, e ); |
644 | } |
645 | catch ( final SAXException e ) |
646 | { |
647 | this.modlets = null; |
648 | String message = getMessage( e ); |
649 | if ( message == null && e.getException() != null ) |
650 | { |
651 | message = getMessage( e.getException() ); |
652 | } |
653 | |
654 | throw new ModelException( message, e ); |
655 | } |
656 | } |
657 | |
658 | /** |
659 | * Sets the {@code Modlets} of the context. |
660 | * |
661 | * @param value The new {@code Modlets} of the context or {@code null}. |
662 | * |
663 | * @see #getModlets() |
664 | */ |
665 | public final void setModlets( final Modlets value ) |
666 | { |
667 | this.modlets = value; |
668 | } |
669 | |
670 | /** |
671 | * Searches the context for a class with a given name. |
672 | * |
673 | * @param name The name of the class to search. |
674 | * |
675 | * @return A class object of the class with name {@code name} or {@code null}, if no such class is found. |
676 | * |
677 | * @throws NullPointerException if {@code name} is {@code null}. |
678 | * @throws ModelException if searching fails. |
679 | * |
680 | * @see #getClassLoader() |
681 | */ |
682 | public Class<?> findClass( final String name ) throws ModelException |
683 | { |
684 | if ( name == null ) |
685 | { |
686 | throw new NullPointerException( "name" ); |
687 | } |
688 | |
689 | try |
690 | { |
691 | return Class.forName( name, false, this.getClassLoader() ); |
692 | } |
693 | catch ( final ClassNotFoundException e ) |
694 | { |
695 | if ( this.isLoggable( Level.FINE ) ) |
696 | { |
697 | this.log( Level.FINE, getMessage( e ), e ); |
698 | } |
699 | |
700 | return null; |
701 | } |
702 | } |
703 | |
704 | /** |
705 | * Searches the context for a resource with a given name. |
706 | * |
707 | * @param name The name of the resource to search. |
708 | * |
709 | * @return An URL object for reading the resource or {@code null}, if no such resource is found. |
710 | * |
711 | * @throws NullPointerException if {@code name} is {@code null}. |
712 | * @throws ModelException if searching fails. |
713 | * |
714 | * @see #getClassLoader() |
715 | */ |
716 | public URL findResource( final String name ) throws ModelException |
717 | { |
718 | if ( name == null ) |
719 | { |
720 | throw new NullPointerException( "name" ); |
721 | } |
722 | |
723 | if ( this.getClassLoader() == null ) |
724 | { |
725 | return ClassLoader.getSystemResource( name ); |
726 | } |
727 | else |
728 | { |
729 | return this.getClassLoader().getResource( name ); |
730 | } |
731 | } |
732 | |
733 | /** |
734 | * Searches the context for resources with a given name. |
735 | * |
736 | * @param name The name of the resources to search. |
737 | * |
738 | * @return An enumeration of URL objects for reading the resources. If no resources are found, the enumeration will |
739 | * be empty. |
740 | * |
741 | * @throws NullPointerException if {@code name} is {@code null}. |
742 | * @throws ModelException if searching fails. |
743 | * |
744 | * @see #getClassLoader() |
745 | */ |
746 | public Enumeration<URL> findResources( final String name ) throws ModelException |
747 | { |
748 | if ( name == null ) |
749 | { |
750 | throw new NullPointerException( "name" ); |
751 | } |
752 | |
753 | try |
754 | { |
755 | if ( this.getClassLoader() == null ) |
756 | { |
757 | return ClassLoader.getSystemResources( name ); |
758 | } |
759 | else |
760 | { |
761 | return this.getClassLoader().getResources( name ); |
762 | } |
763 | } |
764 | catch ( final IOException e ) |
765 | { |
766 | throw new ModelException( getMessage( e ), e ); |
767 | } |
768 | } |
769 | |
770 | /** |
771 | * Searches the context for {@code Modlets}. |
772 | * |
773 | * @return The {@code Modlets} found in the context or {@code null}. |
774 | * |
775 | * @throws ModelException if searching {@code Modlets} fails. |
776 | * |
777 | * @see ModletProvider META-INF/services/org.jomc.modlet.ModletProvider |
778 | * @see #getModlets() |
779 | * @deprecated As of JOMC 1.6, replaced by {@link #findModlets(org.jomc.modlet.Modlets)}. This method will be |
780 | * removed in JOMC 2.0. |
781 | */ |
782 | @Deprecated |
783 | public abstract Modlets findModlets() throws ModelException; |
784 | |
785 | /** |
786 | * Searches the context for {@code Modlets}. |
787 | * |
788 | * @param modlets The {@code Modlets} currently being searched. |
789 | * |
790 | * @return The {@code Modlets} found in the context or {@code null}. |
791 | * |
792 | * @throws NullPointerException if {@code modlets} is {@code null}. |
793 | * @throws ModelException if searching {@code Modlets} fails. |
794 | * |
795 | * @see ModletProvider META-INF/services/org.jomc.modlet.ModletProvider |
796 | * @see #getModlets() |
797 | * @since 1.6 |
798 | */ |
799 | public abstract Modlets findModlets( Modlets modlets ) throws ModelException; |
800 | |
801 | /** |
802 | * Processes a list of {@code Modlet}s. |
803 | * |
804 | * @param modlets The {@code Modlets} currently being processed. |
805 | * |
806 | * @return The processed {@code Modlets} or {@code null}. |
807 | * |
808 | * @throws NullPointerException if {@code modlets} is {@code null}. |
809 | * @throws ModelException if processing {@code Modlets} fails. |
810 | * |
811 | * @see ModletProcessor META-INF/services/org.jomc.modlet.ModletProcessor |
812 | * @see #getModlets() |
813 | * @since 1.6 |
814 | */ |
815 | public abstract Modlets processModlets( Modlets modlets ) throws ModelException; |
816 | |
817 | /** |
818 | * Creates a new {@code Model} instance. |
819 | * |
820 | * @param model The identifier of the {@code Model} to create. |
821 | * |
822 | * @return A new instance of the {@code Model} identified by {@code model}. |
823 | * |
824 | * @throws NullPointerException if {@code model} is {@code null}. |
825 | * @throws ModelException if creating a new {@code Model} instance fails. |
826 | * |
827 | * @see #createServiceObject(org.jomc.modlet.Service, java.lang.Class) createServiceObject( <i>service</i>, ModelProvider.class ) |
828 | * @see ModletObject#MODEL_PUBLIC_ID |
829 | */ |
830 | public abstract Model findModel( String model ) throws ModelException; |
831 | |
832 | /** |
833 | * Populates a given {@code Model} instance. |
834 | * |
835 | * @param model The {@code Model} to populate. |
836 | * |
837 | * @return The populated model. |
838 | * |
839 | * @throws NullPointerException if {@code model} is {@code null}. |
840 | * @throws ModelException if populating {@code model} fails. |
841 | * |
842 | * @see #createServiceObject(org.jomc.modlet.Service, java.lang.Class) createServiceObject( <i>service</i>, ModelProvider.class ) |
843 | * |
844 | * @since 1.2 |
845 | */ |
846 | public abstract Model findModel( Model model ) throws ModelException; |
847 | |
848 | /** |
849 | * Gets the name of the class providing the default {@code ModelContext} implementation. |
850 | * <p>The name of the class providing the default {@code ModelContext} implementation returned by method |
851 | * {@link #createModelContext(java.lang.ClassLoader)} is controlled by system property |
852 | * {@code org.jomc.modlet.ModelContext.className}. If that property is not set, the name of the |
853 | * {@link org.jomc.modlet.DefaultModelContext} class is returned.</p> |
854 | * |
855 | * @return The name of the class providing the default {@code ModelContext} implementation. |
856 | * |
857 | * @see #setModelContextClassName(java.lang.String) |
858 | * |
859 | * @deprecated As of JOMC 1.2, replaced by class {@link ModelContextFactory}. This method will be removed in version |
860 | * 2.0. |
861 | */ |
862 | @Deprecated |
863 | public static String getModelContextClassName() |
864 | { |
865 | if ( modelContextClassName == null ) |
866 | { |
867 | modelContextClassName = System.getProperty( "org.jomc.modlet.ModelContext.className", |
868 | DefaultModelContext.class.getName() ); |
869 | |
870 | } |
871 | |
872 | return modelContextClassName; |
873 | } |
874 | |
875 | /** |
876 | * Sets the name of the class providing the default {@code ModelContext} implementation. |
877 | * |
878 | * @param value The new name of the class providing the default {@code ModelContext} implementation or {@code null}. |
879 | * |
880 | * @see #getModelContextClassName() |
881 | * |
882 | * @deprecated As of JOMC 1.2, replaced by class {@link ModelContextFactory}. This method will be removed in version |
883 | * 2.0. |
884 | */ |
885 | @Deprecated |
886 | public static void setModelContextClassName( final String value ) |
887 | { |
888 | modelContextClassName = value; |
889 | } |
890 | |
891 | /** |
892 | * Creates a new default {@code ModelContext} instance. |
893 | * |
894 | * @param classLoader The class loader to create a new default {@code ModelContext} instance with or {@code null}, |
895 | * to create a new context using the platform's bootstrap class loader. |
896 | * |
897 | * @return A new {@code ModelContext} instance. |
898 | * |
899 | * @throws ModelException if creating a new {@code ModelContext} instance fails. |
900 | * |
901 | * @see #getModelContextClassName() |
902 | * |
903 | * @deprecated As of JOMC 1.2, replaced by method {@link ModelContextFactory#newModelContext(java.lang.ClassLoader)}. |
904 | * This method will be removed in version 2.0. |
905 | */ |
906 | public static ModelContext createModelContext( final ClassLoader classLoader ) throws ModelException |
907 | { |
908 | if ( getModelContextClassName().equals( DefaultModelContext.class.getName() ) ) |
909 | { |
910 | return new DefaultModelContext( classLoader ); |
911 | } |
912 | |
913 | try |
914 | { |
915 | final Class<?> clazz = Class.forName( getModelContextClassName(), false, classLoader ); |
916 | |
917 | if ( !ModelContext.class.isAssignableFrom( clazz ) ) |
918 | { |
919 | throw new ModelException( getMessage( "illegalContextImplementation", getModelContextClassName(), |
920 | ModelContext.class.getName() ) ); |
921 | |
922 | } |
923 | |
924 | final Constructor<? extends ModelContext> ctor = |
925 | clazz.asSubclass( ModelContext.class ).getDeclaredConstructor( ClassLoader.class ); |
926 | |
927 | return ctor.newInstance( classLoader ); |
928 | } |
929 | catch ( final ClassNotFoundException e ) |
930 | { |
931 | throw new ModelException( getMessage( "contextClassNotFound", getModelContextClassName() ), e ); |
932 | } |
933 | catch ( final NoSuchMethodException e ) |
934 | { |
935 | throw new ModelException( getMessage( "contextConstructorNotFound", getModelContextClassName() ), e ); |
936 | } |
937 | catch ( final InstantiationException e ) |
938 | { |
939 | final String message = getMessage( e ); |
940 | throw new ModelException( getMessage( "contextInstantiationException", getModelContextClassName(), |
941 | message != null ? " " + message : "" ), e ); |
942 | |
943 | } |
944 | catch ( final IllegalAccessException e ) |
945 | { |
946 | final String message = getMessage( e ); |
947 | throw new ModelException( getMessage( "contextConstructorAccessDenied", getModelContextClassName(), |
948 | message != null ? " " + message : "" ), e ); |
949 | |
950 | } |
951 | catch ( final InvocationTargetException e ) |
952 | { |
953 | String message = getMessage( e ); |
954 | if ( message == null && e.getTargetException() != null ) |
955 | { |
956 | message = getMessage( e.getTargetException() ); |
957 | } |
958 | |
959 | throw new ModelException( getMessage( "contextConstructorException", getModelContextClassName(), |
960 | message != null ? " " + message : "" ), e ); |
961 | |
962 | } |
963 | } |
964 | |
965 | /** |
966 | * Creates a new service object. |
967 | * |
968 | * @param <T> The type of the service. |
969 | * @param service The service to create a new object of. |
970 | * @param type The class of the type of the service. |
971 | * |
972 | * @return An new service object for {@code service}. |
973 | * |
974 | * @throws NullPointerException if {@code service} or {@code type} is {@code null}. |
975 | * @throws ModelException if creating the service object fails. |
976 | * |
977 | * @see ModelProvider |
978 | * @see ModelProcessor |
979 | * @see ModelValidator |
980 | * |
981 | * @since 1.2 |
982 | */ |
983 | public abstract <T> T createServiceObject( final Service service, final Class<T> type ) throws ModelException; |
984 | |
985 | /** |
986 | * Creates a new SAX entity resolver instance of a given model. |
987 | * |
988 | * @param model The identifier of the model to create a new SAX entity resolver of. |
989 | * |
990 | * @return A new SAX entity resolver instance of the model identified by {@code model}. |
991 | * |
992 | * @throws NullPointerException if {@code model} is {@code null}. |
993 | * @throws ModelException if creating a new SAX entity resolver instance fails. |
994 | * |
995 | * @see ModletObject#MODEL_PUBLIC_ID |
996 | */ |
997 | public abstract EntityResolver createEntityResolver( String model ) throws ModelException; |
998 | |
999 | /** |
1000 | * Creates a new SAX entity resolver instance for a given public identifier URI. |
1001 | * |
1002 | * @param publicId The public identifier URI to create a new SAX entity resolver for. |
1003 | * |
1004 | * @return A new SAX entity resolver instance for the public identifier URI {@code publicId}. |
1005 | * |
1006 | * @throws NullPointerException if {@code publicId} is {@code null}. |
1007 | * @throws ModelException if creating a new SAX entity resolver instance fails. |
1008 | * |
1009 | * @see ModletObject#PUBLIC_ID |
1010 | * @since 1.2 |
1011 | */ |
1012 | public abstract EntityResolver createEntityResolver( URI publicId ) throws ModelException; |
1013 | |
1014 | /** |
1015 | * Creates a new L/S resource resolver instance of a given model. |
1016 | * |
1017 | * @param model The identifier of the model to create a new L/S resource resolver of. |
1018 | * |
1019 | * @return A new L/S resource resolver instance of the model identified by {@code model}. |
1020 | * |
1021 | * @throws NullPointerException if {@code model} is {@code null}. |
1022 | * @throws ModelException if creating a new L/S resource resolver instance fails. |
1023 | * |
1024 | * @see ModletObject#MODEL_PUBLIC_ID |
1025 | */ |
1026 | public abstract LSResourceResolver createResourceResolver( String model ) throws ModelException; |
1027 | |
1028 | /** |
1029 | * Creates a new L/S resource resolver instance for a given public identifier URI. |
1030 | * |
1031 | * @param publicId The public identifier URI to create a new L/S resource resolver for. |
1032 | * |
1033 | * @return A new L/S resource resolver instance for the public identifier URI {@code publicId}. |
1034 | * |
1035 | * @throws NullPointerException if {@code publicId} is {@code null}. |
1036 | * @throws ModelException if creating a new L/S resource resolver instance fails. |
1037 | * |
1038 | * @see ModletObject#PUBLIC_ID |
1039 | * @since 1.2 |
1040 | */ |
1041 | public abstract LSResourceResolver createResourceResolver( URI publicId ) throws ModelException; |
1042 | |
1043 | /** |
1044 | * Creates a new JAXP schema instance of a given model. |
1045 | * |
1046 | * @param model The identifier of the model to create a new JAXP schema instance of. |
1047 | * |
1048 | * @return A new JAXP schema instance of the model identified by {@code model}. |
1049 | * |
1050 | * @throws NullPointerException if {@code model} is {@code null}. |
1051 | * @throws ModelException if creating a new JAXP schema instance fails. |
1052 | * |
1053 | * @see ModletObject#MODEL_PUBLIC_ID |
1054 | */ |
1055 | public abstract javax.xml.validation.Schema createSchema( String model ) throws ModelException; |
1056 | |
1057 | /** |
1058 | * Creates a new JAXP schema instance for a given public identifier URI. |
1059 | * |
1060 | * @param publicId The public identifier URI to create a new JAXP schema instance for. |
1061 | * |
1062 | * @return A new JAXP schema instance for the public identifier URI {@code publicId}. |
1063 | * |
1064 | * @throws NullPointerException if {@code publicId} is {@code null}. |
1065 | * @throws ModelException if creating a new JAXP schema instance fails. |
1066 | * |
1067 | * @see ModletObject#PUBLIC_ID |
1068 | * @since 1.2 |
1069 | */ |
1070 | public abstract javax.xml.validation.Schema createSchema( URI publicId ) throws ModelException; |
1071 | |
1072 | /** |
1073 | * Creates a new JAXB context instance of a given model. |
1074 | * |
1075 | * @param model The identifier of the model to create a new JAXB context instance of. |
1076 | * |
1077 | * @return A new JAXB context instance of the model identified by {@code model}. |
1078 | * |
1079 | * @throws NullPointerException if {@code model} is {@code null}. |
1080 | * @throws ModelException if creating a new JAXB context instance fails. |
1081 | * |
1082 | * @see ModletObject#MODEL_PUBLIC_ID |
1083 | */ |
1084 | public abstract JAXBContext createContext( String model ) throws ModelException; |
1085 | |
1086 | /** |
1087 | * Creates a new JAXB context instance for a given public identifier URI. |
1088 | * |
1089 | * @param publicId The public identifier URI to create a new JAXB context instance for. |
1090 | * |
1091 | * @return A new JAXB context instance for the public identifier URI {@code publicId}. |
1092 | * |
1093 | * @throws NullPointerException if {@code publicId} is {@code null}. |
1094 | * @throws ModelException if creating a new JAXB context instance fails. |
1095 | * |
1096 | * @see ModletObject#PUBLIC_ID |
1097 | * @since 1.2 |
1098 | */ |
1099 | public abstract JAXBContext createContext( URI publicId ) throws ModelException; |
1100 | |
1101 | /** |
1102 | * Creates a new JAXB marshaller instance of a given model. |
1103 | * |
1104 | * @param model The identifier of the model to create a new JAXB marshaller instance of. |
1105 | * |
1106 | * @return A new JAXB marshaller instance of the model identified by {@code model}. |
1107 | * |
1108 | * @throws NullPointerException if {@code model} is {@code null}. |
1109 | * @throws ModelException if creating a new JAXB marshaller instance fails. |
1110 | * |
1111 | * @see ModletObject#MODEL_PUBLIC_ID |
1112 | */ |
1113 | public abstract Marshaller createMarshaller( String model ) throws ModelException; |
1114 | |
1115 | /** |
1116 | * Creates a new JAXB marshaller instance for a given public identifier URI. |
1117 | * |
1118 | * @param publicId The public identifier URI to create a new JAXB marshaller instance for. |
1119 | * |
1120 | * @return A new JAXB marshaller instance for the public identifier URI {@code publicId}. |
1121 | * |
1122 | * @throws NullPointerException if {@code publicId} is {@code null}. |
1123 | * @throws ModelException if creating a new JAXB marshaller instance fails. |
1124 | * |
1125 | * @see ModletObject#PUBLIC_ID |
1126 | * @since 1.2 |
1127 | */ |
1128 | public abstract Marshaller createMarshaller( URI publicId ) throws ModelException; |
1129 | |
1130 | /** |
1131 | * Creates a new JAXB unmarshaller instance of a given model. |
1132 | * |
1133 | * @param model The identifier of the model to create a new JAXB unmarshaller instance of. |
1134 | * |
1135 | * @return A new JAXB unmarshaller instance of the model identified by {@code model}. |
1136 | * |
1137 | * @throws NullPointerException if {@code model} is {@code null}. |
1138 | * @throws ModelException if creating a new JAXB unmarshaller instance fails. |
1139 | * |
1140 | * @see ModletObject#MODEL_PUBLIC_ID |
1141 | */ |
1142 | public abstract Unmarshaller createUnmarshaller( String model ) throws ModelException; |
1143 | |
1144 | /** |
1145 | * Creates a new JAXB unmarshaller instance for a given given public identifier URI. |
1146 | * |
1147 | * @param publicId The public identifier URI to create a new JAXB unmarshaller instance for. |
1148 | * |
1149 | * @return A new JAXB unmarshaller instance for the public identifier URI {@code publicId}. |
1150 | * |
1151 | * @throws NullPointerException if {@code publicId} is {@code null}. |
1152 | * @throws ModelException if creating a new JAXB unmarshaller instance fails. |
1153 | * |
1154 | * @see ModletObject#PUBLIC_ID |
1155 | * @since 1.2 |
1156 | */ |
1157 | public abstract Unmarshaller createUnmarshaller( URI publicId ) throws ModelException; |
1158 | |
1159 | /** |
1160 | * Processes a {@code Model}. |
1161 | * |
1162 | * @param model The {@code Model} to process. |
1163 | * |
1164 | * @return The processed {@code Model}. |
1165 | * |
1166 | * @throws NullPointerException if {@code model} is {@code null}. |
1167 | * @throws ModelException if processing {@code model} fails. |
1168 | * |
1169 | * @see #createServiceObject(org.jomc.modlet.Service, java.lang.Class) createServiceObject( <i>service</i>, ModelProcessor.class ) |
1170 | */ |
1171 | public abstract Model processModel( Model model ) throws ModelException; |
1172 | |
1173 | /** |
1174 | * Validates a given {@code Model}. |
1175 | * |
1176 | * @param model The {@code Model} to validate. |
1177 | * |
1178 | * @return Validation report. |
1179 | * |
1180 | * @throws NullPointerException if {@code model} is {@code null}. |
1181 | * @throws ModelException if validating the modules fails. |
1182 | * |
1183 | * @see #createServiceObject(org.jomc.modlet.Service, java.lang.Class) createServiceObject( <i>service</i>, ModelValidator.class ) |
1184 | * @see ModelValidationReport#isModelValid() |
1185 | */ |
1186 | public abstract ModelValidationReport validateModel( Model model ) throws ModelException; |
1187 | |
1188 | /** |
1189 | * Validates a given model. |
1190 | * |
1191 | * @param model The identifier of the {@code Model} to use for validating {@code source}. |
1192 | * @param source A source providing the model to validate. |
1193 | * |
1194 | * @return Validation report. |
1195 | * |
1196 | * @throws NullPointerException if {@code model} or {@code source} is {@code null}. |
1197 | * @throws ModelException if validating the model fails. |
1198 | * |
1199 | * @see #createSchema(java.lang.String) |
1200 | * @see ModelValidationReport#isModelValid() |
1201 | * @see ModletObject#MODEL_PUBLIC_ID |
1202 | */ |
1203 | public abstract ModelValidationReport validateModel( String model, Source source ) throws ModelException; |
1204 | |
1205 | private static String getMessage( final String key, final Object... args ) |
1206 | { |
1207 | return MessageFormat.format( ResourceBundle.getBundle( |
1208 | ModelContext.class.getName().replace( '.', '/' ), Locale.getDefault() ).getString( key ), args ); |
1209 | |
1210 | } |
1211 | |
1212 | private static String getMessage( final Throwable t ) |
1213 | { |
1214 | return t != null |
1215 | ? t.getMessage() != null && t.getMessage().trim().length() > 0 |
1216 | ? t.getMessage() |
1217 | : getMessage( t.getCause() ) |
1218 | : null; |
1219 | |
1220 | } |
1221 | |
1222 | } |