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: public class HttpSoapConnector extends HttpSoapInOutBinding {
032: private Connector listener = new SocketConnector();
033:
034: /**
035: * The maximum number of threads for the Jetty SocketListener. It's set
036: * to 256 by default to match the default value in Jetty.
037: */
038: private int maxThreads = 256;
039:
040: private Server server;
041:
042: private String host;
043:
044: private int port;
045:
046: /**
047: * Constructor
048: *
049: * @param host
050: * @param port
051: */
052: public HttpSoapConnector(String host, int port, boolean defaultInOut) {
053: this .host = host;
054: this .port = port;
055: this .defaultInOut = defaultInOut;
056: }
057:
058: public HttpSoapConnector() {
059: }
060:
061: /**
062: * Constructor
063: *
064: * @param listener
065: */
066: public HttpSoapConnector(Connector listener) {
067: this .listener = listener;
068: }
069:
070: /**
071: * Called when the Component is initialized
072: *
073: * @param cc
074: * @throws JBIException
075: */
076: public void init(ComponentContext cc) throws JBIException {
077: super .init(cc);
078: //should set all ports etc here - from the naming context I guess ?
079: if (listener == null) {
080: listener = new SocketConnector();
081: }
082: listener.setHost(host);
083: listener.setPort(port);
084: server = new Server();
085: BoundedThreadPool btp = new BoundedThreadPool();
086: btp.setMaxThreads(getMaxThreads());
087: server.setThreadPool(btp);
088: }
089:
090: /**
091: * start the Component
092: *
093: * @throws JBIException
094: */
095: public void start() throws JBIException {
096: server.setConnectors(new Connector[] { listener });
097: ContextHandler context = new ContextHandler();
098: context.setContextPath("/");
099: ServletHolder holder = new ServletHolder();
100: holder.setName("jbiServlet");
101: holder.setClassName(BindingServlet.class.getName());
102: ServletHandler handler = new ServletHandler();
103: handler.setServlets(new ServletHolder[] { holder });
104: ServletMapping mapping = new ServletMapping();
105: mapping.setServletName("jbiServlet");
106: mapping.setPathSpec("/*");
107: handler.setServletMappings(new ServletMapping[] { mapping });
108: context.setHandler(handler);
109: server.setHandler(context);
110: context.setAttribute("binding", this );
111: try {
112: server.start();
113: } catch (Exception e) {
114: throw new JBIException("Start failed: " + e, e);
115: }
116: }
117:
118: /**
119: * stop
120: */
121: public void stop() throws JBIException {
122: try {
123: if (server != null) {
124: server.stop();
125: }
126: } catch (Exception e) {
127: throw new JBIException("Stop failed: " + e, e);
128: }
129: }
130:
131: /**
132: * shutdown
133: */
134: public void shutDown() throws JBIException {
135: server = null;
136: }
137:
138: // Properties
139: //-------------------------------------------------------------------------
140: public int getPort() {
141: return port;
142: }
143:
144: public void setPort(int port) {
145: this .port = port;
146: }
147:
148: public Server getServer() {
149: return server;
150: }
151:
152: public void setServer(Server server) {
153: this .server = server;
154: }
155:
156: public String getHost() {
157: return host;
158: }
159:
160: public void setHost(String host) {
161: this .host = host;
162: }
163:
164: public int getMaxThreads() {
165: return maxThreads;
166: }
167:
168: public void setMaxThreads(int maxThreads) {
169: this.maxThreads = maxThreads;
170: }
171: }
|