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
61 public static final String ENABLED_ATTRIBUTE_NAME = "org.jomc.modlet.DefaultModletProvider.enabledAttribute";
62
63
64
65
66
67
68
69 private static final String DEFAULT_ENABLED_PROPERTY_NAME =
70 "org.jomc.modlet.DefaultModletProvider.defaultEnabled";
71
72
73
74
75
76
77
78 private static final Boolean DEFAULT_ENABLED = Boolean.TRUE;
79
80
81
82
83 private static volatile Boolean defaultEnabled;
84
85
86
87
88 private Boolean enabled;
89
90
91
92
93
94
95
96
97 public static final String MODLET_LOCATION_ATTRIBUTE_NAME =
98 "org.jomc.modlet.DefaultModletProvider.modletLocationAttribute";
99
100
101
102
103
104
105
106 private static final String DEFAULT_MODLET_LOCATION_PROPERTY_NAME =
107 "org.jomc.modlet.DefaultModletProvider.defaultModletLocation";
108
109
110
111
112
113
114 private static final String DEFAULT_MODLET_LOCATION = "META-INF/jomc-modlet.xml";
115
116
117
118
119 private static volatile String defaultModletLocation;
120
121
122
123
124 private String modletLocation;
125
126
127
128
129
130
131
132
133 public static final String VALIDATING_ATTRIBUTE_NAME =
134 "org.jomc.modlet.DefaultModletProvider.validatingAttribute";
135
136
137
138
139
140
141
142 private static final String DEFAULT_VALIDATING_PROPERTY_NAME =
143 "org.jomc.modlet.DefaultModletProvider.defaultValidating";
144
145
146
147
148
149
150
151 private static final Boolean DEFAULT_VALIDATING = Boolean.TRUE;
152
153
154
155
156
157
158 private static volatile Boolean defaultValidating;
159
160
161
162
163
164
165 private Boolean validating;
166
167
168
169
170
171
172
173 private static final String DEFAULT_ORDINAL_PROPERTY_NAME =
174 "org.jomc.modlet.DefaultModletProvider.defaultOrdinal";
175
176
177
178
179
180
181
182 private static final Integer DEFAULT_ORDINAL = 0;
183
184
185
186
187
188
189 private static volatile Integer defaultOrdinal;
190
191
192
193
194
195
196 private Integer ordinal;
197
198
199
200
201 public DefaultModletProvider()
202 {
203 super();
204 }
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220 public static boolean isDefaultEnabled()
221 {
222 if ( defaultEnabled == null )
223 {
224 defaultEnabled = Boolean.valueOf( System.getProperty(
225 DEFAULT_ENABLED_PROPERTY_NAME, Boolean.toString( DEFAULT_ENABLED ) ) );
226
227 }
228
229 return defaultEnabled;
230 }
231
232
233
234
235
236
237
238
239 public static void setDefaultEnabled( final Boolean value )
240 {
241 defaultEnabled = value;
242 }
243
244
245
246
247
248
249
250
251
252 public final boolean isEnabled()
253 {
254 if ( this.enabled == null )
255 {
256 this.enabled = isDefaultEnabled();
257 }
258
259 return this.enabled;
260 }
261
262
263
264
265
266
267
268
269 public final void setEnabled( final Boolean value )
270 {
271 this.enabled = value;
272 }
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287 public static String getDefaultModletLocation()
288 {
289 if ( defaultModletLocation == null )
290 {
291 defaultModletLocation = System.getProperty(
292 DEFAULT_MODLET_LOCATION_PROPERTY_NAME, DEFAULT_MODLET_LOCATION );
293
294 }
295
296 return defaultModletLocation;
297 }
298
299
300
301
302
303
304
305
306 public static void setDefaultModletLocation( final String value )
307 {
308 defaultModletLocation = value;
309 }
310
311
312
313
314
315
316
317
318
319 public final String getModletLocation()
320 {
321 if ( this.modletLocation == null )
322 {
323 this.modletLocation = getDefaultModletLocation();
324 }
325
326 return this.modletLocation;
327 }
328
329
330
331
332
333
334
335
336 public final void setModletLocation( final String value )
337 {
338 this.modletLocation = value;
339 }
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357 public static boolean isDefaultValidating()
358 {
359 if ( defaultValidating == null )
360 {
361 defaultValidating = Boolean.valueOf( System.getProperty(
362 DEFAULT_VALIDATING_PROPERTY_NAME, Boolean.toString( DEFAULT_VALIDATING ) ) );
363
364 }
365
366 return defaultValidating;
367 }
368
369
370
371
372
373
374
375
376
377
378
379 public static void setDefaultValidating( final Boolean value )
380 {
381 defaultValidating = value;
382 }
383
384
385
386
387
388
389
390
391
392
393
394
395 public final boolean isValidating()
396 {
397 if ( this.validating == null )
398 {
399 this.validating = isDefaultValidating();
400 }
401
402 return this.validating;
403 }
404
405
406
407
408
409
410
411
412
413
414 public final void setValidating( final Boolean value )
415 {
416 this.validating = value;
417 }
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433 public static int getDefaultOrdinal()
434 {
435 if ( defaultOrdinal == null )
436 {
437 defaultOrdinal = Integer.getInteger( DEFAULT_ORDINAL_PROPERTY_NAME, DEFAULT_ORDINAL );
438 }
439
440 return defaultOrdinal;
441 }
442
443
444
445
446
447
448
449
450
451
452 public static void setDefaultOrdinal( final Integer value )
453 {
454 defaultOrdinal = value;
455 }
456
457
458
459
460
461
462
463
464
465
466
467 public final int getOrdinal()
468 {
469 if ( this.ordinal == null )
470 {
471 this.ordinal = getDefaultOrdinal();
472 }
473
474 return this.ordinal;
475 }
476
477
478
479
480
481
482
483
484
485
486 public final void setOrdinal( final Integer value )
487 {
488 this.ordinal = value;
489 }
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506 public Modlets findModlets( final ModelContext context, final String location ) throws ModelException
507 {
508 if ( context == null )
509 {
510 throw new NullPointerException( "context" );
511 }
512 if ( location == null )
513 {
514 throw new NullPointerException( "location" );
515 }
516
517 URL url = null;
518
519 try
520 {
521 boolean contextValidating = this.isValidating();
522 if ( DEFAULT_VALIDATING == contextValidating
523 && context.getAttribute( VALIDATING_ATTRIBUTE_NAME ) instanceof Boolean )
524 {
525 contextValidating = (Boolean) context.getAttribute( VALIDATING_ATTRIBUTE_NAME );
526 }
527
528 Modlets modlets = null;
529 final long t0 = System.currentTimeMillis();
530 final JAXBContext ctx = context.createContext( ModletObject.MODEL_PUBLIC_ID );
531 final Unmarshaller u = ctx.createUnmarshaller();
532 final Enumeration<URL> e = context.findResources( location );
533
534 if ( contextValidating )
535 {
536 u.setSchema( context.createSchema( ModletObject.MODEL_PUBLIC_ID ) );
537 }
538
539 while ( e.hasMoreElements() )
540 {
541 url = e.nextElement();
542 Object content = u.unmarshal( url );
543 if ( content instanceof JAXBElement<?> )
544 {
545 content = ( (JAXBElement<?>) content ).getValue();
546 }
547
548 if ( content instanceof Modlet )
549 {
550 if ( modlets == null )
551 {
552 modlets = new Modlets();
553 }
554
555 modlets.getModlet().add( (Modlet) content );
556 }
557 else if ( content instanceof Modlets )
558 {
559 if ( modlets == null )
560 {
561 modlets = new Modlets();
562 }
563
564 modlets.getModlet().addAll( ( (Modlets) content ).getModlet() );
565 }
566 }
567
568 if ( context.isLoggable( Level.FINE ) )
569 {
570 context.log( Level.FINE, getMessage( "contextReport",
571 modlets != null ? modlets.getModlet().size() : 0,
572 location, System.currentTimeMillis() - t0 ), null );
573
574 }
575
576 return modlets == null || modlets.getModlet().isEmpty() ? null : modlets;
577 }
578 catch ( final UnmarshalException e )
579 {
580 String message = getMessage( e );
581 if ( message == null && e.getLinkedException() != null )
582 {
583 message = getMessage( e.getLinkedException() );
584 }
585
586 if ( url != null )
587 {
588 message = getMessage( "unmarshalException", url.toExternalForm(),
589 message != null ? " " + message : "" );
590
591 }
592
593 throw new ModelException( message, e );
594 }
595 catch ( final JAXBException e )
596 {
597 String message = getMessage( e );
598 if ( message == null && e.getLinkedException() != null )
599 {
600 message = getMessage( e.getLinkedException() );
601 }
602
603 throw new ModelException( message, e );
604 }
605 }
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621 @Deprecated
622 public Modlets findModlets( final ModelContext context ) throws ModelException
623 {
624 if ( context == null )
625 {
626 throw new NullPointerException( "context" );
627 }
628
629 return this.findModlets( context, new Modlets() );
630 }
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645 public Modlets findModlets( final ModelContext context, final Modlets modlets ) throws ModelException
646 {
647 if ( context == null )
648 {
649 throw new NullPointerException( "context" );
650 }
651 if ( modlets == null )
652 {
653 throw new NullPointerException( "context" );
654 }
655
656 Modlets provided = null;
657
658 boolean contextEnabled = this.isEnabled();
659 if ( DEFAULT_ENABLED == contextEnabled && context.getAttribute( ENABLED_ATTRIBUTE_NAME ) instanceof Boolean )
660 {
661 contextEnabled = (Boolean) context.getAttribute( ENABLED_ATTRIBUTE_NAME );
662 }
663
664 String contextModletLocation = this.getModletLocation();
665 if ( DEFAULT_MODLET_LOCATION.equals( contextModletLocation )
666 && context.getAttribute( MODLET_LOCATION_ATTRIBUTE_NAME ) instanceof String )
667 {
668 contextModletLocation = (String) context.getAttribute( MODLET_LOCATION_ATTRIBUTE_NAME );
669 }
670
671 if ( contextEnabled )
672 {
673 final Modlets found = this.findModlets( context, contextModletLocation );
674
675 if ( found != null )
676 {
677 provided = modlets.clone();
678 provided.getModlet().addAll( found.getModlet() );
679 }
680 }
681 else if ( context.isLoggable( Level.FINER ) )
682 {
683 context.log( Level.FINER, getMessage( "disabled", this.getClass().getSimpleName() ), null );
684 }
685
686 return provided;
687 }
688
689 private static String getMessage( final String key, final Object... arguments )
690 {
691 return MessageFormat.format( ResourceBundle.getBundle(
692 DefaultModletProvider.class.getName().replace( '.', '/' ) ).getString( key ), arguments );
693
694 }
695
696 private static String getMessage( final Throwable t )
697 {
698 return t != null
699 ? t.getMessage() != null && t.getMessage().trim().length() > 0
700 ? t.getMessage()
701 : getMessage( t.getCause() )
702 : null;
703
704 }
705
706 }