1 7 package org.jboss.mx.remoting.service; 8 9 import java.io.IOException ; 10 import java.net.InetAddress ; 11 import java.rmi.registry.LocateRegistry ; 12 import java.rmi.registry.Registry ; 13 import java.rmi.RemoteException ; 14 import javax.management.MBeanServer ; 15 import javax.management.ObjectName ; 16 import javax.management.remote.JMXConnectorServer ; 17 import javax.management.remote.JMXConnectorServerFactory ; 18 import javax.management.remote.JMXServiceURL ; 19 import org.jboss.logging.Logger; 20 21 25 public class JMXConnectorServerService implements JMXConnectorServerServiceMBean 26 { 27 31 public static final String JNDI_PATH_DEFAULT = "/jmxconnector"; 32 33 private MBeanServer mbeanServer; 34 35 private int registryPort = Registry.REGISTRY_PORT; 36 private String bindAddress = null; 37 private String jndiPath = JNDI_PATH_DEFAULT; 38 private JMXConnectorServer connectorServer = null; 39 private Registry rmiRegistry = null; 40 41 private static final Logger log = Logger.getLogger(JMXConnectorServerService.class); 42 43 public void setRegistryPort(int registryPort) 44 { 45 this.registryPort = registryPort; 46 } 47 48 public int getRegistryPort() 49 { 50 return registryPort; 51 } 52 53 public void setBindAddress(String bindAddress) 54 { 55 this.bindAddress = bindAddress; 56 } 57 58 public String getBindAddress() 59 { 60 return bindAddress; 61 } 62 63 public String getJndiPath() 64 { 65 return jndiPath; 66 } 67 68 public void setJndiPath(String jndiPath) 69 { 70 this.jndiPath = jndiPath; 71 } 72 73 public void create() throws Exception 74 { 75 } 77 78 public void start() throws Exception 79 { 80 rmiRegistry = LocateRegistry.getRegistry(registryPort); 82 if(rmiRegistry != null) 83 { 84 try 85 { 86 rmiRegistry.list(); 87 } 88 catch(RemoteException e) 89 { 90 log.debug("No registry running at port " + registryPort + ". Will create one."); 91 rmiRegistry = LocateRegistry.createRegistry(registryPort); 92 } 93 } 94 else 95 { 96 rmiRegistry = LocateRegistry.createRegistry(registryPort); 97 } 98 99 if(bindAddress == null || bindAddress.equals("0.0.0.0")) 100 { 101 bindAddress = InetAddress.getLocalHost().getHostName(); 102 } 103 104 String serviceURL = "service:jmx:rmi://" + bindAddress + "/jndi/rmi://" + bindAddress + ":" + registryPort + jndiPath; 105 106 JMXServiceURL url = new JMXServiceURL (serviceURL); 107 108 connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbeanServer); 110 connectorServer.start(); 111 112 log.debug("JMX Connector server: " + serviceURL); 113 } 114 115 public void stop() throws IOException 116 { 117 if(connectorServer != null) 118 { 119 connectorServer.stop(); 120 } 121 } 122 123 public void destroy() 124 { 125 } 127 128 public ObjectName preRegister(MBeanServer mBeanServer, ObjectName objectName) throws Exception 129 { 130 this.mbeanServer = mBeanServer; 131 return objectName; 132 } 133 134 public void postRegister(Boolean aBoolean) 135 { 136 } 138 139 public void preDeregister() throws Exception 140 { 141 } 143 144 public void postDeregister() 145 { 146 } 148 } | Popular Tags |