KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > http > HttpTransportServer


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.transport.http;
19
20 import org.apache.activemq.command.BrokerInfo;
21 import org.apache.activemq.transport.TransportServerSupport;
22 import org.apache.activemq.transport.util.TextWireFormat;
23 import org.apache.activemq.transport.xstream.XStreamWireFormat;
24 import org.apache.activemq.util.ServiceStopper;
25 import org.mortbay.jetty.Connector;
26 import org.mortbay.jetty.Server;
27 import org.mortbay.jetty.bio.SocketConnector;
28 import org.mortbay.jetty.handler.ContextHandler;
29 import org.mortbay.jetty.servlet.ServletHandler;
30 import org.mortbay.jetty.servlet.ServletHolder;
31 import org.mortbay.jetty.servlet.ServletMapping;
32 import org.mortbay.jetty.servlet.SessionHandler;
33
34 import java.net.InetSocketAddress JavaDoc;
35 import java.net.URI JavaDoc;
36
37 /**
38  * @version $Revision$
39  */

40 public class HttpTransportServer extends TransportServerSupport {
41     private URI JavaDoc bindAddress;
42     private TextWireFormat wireFormat;
43     private Server server;
44     private Connector connector;
45
46     public HttpTransportServer(URI JavaDoc uri) {
47         super(uri);
48         this.bindAddress = uri;
49     }
50
51     public void setBrokerInfo(BrokerInfo brokerInfo) {
52     }
53
54     // Properties
55
// -------------------------------------------------------------------------
56
public TextWireFormat getWireFormat() {
57         if (wireFormat == null) {
58             wireFormat = createWireFormat();
59         }
60         return wireFormat;
61     }
62
63     public void setWireFormat(TextWireFormat wireFormat) {
64         this.wireFormat = wireFormat;
65     }
66
67     // Implementation methods
68
// -------------------------------------------------------------------------
69
protected TextWireFormat createWireFormat() {
70         return new XStreamWireFormat();
71     }
72
73     protected void setConnector(Connector connector) {
74         this.connector = connector;
75     }
76     
77     protected void doStart() throws Exception JavaDoc {
78         server = new Server();
79         if (connector==null)
80             connector = new SocketConnector();
81         connector.setHost(bindAddress.getHost());
82         connector.setPort(bindAddress.getPort());
83         connector.setServer(server);
84         server.setConnectors(new Connector[] { connector });
85
86         ContextHandler context_handler = new ContextHandler();
87         context_handler.setContextPath("/");
88         context_handler.setServer(server);
89         server.setHandler(context_handler);
90
91         SessionHandler session_handler = new SessionHandler();
92         context_handler.setHandler(session_handler);
93         
94         ServletHandler servlet_handler = new ServletHandler();
95         session_handler.setHandler(servlet_handler);
96
97         ServletHolder holder = new ServletHolder();
98         holder.setName("httpTunnel");
99         holder.setClassName(HttpTunnelServlet.class.getName());
100         servlet_handler.setServlets(new ServletHolder[] { holder });
101
102         ServletMapping mapping = new ServletMapping();
103         mapping.setServletName("httpTunnel");
104         mapping.setPathSpec("/*");
105         servlet_handler.setServletMappings(new ServletMapping[] { mapping });
106
107         context_handler.setAttribute("acceptListener", getAcceptListener());
108         context_handler.setAttribute("wireFormat", getWireFormat());
109         server.start();
110     }
111
112     protected void doStop(ServiceStopper stopper) throws Exception JavaDoc {
113         Server temp = server;
114         server = null;
115         if (temp != null) {
116             temp.stop();
117         }
118     }
119
120     public InetSocketAddress JavaDoc getSocketAddress() {
121         return null;
122     }
123
124 }
125
Popular Tags