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.activemq.transport.http;
019    
020    import org.activemq.io.TextWireFormat;
021    import org.activemq.transport.TransportServerChannelSupport;
022    import org.activemq.transport.xstream.XStreamWireFormat;
023    import org.activemq.util.JMSExceptionHelper;
024    import org.mortbay.http.HttpContext;
025    import org.mortbay.http.SocketListener;
026    import org.mortbay.jetty.Server;
027    import org.mortbay.jetty.servlet.ServletHandler;
028    
029    import javax.jms.JMSException;
030    import java.net.URI;
031    
032    /**
033     * @version $Revision$
034     */
035    public class HttpTransportConnector extends TransportServerChannelSupport {
036        private URI bindAddress;
037        private TextWireFormat wireFormat;
038        private Server server = new Server();
039        private SocketListener listener = new SocketListener();
040    
041        public HttpTransportConnector(URI uri) {
042            super(uri);
043            this.bindAddress = uri;
044        }
045    
046        public void start() throws JMSException {
047            try {
048                    listener.setHost(bindAddress.getHost());
049                listener.setPort(bindAddress.getPort());
050                server.addListener(listener);
051    
052                HttpContext context = server.addContext("/");
053                ServletHandler handler = new ServletHandler();
054                handler.addServlet("httpTunnel", "/*", HttpTunnelServlet.class.getName());
055    
056                context.addHandler(handler);
057                context.setAttribute("transportChannelListener", getTransportChannelListener());
058                context.setAttribute("wireFormat", getWireFormat());
059                server.start();
060            }
061            catch (Exception e) {
062                throw JMSExceptionHelper.newJMSException("Could not start HTTP server: " + e, e);
063            }
064        }
065    
066        public synchronized void stop() throws JMSException {
067            super.stop();
068            try {
069                server.stop();
070            }
071            catch (InterruptedException e) {
072                throw JMSExceptionHelper.newJMSException("Could not stop HTTP server: " + e, e);
073            }
074        }
075    
076        // Properties
077        //-------------------------------------------------------------------------
078        public TextWireFormat getWireFormat() {
079            if (wireFormat == null) {
080                wireFormat = createWireFormat();
081            }
082            return wireFormat;
083        }
084    
085        public void setWireFormat(TextWireFormat wireFormat) {
086            this.wireFormat = wireFormat;
087        }
088    
089    
090        // Implementation methods
091        //-------------------------------------------------------------------------
092        protected TextWireFormat createWireFormat() {
093            return new XStreamWireFormat();
094        }
095        
096        protected void setSocketListener( SocketListener socketListener ) {
097            listener = socketListener;
098        }
099    }