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
32
33
34
35
36 package org.jomc.cli;
37
38 import java.io.BufferedReader;
39 import java.io.IOException;
40 import java.io.PrintWriter;
41 import java.io.StringReader;
42 import java.io.StringWriter;
43 import java.util.Date;
44 import java.util.logging.Level;
45 import org.apache.commons.cli.CommandLine;
46 import org.apache.commons.cli.HelpFormatter;
47 import org.apache.commons.cli.Options;
48 import org.apache.commons.cli.ParseException;
49 import org.apache.commons.lang.StringUtils;
50 import org.jomc.model.modlet.DefaultModelProcessor;
51 import org.jomc.model.modlet.DefaultModelProvider;
52 import org.jomc.modlet.DefaultModletProvider;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
75
76
77 public final class Jomc
78 {
79
80
81
82
83
84
85
86 private static final Level DEFAULT_LOG_LEVEL = Level.WARNING;
87
88
89
90
91 private static volatile Level defaultLogLevel;
92
93
94
95
96 private PrintWriter printWriter;
97
98
99
100
101 private Level logLevel;
102
103
104
105
106 private Level severity = Level.ALL;
107
108
109
110
111
112
113
114
115 public PrintWriter getPrintWriter()
116 {
117 if ( this.printWriter == null )
118 {
119
120 this.printWriter = new PrintWriter( System.out, true );
121 }
122
123 return this.printWriter;
124 }
125
126
127
128
129
130
131
132
133 public void setPrintWriter( final PrintWriter value )
134 {
135 this.printWriter = value;
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150 public static Level getDefaultLogLevel()
151 {
152 if ( defaultLogLevel == null )
153 {
154 defaultLogLevel = Level.parse( System.getProperty(
155 "org.jomc.cli.Jomc.defaultLogLevel", DEFAULT_LOG_LEVEL.getName() ) );
156
157 }
158
159 return defaultLogLevel;
160 }
161
162
163
164
165
166
167
168
169 public static void setDefaultLogLevel( final Level value )
170 {
171 defaultLogLevel = value;
172 }
173
174
175
176
177
178
179
180
181
182
183 public Level getLogLevel()
184 {
185 if ( this.logLevel == null )
186 {
187 this.logLevel = getDefaultLogLevel();
188
189 if ( this.isLoggable( Level.CONFIG ) )
190 {
191 this.log( Level.CONFIG,
192 this.getDefaultLogLevelInfo( this.getLocale(), this.logLevel.getLocalizedName() ), null );
193
194 }
195 }
196
197 return this.logLevel;
198 }
199
200
201
202
203
204
205
206
207
208 public void setLogLevel( final Level value )
209 {
210 this.logLevel = value;
211 }
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226 public boolean isLoggable( final Level level )
227 {
228 if ( level == null )
229 {
230 throw new NullPointerException( "level" );
231 }
232
233 return level.intValue() >= this.getLogLevel().intValue();
234 }
235
236
237
238
239
240
241
242
243
244
245
246 public int jomc( final String[] args )
247 {
248 Command cmd = null;
249 this.severity = Level.ALL;
250
251 try
252 {
253 DefaultModelProvider.setDefaultModuleLocation( "META-INF/jomc-cli.xml" );
254 DefaultModelProcessor.setDefaultTransformerLocation( "META-INF/jomc-cli.xsl" );
255 DefaultModletProvider.setDefaultModletLocation( "META-INF/jomc-modlet.xml" );
256
257 final StringBuilder commandInfo = new StringBuilder();
258
259 for ( final Command c : this.getCommands() )
260 {
261 if ( cmd == null && args != null && args.length > 0
262 && ( args[0].equals( c.getName() ) || args[0].equals( c.getAbbreviatedName() ) ) )
263 {
264 cmd = c;
265 }
266
267 commandInfo.append( StringUtils.rightPad( c.getName(), 25 ) ).append( " : " ).
268 append( c.getShortDescription( this.getLocale() ) ).append( " (" ).append( c.getAbbreviatedName() ).
269 append( ")" ).append( System.getProperty( "line.separator", "\n" ) );
270
271 }
272
273 if ( cmd == null )
274 {
275 this.getPrintWriter().println( this.getUsage( this.getLocale(), this.getHelpCommandName() ) );
276 this.getPrintWriter().println();
277 this.getPrintWriter().println( commandInfo.toString() );
278 return Command.STATUS_FAILURE;
279 }
280
281 final String[] commandArguments = new String[ args.length - 1 ];
282 System.arraycopy( args, 1, commandArguments, 0, commandArguments.length );
283
284 final Options options = cmd.getOptions();
285 options.addOption( this.getDebugOption() );
286 options.addOption( this.getVerboseOption() );
287 options.addOption( this.getFailOnWarningsOption() );
288
289 if ( commandArguments.length > 0 && this.getHelpCommandName().equals( commandArguments[0] ) )
290 {
291 final StringWriter usage = new StringWriter();
292 final StringWriter opts = new StringWriter();
293 final HelpFormatter formatter = new HelpFormatter();
294
295 PrintWriter pw = new PrintWriter( usage );
296 formatter.printUsage( pw, this.getWidth(), cmd.getName(), options );
297 pw.close();
298 assert !pw.checkError() : "Unexpected error printing usage.";
299
300 pw = new PrintWriter( opts );
301 formatter.printOptions( pw, this.getWidth(), options, this.getLeftPad(), this.getDescPad() );
302 pw.close();
303 assert !pw.checkError() : "Unexpected error printing options.";
304
305 this.getPrintWriter().println( cmd.getShortDescription( this.getLocale() ) );
306 this.getPrintWriter().println();
307 this.getPrintWriter().println( usage.toString() );
308 this.getPrintWriter().println( opts.toString() );
309 this.getPrintWriter().println();
310 this.getPrintWriter().println( cmd.getLongDescription( this.getLocale() ) );
311 this.getPrintWriter().println();
312 return Command.STATUS_SUCCESS;
313 }
314
315 cmd.getListeners().add( new Command.Listener()
316 {
317
318 public void onLog( final Level level, final String message, final Throwable t )
319 {
320 log( level, message, t );
321 }
322
323 } );
324
325 DefaultModelProvider.setDefaultModuleLocation( null );
326 DefaultModelProcessor.setDefaultTransformerLocation( null );
327 DefaultModletProvider.setDefaultModletLocation( null );
328
329 final CommandLine commandLine = this.getCommandLineParser().parse( options, commandArguments );
330 final boolean debug = commandLine.hasOption( this.getDebugOption().getOpt() );
331 final boolean verbose = commandLine.hasOption( this.getVerboseOption().getOpt() );
332 Level debugLevel = Level.ALL;
333
334 if ( debug )
335 {
336 final String debugOption = commandLine.getOptionValue( this.getDebugOption().getOpt() );
337 if ( debugOption != null )
338 {
339 debugLevel = Level.parse( debugOption );
340 }
341 }
342
343 if ( debug || verbose )
344 {
345 this.setLogLevel( debug ? debugLevel : Level.INFO );
346 }
347
348 cmd.setLogLevel( this.getLogLevel() );
349
350 if ( this.isLoggable( Level.FINER ) )
351 {
352 for ( int i = 0; i < args.length; i++ )
353 {
354 this.log( Level.FINER, new StringBuilder().append( "[" ).append( i ).append( "] -> '" ).
355 append( args[i] ).append( "'" ).append( System.getProperty( "line.separator", "\n" ) ).
356 toString(), null );
357
358 }
359 }
360
361 final boolean failOnWarnings = commandLine.hasOption( this.getFailOnWarningsOption().getOpt() );
362
363 final int status = cmd.execute( commandLine );
364 if ( status == Command.STATUS_SUCCESS && failOnWarnings
365 && this.severity.intValue() >= Level.WARNING.intValue() )
366 {
367 return Command.STATUS_FAILURE;
368 }
369
370 return status;
371 }
372 catch ( final ParseException e )
373 {
374 this.log( Level.SEVERE, this.getIllegalArgumentsInfo(
375 this.getLocale(), cmd.getName(), this.getHelpCommandName() ), e );
376
377 return Command.STATUS_FAILURE;
378 }
379 catch ( final Throwable t )
380 {
381 this.log( Level.SEVERE, null, t );
382 return Command.STATUS_FAILURE;
383 }
384 finally
385 {
386 DefaultModelProvider.setDefaultModuleLocation( null );
387 DefaultModelProcessor.setDefaultTransformerLocation( null );
388 DefaultModletProvider.setDefaultModletLocation( null );
389 this.getPrintWriter().flush();
390 this.severity = Level.ALL;
391 }
392 }
393
394
395
396
397
398
399 public static void main( final String[] args )
400 {
401 System.exit( run( args ) );
402 }
403
404
405
406
407
408
409
410
411
412
413
414 public static int run( final String[] args )
415 {
416 return new Jomc().jomc( args );
417 }
418
419
420
421
422
423
424
425
426
427
428 private void log( final Level level, final String message, final Throwable throwable )
429 {
430 if ( level == null )
431 {
432 throw new NullPointerException( "level" );
433 }
434
435 if ( this.severity.intValue() < level.intValue() )
436 {
437 this.severity = level;
438 }
439
440 if ( this.isLoggable( level ) )
441 {
442 if ( message != null )
443 {
444 this.getPrintWriter().print( this.formatLogLines( level, "" ) );
445 this.getPrintWriter().print( this.formatLogLines( level, message ) );
446 }
447
448 if ( throwable != null )
449 {
450 this.getPrintWriter().print( this.formatLogLines( level, "" ) );
451 final String m = getMessage( throwable );
452
453 if ( m != null && m.length() > 0 )
454 {
455 this.getPrintWriter().print( this.formatLogLines( level, m ) );
456 }
457 else
458 {
459 this.getPrintWriter().print( this.formatLogLines(
460 level, this.getDefaultExceptionMessage( this.getLocale() ) ) );
461
462 }
463
464 if ( this.getLogLevel().intValue() < Level.INFO.intValue() )
465 {
466 final StringWriter stackTrace = new StringWriter();
467 final PrintWriter pw = new PrintWriter( stackTrace );
468 throwable.printStackTrace( pw );
469 pw.flush();
470 this.getPrintWriter().print( this.formatLogLines( level, stackTrace.toString() ) );
471 }
472 }
473 }
474
475 this.getPrintWriter().flush();
476 }
477
478 private String formatLogLines( final Level level, final String text )
479 {
480 BufferedReader reader = null;
481 boolean suppressExceptionOnClose = true;
482
483 try
484 {
485 final StringBuilder lines = new StringBuilder( text.length() );
486 reader = new BufferedReader( new StringReader( text ) );
487
488 String line;
489 while ( ( line = reader.readLine() ) != null )
490 {
491 final boolean debug = this.getLogLevel().intValue() < Level.INFO.intValue();
492 lines.append( "[" ).append( level.getLocalizedName() );
493
494 if ( debug )
495 {
496 lines.append( "|" ).append( Thread.currentThread().getName() ).append( "|" ).
497 append( this.getTimeInfo( this.getLocale(), new Date( System.currentTimeMillis() ) ) );
498
499 }
500
501 lines.append( "] " ).append( line ).append( System.getProperty( "line.separator", "\n" ) );
502 }
503
504 suppressExceptionOnClose = false;
505 return lines.toString();
506 }
507 catch ( final IOException e )
508 {
509 throw new AssertionError( e );
510 }
511 finally
512 {
513 try
514 {
515 if ( reader != null )
516 {
517 reader.close();
518 }
519 }
520 catch ( final IOException e )
521 {
522 if ( suppressExceptionOnClose )
523 {
524 this.log( Level.SEVERE, getMessage( e ), e );
525 }
526 else
527 {
528 throw new AssertionError( e );
529 }
530 }
531 }
532 }
533
534 private static String getMessage( final Throwable t )
535 {
536 return t != null
537 ? t.getMessage() != null && t.getMessage().trim().length() > 0
538 ? t.getMessage()
539 : getMessage( t.getCause() )
540 : null;
541
542 }
543
544
545
546
547
548 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
549 public Jomc()
550 {
551
552 super();
553
554 }
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571 @SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
572 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
573 private org.apache.commons.cli.CommandLineParser getCommandLineParser()
574 {
575 final org.apache.commons.cli.CommandLineParser _d = (org.apache.commons.cli.CommandLineParser) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Command Line Parser" );
576 assert _d != null : "'Command Line Parser' dependency not found.";
577 return _d;
578 }
579
580
581
582
583
584
585
586
587
588
589
590
591 @SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
592 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
593 private org.jomc.cli.Command[] getCommands()
594 {
595 final org.jomc.cli.Command[] _d = (org.jomc.cli.Command[]) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Commands" );
596 assert _d != null : "'Commands' dependency not found.";
597 return _d;
598 }
599
600
601
602
603
604
605
606
607
608
609
610
611 @SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
612 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
613 private org.apache.commons.cli.Option getDebugOption()
614 {
615 final org.apache.commons.cli.Option _d = (org.apache.commons.cli.Option) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Debug Option" );
616 assert _d != null : "'Debug Option' dependency not found.";
617 return _d;
618 }
619
620
621
622
623
624
625
626
627
628
629
630
631 @SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
632 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
633 private org.apache.commons.cli.Option getFailOnWarningsOption()
634 {
635 final org.apache.commons.cli.Option _d = (org.apache.commons.cli.Option) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Fail On Warnings Option" );
636 assert _d != null : "'Fail On Warnings Option' dependency not found.";
637 return _d;
638 }
639
640
641
642
643
644
645
646
647
648
649
650
651 @SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
652 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
653 private java.util.Locale getLocale()
654 {
655 final java.util.Locale _d = (java.util.Locale) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Locale" );
656 assert _d != null : "'Locale' dependency not found.";
657 return _d;
658 }
659
660
661
662
663
664
665
666
667
668
669
670
671 @SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
672 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
673 private org.apache.commons.cli.Option getVerboseOption()
674 {
675 final org.apache.commons.cli.Option _d = (org.apache.commons.cli.Option) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Verbose Option" );
676 assert _d != null : "'Verbose Option' dependency not found.";
677 return _d;
678 }
679
680
681
682
683
684
685
686
687
688
689
690
691 @SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
692 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
693 private int getDescPad()
694 {
695 final java.lang.Integer _p = (java.lang.Integer) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "Desc Pad" );
696 assert _p != null : "'Desc Pad' property not found.";
697 return _p.intValue();
698 }
699
700
701
702
703
704
705
706
707 @SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
708 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
709 private java.lang.String getHelpCommandName()
710 {
711 final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "Help Command Name" );
712 assert _p != null : "'Help Command Name' property not found.";
713 return _p;
714 }
715
716
717
718
719
720
721
722
723 @SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
724 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
725 private int getLeftPad()
726 {
727 final java.lang.Integer _p = (java.lang.Integer) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "Left Pad" );
728 assert _p != null : "'Left Pad' property not found.";
729 return _p.intValue();
730 }
731
732
733
734
735
736
737
738
739 @SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
740 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
741 private int getWidth()
742 {
743 final java.lang.Integer _p = (java.lang.Integer) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "width" );
744 assert _p != null : "'width' property not found.";
745 return _p.intValue();
746 }
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763 @SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
764 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
765 private String getDefaultExceptionMessage( final java.util.Locale locale )
766 {
767 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Default Exception Message", locale );
768 assert _m != null : "'Default Exception Message' message not found.";
769 return _m;
770 }
771
772
773
774
775
776
777
778
779
780
781
782
783
784 @SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
785 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
786 private String getDefaultLogLevelInfo( final java.util.Locale locale, final java.lang.String defaultLogLevel )
787 {
788 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Default Log Level Info", locale, defaultLogLevel );
789 assert _m != null : "'Default Log Level Info' message not found.";
790 return _m;
791 }
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806 @SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
807 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
808 private String getIllegalArgumentsInfo( final java.util.Locale locale, final java.lang.String command, final java.lang.String helpCommandName )
809 {
810 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Illegal Arguments Info", locale, command, helpCommandName );
811 assert _m != null : "'Illegal Arguments Info' message not found.";
812 return _m;
813 }
814
815
816
817
818
819
820
821
822
823
824
825
826
827 @SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
828 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
829 private String getTimeInfo( final java.util.Locale locale, final java.util.Date time )
830 {
831 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Time Info", locale, time );
832 assert _m != null : "'Time Info' message not found.";
833 return _m;
834 }
835
836
837
838
839
840
841
842
843
844
845
846
847
848 @SuppressWarnings({"unchecked", "unused", "PMD.UnnecessaryFullyQualifiedName"})
849 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.9", comments = "See http://www.jomc.org/jomc/1.9/jomc-tools-1.9" )
850 private String getUsage( final java.util.Locale locale, final java.lang.String helpCommandName )
851 {
852 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Usage", locale, helpCommandName );
853 assert _m != null : "'Usage' message not found.";
854 return _m;
855 }
856
857
858
859 }