Index: JettyHttpMessageReceiver.java =================================================================== --- JettyHttpMessageReceiver.java (revision 5734) +++ JettyHttpMessageReceiver.java (working copy) @@ -12,14 +12,8 @@ import org.apache.commons.lang.StringUtils; import org.mortbay.http.HttpContext; -import org.mortbay.http.SocketListener; -import org.mortbay.jetty.Server; import org.mortbay.jetty.servlet.ServletHandler; -import org.mortbay.util.InetAddrPort; import org.mule.MuleManager; -import org.mule.config.i18n.Message; -import org.mule.config.i18n.Messages; -import org.mule.config.ThreadingProfile; import org.mule.impl.endpoint.MuleEndpoint; import org.mule.providers.AbstractMessageReceiver; import org.mule.providers.http.servlet.MuleRESTReceiverServlet; @@ -28,154 +22,107 @@ import org.mule.umo.UMOException; import org.mule.umo.endpoint.UMOEndpoint; import org.mule.umo.lifecycle.InitialisationException; -import org.mule.umo.lifecycle.LifecycleException; import org.mule.umo.provider.UMOConnector; /** * HttpMessageReceiver is a simple http server that can be used to * listen for http requests on a particular port - * */ -public class JettyHttpMessageReceiver extends AbstractMessageReceiver -{ - public static final String JETTY_SERVLET_CONNECTOR_NAME= "_jettyConnector"; +public class JettyHttpMessageReceiver extends AbstractMessageReceiver { + public static final String JETTY_SERVLET_CONNECTOR_NAME = "_jettyConnector"; - private Server httpServer; - public JettyHttpMessageReceiver(UMOConnector connector, UMOComponent component, UMOEndpoint endpoint) - throws InitialisationException - { + throws InitialisationException { super(connector, component, endpoint); if ("rest".equals(endpoint.getEndpointURI().getScheme())) + { //We need to a Servlet Connecotor pointing to our servlet so the Servlets can //find the listeners for incoming requests - ServletConnector scon = (ServletConnector) MuleManager.getInstance().lookupConnector(JETTY_SERVLET_CONNECTOR_NAME); - if(scon!=null) { - throw new InitialisationException(new Message("http", 10), this); + + ServletConnector scon = + (ServletConnector) MuleManager.getInstance().lookupConnector(JETTY_SERVLET_CONNECTOR_NAME); + + if (scon == null) { + scon = new ServletConnector(); + scon.setName(JETTY_SERVLET_CONNECTOR_NAME); + scon.setServletUrl(endpoint.getEndpointURI().getAddress()); + try { + MuleManager.getInstance().registerConnector(scon); + } catch (UMOException e) { + throw new InitialisationException(e, this); + } } - scon = new ServletConnector(); - scon.setName(JETTY_SERVLET_CONNECTOR_NAME); - scon.setServletUrl(endpoint.getEndpointURI().getAddress()); - try - { - MuleManager.getInstance().registerConnector(scon); + try { String path = endpoint.getEndpointURI().getPath(); - if (StringUtils.isEmpty(path)) - { + if (StringUtils.isEmpty(path)) { path = "/"; } UMOEndpoint ep = new MuleEndpoint("servlet://" + path.substring(1), true); scon.registerListener(component, ep); } - catch (Exception e) - { + catch (Exception e) { throw new InitialisationException(e, this); } } } - public void doConnect() throws Exception - { - httpServer = new Server(); - SocketListener socketListener = new SocketListener(new InetAddrPort(endpoint.getEndpointURI() - .getPort())); + public void doConnect() throws Exception { - // apply Threading settings - ThreadingProfile tp = connector.getReceiverThreadingProfile(); - socketListener.setMaxIdleTimeMs((int)tp.getThreadTTL()); - int threadsActive = tp.getMaxThreadsActive(); - int threadsMin = socketListener.getMinThreads(); - if (threadsMin >= threadsActive) - { - socketListener.setMinThreads(threadsActive - 1); - } - socketListener.setMaxThreads(threadsActive); - socketListener.setThreadsPriority(tp.getThreadPriority()); - - httpServer.addListener(socketListener); - String path = endpoint.getEndpointURI().getPath(); - if (StringUtils.isEmpty(path)) - { + if (StringUtils.isEmpty(path)) { path = "/"; } - if (!path.endsWith("/")) - { + if (!path.endsWith("/")) { path += "/"; } - HttpContext context = httpServer.getContext("/"); + JettyConnector conn = (JettyConnector) this.getConnector(); + HttpContext context = conn.getContext(endpoint); context.setRequestLog(null); ServletHandler handler = new ServletHandler(); - if ("rest".equals(endpoint.getEndpointURI().getScheme())) - { - handler.addServlet("MuleRESTReceiverServlet", path + "*", MuleRESTReceiverServlet.class.getName()); + if ("rest".equals(endpoint.getEndpointURI().getScheme())) { + handler.addServlet(endpoint.getName(), path + "*", MuleRESTReceiverServlet.class.getName()); + } else { + handler.addServlet(endpoint.getName(), path + "*", JettyReceiverServlet.class.getName()); } - else - { - handler.addServlet("JettyReceiverServlet", path + "*", JettyReceiverServlet.class.getName()); - } - context.addHandler(handler); - context.setAttribute("messageReceiver", this); + // push the receiver into the context + context.setAttribute(endpoint.getName() + "Receiver", this); + // ToDo There is probably a less intrusive way to push the new handler w/o stopping and starting jetty + if (context.isStarted()) { + context.getHttpServer().stop(); + } + context.getHttpServer().start(); } - public void doDisconnect() throws Exception - { + public void doDisconnect() throws Exception { // stop is automativcally called by Mule } - /** * Template method to dispose any resources associated with this receiver. There * is not need to dispose the connector as this is already done by the framework */ - protected void doDispose() - { - try - { - httpServer.stop(false); - } - catch (InterruptedException e) - { - logger.error("Error disposing Jetty recevier on: " + endpoint.getEndpointURI().toString(), e); - } + protected void doDispose() { + } - public void doStart() throws UMOException - { - try - { - httpServer.start(); - } - catch (Exception e) - { - throw new LifecycleException(new Message(Messages.FAILED_TO_START_X, "Jetty Http Receiver"), e, - this); - } + public void doStart() throws UMOException { + } - public void doStop() throws UMOException - { - try - { - httpServer.stop(true); - } - catch (InterruptedException e) - { - throw new LifecycleException(new Message(Messages.FAILED_TO_STOP_X, "Jetty Http Receiver"), e, - this); - } + public void doStop() throws UMOException { } + }