1 17 package org.apache.servicemix.components.http; 18 19 import javax.jbi.JBIException; 20 import javax.jbi.component.ComponentContext; 21 22 import org.mortbay.jetty.Connector; 23 import org.mortbay.jetty.Server; 24 import org.mortbay.jetty.bio.SocketConnector; 25 import org.mortbay.jetty.handler.ContextHandler; 26 import org.mortbay.jetty.servlet.ServletHandler; 27 import org.mortbay.jetty.servlet.ServletHolder; 28 import org.mortbay.jetty.servlet.ServletMapping; 29 import org.mortbay.thread.BoundedThreadPool; 30 31 36 public class HttpConnector extends HttpInOutBinding { 37 private Connector listener = new SocketConnector(); 38 39 43 private int maxThreads = 256; 44 45 private Server server; 46 private String host; 47 private int port; 48 49 55 public HttpConnector(String host, int port) { 56 this.host = host; 57 this.port = port; 58 } 59 60 public HttpConnector() { 61 } 62 63 68 public HttpConnector(Connector listener) { 69 this.listener = listener; 70 } 71 72 78 public void init(ComponentContext cc) throws JBIException { 79 super.init(cc); 80 if (listener == null) { 82 listener = new SocketConnector(); 83 } 84 listener.setHost(host); 85 listener.setPort(port); 86 server = new Server(); 87 BoundedThreadPool btp = new BoundedThreadPool(); 88 btp.setMaxThreads(getMaxThreads()); 89 server.setThreadPool(btp); 90 } 91 92 97 public void start() throws JBIException { 98 server.setConnectors(new Connector[] { listener }); 99 ContextHandler context = new ContextHandler(); 100 context.setContextPath("/"); 101 ServletHolder holder = new ServletHolder(); 102 holder.setName("jbiServlet"); 103 holder.setClassName(BindingServlet.class.getName()); 104 ServletHandler handler = new ServletHandler(); 105 handler.setServlets(new ServletHolder[] { holder }); 106 ServletMapping mapping = new ServletMapping(); 107 mapping.setServletName("jbiServlet"); 108 mapping.setPathSpec("/*"); 109 handler.setServletMappings(new ServletMapping[] { mapping }); 110 context.setHandler(handler); 111 server.setHandler(context); 112 context.setAttribute("binding", this); 113 try { 114 server.start(); 115 } 116 catch (Exception e) { 117 throw new JBIException("Start failed: " + e, e); 118 } 119 } 120 121 124 public void stop() throws JBIException { 125 try { 126 if (server != null) { 127 server.stop(); 128 } 129 } 130 catch (Exception e) { 131 throw new JBIException("Stop failed: " + e, e); 132 } 133 } 134 135 138 public void shutDown() throws JBIException { 139 super.shutDown(); 140 server = null; 141 } 142 143 144 public int getPort() { 147 return port; 148 } 149 150 public void setPort(int port) { 151 this.port = port; 152 } 153 154 public Server getServer() { 155 return server; 156 } 157 158 public void setServer(Server server) { 159 this.server = server; 160 } 161 162 public String getHost() { 163 return host; 164 } 165 166 public void setHost(String host) { 167 this.host = host; 168 } 169 170 public int getMaxThreads() { 171 return maxThreads; 172 } 173 174 public void setMaxThreads(int maxThreads) { 175 this.maxThreads = maxThreads; 176 } 177 } 178 | Popular Tags |