KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > remote > http > jetty > JettyWebContainer


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.jetty;
10
11 import java.io.IOException JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import javax.management.remote.JMXServiceURL JavaDoc;
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 /**
26  * Jetty's implementation of WebContainer interface
27  *
28  * @version $Revision: 1.6 $
29  */

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 JavaDoc url, Map JavaDoc environment) throws IOException JavaDoc
50    {
51       try
52       {
53          configure(url, environment);
54          getServer().start();
55       }
56       catch (IOException JavaDoc x)
57       {
58          throw x;
59       }
60       catch (Exception JavaDoc x)
61       {
62          throw new IOException JavaDoc(x.toString());
63       }
64    }
65
66    private void configure(JMXServiceURL JavaDoc url, Map JavaDoc environment) throws IOException JavaDoc
67    {
68       Logger logger = getLogger();
69
70       if (environment != null)
71       {
72          Object JavaDoc config = environment.get(HTTPConnectorServer.WEB_CONTAINER_CONFIGURATION);
73          if (config instanceof String JavaDoc)
74          {
75             if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Configuring Jetty with configuration " + config);
76             getServer().configure((String JavaDoc)config);
77
78             // Be sure there is at least one listener on the port specified by the JMXServiceURL
79
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 JavaDoc("No listener configured with configuration " + config + " matches JMXServiceURL " + url);
93                // Configured successfully, return
94
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 JavaDoc url, Map JavaDoc environment) throws IOException JavaDoc
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 JavaDoc host = url.getHost();
115       if (host == null) host = "";
116       getServer().addListener(host + ":" + url.getPort());
117    }
118
119    public void stop() throws IOException JavaDoc
120    {
121       try
122       {
123          getServer().stop();
124       }
125       catch (InterruptedException JavaDoc x)
126       {
127          Thread.currentThread().interrupt();
128       }
129    }
130
131    public void deploy(String JavaDoc servletClassName, JMXServiceURL JavaDoc url, Map JavaDoc environment) throws IOException JavaDoc
132    {
133       try
134       {
135          String JavaDoc urlPattern = resolveServletMapping(url);
136          ServletHttpContext context = (ServletHttpContext)getServer().getContext("/");
137          context.addServlet(urlPattern, servletClassName);
138          // TODO: be sure an undeployed url is not restarted !
139
if (!context.isStarted()) context.start();
140       }
141       catch (Exception JavaDoc x)
142       {
143          throw new IOException JavaDoc(x.toString());
144       }
145    }
146
147    public void undeploy(String JavaDoc servletName, JMXServiceURL JavaDoc url, Map JavaDoc environment)
148    {
149       String JavaDoc urlPattern = resolveServletMapping(url);
150       ServletHttpContext context = (ServletHttpContext)getServer().getContext("/");
151       ServletHandler handler = context.getServletHandler();
152       handler.getServletMap().remove(urlPattern);
153    }
154
155    private String JavaDoc resolveServletMapping(JMXServiceURL JavaDoc url)
156    {
157       String JavaDoc path = url.getURLPath();
158       String JavaDoc 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