KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > console > command > AbstractJmxCommand


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.console.command;
19
20 import org.apache.activemq.console.formatter.GlobalWriter;
21
22 import javax.management.remote.JMXServiceURL JavaDoc;
23 import javax.management.remote.JMXConnector JavaDoc;
24 import javax.management.remote.JMXConnectorFactory JavaDoc;
25 import java.util.List JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 public abstract class AbstractJmxCommand extends AbstractCommand {
30     public static final String JavaDoc DEFAULT_JMX_URL = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi";
31
32     private JMXServiceURL JavaDoc jmxServiceUrl;
33     private JMXConnector JavaDoc jmxConnector;
34
35     /**
36      * Get the current specified JMX service url.
37      * @return JMX service url
38      */

39     protected JMXServiceURL JavaDoc getJmxServiceUrl() {
40         return jmxServiceUrl;
41     }
42
43     /**
44      * Get the current JMX service url being used, or create a default one if no JMX service url has been specified.
45      * @return JMX service url
46      * @throws MalformedURLException
47      */

48     protected JMXServiceURL JavaDoc useJmxServiceUrl() throws MalformedURLException JavaDoc {
49         if (getJmxServiceUrl() == null) {
50             setJmxServiceUrl(DEFAULT_JMX_URL);
51         }
52
53         return getJmxServiceUrl();
54     }
55
56     /**
57      * Sets the JMX service url to use.
58      * @param jmxServiceUrl - new JMX service url to use
59      */

60     protected void setJmxServiceUrl(JMXServiceURL JavaDoc jmxServiceUrl) {
61         this.jmxServiceUrl = jmxServiceUrl;
62     }
63
64     /**
65      * Sets the JMX service url to use.
66      * @param jmxServiceUrl - new JMX service url to use
67      * @throws MalformedURLException
68      */

69     protected void setJmxServiceUrl(String JavaDoc jmxServiceUrl) throws MalformedURLException JavaDoc {
70         setJmxServiceUrl(new JMXServiceURL JavaDoc(jmxServiceUrl));
71     }
72
73     /**
74      * Create a JMX connector using the current specified JMX service url. If there is an existing connection,
75      * it tries to reuse this connection.
76      * @return created JMX connector
77      * @throws IOException
78      */

79     protected JMXConnector JavaDoc createJmxConnector() throws IOException JavaDoc {
80         // Reuse the previous connection
81
if (jmxConnector != null) {
82             jmxConnector.connect();
83             return jmxConnector;
84         }
85
86         // Create a new JMX connector
87
jmxConnector = JMXConnectorFactory.connect(useJmxServiceUrl());
88         return jmxConnector;
89     }
90
91     /**
92      * Close the current JMX connector
93      */

94     protected void closeJmxConnector() {
95         try {
96             if (jmxConnector != null) {
97                 jmxConnector.close();
98                 jmxConnector = null;
99             }
100         } catch (IOException JavaDoc e) {
101         }
102     }
103
104     /**
105      * Handle the --jmxurl option.
106      * @param token - option token to handle
107      * @param tokens - succeeding command arguments
108      * @throws Exception
109      */

110     protected void handleOption(String JavaDoc token, List JavaDoc tokens) throws Exception JavaDoc {
111         // Try to handle the options first
112
if (token.equals("--jmxurl")) {
113             // If no jmx url specified, or next token is a new option
114
if (tokens.isEmpty() || ((String JavaDoc)tokens.get(0)).startsWith("-")) {
115                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("JMX URL not specified."));
116             }
117
118             // If jmx url already specified
119
if (getJmxServiceUrl() != null) {
120                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("Multiple JMX URL cannot be specified."));
121                 tokens.clear();
122             }
123
124             String JavaDoc strJmxUrl = (String JavaDoc)tokens.remove(0);
125             try {
126                 this.setJmxServiceUrl(new JMXServiceURL JavaDoc(strJmxUrl));
127             } catch (MalformedURLException JavaDoc e) {
128                 GlobalWriter.printException(e);
129                 tokens.clear();
130             }
131         } else {
132             // Let the super class handle the option
133
super.handleOption(token, tokens);
134         }
135     }
136 }
137
Popular Tags