HttpTransportConnector xref

View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.transport.http;
    19
    20  import org.codehaus.activemq.message.TextWireFormat;
    21  import org.codehaus.activemq.transport.TransportServerChannelSupport;
    22  import org.codehaus.activemq.transport.xstream.XStreamWireFormat;
    23  import org.codehaus.activemq.util.JMSExceptionHelper;
    24  import org.mortbay.http.HttpContext;
    25  import org.mortbay.http.SocketListener;
    26  import org.mortbay.jetty.Server;
    27  import org.mortbay.jetty.servlet.ServletHandler;
    28
    29  import javax.jms.JMSException;
    30  import java.net.URI;
    31
    32  /***
    33   * @version $Revision: 1.2 $
    34   */
    35  public class HttpTransportConnector extends TransportServerChannelSupport {
    36      private URI bindAddress;
    37      private TextWireFormat wireFormat;
    38      private Server server = new Server();
    39      private SocketListener listener = new SocketListener();
    40
    41      public HttpTransportConnector(URI uri) {
    42          super(uri);
    43          this.bindAddress = uri;
    44      }
    45
    46      public void start() throws JMSException {
    47          try {
    48              listener.setPort(bindAddress.getPort());
    49              server.addListener(listener);
    50
    51              HttpContext context = server.addContext("/");
    52              ServletHandler handler = new ServletHandler();
    53              handler.addServlet("httpTunnel", "/*", HttpTunnelServlet.class.getName());
    54
    55              context.addHandler(handler);
    56              context.setAttribute("transportChannelListener", getTransportChannelListener());
    57              context.setAttribute("wireFormat", getWireFormat());
    58              server.start();
    59          }
    60          catch (Exception e) {
    61              throw JMSExceptionHelper.newJMSException("Could not start HTTP server: " + e, e);
    62          }
    63      }
    64
    65      public synchronized void stop() throws JMSException {
    66          super.stop();
    67          try {
    68              server.stop();
    69          }
    70          catch (InterruptedException e) {
    71              throw JMSExceptionHelper.newJMSException("Could not stop HTTP server: " + e, e);
    72          }
    73      }
    74
    75      // Properties
    76      //-------------------------------------------------------------------------
    77      public TextWireFormat getWireFormat() {
    78          if (wireFormat == null) {
    79              wireFormat = createWireFormat();
    80          }
    81          return wireFormat;
    82      }
    83
    84      public void setWireFormat(TextWireFormat wireFormat) {
    85          this.wireFormat = wireFormat;
    86      }
    87
    88
    89      // Implementation methods
    90      //-------------------------------------------------------------------------
    91      protected TextWireFormat createWireFormat() {
    92          return new XStreamWireFormat();
    93      }
    94  }