1 8 9 package mx4j.tools.remote.http; 10 11 import java.io.IOException ; 12 import java.util.HashMap ; 13 import java.util.HashSet ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 import javax.management.remote.JMXConnectorServerFactory ; 18 import javax.management.remote.JMXServiceURL ; 19 20 import mx4j.remote.ConnectionResolver; 21 22 25 public abstract class HTTPResolver extends ConnectionResolver 26 { 27 protected static final String DEFAULT_WEB_CONTAINER_CLASS = "mx4j.tools.remote.http.jetty.JettyWebContainer"; 28 29 private static Map webContainers = new HashMap (); 31 private static Map deployedURLs = new HashMap (); 32 private static final WebContainer EXTERNAL_WEB_CONTAINER = new ExternalWebContainer(); 33 34 public Object bindClient(Object client, Map environment) throws IOException 35 { 36 return client; 37 } 38 39 protected String getEndpoint(JMXServiceURL address, Map environment) 40 { 41 String transport = getEndpointProtocol(environment); 42 return transport + getEndpointPath(address); 43 } 44 45 protected String getEndpointProtocol(Map environment) 46 { 47 return "http"; 48 } 49 50 private String getEndpointPath(JMXServiceURL url) 51 { 52 String address = url.toString(); 53 String prefix = "service:jmx:" + url.getProtocol(); 54 return address.substring(prefix.length()); 55 } 56 57 public Object createServer(JMXServiceURL url, Map environment) throws IOException 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 String webContainerClassName = environment == null ? null : (String )environment.get(HTTPConnectorServer.EMBEDDED_WEB_CONTAINER_CLASS); 65 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 if (result == null) throw new IOException ("Could not start embedded web container"); 77 } 78 return result; 79 } 80 81 private WebContainer findWebContainer(JMXServiceURL url, String webContainerClassName) 82 { 83 String key = createWebContainerKey(url, webContainerClassName); 84 return (WebContainer)webContainers.get(key); 85 } 86 87 private String createWebContainerKey(JMXServiceURL url, String webContainerClassName) 88 { 89 return new StringBuffer (webContainerClassName).append("|").append(url.getHost()).append("|").append(url.getPort()).toString(); 90 } 91 92 public JMXServiceURL bindServer(Object server, JMXServiceURL url, Map environment) throws IOException 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 deploy(url, environment); 102 } 103 addDeployed(webContainer, url); 104 } 105 return url; 106 } 107 108 protected abstract String getServletClassName(); 109 110 protected void deploy(JMXServiceURL address, Map environment) throws IOException 111 { 112 } 113 114 public void unbindServer(Object server, JMXServiceURL address, Map environment) throws IOException 115 { 116 WebContainer webContainer = (WebContainer)server; 117 if (isDeployed(webContainer, address)) 118 { 119 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 address, Map environment) throws IOException 130 { 131 } 132 133 public void destroyServer(Object server, JMXServiceURL url, Map environment) throws IOException 134 { 135 WebContainer webContainer = (WebContainer)server; 136 if (webContainer != null && !hasDeployed(webContainer)) 137 { 138 String key = createWebContainerKey(url, server.getClass().getName()); 140 WebContainer container = (WebContainer)webContainers.remove(key); 141 if (webContainer != container) throw new IOException ("Trying to stop the wrong web container: " + server + " should be: " + container); 142 webContainer.stop(); 143 } 144 } 145 146 protected WebContainer createWebContainer(JMXServiceURL url, String webContainerClassName, Map environment) 147 { 148 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 149 if (environment != null) 150 { 151 Object cl = environment.get(JMXConnectorServerFactory.PROTOCOL_PROVIDER_CLASS_LOADER); 152 if (cl instanceof ClassLoader ) loader = (ClassLoader )cl; 153 } 154 155 try 156 { 157 WebContainer webContainer = (WebContainer)loader.loadClass(webContainerClassName).newInstance(); 158 String key = createWebContainerKey(url, webContainerClassName); 159 webContainers.put(key, webContainer); 160 return webContainer; 161 } 162 catch (Exception x) 163 { 164 } 165 return null; 166 } 167 168 private boolean isDeployed(WebContainer webContainer, JMXServiceURL url) 169 { 170 if (webContainer == null) webContainer = EXTERNAL_WEB_CONTAINER; 171 Set urls = (Set )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 urls = (Set )deployedURLs.get(webContainer); 180 if (urls == null) return false; 181 return !urls.isEmpty(); 182 } 183 184 private void addDeployed(WebContainer webContainer, JMXServiceURL url) 185 { 186 if (webContainer == null) webContainer = EXTERNAL_WEB_CONTAINER; 187 Set urls = (Set )deployedURLs.get(webContainer); 188 if (urls == null) 189 { 190 urls = new HashSet (); 191 deployedURLs.put(webContainer, urls); 192 } 193 urls.add(url); 194 } 195 196 private void removeDeployed(WebContainer webContainer, JMXServiceURL url) 197 { 198 if (webContainer == null) webContainer = EXTERNAL_WEB_CONTAINER; 199 Set urls = (Set )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 url, Map environment) throws IOException 210 { 211 } 212 213 public void stop() throws IOException 214 { 215 } 216 217 public void deploy(String servletClassName, JMXServiceURL url, Map environment) throws IOException 218 { 219 } 220 221 public void undeploy(String servletClassName, JMXServiceURL url, Map environment) 222 { 223 } 224 225 public String toString() 226 { 227 return "External WebContainer"; 228 } 229 } 230 } 231 | Popular Tags |