KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > http > HttpConnector


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 package org.apache.servicemix.components.http;
18
19 import javax.jbi.JBIException;
20 import javax.jbi.component.ComponentContext;
21
22 import org.mortbay.jetty.Connector;
23 import org.mortbay.jetty.Server;
24 import org.mortbay.jetty.bio.SocketConnector;
25 import org.mortbay.jetty.handler.ContextHandler;
26 import org.mortbay.jetty.servlet.ServletHandler;
27 import org.mortbay.jetty.servlet.ServletHolder;
28 import org.mortbay.jetty.servlet.ServletMapping;
29 import org.mortbay.thread.BoundedThreadPool;
30
31 /**
32  * An embedded Servlet engine to implement a HTTP connector
33  *
34  * @version $Revision: 426415 $
35  */

36 public class HttpConnector extends HttpInOutBinding {
37     private Connector listener = new SocketConnector();
38     
39     /**
40      * The maximum number of threads for the Jetty SocketListener. It's set
41      * to 256 by default to match the default value in Jetty.
42      */

43     private int maxThreads = 256;
44
45     private Server server;
46     private String JavaDoc host;
47     private int port;
48
49     /**
50      * Constructor
51      *
52      * @param host
53      * @param port
54      */

55     public HttpConnector(String JavaDoc host, int port) {
56         this.host = host;
57         this.port = port;
58     }
59
60     public HttpConnector() {
61     }
62
63     /**
64      * Constructor
65      *
66      * @param listener
67      */

68     public HttpConnector(Connector listener) {
69         this.listener = listener;
70     }
71
72     /**
73      * Called when the Component is initialized
74      *
75      * @param cc
76      * @throws JBIException
77      */

78     public void init(ComponentContext cc) throws JBIException {
79         super.init(cc);
80         //should set all ports etc here - from the naming context I guess ?
81
if (listener == null) {
82             listener = new SocketConnector();
83         }
84         listener.setHost(host);
85         listener.setPort(port);
86         server = new Server();
87         BoundedThreadPool btp = new BoundedThreadPool();
88         btp.setMaxThreads(getMaxThreads());
89         server.setThreadPool(btp);
90     }
91     
92     /**
93      * start the Component
94      *
95      * @throws JBIException
96      */

97     public void start() throws JBIException {
98         server.setConnectors(new Connector[] { listener });
99         ContextHandler context = new ContextHandler();
100         context.setContextPath("/");
101         ServletHolder holder = new ServletHolder();
102         holder.setName("jbiServlet");
103         holder.setClassName(BindingServlet.class.getName());
104         ServletHandler handler = new ServletHandler();
105         handler.setServlets(new ServletHolder[] { holder });
106         ServletMapping mapping = new ServletMapping();
107         mapping.setServletName("jbiServlet");
108         mapping.setPathSpec("/*");
109         handler.setServletMappings(new ServletMapping[] { mapping });
110         context.setHandler(handler);
111         server.setHandler(context);
112         context.setAttribute("binding", this);
113         try {
114             server.start();
115         }
116         catch (Exception JavaDoc e) {
117             throw new JBIException("Start failed: " + e, e);
118         }
119     }
120
121     /**
122      * stop
123      */

124     public void stop() throws JBIException {
125         try {
126             if (server != null) {
127                 server.stop();
128             }
129         }
130         catch (Exception JavaDoc e) {
131             throw new JBIException("Stop failed: " + e, e);
132         }
133     }
134
135     /**
136      * shutdown
137      */

138     public void shutDown() throws JBIException {
139         super.shutDown();
140         server = null;
141     }
142
143
144     // Properties
145
//-------------------------------------------------------------------------
146
public int getPort() {
147         return port;
148     }
149
150     public void setPort(int port) {
151         this.port = port;
152     }
153
154     public Server getServer() {
155         return server;
156     }
157
158     public void setServer(Server server) {
159         this.server = server;
160     }
161
162     public String JavaDoc getHost() {
163         return host;
164     }
165
166     public void setHost(String JavaDoc host) {
167         this.host = host;
168     }
169
170     public int getMaxThreads() {
171         return maxThreads;
172     }
173
174     public void setMaxThreads(int maxThreads) {
175         this.maxThreads = maxThreads;
176     }
177 }
178
Popular Tags