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