KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > remote > http > HTTPConnectorServer


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.tools.remote.http;
10
11 import java.io.IOException JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import javax.management.MBeanServer JavaDoc;
17 import javax.management.remote.JMXServiceURL JavaDoc;
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 /**
26  * @version $Revision: 1.5 $
27  */

28 public class HTTPConnectorServer extends AbstractJMXConnectorServer
29 {
30    /**
31     * MX4J's implementation uses this property to specify a String that points to the configuration
32     * resource used to configure the HTTP server for JSR 160 connectors that use HTTP as transport.
33     * For Jetty, the default HTTP server, this can be a URL or a relative path (in this latter case
34     * the resource must be in classpath).
35     */

36    public static final String JavaDoc WEB_CONTAINER_CONFIGURATION = "jmx.remote.x.http.server.configuration";
37    public static final String JavaDoc USE_EXTERNAL_WEB_CONTAINER = "jmx.remote.x.http.use.external.web.container";
38    public static final String JavaDoc EMBEDDED_WEB_CONTAINER_CLASS = "jmx.remote.x.http.embedded.web.container.class";
39
40    private static Map JavaDoc instances = new HashMap JavaDoc();
41
42    private WebContainer webContainer;
43    private ConnectionManager connectionManager;
44
45    public HTTPConnectorServer(JMXServiceURL JavaDoc url, Map JavaDoc environment, MBeanServer JavaDoc server)
46    {
47       super(url, environment, server);
48    }
49
50    protected void doStart() throws IOException JavaDoc, IllegalStateException JavaDoc
51    {
52       MBeanServer JavaDoc server = getMBeanServer();
53       if (server == null) throw new IllegalStateException JavaDoc("This JMXConnectorServer is not attached to an MBeanServer");
54
55       JMXServiceURL JavaDoc address = getAddress();
56       String JavaDoc protocol = address.getProtocol();
57       Map JavaDoc environment = getEnvironment();
58       ConnectionResolver resolver = ConnectionResolver.newConnectionResolver(protocol, environment);
59       if (resolver == null) throw new MalformedURLException JavaDoc("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       // Here is where we give to clients the possibility to access us
68
register(getAddress(), connectionManager);
69    }
70
71    protected ConnectionManager createConnectionManager(AbstractJMXConnectorServer server, JMXServiceURL JavaDoc url, Map JavaDoc environment)
72    {
73       return new HTTPConnectionManager(server, url.getProtocol(), environment);
74    }
75
76    private void register(JMXServiceURL JavaDoc url, ConnectionManager manager) throws IOException JavaDoc
77    {
78       synchronized (HTTPConnectorServer.class)
79       {
80          // TODO: must use weak references to connection managers, otherwise they're not GC'ed
81
// TODO: in case the connector server is not stopped cleanly
82
if (instances.get(url) != null) throw new IOException JavaDoc("A JMXConnectorServer is already serving at address " + url);
83          instances.put(url, manager);
84       }
85    }
86
87    private void unregister(JMXServiceURL JavaDoc url) throws IOException JavaDoc
88    {
89       synchronized (HTTPConnectorServer.class)
90       {
91          Object JavaDoc removed = instances.remove(url);
92          if (removed == null) throw new IOException JavaDoc("No JMXConnectorServer is present for address " + url);
93       }
94    }
95
96    static ConnectionManager find(JMXServiceURL JavaDoc 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 JavaDoc
110    {
111       JMXServiceURL JavaDoc url = getAddress();
112       unregister(url);
113
114       if (connectionManager != null)
115       {
116          connectionManager.close();
117          connectionManager = null;
118       }
119
120       String JavaDoc protocol = url.getProtocol();
121       Map JavaDoc environment = getEnvironment();
122       ConnectionResolver resolver = ConnectionResolver.newConnectionResolver(protocol, environment);
123       if (resolver == null) throw new MalformedURLException JavaDoc("Unsupported protocol: " + protocol);
124
125       resolver.unbindServer(webContainer, url, environment);
126
127       resolver.destroyServer(webContainer, url, environment);
128    }
129 }
130
Popular Tags