Coverage Report - org.mule.providers.http.jetty.JettyHttpMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
JettyHttpMessageReceiver
81%
48/59
78%
14/18
3.5
 
 1  
 /*
 2  
  * $Id: JettyHttpMessageReceiver.java 8410 2007-09-14 14:07:00Z aperepel $
 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  8
         super(connector, component, endpoint);
 50  
 
 51  8
         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  2
             ServletConnector scon = (ServletConnector) MuleManager.getInstance().lookupConnector(JETTY_SERVLET_CONNECTOR_NAME);
 56  2
             if(scon!=null) {
 57  0
                 throw new InitialisationException(
 58  
                     HttpMessages.noServletConnectorFound(JETTY_SERVLET_CONNECTOR_NAME), this);
 59  
             }
 60  
 
 61  2
             scon = new ServletConnector();
 62  2
             scon.setName(JETTY_SERVLET_CONNECTOR_NAME);
 63  2
             scon.setServletUrl(endpoint.getEndpointURI().getAddress());
 64  
             try
 65  
             {
 66  2
                 MuleManager.getInstance().registerConnector(scon);
 67  2
                 String path = endpoint.getEndpointURI().getPath();
 68  2
                 if (StringUtils.isEmpty(path))
 69  
                 {
 70  0
                     path = "/";
 71  
                 }
 72  
 
 73  2
                 UMOEndpoint ep = new MuleEndpoint("servlet://" + path.substring(1), true);
 74  2
                 ep.setTransformer(endpoint.getTransformer());
 75  2
                 scon.registerListener(component, ep);
 76  
             }
 77  0
             catch (Exception e)
 78  
             {
 79  0
                 throw new InitialisationException(e, this);
 80  2
             }
 81  
         }
 82  
 
 83  8
     }
 84  
 
 85  
     protected void doConnect() throws Exception
 86  
     {
 87  8
         httpServer = new Server();
 88  8
         SocketListener socketListener = new SocketListener(new InetAddrPort(endpoint.getEndpointURI()
 89  
             .getPort()));
 90  
 
 91  
         // apply Threading settings
 92  8
         ThreadingProfile tp = connector.getReceiverThreadingProfile();
 93  8
         socketListener.setMaxIdleTimeMs((int)tp.getThreadTTL());
 94  8
         int threadsActive = tp.getMaxThreadsActive();
 95  8
         int threadsMin = socketListener.getMinThreads();
 96  8
         if (threadsMin >= threadsActive)
 97  
         {
 98  0
             socketListener.setMinThreads(threadsActive - 1);
 99  
         }
 100  8
         socketListener.setMaxThreads(threadsActive);
 101  
         // thread priorities are evil and gone from ThreadingProfile
 102  
         // (google for priority inversion)
 103  
         // socketListener.setThreadsPriority(tp.getThreadPriority());
 104  
 
 105  8
         httpServer.addListener(socketListener);
 106  
 
 107  8
         String path = endpoint.getEndpointURI().getPath();
 108  8
         if (StringUtils.isEmpty(path))
 109  
         {
 110  2
             path = "/";
 111  
         }
 112  
 
 113  8
         if (!path.endsWith("/"))
 114  
         {
 115  6
             path += "/";
 116  
         }
 117  
 
 118  8
         HttpContext context = httpServer.getContext("/");
 119  8
         context.setRequestLog(null);
 120  
 
 121  8
         ServletHandler handler = new ServletHandler();
 122  8
         if ("rest".equals(endpoint.getEndpointURI().getScheme()))
 123  
         {
 124  6
             handler.addServlet("MuleRESTReceiverServlet", path + "*", MuleRESTReceiverServlet.class.getName());
 125  
         }
 126  
         else
 127  
         {
 128  6
             handler.addServlet("JettyReceiverServlet", path + "*", JettyReceiverServlet.class.getName());
 129  
         }
 130  
 
 131  
 
 132  8
         context.addHandler(handler);
 133  
         // setAttribute is already synchronized in Jetty
 134  8
         context.setAttribute("messageReceiver", this);
 135  
 
 136  8
     }
 137  
 
 138  
     protected void doDisconnect() throws Exception
 139  
     {
 140  
         // stop is automativcally called by Mule
 141  8
     }
 142  
 
 143  
 
 144  
 
 145  
     /**
 146  
      * Template method to dispose any resources associated with this receiver. There
 147  
      * is not need to dispose the connector as this is already done by the framework
 148  
      */
 149  
     protected void doDispose()
 150  
     {
 151  
         try
 152  
         {
 153  8
             httpServer.stop(false);
 154  
         }
 155  0
         catch (InterruptedException e)
 156  
         {
 157  0
             logger.error("Error disposing Jetty recevier on: " + endpoint.getEndpointURI().toString(), e);
 158  8
         }
 159  8
     }
 160  
 
 161  
     protected void doStart() throws UMOException
 162  
     {
 163  
         try
 164  
         {
 165  8
             httpServer.start();
 166  
         }
 167  0
         catch (Exception e)
 168  
         {
 169  0
             throw new LifecycleException(CoreMessages.failedToStart("Jetty Http Receiver"), e, this);
 170  8
         }
 171  8
     }
 172  
 
 173  
     protected void doStop() throws UMOException
 174  
     {
 175  
         try
 176  
         {
 177  8
             httpServer.stop(true);
 178  
         }
 179  0
         catch (InterruptedException e)
 180  
         {
 181  0
             throw new LifecycleException(CoreMessages.failedToStop("Jetty Http Receiver"), e, this);
 182  8
         }
 183  8
     }
 184  
 
 185  
 }