/**
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.activemq.transport.http;

import org.activemq.io.TextWireFormat;
import org.activemq.transport.TransportServerChannelSupport;
import org.activemq.transport.xstream.XStreamWireFormat;
import org.activemq.util.JMSExceptionHelper;
import org.mortbay.http.HttpContext;
import org.mortbay.http.SocketListener;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.ServletHandler;

import javax.jms.JMSException;
import java.net.URI;

/**
 * @version $Revision$
 */
public class HttpTransportConnector extends TransportServerChannelSupport {
    private URI bindAddress;
    private TextWireFormat wireFormat;
    private Server server = new Server();
    private SocketListener listener = new SocketListener();

    public HttpTransportConnector(URI uri) {
        super(uri);
        this.bindAddress = uri;
    }

    public void start() throws JMSException {
        try {
            listener.setPort(bindAddress.getPort());
            server.addListener(listener);

            HttpContext context = server.addContext("/");
            ServletHandler handler = new ServletHandler();
            handler.addServlet("httpTunnel", "/*", HttpTunnelServlet.class.getName());

            context.addHandler(handler);
            context.setAttribute("transportChannelListener", getTransportChannelListener());
            context.setAttribute("wireFormat", getWireFormat());
            server.start();
        }
        catch (Exception e) {
            throw JMSExceptionHelper.newJMSException("Could not start HTTP server: " + e, e);
        }
    }

    public synchronized void stop() throws JMSException {
        super.stop();
        try {
            server.stop();
        }
        catch (InterruptedException e) {
            throw JMSExceptionHelper.newJMSException("Could not stop HTTP server: " + e, e);
        }
    }

    // Properties
    //-------------------------------------------------------------------------
    public TextWireFormat getWireFormat() {
        if (wireFormat == null) {
            wireFormat = createWireFormat();
        }
        return wireFormat;
    }

    public void setWireFormat(TextWireFormat wireFormat) {
        this.wireFormat = wireFormat;
    }


    // Implementation methods
    //-------------------------------------------------------------------------
    protected TextWireFormat createWireFormat() {
        return new XStreamWireFormat();
    }
}
