001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * 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     **/
018    package org.codehaus.activesoap.transport.http;
019    
020    import org.codehaus.activesoap.RestService;
021    import org.codehaus.activesoap.transport.TransportConnector;
022    import org.mortbay.http.HttpContext;
023    import org.mortbay.http.SocketListener;
024    import org.mortbay.jetty.Server;
025    import org.mortbay.jetty.servlet.ServletHandler;
026    
027    import java.net.UnknownHostException;
028    
029    
030    /**
031     * An embedded Servlet engine to implement a HTTP transport connector
032     *
033     * @version $Revision: 1.3 $
034     */
035    public class HttpTransportConnector extends TransportConnector {
036    
037        private SocketListener listener = new SocketListener();
038        private Server server = new Server();
039        private RestService service;
040    
041        public HttpTransportConnector(String host, int port, RestService service) throws UnknownHostException {
042            listener = new SocketListener();
043            listener.setHost(host);
044            listener.setPort(port);
045            this.service = service;
046        }
047    
048        public HttpTransportConnector(SocketListener listener, RestService service) {
049            this.listener = listener;
050            this.service = service;
051        }
052    
053        public void start() throws Exception {
054            server.addListener(listener);
055    
056            HttpContext context = server.addContext("/");
057            ServletHandler handler = new ServletHandler();
058            handler.addServlet("soapServlet", "/*", EndpointServlet.class.getName());
059            context.addHandler(handler);
060            context.setAttribute("org.codehaus.activesoap.Service", service);
061            server.start();
062        }
063    
064        public void stop() throws Exception {
065            server.stop();
066        }
067    
068    }