View Javadoc

1   package org.eclipse.jetty.nested;
2   
3   import java.io.File;
4   import java.io.IOException;
5   
6   import javax.servlet.Servlet;
7   import javax.servlet.ServletConfig;
8   import javax.servlet.ServletContext;
9   import javax.servlet.ServletException;
10  import javax.servlet.ServletRequest;
11  import javax.servlet.ServletResponse;
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.http.HttpServletResponse;
14  
15  import org.eclipse.jetty.server.LocalConnector;
16  import org.eclipse.jetty.server.Server;
17  import org.eclipse.jetty.server.bio.SocketConnector;
18  import org.eclipse.jetty.util.log.Log;
19  import org.eclipse.jetty.util.thread.ThreadPool;
20  import org.eclipse.jetty.webapp.WebAppContext;
21  import org.eclipse.jetty.xml.XmlConfiguration;
22  
23  /**
24   * Nested Jetty Servlet.
25   * <p>
26   * This servlet runs Jetty as a nested server inside another servlet container.   The requests received by
27   * this servlet are routed via a {@link NestedConnector} to the nested jetty servlet and handled by jetty contexts,
28   * handlers, webapps and/or servlets.
29   * <p>
30   * The servlet can be configured with the following init parameters:<ul>
31   * <li>debug - if true then jetty debugging is turned on</li>
32   * <li>webapp - set to the resource path of the webapplication to deploy
33   * <li>jetty.xml - set the the resource path of a jetty xml file used to configure the server
34   * </ul>
35   *
36   */
37  public class NestedJettyServlet implements Servlet
38  {
39      private Server _server;
40      private ServletConfig _config;
41      private ServletContext _context;
42      private NestedConnector _connector;
43      
44      public void init(ServletConfig config) throws ServletException
45      {    
46          ClassLoader orig = Thread.currentThread().getContextClassLoader();
47          try
48          {
49              Thread.currentThread().setContextClassLoader(NestedJettyServlet.class.getClassLoader());
50              _config=config;
51              _context=config.getServletContext();
52              
53              Log.getLog().setDebugEnabled(Boolean.parseBoolean(_config.getInitParameter("debug")));
54              
55              String jetty_xml=config.getInitParameter("jetty.xml");
56              if (jetty_xml!=null)
57              {
58                  XmlConfiguration xml_config = new XmlConfiguration(_context.getResourceAsStream(jetty_xml));
59                  _server=(Server)xml_config.configure();
60              }
61              if (_server==null)
62                  _server=new Server();
63              
64              if (_server.getConnectors().length==0)
65              {
66                  _connector=new NestedConnector();
67                  _server.addConnector(_connector);
68              }
69              else
70                  _connector=(NestedConnector)_server.getConnectors()[0];
71              
72              WebAppContext webapp = new WebAppContext();
73              
74              webapp.setContextPath(_context.getContextPath());
75              webapp.setTempDirectory(new File((File)_context.getAttribute("javax.servlet.context.tempdir"),"jetty"));
76              String docroot=config.getInitParameter("webapp");
77             
78              String realpath=_context.getRealPath(docroot);
79              if (realpath!=null)
80                  webapp.setWar(realpath);
81              else
82                  webapp.setWar(_context.getResource(docroot).toString());
83  
84              _server.setHandler(webapp);
85  
86              _server.start();
87              _context.log("Started Jetty/"+_server.getVersion()+" for "+webapp.getWar()+" nested in "+_context.getServerInfo());
88          }
89          catch(Exception e)
90          {
91              throw new ServletException(e);
92          }
93          finally
94          {
95              Thread.currentThread().setContextClassLoader(orig);
96          }
97      }
98  
99      public ServletConfig getServletConfig()
100     {
101         return _config;
102     }
103 
104     public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
105     {
106         _connector.service(req,res);
107     }
108 
109     public String getServletInfo()
110     {
111         return this.toString();
112     }
113 
114     public void destroy()
115     {
116         try
117         {
118             _server.stop();
119         }
120         catch(Exception e)
121         {
122             _context.log("stopping",e);
123         }
124     }
125 }