View Javadoc

1   /*
2    * $Id: EmbeddedJettyServer.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.transport.servlet.jetty.util;
11  
12  import org.mule.api.MuleContext;
13  import org.mule.api.config.MuleProperties;
14  
15  import javax.servlet.Servlet;
16  import javax.servlet.ServletContextEvent;
17  import javax.servlet.ServletContextListener;
18  
19  import org.mortbay.jetty.Server;
20  import org.mortbay.jetty.servlet.Context;
21  import org.mortbay.jetty.servlet.ServletHolder;
22  
23  /**
24   * A simple helper class for Mule testing that creates an embedded Jetty Server
25   */
26  public class EmbeddedJettyServer
27  {
28      private Server httpServer;
29  
30      public EmbeddedJettyServer(int port, String contextPath, String servletPath, Servlet servlet, final MuleContext context)
31      {
32           httpServer = new Server(port);
33  
34          Context c = new Context(httpServer, contextPath, Context.SESSIONS);
35          c.addServlet(new ServletHolder(servlet), servletPath);
36          c.addEventListener(new ServletContextListener() {
37              public void contextInitialized(ServletContextEvent sce)
38              {
39                  sce.getServletContext().setAttribute(MuleProperties.MULE_CONTEXT_PROPERTY, context);
40              }
41  
42              public void contextDestroyed(ServletContextEvent sce) { }
43          });
44      }
45  
46      public void start() throws Exception
47      {
48          httpServer.start();
49      }
50  
51      public void stop() throws Exception
52      {
53          httpServer.stop();
54      }
55  
56      public void destroy()
57      {
58          httpServer.destroy();
59      }
60  
61      public boolean isStarted()
62      {
63          return httpServer.isStarted();
64      }
65  }