View Javadoc
1   package org.cyclopsgroup.jmxterm;
2   
3   import java.io.IOException;
4   import java.util.Map;
5   
6   import javax.management.remote.JMXServiceURL;
7   
8   import org.apache.commons.lang3.Validate;
9   import org.cyclopsgroup.jmxterm.io.CommandInput;
10  import org.cyclopsgroup.jmxterm.io.CommandOutput;
11  import org.cyclopsgroup.jmxterm.io.UnimplementedCommandInput;
12  import org.cyclopsgroup.jmxterm.io.VerboseCommandOutput;
13  import org.cyclopsgroup.jmxterm.io.VerboseCommandOutputConfig;
14  import org.cyclopsgroup.jmxterm.io.VerboseLevel;
15  
16  /**
17   * JMX communication context. This class exists for the whole lifecycle of a command execution. It
18   * is NOT thread safe. The caller(CommandCenter) makes sure all calls are synchronized.
19   * 
20   * @author <a href="mailto:jiaqi.guo@gmail.com">Jiaqi Guo</a>
21   */
22  public abstract class Session implements VerboseCommandOutputConfig {
23    private String bean;
24  
25    private boolean closed;
26  
27    private String domain;
28  
29    private final CommandInput input;
30  
31    /**
32     * Public output field. TODO Reevaluate if this field should be public or exposed by a getter
33     * method
34     */
35    public final CommandOutput output;
36  
37    private final JavaProcessManager processManager;
38  
39    private VerboseLevel verboseLevel = VerboseLevel.BRIEF;
40  
41    /**
42     * @param output Output destination
43     * @param input Command line input
44     * @param processManager Process manager
45     */
46    protected Session(CommandOutput output, CommandInput input, JavaProcessManager processManager) {
47      Validate.notNull(output, "Output can't be NULL");
48      Validate.notNull(processManager, "Process manager can't be NULL");
49      this.output = new VerboseCommandOutput(output, this);
50      this.input = input == null ? new UnimplementedCommandInput() : input;
51      this.processManager = processManager;
52    }
53  
54    /**
55     * Close JMX terminal console. Supposedly, process terminates after this call
56     */
57    public void close() {
58      if (closed) {
59        return;
60      }
61      closed = true;
62    }
63  
64    /**
65     * Connect to MBean server
66     * 
67     * @param url URL to connect
68     * @param env Environment variables
69     * @throws IOException allows IO exceptions.
70     */
71    public abstract void connect(JMXServiceURL url, Map<String, Object> env) throws IOException;
72  
73    /**
74     * Close JMX connector
75     * 
76     * @throws IOException Thrown when connection can't be closed
77     */
78    public abstract void disconnect() throws IOException;
79  
80    /**
81     * @return Current selected bean
82     */
83    public final String getBean() {
84      return bean;
85    }
86  
87    /**
88     * @return Current open JMX server connection
89     */
90    public abstract Connection getConnection();
91  
92    /**
93     * @return Current domain
94     */
95    public final String getDomain() {
96      return domain;
97    }
98  
99    /**
100    * @return General input of command lines, which could be interactive environment, a file or piped
101    *         input
102    */
103   public final CommandInput getInput() {
104     return input;
105   }
106 
107   /**
108    * @return Java process manager to load processes
109    */
110   public JavaProcessManager getProcessManager() {
111     return processManager;
112   }
113 
114   @Override
115   public final VerboseLevel getVerboseLevel() {
116     return verboseLevel;
117   }
118 
119   /**
120    * @return True if {@link #close()} has been called
121    */
122   public final boolean isClosed() {
123     return closed;
124   }
125 
126   /**
127    * @return True if there's a open connection to JMX server
128    */
129   public abstract boolean isConnected();
130 
131   /**
132    * Set current selected bean
133    * 
134    * @param bean Bean to select
135    */
136   public final void setBean(String bean) {
137     this.bean = bean;
138   }
139 
140   /**
141    * Set current selected domain
142    * 
143    * @param domain Domain to select
144    */
145   public final void setDomain(String domain) {
146     Validate.notNull(domain, "domain can't be NULL");
147     this.domain = domain;
148   }
149 
150   /**
151    * @param verboseLevel Level of verbose
152    */
153   public final void setVerboseLevel(VerboseLevel verboseLevel) {
154     Validate.notNull(verboseLevel, "Verbose level can't be NULL");
155     this.verboseLevel = verboseLevel;
156   }
157 
158   /**
159    * Set domain and bean to be NULL
160    */
161   public void unsetDomain() {
162     bean = null;
163     domain = null;
164   }
165 }