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: */package org.apache.geronimo.jetty6.connector;
017:
018: import java.net.InetAddress;
019: import java.net.InetSocketAddress;
020: import java.net.UnknownHostException;
021:
022: import javax.management.j2ee.statistics.Stats;
023:
024: import org.apache.geronimo.gbean.GBeanInfo;
025: import org.apache.geronimo.gbean.GBeanInfoBuilder;
026: import org.apache.geronimo.gbean.GBeanLifecycle;
027: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
028: import org.apache.geronimo.jetty6.JettyContainer;
029: import org.apache.geronimo.jetty6.JettyWebConnector;
030: import org.apache.geronimo.management.LazyStatisticsProvider;
031: import org.apache.geronimo.management.geronimo.stats.JettyWebConnectorStatsImpl;
032: import org.apache.geronimo.system.threads.ThreadPool;
033: import org.mortbay.jetty.AbstractConnector;
034:
035: /**
036: * Base class for GBeans for Jetty network connectors (HTTP, HTTPS, AJP, etc.).
037: *
038: * @version $Rev: 605403 $ $Date: 2007-12-18 19:27:20 -0800 (Tue, 18 Dec 2007) $
039: */
040: public abstract class JettyConnector implements GBeanLifecycle,
041: JettyWebConnector, LazyStatisticsProvider {
042: public final static String CONNECTOR_CONTAINER_REFERENCE = "JettyContainer";
043: private final JettyContainer container;
044: protected final AbstractConnector listener;
045: private JettyWebConnectorStatsImpl stats; // data structure for jsr77 stats
046: private String connectHost;
047:
048: /**
049: * Only used to allow declaration as a reference.
050: */
051: public JettyConnector() {
052: container = null;
053: listener = null;
054: }
055:
056: public JettyConnector(JettyContainer container,
057: ThreadPool threadPool) {
058: this .container = container;
059: this .listener = null;
060: }
061:
062: public JettyConnector(JettyContainer container,
063: AbstractConnector listener, ThreadPool threadPool,
064: String name) {
065: this .container = container;
066: this .listener = listener;
067: if (threadPool != null) {
068: JettyThreadPool jettyThreadPool = new JettyThreadPool(
069: threadPool, name);
070: listener.setThreadPool(jettyThreadPool);
071: }
072: stats = new JettyWebConnectorStatsImpl();
073: }
074:
075: //TODO: support the jetty6 specific methods
076: public String getHost() {
077: return listener.getHost();
078: }
079:
080: public void setHost(String host) throws UnknownHostException {
081: // underlying impl treats null as 0.0.0.0
082: listener.setHost(host);
083: }
084:
085: public int getPort() {
086: return listener.getPort();
087: }
088:
089: public void setPort(int port) {
090: listener.setPort(port);
091: }
092:
093: public int getHeaderBufferSizeBytes() {
094: return listener.getHeaderBufferSize();
095: }
096:
097: public void setHeaderBufferSizeBytes(int size) {
098: listener.setHeaderBufferSize(size);
099: }
100:
101: public abstract int getDefaultPort();
102:
103: public String getDefaultScheme() {
104: return null;
105: }
106:
107: public String getConnectUrl() {
108: if (connectHost == null) {
109: String host = getHost();
110: if (host == null || host.equals("0.0.0.0")) {
111: InetAddress address = null;
112: try {
113: address = InetAddress.getLocalHost();
114: } catch (UnknownHostException e) {
115: host = "unknown-host";
116: }
117: if (address != null) {
118: host = address.getHostName();
119: if (host == null || host.equals("")) {
120: host = address.getHostAddress();
121: }
122: }
123: }
124: connectHost = host;
125: }
126: return getProtocol().toLowerCase()
127: + "://"
128: + connectHost
129: + (getPort() == getDefaultPort() ? "" : ":" + getPort());
130: }
131:
132: public int getMaxIdleTimeMs() {
133: return listener.getMaxIdleTime();
134: }
135:
136: public void setMaxIdleTimeMs(int idleTime) {
137: listener.setMaxIdleTime(idleTime);
138: }
139:
140: public int getBufferSizeBytes() {
141: //TODO return the request buffer size, what about the response and header buffer size?
142: return listener.getRequestBufferSize();
143: }
144:
145: public void setBufferSizeBytes(int bytes) {
146: //TODO what about the response and header buffer size?
147: listener.setRequestBufferSize(bytes);
148: }
149:
150: public int getAcceptQueueSize() {
151: return listener.getAcceptQueueSize();
152: }
153:
154: public void setAcceptQueueSize(int size) {
155: listener.setAcceptQueueSize(size);
156: }
157:
158: public int getLingerMillis() {
159: return (int) ((AbstractConnector) listener).getSoLingerTime();
160: }
161:
162: public void setLingerMillis(int millis) {
163: listener.setSoLingerTime(millis);
164: }
165:
166: public boolean isTcpNoDelay() {
167: return true;
168: }
169:
170: public void setTcpNoDelay(boolean enable) {
171: throw new UnsupportedOperationException(
172: listener == null ? "No Listener" : listener.getClass()
173: .getName());
174: }
175:
176: public void setMaxThreads(int maxThreads) {
177: //TODO: in jetty6 connectors have a number of acceptor threads
178: listener.setAcceptors(maxThreads);
179: }
180:
181: public int getMaxThreads() {
182: //TODO: confirm that this is reasonable
183: return listener.getAcceptors();
184: }
185:
186: public int getRedirectPort() {
187: return listener.getConfidentialPort();
188: }
189:
190: public InetSocketAddress getListenAddress() {
191: try {
192: return new InetSocketAddress(InetAddress.getByName(listener
193: .getHost()), listener.getPort());
194: } catch (UnknownHostException e) {
195: throw new IllegalStateException(
196: "InetSocketAddress cannot be determined for host="
197: + listener.getHost(), e);
198: }
199: }
200:
201: public void setRedirectPort(int port) {
202: throw new UnsupportedOperationException("No redirect port on "
203: + this .getClass().getName());
204: }
205:
206: public abstract String getProtocol();
207:
208: public void doStart() throws Exception {
209: container.addListener(listener);
210: listener.start();
211: }
212:
213: public void doStop() {
214: while (true) {
215: try {
216: listener.stop();
217: container.removeListener(listener);
218: return;
219: } catch (Exception e) {
220: continue;
221: }
222: }
223: }
224:
225: public void doFail() {
226: while (true) {
227: try {
228: listener.stop();
229: container.removeListener(listener);
230: return;
231: } catch (Exception e) {
232: continue;
233: }
234: }
235: }
236:
237: public boolean isStatsOn() {
238: return listener.getStatsOn();
239: }
240:
241: public void setStatsOn(boolean on) {
242: listener.setStatsOn(on);
243: if (on)
244: stats.setStartTime();
245: }
246:
247: /**
248: * Gets the statistics collected for this class.
249: * The first call to this method initializes the startTime for
250: * all statistics.
251: *
252: * @return gets collected for this class
253: */
254: public Stats getStats() {
255: if (isStatsOn()) {
256: stats.setLastSampleTime();
257: // connections open
258: stats.getOpenConnectionCountImpl().setCurrent(
259: listener.getConnectionsOpen());
260: stats.getOpenConnectionCountImpl().setHighWaterMark(
261: listener.getConnectionsOpenMax());
262: stats.getOpenConnectionCountImpl().setLowWaterMark(
263: listener.getConnectionsOpenMin());
264: // request count
265: stats.getRequestCountImpl()
266: .setCount(listener.getRequests());
267: // connections count and durations
268: stats.getConnectionsDurationImpl().setCount(
269: listener.getConnections());
270: stats.getConnectionsDurationImpl().setMaxTime(
271: listener.getConnectionsDurationMax());
272: stats.getConnectionsDurationImpl().setMinTime(
273: listener.getConnectionsDurationMin());
274: stats.getConnectionsDurationImpl().setTotalTime(
275: listener.getConnectionsDurationTotal());
276: // requests per connection (connection requests)
277: stats.getConnectionsRequestImpl().setCurrent(
278: listener.getConnectionsRequestsAve());
279: stats.getConnectionsRequestImpl().setHighWaterMark(
280: listener.getConnectionsRequestsMax());
281: stats.getConnectionsRequestImpl().setLowWaterMark(
282: listener.getConnectionsRequestsMin());
283: }
284: return stats;
285: }
286:
287: /**
288: * Reset the startTime for all statistics
289: */
290: public void resetStats() {
291: listener.statsReset();
292: stats.setStartTime(); // sets atartTime for all stats to Now
293: }
294:
295: public boolean isStateManageable() {
296: return true;
297: }
298:
299: public boolean isStatisticsProvider() {
300: return true;
301: }
302:
303: public static final GBeanInfo GBEAN_INFO;
304:
305: static {
306: GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(
307: "Jetty HTTP Connector", JettyConnector.class);
308: infoFactory.addReference(CONNECTOR_CONTAINER_REFERENCE,
309: JettyContainer.class, NameFactory.GERONIMO_SERVICE);
310: infoFactory.addReference("ThreadPool", ThreadPool.class,
311: NameFactory.GERONIMO_SERVICE);
312: // this is needed because the getters/setters are not added automatically
313: infoFactory.addOperation("setStatsOn",
314: new Class[] { boolean.class }, "void");
315: // removed 'minThreads' from persistent and manageable String[]
316: // removed 'tcpNoDelay' from persistent String[]
317: // added 'protocol' to persistent and manageable String[]
318: infoFactory.addInterface(JettyWebConnector.class, new String[] {
319: "host", "port", "minThreads", "maxThreads",
320: "bufferSizeBytes", "headerBufferSizeBytes",
321: "acceptQueueSize", "lingerMillis", "protocol",
322: "redirectPort", "connectUrl", "maxIdleTimeMs" },
323: new String[] { "host", "port", "minThreads",
324: "maxThreads", "bufferSizeBytes",
325: "headerBufferSizeBytes", "acceptQueueSize",
326: "lingerMillis", "protocol", "redirectPort" });
327: infoFactory.setConstructor(new String[] { "JettyContainer",
328: "ThreadPool" });
329: GBEAN_INFO = infoFactory.getBeanInfo();
330: }
331: }
|