KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
18 import javax.management.remote.JMXServiceURL JavaDoc;
19
20 import mx4j.remote.ConnectionResolver;
21
22 /**
23  * @version $Revision: 1.4 $
24  */

25 public abstract class HTTPResolver extends ConnectionResolver
26 {
27    protected static final String JavaDoc DEFAULT_WEB_CONTAINER_CLASS = "mx4j.tools.remote.http.jetty.JettyWebContainer";
28
29    // TODO: maybe worth to use weak references to hold web containers
30
private static Map JavaDoc webContainers = new HashMap JavaDoc();
31    private static Map JavaDoc deployedURLs = new HashMap JavaDoc();
32    private static final WebContainer EXTERNAL_WEB_CONTAINER = new ExternalWebContainer();
33
34    public Object JavaDoc bindClient(Object JavaDoc client, Map JavaDoc environment) throws IOException JavaDoc
35    {
36       return client;
37    }
38
39    protected String JavaDoc getEndpoint(JMXServiceURL JavaDoc address, Map JavaDoc environment)
40    {
41       String JavaDoc transport = getEndpointProtocol(environment);
42       return transport + getEndpointPath(address);
43    }
44
45    protected String JavaDoc getEndpointProtocol(Map JavaDoc environment)
46    {
47       return "http";
48    }
49
50    private String JavaDoc getEndpointPath(JMXServiceURL JavaDoc url)
51    {
52       String JavaDoc address = url.toString();
53       String JavaDoc prefix = "service:jmx:" + url.getProtocol();
54       return address.substring(prefix.length());
55    }
56
57    public Object JavaDoc createServer(JMXServiceURL JavaDoc url, Map JavaDoc environment) throws IOException JavaDoc
58    {
59       WebContainer result = null;
60       boolean useExternalWebContainer = environment == null ? false : Boolean.valueOf(String.valueOf(environment.get(HTTPConnectorServer.USE_EXTERNAL_WEB_CONTAINER))).booleanValue();
61       if (!useExternalWebContainer)
62       {
63          // Create and start an embedded web container
64
String JavaDoc webContainerClassName = environment == null ? null : (String JavaDoc)environment.get(HTTPConnectorServer.EMBEDDED_WEB_CONTAINER_CLASS);
65          // Not present, by default use Jetty
66
if (webContainerClassName == null || webContainerClassName.length() == 0) webContainerClassName = DEFAULT_WEB_CONTAINER_CLASS;
67
68          result = findWebContainer(url, webContainerClassName);
69          if (result == null)
70          {
71             result = createWebContainer(url, webContainerClassName, environment);
72             if (result != null) result.start(url, environment);
73          }
74
75          // Nothing present, give up
76
if (result == null) throw new IOException JavaDoc("Could not start embedded web container");
77       }
78       return result;
79    }
80
81    private WebContainer findWebContainer(JMXServiceURL JavaDoc url, String JavaDoc webContainerClassName)
82    {
83       String JavaDoc key = createWebContainerKey(url, webContainerClassName);
84       return (WebContainer)webContainers.get(key);
85    }
86
87    private String JavaDoc createWebContainerKey(JMXServiceURL JavaDoc url, String JavaDoc webContainerClassName)
88    {
89       return new StringBuffer JavaDoc(webContainerClassName).append("|").append(url.getHost()).append("|").append(url.getPort()).toString();
90    }
91
92    public JMXServiceURL JavaDoc bindServer(Object JavaDoc server, JMXServiceURL JavaDoc url, Map JavaDoc environment) throws IOException JavaDoc
93    {
94       WebContainer webContainer = (WebContainer)server;
95       if (!isDeployed(webContainer, url))
96       {
97          if (webContainer != null) webContainer.deploy(getServletClassName(), url, environment);
98          if (!hasDeployed(webContainer))
99          {
100             // The jmxconnector web service has never been deployed, deploy it now
101
deploy(url, environment);
102          }
103          addDeployed(webContainer, url);
104       }
105       return url;
106    }
107
108    protected abstract String JavaDoc getServletClassName();
109
110    protected void deploy(JMXServiceURL JavaDoc address, Map JavaDoc environment) throws IOException JavaDoc
111    {
112    }
113
114    public void unbindServer(Object JavaDoc server, JMXServiceURL JavaDoc address, Map JavaDoc environment) throws IOException JavaDoc
115    {
116       WebContainer webContainer = (WebContainer)server;
117       if (isDeployed(webContainer, address))
118       {
119          // First undeploy the jmxconnector web service, then undeploy the webContainer: otherwise the service cannot be undeployed
120
removeDeployed(webContainer, address);
121          if (!hasDeployed(webContainer))
122          {
123             undeploy(address, environment);
124          }
125          if (webContainer != null) webContainer.undeploy(getServletClassName(), address, environment);
126       }
127    }
128
129    protected void undeploy(JMXServiceURL JavaDoc address, Map JavaDoc environment) throws IOException JavaDoc
130    {
131    }
132
133    public void destroyServer(Object JavaDoc server, JMXServiceURL JavaDoc url, Map JavaDoc environment) throws IOException JavaDoc
134    {
135       WebContainer webContainer = (WebContainer)server;
136       if (webContainer != null && !hasDeployed(webContainer))
137       {
138          // No more deployed stuff here, shutdown also the web container
139
String JavaDoc key = createWebContainerKey(url, server.getClass().getName());
140          WebContainer container = (WebContainer)webContainers.remove(key);
141          if (webContainer != container) throw new IOException JavaDoc("Trying to stop the wrong web container: " + server + " should be: " + container);
142          webContainer.stop();
143       }
144    }
145
146    protected WebContainer createWebContainer(JMXServiceURL JavaDoc url, String JavaDoc webContainerClassName, Map JavaDoc environment)
147    {
148       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
149       if (environment != null)
150       {
151          Object JavaDoc cl = environment.get(JMXConnectorServerFactory.PROTOCOL_PROVIDER_CLASS_LOADER);
152          if (cl instanceof ClassLoader JavaDoc) loader = (ClassLoader JavaDoc)cl;
153       }
154
155       try
156       {
157          WebContainer webContainer = (WebContainer)loader.loadClass(webContainerClassName).newInstance();
158          String JavaDoc key = createWebContainerKey(url, webContainerClassName);
159          webContainers.put(key, webContainer);
160          return webContainer;
161       }
162       catch (Exception JavaDoc x)
163       {
164       }
165       return null;
166    }
167
168    private boolean isDeployed(WebContainer webContainer, JMXServiceURL JavaDoc url)
169    {
170       if (webContainer == null) webContainer = EXTERNAL_WEB_CONTAINER;
171       Set JavaDoc urls = (Set JavaDoc)deployedURLs.get(webContainer);
172       if (urls == null) return false;
173       return urls.contains(url);
174    }
175
176    private boolean hasDeployed(WebContainer webContainer)
177    {
178       if (webContainer == null) webContainer = EXTERNAL_WEB_CONTAINER;
179       Set JavaDoc urls = (Set JavaDoc)deployedURLs.get(webContainer);
180       if (urls == null) return false;
181       return !urls.isEmpty();
182    }
183
184    private void addDeployed(WebContainer webContainer, JMXServiceURL JavaDoc url)
185    {
186       if (webContainer == null) webContainer = EXTERNAL_WEB_CONTAINER;
187       Set JavaDoc urls = (Set JavaDoc)deployedURLs.get(webContainer);
188       if (urls == null)
189       {
190          urls = new HashSet JavaDoc();
191          deployedURLs.put(webContainer, urls);
192       }
193       urls.add(url);
194    }
195
196    private void removeDeployed(WebContainer webContainer, JMXServiceURL JavaDoc url)
197    {
198       if (webContainer == null) webContainer = EXTERNAL_WEB_CONTAINER;
199       Set JavaDoc urls = (Set JavaDoc)deployedURLs.get(webContainer);
200       if (urls != null)
201       {
202          urls.remove(url);
203          if (urls.isEmpty()) deployedURLs.remove(webContainer);
204       }
205    }
206
207    private static class ExternalWebContainer implements WebContainer
208    {
209       public void start(JMXServiceURL JavaDoc url, Map JavaDoc environment) throws IOException JavaDoc
210       {
211       }
212
213       public void stop() throws IOException JavaDoc
214       {
215       }
216
217       public void deploy(String JavaDoc servletClassName, JMXServiceURL JavaDoc url, Map JavaDoc environment) throws IOException JavaDoc
218       {
219       }
220
221       public void undeploy(String JavaDoc servletClassName, JMXServiceURL JavaDoc url, Map JavaDoc environment)
222       {
223       }
224
225       public String JavaDoc toString()
226       {
227          return "External WebContainer";
228       }
229    }
230 }
231
Popular Tags