View Javadoc

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