1 10 11 package org.mule.providers.http.jetty; 12 13 import org.apache.commons.lang.StringUtils; 14 import org.mortbay.http.HttpContext; 15 import org.mortbay.http.SocketListener; 16 import org.mortbay.jetty.Server; 17 import org.mortbay.jetty.servlet.ServletHandler; 18 import org.mortbay.util.InetAddrPort; 19 import org.mule.MuleManager; 20 import org.mule.config.i18n.Message; 21 import org.mule.config.i18n.Messages; 22 import org.mule.config.ThreadingProfile; 23 import org.mule.impl.endpoint.MuleEndpoint; 24 import org.mule.providers.AbstractMessageReceiver; 25 import org.mule.providers.http.servlet.MuleRESTReceiverServlet; 26 import org.mule.providers.http.servlet.ServletConnector; 27 import org.mule.umo.UMOComponent; 28 import org.mule.umo.UMOException; 29 import org.mule.umo.endpoint.UMOEndpoint; 30 import org.mule.umo.lifecycle.InitialisationException; 31 import org.mule.umo.lifecycle.LifecycleException; 32 import org.mule.umo.provider.UMOConnector; 33 34 39 public class JettyHttpMessageReceiver extends AbstractMessageReceiver 40 { 41 public static final String JETTY_SERVLET_CONNECTOR_NAME= "_jettyConnector"; 42 43 private Server httpServer; 44 45 public JettyHttpMessageReceiver(UMOConnector connector, UMOComponent component, UMOEndpoint endpoint) 46 throws InitialisationException 47 { 48 super(connector, component, endpoint); 49 50 if ("rest".equals(endpoint.getEndpointURI().getScheme())) 51 { 52 ServletConnector scon = (ServletConnector) MuleManager.getInstance().lookupConnector(JETTY_SERVLET_CONNECTOR_NAME); 55 if(scon!=null) { 56 throw new InitialisationException(new Message("http", 10), this); 57 } 58 59 scon = new ServletConnector(); 60 scon.setName(JETTY_SERVLET_CONNECTOR_NAME); 61 scon.setServletUrl(endpoint.getEndpointURI().getAddress()); 62 try 63 { 64 MuleManager.getInstance().registerConnector(scon); 65 String path = endpoint.getEndpointURI().getPath(); 66 if (StringUtils.isEmpty(path)) 67 { 68 path = "/"; 69 } 70 71 UMOEndpoint ep = new MuleEndpoint("servlet://" + path.substring(1), true); 72 scon.registerListener(component, ep); 73 } 74 catch (Exception e) 75 { 76 throw new InitialisationException(e, this); 77 } 78 } 79 80 } 81 82 public void doConnect() throws Exception 83 { 84 httpServer = new Server(); 85 SocketListener socketListener = new SocketListener(new InetAddrPort(endpoint.getEndpointURI() 86 .getPort())); 87 88 ThreadingProfile tp = connector.getReceiverThreadingProfile(); 90 socketListener.setMaxIdleTimeMs((int)tp.getThreadTTL()); 91 int threadsActive = tp.getMaxThreadsActive(); 92 int threadsMin = socketListener.getMinThreads(); 93 if (threadsMin >= threadsActive) 94 { 95 socketListener.setMinThreads(threadsActive - 1); 96 } 97 socketListener.setMaxThreads(threadsActive); 98 socketListener.setThreadsPriority(tp.getThreadPriority()); 99 100 httpServer.addListener(socketListener); 101 102 String path = endpoint.getEndpointURI().getPath(); 103 if (StringUtils.isEmpty(path)) 104 { 105 path = "/"; 106 } 107 108 if (!path.endsWith("/")) 109 { 110 path += "/"; 111 } 112 113 HttpContext context = httpServer.getContext("/"); 114 context.setRequestLog(null); 115 116 ServletHandler handler = new ServletHandler(); 117 if ("rest".equals(endpoint.getEndpointURI().getScheme())) 118 { 119 handler.addServlet("MuleRESTReceiverServlet", path + "*", MuleRESTReceiverServlet.class.getName()); 120 } 121 else 122 { 123 handler.addServlet("JettyReceiverServlet", path + "*", JettyReceiverServlet.class.getName()); 124 } 125 126 127 context.addHandler(handler); 128 context.setAttribute("messageReceiver", this); 129 130 } 131 132 public void doDisconnect() throws Exception 133 { 134 } 136 137 138 139 143 protected void doDispose() 144 { 145 try 146 { 147 httpServer.stop(false); 148 } 149 catch (InterruptedException e) 150 { 151 logger.error("Error disposing Jetty recevier on: " + endpoint.getEndpointURI().toString(), e); 152 } 153 } 154 155 public void doStart() throws UMOException 156 { 157 try 158 { 159 httpServer.start(); 160 } 161 catch (Exception e) 162 { 163 throw new LifecycleException(new Message(Messages.FAILED_TO_START_X, "Jetty Http Receiver"), e, 164 this); 165 } 166 } 167 168 public void doStop() throws UMOException 169 { 170 try 171 { 172 httpServer.stop(true); 173 } 174 catch (InterruptedException e) 175 { 176 throw new LifecycleException(new Message(Messages.FAILED_TO_STOP_X, "Jetty Http Receiver"), e, 177 this); 178 } 179 } 180 181 } 182 | Popular Tags |