1 8 9 package mx4j.tools.remote.http.jetty; 10 11 import java.io.IOException ; 12 import java.util.Map ; 13 14 import javax.management.remote.JMXServiceURL ; 15 16 import mx4j.log.Log; 17 import mx4j.log.Logger; 18 import mx4j.tools.remote.http.HTTPConnectorServer; 19 import mx4j.tools.remote.http.WebContainer; 20 import org.mortbay.http.HttpListener; 21 import org.mortbay.jetty.Server; 22 import org.mortbay.jetty.servlet.ServletHandler; 23 import org.mortbay.jetty.servlet.ServletHttpContext; 24 25 30 public class JettyWebContainer implements WebContainer 31 { 32 private final Server server; 33 34 public JettyWebContainer() 35 { 36 server = new Server(); 37 } 38 39 protected Logger getLogger() 40 { 41 return Log.getLogger(getClass().getName()); 42 } 43 44 protected Server getServer() 45 { 46 return server; 47 } 48 49 public void start(JMXServiceURL url, Map environment) throws IOException 50 { 51 try 52 { 53 configure(url, environment); 54 getServer().start(); 55 } 56 catch (IOException x) 57 { 58 throw x; 59 } 60 catch (Exception x) 61 { 62 throw new IOException (x.toString()); 63 } 64 } 65 66 private void configure(JMXServiceURL url, Map environment) throws IOException 67 { 68 Logger logger = getLogger(); 69 70 if (environment != null) 71 { 72 Object config = environment.get(HTTPConnectorServer.WEB_CONTAINER_CONFIGURATION); 73 if (config instanceof String ) 74 { 75 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Configuring Jetty with configuration " + config); 76 getServer().configure((String )config); 77 78 HttpListener[] listeners = getServer().getListeners(); 80 if (listeners != null) 81 { 82 boolean found = false; 83 for (int i = 0; i < listeners.length; ++i) 84 { 85 HttpListener listener = listeners[i]; 86 if (listener.getPort() == url.getPort()) 87 { 88 found = true; 89 break; 90 } 91 } 92 if (!found) throw new IOException ("No listener configured with configuration " + config + " matches JMXServiceURL " + url); 93 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Configured Jetty successfully with configuration " + config); 95 return; 96 } 97 else 98 { 99 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Jetty configuration " + config + " does not have any listener"); 100 } 101 } 102 else 103 { 104 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Skipping Jetty configuration " + config + " (must be a String)"); 105 } 106 } 107 configureListener(url, environment); 108 } 109 110 protected void configureListener(JMXServiceURL url, Map environment) throws IOException 111 { 112 Logger logger = getLogger(); 113 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Configuring Jetty with a default listener on port " + url.getPort()); 114 String host = url.getHost(); 115 if (host == null) host = ""; 116 getServer().addListener(host + ":" + url.getPort()); 117 } 118 119 public void stop() throws IOException 120 { 121 try 122 { 123 getServer().stop(); 124 } 125 catch (InterruptedException x) 126 { 127 Thread.currentThread().interrupt(); 128 } 129 } 130 131 public void deploy(String servletClassName, JMXServiceURL url, Map environment) throws IOException 132 { 133 try 134 { 135 String urlPattern = resolveServletMapping(url); 136 ServletHttpContext context = (ServletHttpContext)getServer().getContext("/"); 137 context.addServlet(urlPattern, servletClassName); 138 if (!context.isStarted()) context.start(); 140 } 141 catch (Exception x) 142 { 143 throw new IOException (x.toString()); 144 } 145 } 146 147 public void undeploy(String servletName, JMXServiceURL url, Map environment) 148 { 149 String urlPattern = resolveServletMapping(url); 150 ServletHttpContext context = (ServletHttpContext)getServer().getContext("/"); 151 ServletHandler handler = context.getServletHandler(); 152 handler.getServletMap().remove(urlPattern); 153 } 154 155 private String resolveServletMapping(JMXServiceURL url) 156 { 157 String path = url.getURLPath(); 158 String urlPattern = null; 159 if (path.endsWith("/")) 160 urlPattern = path + "*"; 161 else 162 urlPattern = path + "/*"; 163 if (!urlPattern.startsWith("/")) urlPattern = "/" + urlPattern; 164 return urlPattern; 165 } 166 } 167 | Popular Tags |