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