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.activemq.transport.http; 018 019 import java.net.InetSocketAddress; 020 import java.net.URI; 021 022 import org.apache.activemq.command.BrokerInfo; 023 import org.apache.activemq.transport.TransportServerSupport; 024 import org.apache.activemq.transport.util.TextWireFormat; 025 import org.apache.activemq.transport.xstream.XStreamWireFormat; 026 import org.apache.activemq.util.ServiceStopper; 027 import org.mortbay.jetty.Connector; 028 import org.mortbay.jetty.Server; 029 import org.mortbay.jetty.bio.SocketConnector; 030 import org.mortbay.jetty.handler.ContextHandler; 031 import org.mortbay.jetty.servlet.ServletHandler; 032 import org.mortbay.jetty.servlet.ServletHolder; 033 import org.mortbay.jetty.servlet.ServletMapping; 034 import org.mortbay.jetty.servlet.SessionHandler; 035 036 /** 037 * @version $Revision$ 038 */ 039 public class HttpTransportServer extends TransportServerSupport { 040 private URI bindAddress; 041 private TextWireFormat wireFormat; 042 private Server server; 043 private Connector connector; 044 045 public HttpTransportServer(URI uri) { 046 super(uri); 047 this.bindAddress = uri; 048 } 049 050 public void setBrokerInfo(BrokerInfo brokerInfo) { 051 } 052 053 // Properties 054 // ------------------------------------------------------------------------- 055 public TextWireFormat getWireFormat() { 056 if (wireFormat == null) { 057 wireFormat = createWireFormat(); 058 } 059 return wireFormat; 060 } 061 062 public void setWireFormat(TextWireFormat wireFormat) { 063 this.wireFormat = wireFormat; 064 } 065 066 // Implementation methods 067 // ------------------------------------------------------------------------- 068 protected TextWireFormat createWireFormat() { 069 return new XStreamWireFormat(); 070 } 071 072 protected void setConnector(Connector connector) { 073 this.connector = connector; 074 } 075 076 protected void doStart() throws Exception { 077 server = new Server(); 078 if (connector == null) { 079 connector = new SocketConnector(); 080 } 081 connector.setHost(bindAddress.getHost()); 082 connector.setPort(bindAddress.getPort()); 083 connector.setServer(server); 084 server.setConnectors(new Connector[] { 085 connector 086 }); 087 088 ContextHandler contextHandler = new ContextHandler(); 089 contextHandler.setContextPath("/"); 090 contextHandler.setServer(server); 091 server.setHandler(contextHandler); 092 093 SessionHandler sessionHandler = new SessionHandler(); 094 contextHandler.setHandler(sessionHandler); 095 096 ServletHandler servletHandler = new ServletHandler(); 097 sessionHandler.setHandler(servletHandler); 098 099 ServletHolder holder = new ServletHolder(); 100 holder.setName("httpTunnel"); 101 holder.setClassName(HttpTunnelServlet.class.getName()); 102 servletHandler.setServlets(new ServletHolder[] { 103 holder 104 }); 105 106 ServletMapping mapping = new ServletMapping(); 107 mapping.setServletName("httpTunnel"); 108 mapping.setPathSpec("/*"); 109 servletHandler.setServletMappings(new ServletMapping[] { 110 mapping 111 }); 112 113 contextHandler.setAttribute("acceptListener", getAcceptListener()); 114 contextHandler.setAttribute("wireFormat", getWireFormat()); 115 server.start(); 116 } 117 118 protected void doStop(ServiceStopper stopper) throws Exception { 119 Server temp = server; 120 server = null; 121 if (temp != null) { 122 temp.stop(); 123 } 124 } 125 126 public InetSocketAddress getSocketAddress() { 127 return null; 128 } 129 130 }