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