diff options
author | Greg Wilkins | 2011-04-04 05:19:17 +0000 |
---|---|---|
committer | Greg Wilkins | 2011-04-04 05:19:17 +0000 |
commit | 2ca6b4521b07323ae46650d4e688a4a199f42349 (patch) | |
tree | c6e70d12dd7cd8cf90b2d0805d8693a209449f22 /test-jetty-nested/src/main/java/org/eclipse/jetty/nested/NestedJettyServlet.java | |
parent | d99f4e276b38a47997e35b42c980ad8df64c1d81 (diff) | |
download | org.eclipse.jetty.project-2ca6b4521b07323ae46650d4e688a4a199f42349.tar.gz org.eclipse.jetty.project-2ca6b4521b07323ae46650d4e688a4a199f42349.tar.xz org.eclipse.jetty.project-2ca6b4521b07323ae46650d4e688a4a199f42349.zip |
341736 Split jetty-nested out of war module
git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2959 7e9141cc-0065-0410-87d8-b60c137991c4
Diffstat (limited to 'test-jetty-nested/src/main/java/org/eclipse/jetty/nested/NestedJettyServlet.java')
-rw-r--r-- | test-jetty-nested/src/main/java/org/eclipse/jetty/nested/NestedJettyServlet.java | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/test-jetty-nested/src/main/java/org/eclipse/jetty/nested/NestedJettyServlet.java b/test-jetty-nested/src/main/java/org/eclipse/jetty/nested/NestedJettyServlet.java new file mode 100644 index 0000000000..e954b5893b --- /dev/null +++ b/test-jetty-nested/src/main/java/org/eclipse/jetty/nested/NestedJettyServlet.java @@ -0,0 +1,124 @@ +package org.eclipse.jetty.nested; + +import java.io.File; +import java.io.IOException; + +import javax.servlet.Servlet; +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.eclipse.jetty.server.LocalConnector; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.util.thread.ThreadPool; +import org.eclipse.jetty.webapp.WebAppContext; +import org.eclipse.jetty.xml.XmlConfiguration; + +/** + * Nested Jetty Servlet. + * <p> + * This servlet runs Jetty as a nested server inside another servlet container. The requests received by + * this servlet are routed via a {@link NestedConnector} to the nested jetty servlet and handled by jetty contexts, + * handlers, webapps and/or servlets. + * <p> + * The servlet can be configured with the following init parameters:<ul> + * <li>debug - if true then jetty debugging is turned on</li> + * <li>webapp - set to the resource path of the webapplication to deploy + * <li>jetty.xml - set the the resource path of a jetty xml file used to configure the server + * </ul> + * + */ +public class NestedJettyServlet implements Servlet +{ + private Server _server; + private ServletConfig _config; + private ServletContext _context; + private NestedConnector _connector; + + public void init(ServletConfig config) throws ServletException + { + ClassLoader orig = Thread.currentThread().getContextClassLoader(); + try + { + Thread.currentThread().setContextClassLoader(NestedJettyServlet.class.getClassLoader()); + _config=config; + _context=config.getServletContext(); + + Log.getLog().setDebugEnabled(Boolean.parseBoolean(_config.getInitParameter("debug"))); + + String jetty_xml=config.getInitParameter("jetty.xml"); + if (jetty_xml!=null) + { + XmlConfiguration xml_config = new XmlConfiguration(_context.getResourceAsStream(jetty_xml)); + _server=(Server)xml_config.configure(); + } + if (_server==null) + _server=new Server(); + + if (_server.getConnectors().length==0) + { + _connector=new NestedConnector(); + _server.addConnector(_connector); + } + else + _connector=(NestedConnector)_server.getConnectors()[0]; + + WebAppContext webapp = new WebAppContext(); + + webapp.setContextPath(_context.getContextPath()); + webapp.setTempDirectory(new File((File)_context.getAttribute("javax.servlet.context.tempdir"),"jetty")); + String docroot=config.getInitParameter("webapp"); + + String realpath=_context.getRealPath(docroot); + if (realpath!=null) + webapp.setWar(realpath); + else + webapp.setWar(_context.getResource(docroot).toString()); + + _server.setHandler(webapp); + + _server.start(); + _context.log("Started Jetty/"+_server.getVersion()+" for "+webapp.getWar()+" nested in "+_context.getServerInfo()); + } + catch(Exception e) + { + throw new ServletException(e); + } + finally + { + Thread.currentThread().setContextClassLoader(orig); + } + } + + public ServletConfig getServletConfig() + { + return _config; + } + + public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException + { + _connector.service(req,res); + } + + public String getServletInfo() + { + return this.toString(); + } + + public void destroy() + { + try + { + _server.stop(); + } + catch(Exception e) + { + _context.log("stopping",e); + } + } +} |