View Javadoc
1   package org.cyclopsgroup.jmxterm.boot;
2   
3   import org.apache.commons.lang3.Validate;
4   import org.cyclopsgroup.jcli.annotation.Cli;
5   import org.cyclopsgroup.jcli.annotation.Option;
6   
7   import java.io.File;
8   
9   /**
10   * Options for main class
11   *
12   * @author <a href="mailto:jiaqi.guo@gmail.com">Jiaqi Guo</a>
13   */
14  @Cli(name = "jmxterm", description = "Main executable of JMX terminal CLI tool",
15      note = "Without any option, this command opens an interactive command line based console. With a given input file, commands in file will be executed and process ends after file is processed")
16  public class CliMainOptions {
17    /**
18     * Constant <code>stderr</code> that identifies standard error output
19     */
20    public static final String STDERR = "stderr";
21  
22    /**
23     * Constant <code>stdin</code> that identifies standard input
24     */
25    public static final String STDIN = "stdin";
26  
27    /**
28     * Constant <code>stdout</code> that identifies standard output
29     */
30    public static final String STDOUT = "stdout";
31  
32    private boolean exitOnFailure;
33  
34    private boolean help;
35  
36    private String input = STDIN;
37  
38    private boolean nonInteractive;
39  
40    private boolean appendToOutput = false;
41  
42    private String output = STDOUT;
43  
44    private String password;
45  
46    private String url;
47  
48    private String user;
49  
50    private String verboseLevel;
51  
52    /**
53     * @return #setInput(String)
54     */
55    public final String getInput() {
56      return input;
57    }
58  
59    /**
60     * @return #setOutput(String)
61     */
62    public final String getOutput() {
63      return output;
64    }
65  
66    /**
67     * @return Password for user/password authentication
68     */
69    public final String getPassword() {
70      return password;
71    }
72  
73    /**
74     * @return #setUrl(String)
75     */
76    public final String getUrl() {
77      return url;
78    }
79  
80    /**
81     * @return User name for user/password authentication
82     */
83    public final String getUser() {
84      return user;
85    }
86  
87    /**
88     * @return Verbose option
89     */
90    public final String getVerboseLevel() {
91      return verboseLevel;
92    }
93  
94    /**
95     * @return True if terminal exits on any failure
96     */
97    public final boolean isExitOnFailure() {
98      return exitOnFailure;
99    }
100 
101   /**
102    * @return True if terminal exits on any failure
103    */
104   public final boolean isAppendToOutput() {
105     return appendToOutput;
106   }
107 
108   /**
109    * @return {@link #setHelp(boolean)}
110    */
111   public final boolean isHelp() {
112     return help;
113   }
114 
115   /**
116    * @return True if CLI runs without user interaction, such as piped input
117    */
118   public final boolean isNonInteractive() {
119     return nonInteractive;
120   }
121 
122   /**
123    * @param exitOnFailure True if terminal exits on any failure
124    */
125   @Option(name = "e", longName = "exitonfailure",
126       description = "With this flag, terminal exits for any Exception")
127   public final void setExitOnFailure(boolean exitOnFailure) {
128     this.exitOnFailure = exitOnFailure;
129   }
130 
131   /**
132    * @param help True to turn on <code>help</code> flag
133    */
134   @Option(name = "h", longName = "help", description = "Show usage of this command line")
135   public final void setHelp(boolean help) {
136     this.help = help;
137   }
138 
139   /**
140    * @param file Input script path or <code>stdin</code> as default value for console input
141    */
142   @Option(name = "i", longName = "input",
143       description = "Input script file. There can only be one input file. \"stdin\" is the default value which means console input")
144   public final void setInput(String file) {
145     Validate.notNull(file, "Input file can't be NULL");
146     Validate.isTrue(new File(file).isFile(), "File " + file + " doesn't exist");
147     this.input = file;
148   }
149 
150   /**
151    * @param nonInteractive True if CLI runs without user interaction, such as piped input
152    */
153   @Option(name = "n", longName = "noninteract",
154       description = "Non interactive mode. Use this mode if input doesn't come from human or jmxterm is embedded")
155   public final void setNonInteractive(boolean nonInteractive) {
156     this.nonInteractive = nonInteractive;
157   }
158 
159   /**
160    * @param outputFile It can be a file or {@link #STDERR} or {@link #STDERR}
161    */
162   @Option(name = "o", longName = "output",
163       description = "Output file, stdout or stderr. Default value is stdout")
164   public final void setOutput(String outputFile) {
165     Validate.notNull(outputFile, "Output file can't be NULL");
166     this.output = outputFile;
167   }
168 
169   /**
170    * @param password Password for user/password authentication
171    */
172   @Option(name = "p", longName = "password",
173       description = "Password for user/password authentication")
174   public final void setPassword(String password) {
175     Validate.notNull(password, "Password can't be NULL");
176     this.password = password;
177   }
178 
179   /**
180    * @param url MBean server URL
181    */
182   @Option(name = "l", longName = "url",
183       description = "Location of MBean service. It can be <host>:<port> or full service URL.")
184   public final void setUrl(String url) {
185     Validate.notNull(url, "URL can't be NULL");
186     this.url = url;
187   }
188 
189   /**
190    * @param user User name for user/password authentication
191    */
192   @Option(name = "u", longName = "user", description = "User name for user/password authentication")
193   public final void setUser(String user) {
194     Validate.notNull(user, "User can't be NULL");
195     this.user = user;
196   }
197 
198   /**
199    * @param verboseLevel Verbose level
200    */
201   @Option(name = "v", longName = "verbose",
202       description = "Verbose level, could be silent|brief|verbose. Default value is brief")
203   public final void setVerboseLevel(String verboseLevel) {
204     this.verboseLevel = verboseLevel;
205   }
206 
207   /**
208    * @param appendToOutput True if outputfile is preserved
209    */
210   @Option(name = "a", longName = "appendtooutput",
211       description = "With this flag, the outputfile is preserved and content is appended to it")
212   public final void setAppendToOutput(boolean appendToOutput) {
213     this.appendToOutput = appendToOutput;
214   }
215 }