View Javadoc
1   package org.cyclopsgroup.jmxterm;
2   
3   import org.apache.commons.lang3.Validate;
4   import org.cyclopsgroup.jcli.AutoCompletable;
5   import org.cyclopsgroup.jcli.annotation.Option;
6   import org.slf4j.Logger;
7   import org.slf4j.LoggerFactory;
8   
9   import javax.management.JMException;
10  import java.io.IOException;
11  import java.util.List;
12  
13  /**
14   * Base class of all commands. Command is executed in single thread. Extending classes don't need to
15   * worry about concurrency. Command is transient, every command in console creates a new instance of
16   * Command object which is disposed after execution finishes.
17   *
18   * @author <a href="mailto:jiaqi.guo@gmail.com">Jiaqi Guo</a>
19   */
20  public abstract class Command implements AutoCompletable {
21    private static final Logger LOG = LoggerFactory.getLogger(Command.class);
22  
23    private boolean help;
24  
25    private Session session;
26  
27    /**
28     * Provide a list of possible arguments for auto completion. This method returns list of
29     * arguments(not option) and is called when user presses tab key.
30     *
31     * @return List of possible arguments used by auto completion or NULL
32     * @throws IOException IO errors
33     * @throws JMException JMX problemo
34     */
35    protected List<String> doSuggestArgument() throws IOException, JMException {
36      return null;
37    }
38  
39    /**
40     * Provide a list of possible option values for auto completion
41     *
42     * @param optionName Name of option
43     * @return List of possible arguments used by auto completion or NULL
44     * @throws IOException Network communication errors
45     * @throws JMException JMX errors
46     */
47    protected List<String> doSuggestOption(String optionName) throws IOException, JMException {
48      return null;
49    }
50  
51    /**
52     * Execute command
53     *
54     * @throws IOException IO errors
55     * @throws JMException JMX errors
56     */
57    public abstract void execute() throws IOException, JMException;
58  
59    /**
60     * @return Session where command runs
61     */
62    public final Session getSession() {
63      return session;
64    }
65  
66    /**
67     * @return True if help option is on
68     */
69    public final boolean isHelp() {
70      return help;
71    }
72  
73    /**
74     * @param help True to display usage
75     */
76    @Option(name = "h", longName = "help", description = "Display usage")
77    public final void setHelp(boolean help) {
78      this.help = help;
79    }
80  
81    /**
82     * @param session Session where command runs
83     */
84    public final void setSession(Session session) {
85      Validate.notNull(session, "Session can't be NULL");
86      this.session = session;
87    }
88  
89    public final List<String> suggestArgument(String partialArg) {
90      if (partialArg != null) {
91        return null;
92      }
93      try {
94        return doSuggestArgument();
95      } catch (IOException e) {
96        if (LOG.isDebugEnabled()) {
97          LOG.debug("Couldn't suggest option", e);
98        }
99        return null;
100     } catch (JMException e) {
101       if (LOG.isDebugEnabled()) {
102         LOG.debug("Couldn't suggest option", e);
103       }
104       return null;
105     }
106   }
107 
108   public final List<String> suggestOption(String name, String partialValue) {
109     if (partialValue != null) {
110       return null;
111     }
112     try {
113       return doSuggestOption(name);
114     } catch (IOException e) {
115       if (LOG.isDebugEnabled()) {
116         LOG.debug("Couldn't suggest option", e);
117       }
118       return null;
119     } catch (JMException e) {
120       if (LOG.isDebugEnabled()) {
121         LOG.debug("Couldn't suggest option", e);
122       }
123       return null;
124     }
125   }
126 }