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.commands;
37
38 import java.util.LinkedList;
39 import java.util.List;
40 import java.util.Locale;
41 import java.util.logging.Level;
42 import org.apache.commons.cli.CommandLine;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
67
68
69 public abstract class AbstractCommand
70 implements
71 org.jomc.cli.Command
72 {
73
74
75
76 private List<Listener> listeners;
77
78
79 private Level logLevel;
80
81
82
83
84
85
86
87
88
89
90
91 public final List<Listener> getListeners()
92 {
93 if ( this.listeners == null )
94 {
95 this.listeners = new LinkedList<Listener>();
96 }
97
98 return this.listeners;
99 }
100
101
102
103
104
105
106
107
108
109
110 public final Level getLogLevel()
111 {
112 if ( this.logLevel == null )
113 {
114 this.logLevel = getDefaultLogLevel();
115
116 if ( this.isLoggable( Level.CONFIG ) )
117 {
118 this.log( Level.CONFIG,
119 this.getDefaultLogLevelInfo( this.getLocale(), this.logLevel.getLocalizedName() ), null );
120
121 }
122 }
123
124 return this.logLevel;
125 }
126
127
128
129
130
131
132
133
134
135 public final void setLogLevel( final Level value )
136 {
137 this.logLevel = value;
138 }
139
140 public final String getName()
141 {
142 return this.getCommandName();
143 }
144
145 public final String getAbbreviatedName()
146 {
147 return this.getAbbreviatedCommandName();
148 }
149
150 public final String getShortDescription( final Locale locale )
151 {
152 return this.getShortDescriptionMessage( locale );
153 }
154
155 public final String getLongDescription( final Locale locale )
156 {
157 return this.getLongDescriptionMessage( locale );
158 }
159
160 public final int execute( final CommandLine commandLine )
161 {
162 if ( commandLine == null )
163 {
164 throw new NullPointerException( "commandLine" );
165 }
166
167 int status;
168
169 try
170 {
171 if ( this.isLoggable( Level.INFO ) )
172 {
173 this.log( Level.INFO, this.getSeparator( this.getLocale() ), null );
174 this.log( Level.INFO, this.getApplicationTitle( this.getLocale() ), null );
175 this.log( Level.INFO, this.getSeparator( this.getLocale() ), null );
176 this.log( Level.INFO, this.getCommandInfoMessage( this.getLocale(), this.getCommandName() ), null );
177 }
178
179 this.preExecuteCommand( commandLine );
180 this.executeCommand( commandLine );
181 status = STATUS_SUCCESS;
182 }
183 catch ( final Throwable t )
184 {
185 this.log( Level.SEVERE, null, t );
186 status = STATUS_FAILURE;
187 }
188 finally
189 {
190 try
191 {
192 this.postExecuteCommand( commandLine );
193 }
194 catch ( final Throwable t )
195 {
196 this.log( Level.SEVERE, null, t );
197 status = STATUS_FAILURE;
198 }
199 }
200
201 if ( this.isLoggable( Level.INFO ) )
202 {
203 if ( status == STATUS_SUCCESS )
204 {
205 this.log( Level.INFO, this.getCommandSuccessMessage( this.getLocale(), this.getCommandName() ), null );
206 }
207 else if ( status == STATUS_FAILURE )
208 {
209 this.log( Level.INFO, this.getCommandFailureMessage( this.getLocale(), this.getCommandName() ), null );
210 }
211
212 this.log( Level.INFO, this.getSeparator( this.getLocale() ), null );
213 }
214
215 return status;
216 }
217
218
219
220
221 private static volatile Level defaultLogLevel;
222
223
224
225
226
227
228
229
230
231
232
233
234 public static Level getDefaultLogLevel()
235 {
236 if ( defaultLogLevel == null )
237 {
238 defaultLogLevel = Level.parse(
239 System.getProperty( "org.jomc.cli.command.AbstractJomcCommand.defaultLogLevel",
240 System.getProperty( "org.jomc.cli.commands.AbstractCommand.defaultLogLevel",
241 Level.WARNING.getName() ) ) );
242
243 }
244
245 return defaultLogLevel;
246 }
247
248
249
250
251
252
253
254
255 public static void setDefaultLogLevel( final Level value )
256 {
257 defaultLogLevel = value;
258 }
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273 protected boolean isLoggable( final Level level )
274 {
275 if ( level == null )
276 {
277 throw new NullPointerException( "level" );
278 }
279
280 return level.intValue() >= this.getLogLevel().intValue();
281 }
282
283
284
285
286
287
288
289
290
291
292
293
294
295 protected void log( final Level level, final String message, final Throwable throwable )
296 {
297 if ( level == null )
298 {
299 throw new NullPointerException( "level" );
300 }
301
302 if ( this.isLoggable( level ) )
303 {
304 for ( Listener l : this.getListeners() )
305 {
306 l.onLog( level, message, throwable );
307 }
308 }
309 }
310
311
312
313
314
315
316
317
318
319
320
321 protected void preExecuteCommand( final CommandLine commandLine ) throws CommandExecutionException
322 {
323 if ( commandLine == null )
324 {
325 throw new NullPointerException( "commandLine" );
326 }
327 }
328
329
330
331
332
333
334
335
336
337
338 protected abstract void executeCommand( final CommandLine commandLine ) throws CommandExecutionException;
339
340
341
342
343
344
345
346
347
348
349
350
351 protected void postExecuteCommand( final CommandLine commandLine ) throws CommandExecutionException
352 {
353 if ( commandLine == null )
354 {
355 throw new NullPointerException( "commandLine" );
356 }
357 }
358
359
360
361
362
363
364
365
366 protected static String getExceptionMessage( final Throwable t )
367 {
368 return t != null ? t.getMessage() != null ? t.getMessage() : getExceptionMessage( t.getCause() ) : null;
369 }
370
371
372
373
374
375 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
376 public AbstractCommand()
377 {
378
379 super();
380
381 }
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
399 private java.util.Locale getLocale()
400 {
401 final java.util.Locale _d = (java.util.Locale) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Locale" );
402 assert _d != null : "'Locale' dependency not found.";
403 return _d;
404 }
405
406
407
408
409
410
411
412
413
414
415
416
417 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
418 private java.lang.String getAbbreviatedCommandName()
419 {
420 final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "abbreviatedCommandName" );
421 assert _p != null : "'abbreviatedCommandName' property not found.";
422 return _p;
423 }
424
425
426
427
428
429
430
431
432 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
433 private java.lang.String getCommandName()
434 {
435 final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "commandName" );
436 assert _p != null : "'commandName' property not found.";
437 return _p;
438 }
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
455 private String getApplicationTitle( final java.util.Locale locale )
456 {
457 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "applicationTitle", locale );
458 assert _m != null : "'applicationTitle' message not found.";
459 return _m;
460 }
461
462
463
464
465
466
467
468
469
470
471
472
473
474 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
475 private String getCommandFailureMessage( final java.util.Locale locale, final java.lang.String toolName )
476 {
477 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "commandFailureMessage", locale, toolName );
478 assert _m != null : "'commandFailureMessage' message not found.";
479 return _m;
480 }
481
482
483
484
485
486
487
488
489
490
491
492
493
494 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
495 private String getCommandInfoMessage( final java.util.Locale locale, final java.lang.String toolName )
496 {
497 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "commandInfoMessage", locale, toolName );
498 assert _m != null : "'commandInfoMessage' message not found.";
499 return _m;
500 }
501
502
503
504
505
506
507
508
509
510
511
512
513
514 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
515 private String getCommandSuccessMessage( final java.util.Locale locale, final java.lang.String toolName )
516 {
517 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "commandSuccessMessage", locale, toolName );
518 assert _m != null : "'commandSuccessMessage' message not found.";
519 return _m;
520 }
521
522
523
524
525
526
527
528
529
530
531
532
533
534 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
535 private String getDefaultLogLevelInfo( final java.util.Locale locale, final java.lang.String defaultLogLevel )
536 {
537 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "defaultLogLevelInfo", locale, defaultLogLevel );
538 assert _m != null : "'defaultLogLevelInfo' message not found.";
539 return _m;
540 }
541
542
543
544
545
546
547
548
549
550
551
552 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
553 private String getLongDescriptionMessage( final java.util.Locale locale )
554 {
555 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "longDescriptionMessage", locale );
556 assert _m != null : "'longDescriptionMessage' message not found.";
557 return _m;
558 }
559
560
561
562
563
564
565
566
567
568
569
570 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
571 private String getSeparator( final java.util.Locale locale )
572 {
573 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "separator", locale );
574 assert _m != null : "'separator' message not found.";
575 return _m;
576 }
577
578
579
580
581
582
583
584
585
586
587
588 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
589 private String getShortDescriptionMessage( final java.util.Locale locale )
590 {
591 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "shortDescriptionMessage", locale );
592 assert _m != null : "'shortDescriptionMessage' message not found.";
593 return _m;
594 }
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
612 public org.apache.commons.cli.Options getOptions()
613 {
614 final org.apache.commons.cli.Options options = new org.apache.commons.cli.Options();
615 return options;
616 }
617
618
619 }