001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.components.http;
018:
019: import javax.jbi.JBIException;
020: import javax.jbi.component.ComponentContext;
021:
022: import org.mortbay.jetty.Connector;
023: import org.mortbay.jetty.Server;
024: import org.mortbay.jetty.bio.SocketConnector;
025: import org.mortbay.jetty.handler.ContextHandler;
026: import org.mortbay.jetty.servlet.ServletHandler;
027: import org.mortbay.jetty.servlet.ServletHolder;
028: import org.mortbay.jetty.servlet.ServletMapping;
029: import org.mortbay.thread.BoundedThreadPool;
030:
031: /**
032: * An embedded Servlet engine to implement a HTTP connector
033: *
034: * @version $Revision: 426415 $
035: */
036: public class HttpConnector extends HttpInOutBinding {
037: private Connector listener = new SocketConnector();
038:
039: /**
040: * The maximum number of threads for the Jetty SocketListener. It's set
041: * to 256 by default to match the default value in Jetty.
042: */
043: private int maxThreads = 256;
044:
045: private Server server;
046: private String host;
047: private int port;
048:
049: /**
050: * Constructor
051: *
052: * @param host
053: * @param port
054: */
055: public HttpConnector(String host, int port) {
056: this .host = host;
057: this .port = port;
058: }
059:
060: public HttpConnector() {
061: }
062:
063: /**
064: * Constructor
065: *
066: * @param listener
067: */
068: public HttpConnector(Connector listener) {
069: this .listener = listener;
070: }
071:
072: /**
073: * Called when the Component is initialized
074: *
075: * @param cc
076: * @throws JBIException
077: */
078: public void init(ComponentContext cc) throws JBIException {
079: super .init(cc);
080: //should set all ports etc here - from the naming context I guess ?
081: if (listener == null) {
082: listener = new SocketConnector();
083: }
084: listener.setHost(host);
085: listener.setPort(port);
086: server = new Server();
087: BoundedThreadPool btp = new BoundedThreadPool();
088: btp.setMaxThreads(getMaxThreads());
089: server.setThreadPool(btp);
090: }
091:
092: /**
093: * start the Component
094: *
095: * @throws JBIException
096: */
097: public void start() throws JBIException {
098: server.setConnectors(new Connector[] { listener });
099: 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: } catch (Exception e) {
116: throw new JBIException("Start failed: " + e, e);
117: }
118: }
119:
120: /**
121: * stop
122: */
123: public void stop() throws JBIException {
124: try {
125: if (server != null) {
126: server.stop();
127: }
128: } catch (Exception e) {
129: throw new JBIException("Stop failed: " + e, e);
130: }
131: }
132:
133: /**
134: * shutdown
135: */
136: public void shutDown() throws JBIException {
137: super .shutDown();
138: server = null;
139: }
140:
141: // Properties
142: //-------------------------------------------------------------------------
143: public int getPort() {
144: return port;
145: }
146:
147: public void setPort(int port) {
148: this .port = port;
149: }
150:
151: public Server getServer() {
152: return server;
153: }
154:
155: public void setServer(Server server) {
156: this .server = server;
157: }
158:
159: public String getHost() {
160: return host;
161: }
162:
163: public void setHost(String host) {
164: this .host = host;
165: }
166:
167: public int getMaxThreads() {
168: return maxThreads;
169: }
170:
171: public void setMaxThreads(int maxThreads) {
172: this.maxThreads = maxThreads;
173: }
174: }
|