1 18 package org.apache.activemq.console.command; 19 20 import org.apache.activemq.console.formatter.GlobalWriter; 21 22 import javax.management.remote.JMXServiceURL ; 23 import javax.management.remote.JMXConnector ; 24 import javax.management.remote.JMXConnectorFactory ; 25 import java.util.List ; 26 import java.net.MalformedURLException ; 27 import java.io.IOException ; 28 29 public abstract class AbstractJmxCommand extends AbstractCommand { 30 public static final String DEFAULT_JMX_URL = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi"; 31 32 private JMXServiceURL jmxServiceUrl; 33 private JMXConnector jmxConnector; 34 35 39 protected JMXServiceURL getJmxServiceUrl() { 40 return jmxServiceUrl; 41 } 42 43 48 protected JMXServiceURL useJmxServiceUrl() throws MalformedURLException { 49 if (getJmxServiceUrl() == null) { 50 setJmxServiceUrl(DEFAULT_JMX_URL); 51 } 52 53 return getJmxServiceUrl(); 54 } 55 56 60 protected void setJmxServiceUrl(JMXServiceURL jmxServiceUrl) { 61 this.jmxServiceUrl = jmxServiceUrl; 62 } 63 64 69 protected void setJmxServiceUrl(String jmxServiceUrl) throws MalformedURLException { 70 setJmxServiceUrl(new JMXServiceURL (jmxServiceUrl)); 71 } 72 73 79 protected JMXConnector createJmxConnector() throws IOException { 80 if (jmxConnector != null) { 82 jmxConnector.connect(); 83 return jmxConnector; 84 } 85 86 jmxConnector = JMXConnectorFactory.connect(useJmxServiceUrl()); 88 return jmxConnector; 89 } 90 91 94 protected void closeJmxConnector() { 95 try { 96 if (jmxConnector != null) { 97 jmxConnector.close(); 98 jmxConnector = null; 99 } 100 } catch (IOException e) { 101 } 102 } 103 104 110 protected void handleOption(String token, List tokens) throws Exception { 111 if (token.equals("--jmxurl")) { 113 if (tokens.isEmpty() || ((String )tokens.get(0)).startsWith("-")) { 115 GlobalWriter.printException(new IllegalArgumentException ("JMX URL not specified.")); 116 } 117 118 if (getJmxServiceUrl() != null) { 120 GlobalWriter.printException(new IllegalArgumentException ("Multiple JMX URL cannot be specified.")); 121 tokens.clear(); 122 } 123 124 String strJmxUrl = (String )tokens.remove(0); 125 try { 126 this.setJmxServiceUrl(new JMXServiceURL (strJmxUrl)); 127 } catch (MalformedURLException e) { 128 GlobalWriter.printException(e); 129 tokens.clear(); 130 } 131 } else { 132 super.handleOption(token, tokens); 134 } 135 } 136 } 137 | Popular Tags |