1 8 9 package mx4j.tools.remote.local; 10 11 import java.io.IOException ; 12 import java.net.MalformedURLException ; 13 import java.util.HashMap ; 14 import java.util.Map ; 15 import javax.management.MBeanServer ; 16 import javax.management.remote.JMXServiceURL ; 17 18 import mx4j.remote.ConnectionResolver; 19 import mx4j.tools.remote.AbstractJMXConnectorServer; 20 21 24 public class LocalConnectorServer extends AbstractJMXConnectorServer 25 { 26 private static Map instances = new HashMap (); 27 28 public static LocalConnectionManager find(JMXServiceURL url) 29 { 30 synchronized (LocalConnectorServer.class) 31 { 32 return (LocalConnectionManager)instances.get(url); 33 } 34 } 35 36 private MBeanServer mbeanServer; 37 private LocalConnectionManager connectionManager; 38 39 public LocalConnectorServer(JMXServiceURL url, Map environment, MBeanServer server) 40 { 41 super(url, environment, server); 42 } 43 44 public MBeanServer getMBeanServer() 45 { 46 return mbeanServer; 47 } 48 49 protected void doStart() throws IOException 50 { 51 JMXServiceURL address = getAddress(); 52 String protocol = address.getProtocol(); 53 Map environment = getEnvironment(); 54 ConnectionResolver resolver = ConnectionResolver.newConnectionResolver(protocol, environment); 55 if (resolver == null) throw new MalformedURLException ("Unsupported protocol: " + protocol); 56 57 MBeanServer realServer = null; 58 MBeanServer server = super.getMBeanServer(); 59 60 MBeanServer resolvedServer = (MBeanServer )resolver.createServer(address, environment); 61 if (resolvedServer == null) 62 { 63 if (server == null) throw new IllegalStateException ("This LocalConnectorServer is not attached to an MBeanServer"); 64 realServer = server; 65 } 66 else 67 { 68 if (server == null) 69 { 70 realServer = resolvedServer; 71 } 72 else 73 { 74 if (server != resolvedServer) throw new IllegalStateException ("This LocalConnectorServer cannot be attached to 2 MBeanServers"); 75 realServer = server; 76 } 77 } 78 this.mbeanServer = realServer; 79 80 connectionManager = new LocalConnectionManager(this, environment); 81 82 setAddress(resolver.bindServer(realServer, address, environment)); 83 84 register(getAddress(), connectionManager); 86 } 87 88 private void register(JMXServiceURL url, LocalConnectionManager manager) throws IOException 89 { 90 synchronized (LocalConnectorServer.class) 91 { 92 if (instances.get(url) != null) throw new IOException ("A LocalConnectorServer is already serving at address " + url); 93 instances.put(url, manager); 94 } 95 } 96 97 protected void doStop() throws IOException 98 { 99 connectionManager.close(); 100 101 JMXServiceURL address = getAddress(); 102 String protocol = address.getProtocol(); 103 Map environment = getEnvironment(); 104 ConnectionResolver resolver = ConnectionResolver.newConnectionResolver(protocol, environment); 105 if (resolver == null) throw new MalformedURLException ("Unsupported protocol: " + protocol); 106 MBeanServer server = getMBeanServer(); 107 resolver.unbindServer(server, address, environment); 108 resolver.destroyServer(server, address, environment); 109 110 unregister(address); 111 } 112 113 private void unregister(JMXServiceURL url) throws IOException 114 { 115 synchronized (LocalConnectorServer.class) 116 { 117 Object removed = instances.remove(url); 118 if (removed == null) throw new IOException ("No LocalConnectorServer is present for address " + url); 119 } 120 } 121 } 122 | Popular Tags |