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
30
31 package org.jomc.modlet;
32
33 import java.net.URL;
34 import java.text.MessageFormat;
35 import java.util.Enumeration;
36 import java.util.ResourceBundle;
37 import java.util.logging.Level;
38 import javax.xml.bind.JAXBContext;
39 import javax.xml.bind.JAXBElement;
40 import javax.xml.bind.JAXBException;
41 import javax.xml.bind.UnmarshalException;
42 import javax.xml.bind.Unmarshaller;
43
44
45
46
47
48
49
50
51 public class DefaultModletProvider implements ModletProvider
52 {
53
54
55
56
57
58
59
60 public static final String ENABLED_ATTRIBUTE_NAME = "org.jomc.modlet.DefaultModletProvider.enabledAttribute";
61
62
63
64
65
66
67 private static final String DEFAULT_ENABLED_PROPERTY_NAME =
68 "org.jomc.modlet.DefaultModletProvider.defaultEnabled";
69
70
71
72
73
74
75 private static final Boolean DEFAULT_ENABLED = Boolean.TRUE;
76
77
78 private static volatile Boolean defaultEnabled;
79
80
81 private Boolean enabled;
82
83
84
85
86
87
88
89 public static final String MODLET_LOCATION_ATTRIBUTE_NAME =
90 "org.jomc.modlet.DefaultModletProvider.modletLocationAttribute";
91
92
93
94
95
96
97 private static final String DEFAULT_MODLET_LOCATION_PROPERTY_NAME =
98 "org.jomc.modlet.DefaultModletProvider.defaultModletLocation";
99
100
101
102
103
104 private static final String DEFAULT_MODLET_LOCATION = "META-INF/jomc-modlet.xml";
105
106
107 private static volatile String defaultModletLocation;
108
109
110 private String modletLocation;
111
112
113
114
115
116
117
118 public static final String VALIDATING_ATTRIBUTE_NAME =
119 "org.jomc.modlet.DefaultModletProvider.validatingAttribute";
120
121
122
123
124
125
126 private static final String DEFAULT_VALIDATING_PROPERTY_NAME =
127 "org.jomc.modlet.DefaultModletProvider.defaultValidating";
128
129
130
131
132
133
134 private static final Boolean DEFAULT_VALIDATING = Boolean.TRUE;
135
136
137
138
139
140 private static volatile Boolean defaultValidating;
141
142
143
144
145
146 private Boolean validating;
147
148
149 public DefaultModletProvider()
150 {
151 super();
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166 public static boolean isDefaultEnabled()
167 {
168 if ( defaultEnabled == null )
169 {
170 defaultEnabled = Boolean.valueOf( System.getProperty(
171 DEFAULT_ENABLED_PROPERTY_NAME, Boolean.toString( DEFAULT_ENABLED ) ) );
172
173 }
174
175 return defaultEnabled;
176 }
177
178
179
180
181
182
183
184
185 public static void setDefaultEnabled( final Boolean value )
186 {
187 defaultEnabled = value;
188 }
189
190
191
192
193
194
195
196
197
198 public final boolean isEnabled()
199 {
200 if ( this.enabled == null )
201 {
202 this.enabled = isDefaultEnabled();
203 }
204
205 return this.enabled;
206 }
207
208
209
210
211
212
213
214
215 public final void setEnabled( final Boolean value )
216 {
217 this.enabled = value;
218 }
219
220
221
222
223
224
225
226
227
228
229
230
231 public static String getDefaultModletLocation()
232 {
233 if ( defaultModletLocation == null )
234 {
235 defaultModletLocation = System.getProperty(
236 DEFAULT_MODLET_LOCATION_PROPERTY_NAME, DEFAULT_MODLET_LOCATION );
237
238 }
239
240 return defaultModletLocation;
241 }
242
243
244
245
246
247
248
249
250 public static void setDefaultModletLocation( final String value )
251 {
252 defaultModletLocation = value;
253 }
254
255
256
257
258
259
260
261
262
263 public final String getModletLocation()
264 {
265 if ( this.modletLocation == null )
266 {
267 this.modletLocation = getDefaultModletLocation();
268 }
269
270 return this.modletLocation;
271 }
272
273
274
275
276
277
278
279
280 public final void setModletLocation( final String value )
281 {
282 this.modletLocation = value;
283 }
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299 public static boolean isDefaultValidating()
300 {
301 if ( defaultValidating == null )
302 {
303 defaultValidating = Boolean.valueOf( System.getProperty(
304 DEFAULT_VALIDATING_PROPERTY_NAME, Boolean.toString( DEFAULT_VALIDATING ) ) );
305
306 }
307
308 return defaultValidating;
309 }
310
311
312
313
314
315
316
317
318
319
320
321 public static void setDefaultValidating( final Boolean value )
322 {
323 defaultValidating = value;
324 }
325
326
327
328
329
330
331
332
333
334
335
336
337 public final boolean isValidating()
338 {
339 if ( this.validating == null )
340 {
341 this.validating = isDefaultValidating();
342 }
343
344 return this.validating;
345 }
346
347
348
349
350
351
352
353
354
355
356 public final void setValidating( final Boolean value )
357 {
358 this.validating = value;
359 }
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376 public Modlets findModlets( final ModelContext context, final String location ) throws ModelException
377 {
378 if ( context == null )
379 {
380 throw new NullPointerException( "context" );
381 }
382 if ( location == null )
383 {
384 throw new NullPointerException( "location" );
385 }
386
387 URL url = null;
388
389 try
390 {
391 boolean contextValidating = this.isValidating();
392 if ( DEFAULT_VALIDATING == contextValidating
393 && context.getAttribute( VALIDATING_ATTRIBUTE_NAME ) instanceof Boolean )
394 {
395 contextValidating = (Boolean) context.getAttribute( VALIDATING_ATTRIBUTE_NAME );
396 }
397
398 Modlets modlets = null;
399 final long t0 = System.currentTimeMillis();
400 final JAXBContext ctx = context.createContext( ModletObject.MODEL_PUBLIC_ID );
401 final Unmarshaller u = ctx.createUnmarshaller();
402 final Enumeration<URL> e = context.findResources( location );
403
404 if ( contextValidating )
405 {
406 u.setSchema( context.createSchema( ModletObject.MODEL_PUBLIC_ID ) );
407 }
408
409 while ( e.hasMoreElements() )
410 {
411 url = e.nextElement();
412 Object content = u.unmarshal( url );
413 if ( content instanceof JAXBElement<?> )
414 {
415 content = ( (JAXBElement<?>) content ).getValue();
416 }
417
418 if ( content instanceof Modlet )
419 {
420 if ( modlets == null )
421 {
422 modlets = new Modlets();
423 }
424
425 modlets.getModlet().add( (Modlet) content );
426 }
427 else if ( content instanceof Modlets )
428 {
429 if ( modlets == null )
430 {
431 modlets = new Modlets();
432 }
433
434 modlets.getModlet().addAll( ( (Modlets) content ).getModlet() );
435 }
436 }
437
438 if ( context.isLoggable( Level.FINE ) )
439 {
440 context.log( Level.FINE, getMessage( "contextReport",
441 modlets != null ? modlets.getModlet().size() : 0,
442 location, System.currentTimeMillis() - t0 ), null );
443
444 }
445
446 return modlets == null || modlets.getModlet().isEmpty() ? null : modlets;
447 }
448 catch ( final UnmarshalException e )
449 {
450 String message = getMessage( e );
451 if ( message == null && e.getLinkedException() != null )
452 {
453 message = getMessage( e.getLinkedException() );
454 }
455
456 if ( url != null )
457 {
458 message = getMessage( "unmarshalException", url.toExternalForm(),
459 message != null ? " " + message : "" );
460
461 }
462
463 throw new ModelException( message, e );
464 }
465 catch ( final JAXBException e )
466 {
467 String message = getMessage( e );
468 if ( message == null && e.getLinkedException() != null )
469 {
470 message = getMessage( e.getLinkedException() );
471 }
472
473 throw new ModelException( message, e );
474 }
475 }
476
477
478
479
480
481
482
483
484
485
486
487
488
489 public Modlets findModlets( final ModelContext context ) throws ModelException
490 {
491 if ( context == null )
492 {
493 throw new NullPointerException( "context" );
494 }
495
496 Modlets found = null;
497
498 boolean contextEnabled = this.isEnabled();
499 if ( DEFAULT_ENABLED == contextEnabled && context.getAttribute( ENABLED_ATTRIBUTE_NAME ) instanceof Boolean )
500 {
501 contextEnabled = (Boolean) context.getAttribute( ENABLED_ATTRIBUTE_NAME );
502 }
503
504 String contextModletLocation = this.getModletLocation();
505 if ( DEFAULT_MODLET_LOCATION.equals( contextModletLocation )
506 && context.getAttribute( MODLET_LOCATION_ATTRIBUTE_NAME ) instanceof String )
507 {
508 contextModletLocation = (String) context.getAttribute( MODLET_LOCATION_ATTRIBUTE_NAME );
509 }
510
511 if ( contextEnabled )
512 {
513 found = this.findModlets( context, contextModletLocation );
514 }
515 else if ( context.isLoggable( Level.FINER ) )
516 {
517 context.log( Level.FINER, getMessage( "disabled", this.getClass().getSimpleName() ), null );
518 }
519
520 return found;
521 }
522
523 private static String getMessage( final String key, final Object... arguments )
524 {
525 return MessageFormat.format( ResourceBundle.getBundle(
526 DefaultModletProvider.class.getName().replace( '.', '/' ) ).getString( key ), arguments );
527
528 }
529
530 private static String getMessage( final Throwable t )
531 {
532 return t != null ? t.getMessage() != null ? t.getMessage() : getMessage( t.getCause() ) : null;
533 }
534
535 }