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.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
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.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
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 @SuppressWarnings("unused")
399 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
400 private java.util.Locale getLocale()
401 {
402 final java.util.Locale _d = (java.util.Locale) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Locale" );
403 assert _d != null : "'Locale' dependency not found.";
404 return _d;
405 }
406
407
408
409
410
411
412
413
414
415
416
417
418 @SuppressWarnings("unused")
419 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
420 private java.lang.String getAbbreviatedCommandName()
421 {
422 final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "Abbreviated Command Name" );
423 assert _p != null : "'Abbreviated Command Name' property not found.";
424 return _p;
425 }
426
427
428
429
430
431
432
433
434 @SuppressWarnings("unused")
435 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
436 private java.lang.String getCommandName()
437 {
438 final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "Command Name" );
439 assert _p != null : "'Command Name' property not found.";
440 return _p;
441 }
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457 @SuppressWarnings("unused")
458 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
459 private String getApplicationTitle( final java.util.Locale locale )
460 {
461 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Application Title", locale );
462 assert _m != null : "'Application Title' message not found.";
463 return _m;
464 }
465
466
467
468
469
470
471
472
473
474
475
476
477
478 @SuppressWarnings("unused")
479 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
480 private String getCommandFailureMessage( final java.util.Locale locale, final java.lang.String toolName )
481 {
482 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Command Failure Message", locale, toolName );
483 assert _m != null : "'Command Failure Message' message not found.";
484 return _m;
485 }
486
487
488
489
490
491
492
493
494
495
496
497
498
499 @SuppressWarnings("unused")
500 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
501 private String getCommandInfoMessage( final java.util.Locale locale, final java.lang.String toolName )
502 {
503 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Command Info Message", locale, toolName );
504 assert _m != null : "'Command Info Message' message not found.";
505 return _m;
506 }
507
508
509
510
511
512
513
514
515
516
517
518
519
520 @SuppressWarnings("unused")
521 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
522 private String getCommandSuccessMessage( final java.util.Locale locale, final java.lang.String toolName )
523 {
524 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Command Success Message", locale, toolName );
525 assert _m != null : "'Command Success Message' message not found.";
526 return _m;
527 }
528
529
530
531
532
533
534
535
536
537
538
539
540
541 @SuppressWarnings("unused")
542 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
543 private String getDefaultLogLevelInfo( final java.util.Locale locale, final java.lang.String defaultLogLevel )
544 {
545 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Default Log Level Info", locale, defaultLogLevel );
546 assert _m != null : "'Default Log Level Info' message not found.";
547 return _m;
548 }
549
550
551
552
553
554
555
556
557
558
559
560 @SuppressWarnings("unused")
561 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
562 private String getLongDescriptionMessage( final java.util.Locale locale )
563 {
564 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Long Description Message", locale );
565 assert _m != null : "'Long Description Message' message not found.";
566 return _m;
567 }
568
569
570
571
572
573
574
575
576
577
578
579 @SuppressWarnings("unused")
580 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
581 private String getSeparator( final java.util.Locale locale )
582 {
583 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Separator", locale );
584 assert _m != null : "'Separator' message not found.";
585 return _m;
586 }
587
588
589
590
591
592
593
594
595
596
597
598 @SuppressWarnings("unused")
599 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
600 private String getShortDescriptionMessage( final java.util.Locale locale )
601 {
602 final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Short Description Message", locale );
603 assert _m != null : "'Short Description Message' message not found.";
604 return _m;
605 }
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
623 public org.apache.commons.cli.Options getOptions()
624 {
625 final org.apache.commons.cli.Options options = new org.apache.commons.cli.Options();
626 return options;
627 }
628
629
630 }