KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jetty6 > connector > JettyConnector


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
18 package org.apache.geronimo.jetty6.connector;
19
20 import java.net.InetAddress JavaDoc;
21 import java.net.InetSocketAddress JavaDoc;
22 import java.net.UnknownHostException JavaDoc;
23
24 import org.apache.geronimo.gbean.GBeanInfo;
25 import org.apache.geronimo.gbean.GBeanInfoBuilder;
26 import org.apache.geronimo.gbean.GBeanLifecycle;
27 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
28 import org.apache.geronimo.jetty6.JettyContainer;
29 import org.apache.geronimo.jetty6.JettyWebConnector;
30 import org.mortbay.jetty.AbstractConnector;
31 import org.mortbay.jetty.Connector;
32 import org.mortbay.jetty.bio.SocketConnector;
33 import org.mortbay.jetty.nio.SelectChannelConnector;
34
35 /**
36  * Base class for GBeans for Jetty network connectors (HTTP, HTTPS, AJP, etc.).
37  *
38  * @version $Rev: 482336 $ $Date: 2006-12-04 15:12:19 -0500 (Mon, 04 Dec 2006) $
39  */

40 public abstract class JettyConnector implements GBeanLifecycle, JettyWebConnector {
41     public final static String JavaDoc CONNECTOR_CONTAINER_REFERENCE = "JettyContainer";
42     private final JettyContainer container;
43     protected final Connector listener;
44     private String JavaDoc connectHost;
45
46     /**
47      * Only used to allow declaration as a reference.
48      */

49     public JettyConnector() {
50         container = null;
51         listener = null;
52     }
53
54     public JettyConnector(JettyContainer container) {
55         this.container = container;
56         this.listener = null;
57     }
58
59     public JettyConnector(JettyContainer container, Connector listener) {
60         this.container = container;
61         this.listener = listener;
62     }
63
64     //TODO: support the jetty6 specific methods
65
public String JavaDoc getHost() {
66         return listener.getHost();
67     }
68
69     public void setHost(String JavaDoc host) throws UnknownHostException JavaDoc {
70         // underlying impl treats null as 0.0.0.0
71
listener.setHost(host);
72     }
73
74     public int getPort() {
75         return listener.getPort();
76     }
77
78     public void setPort(int port) {
79         listener.setPort(port);
80     }
81
82     public abstract int getDefaultPort();
83
84     public String JavaDoc getDefaultScheme() {
85         return null;
86     }
87
88     public String JavaDoc getConnectUrl() {
89         if (connectHost == null) {
90             String JavaDoc host = getHost();
91             if (host == null || host.equals("0.0.0.0")) {
92                 InetAddress JavaDoc address = null;
93                 try {
94                     address = InetAddress.getLocalHost();
95                 } catch (UnknownHostException JavaDoc e) {
96                     host = "unknown-host";
97                 }
98                 if (address != null) {
99                     host = address.getHostName();
100                     if (host == null || host.equals("")) {
101                         host = address.getHostAddress();
102                     }
103                 }
104             }
105             connectHost = host;
106         }
107         return getProtocol().toLowerCase() + "://" + connectHost + (getPort() == getDefaultPort() ? "" : ":" + getPort());
108     }
109
110     public int getMaxIdleTimeMs() {
111         return ((AbstractConnector) listener).getMaxIdleTime();
112     }
113
114     public void setMaxIdleTimeMs(int idleTime) {
115         ((AbstractConnector) listener).setMaxIdleTime(idleTime);
116     }
117
118     public int getBufferSizeBytes() {
119         //TODO return the request buffer size, what about the response and header buffer size?
120
return listener.getRequestBufferSize();
121     }
122
123     public void setBufferSizeBytes(int bytes) {
124         //TODO what about the response and header buffer size?
125
listener.setRequestBufferSize(bytes);
126     }
127
128     public int getAcceptQueueSize() {
129         return ((AbstractConnector) listener).getAcceptQueueSize();
130     }
131
132     public void setAcceptQueueSize(int size) {
133         ((AbstractConnector) listener).setAcceptQueueSize(size);
134     }
135
136     public int getLingerMillis() {
137         return (int) ((AbstractConnector) listener).getSoLingerTime();
138     }
139
140     public void setLingerMillis(int millis) {
141         ((AbstractConnector) listener).setSoLingerTime(millis);
142     }
143
144     public boolean isTcpNoDelay() {
145         return true;
146     }
147
148     public void setTcpNoDelay(boolean enable) {
149         throw new UnsupportedOperationException JavaDoc(listener == null ? "No Listener" : listener.getClass().getName());
150     }
151
152     public void setMaxThreads(int maxThreads) {
153         //TODO: in jetty6 connectors have a number of acceptor threads
154
((AbstractConnector) listener).setAcceptors(maxThreads);
155     }
156
157     public int getMaxThreads() {
158         //TODO: confirm that this is reasonable
159
return ((AbstractConnector) listener).getAcceptors();
160     }
161
162     public int getRedirectPort() {
163         return listener.getConfidentialPort();
164     }
165
166     public InetSocketAddress JavaDoc getListenAddress() {
167         try {
168             return new InetSocketAddress JavaDoc(InetAddress.getByName(listener.getHost()), listener.getPort());
169         } catch (UnknownHostException JavaDoc e) {
170             throw new IllegalStateException JavaDoc("InetSocketAddress cannot be determined for host=" + listener.getHost());
171         }
172     }
173
174
175     public void setRedirectPort(int port) {
176         if (listener instanceof SocketConnector) {
177             SocketConnector socketListener = (SocketConnector) listener;
178             socketListener.setConfidentialPort(port);
179             socketListener.setIntegralPort(port);
180             socketListener.setIntegralScheme("https");
181             socketListener.setConfidentialScheme("https");
182         } else if (listener instanceof SelectChannelConnector) {
183             SelectChannelConnector connector = (SelectChannelConnector) listener;
184             connector.setConfidentialPort(port);
185             connector.setIntegralPort(port);
186             connector.setIntegralScheme("https");
187             connector.setConfidentialScheme("https");
188         }
189         /*
190                  * don't have one of these yet
191                 else if(listener instanceof AJP13Listener) {
192             AJP13Listener ajpListener = (AJP13Listener) listener;
193             ajpListener.setConfidentialPort(port);
194             ajpListener.setIntegralPort(port);
195             ajpListener.setIntegralScheme("https");
196             ajpListener.setConfidentialScheme("https");
197         */

198         else {
199             throw new UnsupportedOperationException JavaDoc(listener == null ? "No Listener" : listener.getClass().getName()); //todo: can this happen?
200
}
201     }
202
203     public abstract String JavaDoc getProtocol();
204
205     public void doStart() throws Exception JavaDoc {
206         container.addListener(listener);
207         listener.start();
208     }
209
210     public void doStop() {
211         while (true) {
212             try {
213                 listener.stop();
214                 container.removeListener(listener);
215                 return;
216             } catch (Exception JavaDoc e) {
217                 continue;
218             }
219         }
220     }
221
222     public void doFail() {
223         while (true) {
224             try {
225                 listener.stop();
226                 container.removeListener(listener);
227                 return;
228             } catch (Exception JavaDoc e) {
229                 continue;
230             }
231         }
232     }
233
234     public static final GBeanInfo GBEAN_INFO;
235
236     static {
237         GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Jetty HTTP Connector", JettyConnector.class);
238         infoFactory.addReference(CONNECTOR_CONTAINER_REFERENCE, JettyContainer.class, NameFactory.GERONIMO_SERVICE);
239         infoFactory.addInterface(JettyWebConnector.class, new String JavaDoc[]{"host", "port", "minThreads", "maxThreads", "bufferSizeBytes", "acceptQueueSize", "lingerMillis", "tcpNoDelay", "redirectPort", "connectUrl", "maxIdleTimeMs"},
240                 new String JavaDoc[]{"host", "port", "redirectPort", "maxThreads", "minThreads"});
241         infoFactory.setConstructor(new String JavaDoc[]{"JettyContainer"});
242         GBEAN_INFO = infoFactory.getBeanInfo();
243     }
244 }
245
Popular Tags