001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.activemq.console.command;
018
019 import java.io.IOException;
020 import java.net.MalformedURLException;
021 import java.util.List;
022
023 import javax.management.remote.JMXConnector;
024 import javax.management.remote.JMXConnectorFactory;
025 import javax.management.remote.JMXServiceURL;
026
027 public abstract class AbstractJmxCommand extends AbstractCommand {
028 public static final String DEFAULT_JMX_URL = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi";
029
030 private JMXServiceURL jmxServiceUrl;
031 private JMXConnector jmxConnector;
032
033 /**
034 * Get the current specified JMX service url.
035 * @return JMX service url
036 */
037 protected JMXServiceURL getJmxServiceUrl() {
038 return jmxServiceUrl;
039 }
040
041 /**
042 * Get the current JMX service url being used, or create a default one if no JMX service url has been specified.
043 * @return JMX service url
044 * @throws MalformedURLException
045 */
046 protected JMXServiceURL useJmxServiceUrl() throws MalformedURLException {
047 if (getJmxServiceUrl() == null) {
048 setJmxServiceUrl(DEFAULT_JMX_URL);
049 }
050
051 return getJmxServiceUrl();
052 }
053
054 /**
055 * Sets the JMX service url to use.
056 * @param jmxServiceUrl - new JMX service url to use
057 */
058 protected void setJmxServiceUrl(JMXServiceURL jmxServiceUrl) {
059 this.jmxServiceUrl = jmxServiceUrl;
060 }
061
062 /**
063 * Sets the JMX service url to use.
064 * @param jmxServiceUrl - new JMX service url to use
065 * @throws MalformedURLException
066 */
067 protected void setJmxServiceUrl(String jmxServiceUrl) throws MalformedURLException {
068 setJmxServiceUrl(new JMXServiceURL(jmxServiceUrl));
069 }
070
071 /**
072 * Create a JMX connector using the current specified JMX service url. If there is an existing connection,
073 * it tries to reuse this connection.
074 * @return created JMX connector
075 * @throws IOException
076 */
077 protected JMXConnector createJmxConnector() throws IOException {
078 // Reuse the previous connection
079 if (jmxConnector != null) {
080 jmxConnector.connect();
081 return jmxConnector;
082 }
083
084 // Create a new JMX connector
085 jmxConnector = JMXConnectorFactory.connect(useJmxServiceUrl());
086 return jmxConnector;
087 }
088
089 /**
090 * Close the current JMX connector
091 */
092 protected void closeJmxConnector() {
093 try {
094 if (jmxConnector != null) {
095 jmxConnector.close();
096 jmxConnector = null;
097 }
098 } catch (IOException e) {
099 }
100 }
101
102 /**
103 * Handle the --jmxurl option.
104 * @param token - option token to handle
105 * @param tokens - succeeding command arguments
106 * @throws Exception
107 */
108 protected void handleOption(String token, List<String> tokens) throws Exception {
109 // Try to handle the options first
110 if (token.equals("--jmxurl")) {
111 // If no jmx url specified, or next token is a new option
112 if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
113 context.printException(new IllegalArgumentException("JMX URL not specified."));
114 }
115
116 // If jmx url already specified
117 if (getJmxServiceUrl() != null) {
118 context.printException(new IllegalArgumentException("Multiple JMX URL cannot be specified."));
119 tokens.clear();
120 }
121
122 String strJmxUrl = (String)tokens.remove(0);
123 try {
124 this.setJmxServiceUrl(new JMXServiceURL(strJmxUrl));
125 } catch (MalformedURLException e) {
126 context.printException(e);
127 tokens.clear();
128 }
129 } else {
130 // Let the super class handle the option
131 super.handleOption(token, tokens);
132 }
133 }
134 }