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.model.modlet;
32
33 import java.net.URL;
34 import java.text.MessageFormat;
35 import java.util.Enumeration;
36 import java.util.Locale;
37 import java.util.ResourceBundle;
38 import java.util.logging.Level;
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 import org.jomc.model.Module;
44 import org.jomc.model.Modules;
45 import org.jomc.model.Text;
46 import org.jomc.model.Texts;
47 import org.jomc.modlet.Model;
48 import org.jomc.modlet.ModelContext;
49 import org.jomc.modlet.ModelException;
50 import org.jomc.modlet.ModelProvider;
51
52
53
54
55
56
57
58
59 public class DefaultModelProvider implements ModelProvider
60 {
61
62
63
64
65
66
67
68
69 public static final String ENABLED_ATTRIBUTE_NAME = "org.jomc.model.modlet.DefaultModelProvider.enabledAttribute";
70
71
72
73
74
75
76 private static final String DEFAULT_ENABLED_PROPERTY_NAME =
77 "org.jomc.model.modlet.DefaultModelProvider.defaultEnabled";
78
79
80
81
82
83 private static final String DEPRECATED_DEFAULT_ENABLED_PROPERTY_NAME =
84 "org.jomc.model.DefaultModelProvider.defaultEnabled";
85
86
87
88
89
90
91
92 private static final Boolean DEFAULT_ENABLED = Boolean.TRUE;
93
94
95
96
97 private static volatile Boolean defaultEnabled;
98
99
100
101
102 private Boolean enabled;
103
104
105
106
107
108
109
110
111 public static final String MODULE_LOCATION_ATTRIBUTE_NAME =
112 "org.jomc.model.modlet.DefaultModelProvider.moduleLocationAttribute";
113
114
115
116
117
118
119 private static final String DEFAULT_MODULE_LOCATION_PROPERTY_NAME =
120 "org.jomc.model.modlet.DefaultModelProvider.defaultModuleLocation";
121
122
123
124
125
126 private static final String DEPRECATED_DEFAULT_MODULE_LOCATION_PROPERTY_NAME =
127 "org.jomc.model.DefaultModelProvider.defaultModuleLocation";
128
129
130
131
132
133
134 private static final String DEFAULT_MODULE_LOCATION = "META-INF/jomc.xml";
135
136
137
138
139 private static volatile String defaultModuleLocation;
140
141
142
143
144 private String moduleLocation;
145
146
147
148
149
150
151
152
153 public static final String VALIDATING_ATTRIBUTE_NAME =
154 "org.jomc.model.modlet.DefaultModelProvider.validatingAttribute";
155
156
157
158
159
160
161
162 private static final String DEFAULT_VALIDATING_PROPERTY_NAME =
163 "org.jomc.model.modlet.DefaultModelProvider.defaultValidating";
164
165
166
167
168
169
170
171 private static final Boolean DEFAULT_VALIDATING = Boolean.TRUE;
172
173
174
175
176
177
178 private static volatile Boolean defaultValidating;
179
180
181
182
183
184
185 private Boolean validating;
186
187
188
189
190 public DefaultModelProvider()
191 {
192 super();
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 public static boolean isDefaultEnabled()
209 {
210 if ( defaultEnabled == null )
211 {
212 defaultEnabled =
213 Boolean.valueOf( System.getProperty( DEFAULT_ENABLED_PROPERTY_NAME,
214 System.getProperty( DEPRECATED_DEFAULT_ENABLED_PROPERTY_NAME,
215 Boolean.toString( DEFAULT_ENABLED ) ) ) );
216
217 }
218
219 return defaultEnabled;
220 }
221
222
223
224
225
226
227
228
229 public static void setDefaultEnabled( final Boolean value )
230 {
231 defaultEnabled = value;
232 }
233
234
235
236
237
238
239
240
241
242 public final boolean isEnabled()
243 {
244 if ( this.enabled == null )
245 {
246 this.enabled = isDefaultEnabled();
247 }
248
249 return this.enabled;
250 }
251
252
253
254
255
256
257
258
259 public final void setEnabled( final Boolean value )
260 {
261 this.enabled = value;
262 }
263
264
265
266
267
268
269
270
271
272
273
274
275
276 public static String getDefaultModuleLocation()
277 {
278 if ( defaultModuleLocation == null )
279 {
280 defaultModuleLocation =
281 System.getProperty( DEFAULT_MODULE_LOCATION_PROPERTY_NAME,
282 System.getProperty( DEPRECATED_DEFAULT_MODULE_LOCATION_PROPERTY_NAME,
283 DEFAULT_MODULE_LOCATION ) );
284
285 }
286
287 return defaultModuleLocation;
288 }
289
290
291
292
293
294
295
296
297 public static void setDefaultModuleLocation( final String value )
298 {
299 defaultModuleLocation = value;
300 }
301
302
303
304
305
306
307
308
309
310 public final String getModuleLocation()
311 {
312 if ( this.moduleLocation == null )
313 {
314 this.moduleLocation = getDefaultModuleLocation();
315 }
316
317 return this.moduleLocation;
318 }
319
320
321
322
323
324
325
326
327 public final void setModuleLocation( final String value )
328 {
329 this.moduleLocation = value;
330 }
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348 public static boolean isDefaultValidating()
349 {
350 if ( defaultValidating == null )
351 {
352 defaultValidating = Boolean.valueOf( System.getProperty(
353 DEFAULT_VALIDATING_PROPERTY_NAME, Boolean.toString( DEFAULT_VALIDATING ) ) );
354
355 }
356
357 return defaultValidating;
358 }
359
360
361
362
363
364
365
366
367
368
369
370 public static void setDefaultValidating( final Boolean value )
371 {
372 defaultValidating = value;
373 }
374
375
376
377
378
379
380
381
382
383
384
385
386 public final boolean isValidating()
387 {
388 if ( this.validating == null )
389 {
390 this.validating = isDefaultValidating();
391 }
392
393 return this.validating;
394 }
395
396
397
398
399
400
401
402
403
404
405 public final void setValidating( final Boolean value )
406 {
407 this.validating = value;
408 }
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425 public Modules findModules( final ModelContext context, final String model, final String location )
426 throws ModelException
427 {
428 if ( context == null )
429 {
430 throw new NullPointerException( "context" );
431 }
432 if ( model == null )
433 {
434 throw new NullPointerException( "model" );
435 }
436 if ( location == null )
437 {
438 throw new NullPointerException( "location" );
439 }
440
441 URL url = null;
442
443 try
444 {
445 boolean contextValidating = this.isValidating();
446 if ( DEFAULT_VALIDATING == contextValidating
447 && context.getAttribute( VALIDATING_ATTRIBUTE_NAME ) instanceof Boolean )
448 {
449 contextValidating = (Boolean) context.getAttribute( VALIDATING_ATTRIBUTE_NAME );
450 }
451
452 final long t0 = System.currentTimeMillis();
453 final Text text = new Text();
454 text.setLanguage( "en" );
455 text.setValue( getMessage( "contextModulesInfo", location ) );
456
457 final Modules modules = new Modules();
458 modules.setDocumentation( new Texts() );
459 modules.getDocumentation().setDefaultLanguage( "en" );
460 modules.getDocumentation().getText().add( text );
461
462 final Unmarshaller u = context.createUnmarshaller( model );
463 final Enumeration<URL> resources = context.findResources( location );
464
465 if ( contextValidating )
466 {
467 u.setSchema( context.createSchema( model ) );
468 }
469
470 int count = 0;
471 while ( resources.hasMoreElements() )
472 {
473 count++;
474 url = resources.nextElement();
475
476 if ( context.isLoggable( Level.FINEST ) )
477 {
478 context.log( Level.FINEST, getMessage( "processing", url.toExternalForm() ), null );
479 }
480
481 Object content = u.unmarshal( url );
482 if ( content instanceof JAXBElement<?> )
483 {
484 content = ( (JAXBElement<?>) content ).getValue();
485 }
486
487 if ( content instanceof Module )
488 {
489 final Module m = (Module) content;
490 if ( context.isLoggable( Level.FINEST ) )
491 {
492 context.log( Level.FINEST, getMessage(
493 "foundModule", m.getName(), m.getVersion() == null ? "" : m.getVersion() ), null );
494
495 }
496
497 modules.getModule().add( m );
498 }
499 else if ( context.isLoggable( Level.WARNING ) )
500 {
501 context.log( Level.WARNING, getMessage( "ignoringDocument",
502 content == null ? "<>" : content.toString(),
503 url.toExternalForm() ), null );
504
505 }
506 }
507
508 if ( context.isLoggable( Level.FINE ) )
509 {
510 context.log( Level.FINE, getMessage( "contextReport", count, location,
511 System.currentTimeMillis() - t0 ), null );
512
513 }
514
515 return modules.getModule().isEmpty() ? null : modules;
516 }
517 catch ( final UnmarshalException e )
518 {
519 String message = getMessage( e );
520 if ( message == null && e.getLinkedException() != null )
521 {
522 message = getMessage( e.getLinkedException() );
523 }
524
525 if ( url != null )
526 {
527 message = getMessage( "unmarshalException", url.toExternalForm(),
528 message != null ? " " + message : "" );
529
530 }
531
532 throw new ModelException( message, e );
533 }
534 catch ( final JAXBException e )
535 {
536 String message = getMessage( e );
537 if ( message == null && e.getLinkedException() != null )
538 {
539 message = getMessage( e.getLinkedException() );
540 }
541
542 throw new ModelException( message, e );
543 }
544 }
545
546
547
548
549
550
551
552
553
554
555
556
557
558 public Model findModel( final ModelContext context, final Model model ) throws ModelException
559 {
560 if ( context == null )
561 {
562 throw new NullPointerException( "context" );
563 }
564 if ( model == null )
565 {
566 throw new NullPointerException( "model" );
567 }
568
569 Model found = null;
570
571 boolean contextEnabled = this.isEnabled();
572 if ( DEFAULT_ENABLED == contextEnabled && context.getAttribute( ENABLED_ATTRIBUTE_NAME ) instanceof Boolean )
573 {
574 contextEnabled = (Boolean) context.getAttribute( ENABLED_ATTRIBUTE_NAME );
575 }
576
577 String contextModuleLocation = this.getModuleLocation();
578 if ( DEFAULT_MODULE_LOCATION.equals( contextModuleLocation )
579 && context.getAttribute( MODULE_LOCATION_ATTRIBUTE_NAME ) instanceof String )
580 {
581 contextModuleLocation = (String) context.getAttribute( MODULE_LOCATION_ATTRIBUTE_NAME );
582 }
583
584 if ( contextEnabled )
585 {
586 final Modules modules = this.findModules( context, model.getIdentifier(), contextModuleLocation );
587
588 if ( modules != null )
589 {
590 found = model.clone();
591 ModelHelper.addModules( found, modules );
592 }
593 }
594 else if ( context.isLoggable( Level.FINER ) )
595 {
596 context.log( Level.FINER, getMessage( "disabled", this.getClass().getSimpleName(),
597 model.getIdentifier() ), null );
598
599 }
600
601 return found;
602 }
603
604 private static String getMessage( final String key, final Object... args )
605 {
606 return MessageFormat.format( ResourceBundle.getBundle(
607 DefaultModelProvider.class.getName().replace( '.', '/' ), Locale.getDefault() ).getString( key ), args );
608
609 }
610
611 private static String getMessage( final Throwable t )
612 {
613 return t != null
614 ? t.getMessage() != null && t.getMessage().trim().length() > 0
615 ? t.getMessage()
616 : getMessage( t.getCause() )
617 : null;
618
619 }
620
621 }