View Javadoc

1   package org.hyphenType.exit;
2   
3   import java.text.MessageFormat;
4   
5   import org.hyphenType.datastructure.Options;
6   import org.hyphenType.util.DefaultAnnotation;
7   import org.hyphenType.util.I18NResourceBundle;
8   
9   /**
10   * The object that is passed to the {@link StatusCode#beforeExit(ExitStatusHelper)}
11   * method.
12   * 
13   * @author akira
14   */
15  public class ExitStatusHelper {
16  
17      /**
18       * Message. Prefix i stands for instance.
19       */
20      private String iMessage;
21      
22      private Throwable throwable;
23      
24      /**
25       * Creates a helper containing the message extracted
26       * from the documentation formatter.
27       * 
28       * @param optionsInterface The options interface parsed using the command line arguments.
29       * @param enumConstant The enum constant used as the basis for creating the key.
30       * @param throwable The throwable that was responsible for this termination, if any.
31       * @param args Replacements.
32       */
33      public ExitStatusHelper(final Class<? extends Options<?>> optionsInterface, final Enum<? extends StatusCode> enumConstant, final Throwable throwable, final Object... args) {
34          try {
35              ExitStatusConstant message = DefaultAnnotation.getAnnotation(enumConstant, ExitStatusConstant.class);
36              DefaultAnnotation.fillWithResourceBundle(message, new I18NResourceBundle(optionsInterface));
37              if(message.userDescription()==null) {
38                  iMessage = "";
39              }
40              else {
41                  iMessage = MessageFormat.format(message.message(), args);
42              }
43          } catch (Exception e) {
44              // TODO Auto-generated catch block
45              e.printStackTrace();
46          }
47          this.throwable = throwable;
48      }
49      
50      /**
51       * Gives access to the message associated with this exit
52       * status code. At this point, the message was already formatted
53       * according with the arguments given in the constructor of
54       * this class: {@link ExitStatusHelper#ExitStatusHelper(String, Object...)}.
55       * In other words, status code enumeration constants cannot
56       * access the raw content of the resource bundles.
57       * 
58       * @return The message associated to this exit status code.
59       */
60      public final String getMessage() {
61          return iMessage;
62      }
63      
64      /**
65       * Gives access to the throwable responsible for this
66       * environment termination. Typically, an environment
67       * can be for instance a JVM.
68       * 
69       * @return The throwable, if any.
70       */
71      public final Throwable getThrowable() {
72          return throwable;
73      }
74  }