Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b8b5fc9c972b69803c94ce977253e05227aa989b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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.server.bio.SocketConnector;
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);
        }
    }
}

Back to the top