View Javadoc

1   /*
2    * $Id: JettyHttpMessageReceiver.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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  
11  package org.mule.providers.http.jetty;
12  
13  import org.mule.MuleManager;
14  import org.mule.config.ThreadingProfile;
15  import org.mule.config.i18n.CoreMessages;
16  import org.mule.impl.endpoint.MuleEndpoint;
17  import org.mule.providers.AbstractMessageReceiver;
18  import org.mule.providers.http.i18n.HttpMessages;
19  import org.mule.providers.http.servlet.MuleRESTReceiverServlet;
20  import org.mule.providers.http.servlet.ServletConnector;
21  import org.mule.umo.UMOComponent;
22  import org.mule.umo.UMOException;
23  import org.mule.umo.endpoint.UMOEndpoint;
24  import org.mule.umo.lifecycle.InitialisationException;
25  import org.mule.umo.lifecycle.LifecycleException;
26  import org.mule.umo.provider.UMOConnector;
27  import org.mule.util.StringUtils;
28  
29  import org.mortbay.http.HttpContext;
30  import org.mortbay.http.SocketListener;
31  import org.mortbay.jetty.Server;
32  import org.mortbay.jetty.servlet.ServletHandler;
33  import org.mortbay.util.InetAddrPort;
34  
35  /**
36   * <code>HttpMessageReceiver</code> is a simple http server that can be used to
37   * listen for http requests on a particular port
38   * 
39   */
40  public class JettyHttpMessageReceiver extends AbstractMessageReceiver
41  {
42      public static final String JETTY_SERVLET_CONNECTOR_NAME = "_jettyConnector";
43  
44      private Server httpServer;
45  
46      public JettyHttpMessageReceiver(UMOConnector connector, UMOComponent component, UMOEndpoint endpoint)
47          throws InitialisationException
48      {
49          super(connector, component, endpoint);
50  
51          if ("rest".equals(endpoint.getEndpointURI().getScheme()))
52          {
53              //We need to a Servlet Connecotor pointing to our servlet so the Servlets can
54              //find the listeners for incoming requests
55              ServletConnector scon = (ServletConnector) MuleManager.getInstance().lookupConnector(JETTY_SERVLET_CONNECTOR_NAME);
56              if(scon!=null) {
57                  throw new InitialisationException(
58                      HttpMessages.noServletConnectorFound(JETTY_SERVLET_CONNECTOR_NAME), this);
59              }
60  
61              scon = new ServletConnector();
62              scon.setName(JETTY_SERVLET_CONNECTOR_NAME);
63              scon.setServletUrl(endpoint.getEndpointURI().getAddress());
64              try
65              {
66                  MuleManager.getInstance().registerConnector(scon);
67                  String path = endpoint.getEndpointURI().getPath();
68                  if (StringUtils.isEmpty(path))
69                  {
70                      path = "/";
71                  }
72  
73                  UMOEndpoint ep = new MuleEndpoint("servlet://" + path.substring(1), true);
74                  ep.setTransformer(endpoint.getTransformer());
75                  scon.registerListener(component, ep);
76              }
77              catch (Exception e)
78              {
79                  throw new InitialisationException(e, this);
80              }
81          }
82  
83      }
84  
85      protected void doConnect() throws Exception
86      {
87          httpServer = new Server();
88          SocketListener socketListener = new SocketListener(new InetAddrPort(endpoint.getEndpointURI()
89              .getPort()));
90  
91          // apply Threading settings
92          ThreadingProfile tp = connector.getReceiverThreadingProfile();
93          socketListener.setMaxIdleTimeMs((int)tp.getThreadTTL());
94          int threadsActive = tp.getMaxThreadsActive();
95          int threadsMin = socketListener.getMinThreads();
96          if (threadsMin >= threadsActive)
97          {
98              socketListener.setMinThreads(threadsActive - 1);
99          }
100         socketListener.setMaxThreads(threadsActive);
101         // thread priorities are evil and gone from ThreadingProfile
102         // (google for priority inversion)
103         // socketListener.setThreadsPriority(tp.getThreadPriority());
104 
105         httpServer.addListener(socketListener);
106 
107         String path = endpoint.getEndpointURI().getPath();
108         if (StringUtils.isEmpty(path))
109         {
110             path = "/";
111         }
112 
113         if (!path.endsWith("/"))
114         {
115             path += "/";
116         }
117 
118         HttpContext context = httpServer.getContext("/");
119         context.setRequestLog(null);
120 
121         ServletHandler handler = new ServletHandler();
122         if ("rest".equals(endpoint.getEndpointURI().getScheme()))
123         {
124             handler.addServlet("MuleRESTReceiverServlet", path + "*", MuleRESTReceiverServlet.class.getName());
125         }
126         else
127         {
128             handler.addServlet("JettyReceiverServlet", path + "*", JettyReceiverServlet.class.getName());
129         }
130 
131 
132         context.addHandler(handler);
133         context.setAttribute("messageReceiver", this);
134 
135     }
136 
137     protected void doDisconnect() throws Exception
138     {
139         // stop is automativcally called by Mule
140     }
141 
142 
143 
144     /**
145      * Template method to dispose any resources associated with this receiver. There
146      * is not need to dispose the connector as this is already done by the framework
147      */
148     protected void doDispose()
149     {
150         try
151         {
152             httpServer.stop(false);
153         }
154         catch (InterruptedException e)
155         {
156             logger.error("Error disposing Jetty recevier on: " + endpoint.getEndpointURI().toString(), e);
157         }
158     }
159 
160     protected void doStart() throws UMOException
161     {
162         try
163         {
164             httpServer.start();
165         }
166         catch (Exception e)
167         {
168             throw new LifecycleException(CoreMessages.failedToStart("Jetty Http Receiver"), e, this);
169         }
170     }
171 
172     protected void doStop() throws UMOException
173     {
174         try
175         {
176             httpServer.stop(true);
177         }
178         catch (InterruptedException e)
179         {
180             throw new LifecycleException(CoreMessages.failedToStop("Jetty Http Receiver"), e, this);
181         }
182     }
183 
184 }