1 8 9 package mx4j.tools.remote.http; 10 11 import java.io.IOException ; 12 import java.net.MalformedURLException ; 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import javax.management.MBeanServer ; 17 import javax.management.remote.JMXServiceURL ; 18 19 import mx4j.log.Log; 20 import mx4j.log.Logger; 21 import mx4j.remote.ConnectionResolver; 22 import mx4j.tools.remote.AbstractJMXConnectorServer; 23 import mx4j.tools.remote.ConnectionManager; 24 25 28 public class HTTPConnectorServer extends AbstractJMXConnectorServer 29 { 30 36 public static final String WEB_CONTAINER_CONFIGURATION = "jmx.remote.x.http.server.configuration"; 37 public static final String USE_EXTERNAL_WEB_CONTAINER = "jmx.remote.x.http.use.external.web.container"; 38 public static final String EMBEDDED_WEB_CONTAINER_CLASS = "jmx.remote.x.http.embedded.web.container.class"; 39 40 private static Map instances = new HashMap (); 41 42 private WebContainer webContainer; 43 private ConnectionManager connectionManager; 44 45 public HTTPConnectorServer(JMXServiceURL url, Map environment, MBeanServer server) 46 { 47 super(url, environment, server); 48 } 49 50 protected void doStart() throws IOException , IllegalStateException 51 { 52 MBeanServer server = getMBeanServer(); 53 if (server == null) throw new IllegalStateException ("This JMXConnectorServer is not attached to an MBeanServer"); 54 55 JMXServiceURL address = getAddress(); 56 String protocol = address.getProtocol(); 57 Map environment = getEnvironment(); 58 ConnectionResolver resolver = ConnectionResolver.newConnectionResolver(protocol, environment); 59 if (resolver == null) throw new MalformedURLException ("Unsupported protocol: " + protocol); 60 61 webContainer = (WebContainer)resolver.createServer(address, environment); 62 63 setAddress(resolver.bindServer(webContainer, address, environment)); 64 65 connectionManager = createConnectionManager(this, address, environment); 66 67 register(getAddress(), connectionManager); 69 } 70 71 protected ConnectionManager createConnectionManager(AbstractJMXConnectorServer server, JMXServiceURL url, Map environment) 72 { 73 return new HTTPConnectionManager(server, url.getProtocol(), environment); 74 } 75 76 private void register(JMXServiceURL url, ConnectionManager manager) throws IOException 77 { 78 synchronized (HTTPConnectorServer.class) 79 { 80 if (instances.get(url) != null) throw new IOException ("A JMXConnectorServer is already serving at address " + url); 83 instances.put(url, manager); 84 } 85 } 86 87 private void unregister(JMXServiceURL url) throws IOException 88 { 89 synchronized (HTTPConnectorServer.class) 90 { 91 Object removed = instances.remove(url); 92 if (removed == null) throw new IOException ("No JMXConnectorServer is present for address " + url); 93 } 94 } 95 96 static ConnectionManager find(JMXServiceURL address) 97 { 98 synchronized (HTTPConnectorServer.class) 99 { 100 ConnectionManager manager = (ConnectionManager)instances.get(address); 101 if (manager != null) return manager; 102 103 Logger logger = Log.getLogger(HTTPConnectorServer.class.getName()); 104 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Known HTTPConnectorServers bound at " + instances.keySet()); 105 return null; 106 } 107 } 108 109 protected void doStop() throws IOException 110 { 111 JMXServiceURL url = getAddress(); 112 unregister(url); 113 114 if (connectionManager != null) 115 { 116 connectionManager.close(); 117 connectionManager = null; 118 } 119 120 String protocol = url.getProtocol(); 121 Map environment = getEnvironment(); 122 ConnectionResolver resolver = ConnectionResolver.newConnectionResolver(protocol, environment); 123 if (resolver == null) throw new MalformedURLException ("Unsupported protocol: " + protocol); 124 125 resolver.unbindServer(webContainer, url, environment); 126 127 resolver.destroyServer(webContainer, url, environment); 128 } 129 } 130 | Popular Tags |