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