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
19 package org.apache.geronimo.jetty.connector;
20
21 import java.net.InetAddress;
22 import java.net.InetSocketAddress;
23 import java.net.UnknownHostException;
24
25 import org.apache.geronimo.gbean.GBeanInfo;
26 import org.apache.geronimo.gbean.GBeanInfoBuilder;
27 import org.apache.geronimo.gbean.GBeanLifecycle;
28 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
29 import org.apache.geronimo.jetty.JettyContainer;
30 import org.apache.geronimo.jetty.JettyWebConnector;
31 import org.mortbay.http.HttpListener;
32 import org.mortbay.http.SocketListener;
33 import org.mortbay.http.ajp.AJP13Listener;
34 import org.mortbay.util.ThreadedServer;
35
36 /**
37 * Base class for GBeans for Jetty network connectors (HTTP, HTTPS, AJP, etc.).
38 *
39 * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
40 */
41 public abstract class JettyConnector implements GBeanLifecycle, JettyWebConnector {
42 public final static String CONNECTOR_CONTAINER_REFERENCE = "JettyContainer";
43 private final JettyContainer container;
44 protected final HttpListener listener;
45 private String connectHost;
46
47 /**
48 * Only used to allow declaration as a reference.
49 */
50 public JettyConnector() {
51 container = null;
52 listener = null;
53 }
54
55 public JettyConnector(JettyContainer container) {
56 this.container = container;
57 this.listener = null;
58 }
59
60 public JettyConnector(JettyContainer container, HttpListener listener) {
61 this.container = container;
62 this.listener = listener;
63 }
64
65 public String getDefaultScheme() {
66 return listener.getDefaultScheme();
67 }
68
69 public String getHost() {
70 return listener.getHost();
71 }
72
73 public void setHost(String host) throws UnknownHostException {
74
75 listener.setHost(host);
76 }
77
78 public int getPort() {
79 return listener.getPort();
80 }
81
82 public void setPort(int port) {
83 listener.setPort(port);
84 }
85
86 public abstract int getDefaultPort();
87
88 public String getConnectUrl() {
89 if(connectHost == null) {
90 String host = getHost();
91 if(host == null || host.equals("0.0.0.0")) {
92 InetAddress address = null;
93 try {
94 address = InetAddress.getLocalHost();
95 } catch (UnknownHostException e) {
96 host = "unknown-host";
97 }
98 if(address != null) {
99 host = address.getCanonicalHostName();
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 void setMinThreads(int minThreads) {
111 ((ThreadedServer)listener).setMinThreads(minThreads);
112 }
113
114 public int getMinThreads() {
115 return ((ThreadedServer)listener).getMinThreads();
116 }
117
118
119 public void setMaxThreads(int maxThreads) {
120 ((ThreadedServer)listener).setMaxThreads(maxThreads);
121 }
122
123 public int getMaxThreads() {
124 return ((ThreadedServer)listener).getMaxThreads();
125 }
126
127 public void setMaxIdleTimeMs(int max) {
128 ((ThreadedServer)listener).setMaxIdleTimeMs(max);
129 }
130
131 public int getMaxIdleTimeMs() {
132 return ((ThreadedServer)listener).getMaxIdleTimeMs();
133 }
134
135 public void setLowThreadsMaxIdleTimeMs(int max) {
136 if (listener instanceof SocketListener)
137 ((SocketListener)listener).setLowResourcePersistTimeMs(max);
138 }
139
140 public int getLowThreadsMaxIdleTimeMs() {
141 if (listener instanceof SocketListener)
142 return ((SocketListener)listener).getLowResourcePersistTimeMs();
143 return -1;
144 }
145
146 public void setLowThreads(int lowThreads) {
147 if (listener instanceof SocketListener)
148 ((SocketListener)listener).setLowResources(lowThreads);
149 }
150
151 public int getLowThreads() {
152 if (listener instanceof SocketListener)
153 return ((SocketListener)listener).getLowResources();
154 return -1;
155 }
156
157 public int getThreads() {
158 return ((ThreadedServer)listener).getThreads();
159 }
160
161 public int getIdlethreads() {
162 return ((ThreadedServer)listener).getIdleThreads();
163 }
164
165 public InetSocketAddress getListenAddress() {
166 return new InetSocketAddress(getHost(), getPort());
167 }
168
169 public int getBufferSizeBytes() {
170 return listener.getBufferSize();
171 }
172
173 public void setBufferSizeBytes(int bytes) {
174 if(listener instanceof SocketListener) {
175 SocketListener socketListener = (SocketListener)listener;
176 socketListener.setBufferSize(bytes);
177 } else if(listener instanceof AJP13Listener) {
178 ((AJP13Listener)listener).setBufferSize(bytes);
179 } else {
180 throw new UnsupportedOperationException(listener == null ? "No Listener" : listener.getClass().getName());
181 }
182 }
183
184 public int getAcceptQueueSize() {
185 return ((ThreadedServer)listener).getAcceptQueueSize();
186 }
187
188 public void setAcceptQueueSize(int size) {
189 ((ThreadedServer)listener).setAcceptQueueSize(size);
190 }
191
192 public int getLingerMillis() {
193 return ((ThreadedServer)listener).getLingerTimeSecs()*1000;
194 }
195
196 public void setLingerMillis(int millis) {
197 ((ThreadedServer)listener).setLingerTimeSecs(Math.round((float)millis/1000f));
198 }
199
200 public boolean isTcpNoDelay() {
201 return ((ThreadedServer)listener).getTcpNoDelay();
202 }
203
204 public void setTcpNoDelay(boolean enable) {
205 ((ThreadedServer)listener).setTcpNoDelay(enable);
206 }
207
208 public int getRedirectPort() {
209 return listener.getConfidentialPort();
210 }
211
212 public void setRedirectPort(int port) {
213 if(listener instanceof SocketListener) {
214 SocketListener socketListener = (SocketListener)listener;
215 socketListener.setConfidentialPort(port);
216 socketListener.setIntegralPort(port);
217 socketListener.setIntegralScheme("https");
218 socketListener.setConfidentialScheme("https");
219 } else if(listener instanceof AJP13Listener) {
220 AJP13Listener ajpListener = (AJP13Listener) listener;
221 ajpListener.setConfidentialPort(port);
222 ajpListener.setIntegralPort(port);
223 ajpListener.setIntegralScheme("https");
224 ajpListener.setConfidentialScheme("https");
225 } else {
226 throw new UnsupportedOperationException(listener == null ? "No Listener" : listener.getClass().getName());
227 }
228 }
229
230 public abstract String getProtocol();
231
232 public void doStart() throws Exception {
233 container.addListener(listener);
234 ((ThreadedServer) listener).open();
235 listener.start();
236 }
237
238 public void doStop() {
239 while (true) {
240 try {
241 listener.stop();
242 container.removeListener(listener);
243 return;
244 } catch (InterruptedException e) {
245 continue;
246 }
247 }
248 }
249
250 public void doFail() {
251 while (true) {
252 try {
253 listener.stop();
254 container.removeListener(listener);
255 return;
256 } catch (InterruptedException e) {
257 continue;
258 }
259 }
260 }
261
262 public static final GBeanInfo GBEAN_INFO;
263
264 static {
265 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Jetty HTTP Connector", JettyConnector.class);
266 infoFactory.addReference(CONNECTOR_CONTAINER_REFERENCE, JettyContainer.class, NameFactory.GERONIMO_SERVICE);
267 infoFactory.addInterface(JettyWebConnector.class, new String[]{"host", "port", "minThreads","maxThreads","bufferSizeBytes","acceptQueueSize","lingerMillis","tcpNoDelay","redirectPort","connectUrl","maxIdleTimeMs","lowThreads","lowThreadsMaxIdleTimeMs",},
268 new String[]{"host", "port", "redirectPort", "maxThreads", "minThreads"});
269 infoFactory.setConstructor(new String[] {"JettyContainer"});
270 GBEAN_INFO = infoFactory.getBeanInfo();
271 }
272 }