001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.geronimo.tomcat.connector;
020:
021: import java.net.InetAddress;
022: import java.net.InetSocketAddress;
023: import java.net.UnknownHostException;
024: import java.util.Map;
025:
026: import javax.management.j2ee.statistics.Stats;
027: import javax.net.ssl.KeyManagerFactory;
028:
029: import org.apache.geronimo.gbean.GBeanInfo;
030: import org.apache.geronimo.gbean.GBeanInfoBuilder;
031: import org.apache.geronimo.management.StatisticsProvider;
032: import org.apache.geronimo.system.serverinfo.ServerInfo;
033: import org.apache.geronimo.tomcat.TomcatContainer;
034: import org.apache.geronimo.tomcat.stats.ConnectorStats;
035:
036: public abstract class BaseHttp11ConnectorGBean extends ConnectorGBean
037: implements BaseHttp11Protocol, StatisticsProvider {
038:
039: protected String connectHost;
040:
041: // JSR77 stats
042: private ConnectorStats connStatsProvider = new ConnectorStats();
043:
044: private boolean reset = true;
045:
046: public BaseHttp11ConnectorGBean(String name, Map initParams,
047: String tomcatProtocol, String host, int port,
048: TomcatContainer container, ServerInfo serverInfo)
049: throws Exception {
050: super (name, initParams, tomcatProtocol, container, serverInfo);
051:
052: // Default the host to listen on all address is one was not specified
053: if (host == null) {
054: host = "0.0.0.0";
055: }
056:
057: // Must have a port
058: if (port == 0) {
059: throw new IllegalArgumentException("Must declare a port.");
060: }
061:
062: connector.setAttribute("address", host);
063: connector.setPort(port);
064:
065: }
066:
067: protected void initProtocol() {
068: }
069:
070: public String getConnectUrl() {
071: if (connectHost == null) {
072: String host = getAddress();
073: if (host == null || host.equals("0.0.0.0")
074: || host.equals("0:0:0:0:0:0:0:1")) {
075: InetAddress address = null;
076: try {
077: address = InetAddress.getLocalHost();
078: } catch (UnknownHostException e) {
079: host = "unknown-host";
080: }
081: if (address != null) {
082: host = address.getCanonicalHostName();
083: if (host == null || host.equals("")) {
084: host = address.getHostAddress();
085: }
086: }
087: }
088: // this host address could be in IPv6 format,
089: // which means we need to wrap it in brackets
090: if (host.indexOf(":") >= 0) {
091: host = "[" + host + "]";
092: }
093: connectHost = host;
094: }
095: return getScheme().toLowerCase()
096: + "://"
097: + connectHost
098: + (getPort() == getDefaultPort() ? "" : ":" + getPort());
099: }
100:
101: public abstract int getDefaultPort();
102:
103: // Generic HTTP
104: public int getAcceptCount() {
105: Object value = connector.getAttribute("acceptCount");
106: return value == null ? 10 : Integer.parseInt(value.toString());
107: }
108:
109: public String getAddress() {
110: Object value = connector.getAttribute("address");
111: if (value == null) {
112: return "0.0.0.0";
113: } else if (value instanceof InetAddress) {
114: return ((InetAddress) value).getHostAddress();
115: } else
116: return value.toString();
117: }
118:
119: public int getBufferSize() {
120: Object value = connector.getAttribute("bufferSize");
121: return value == null ? 2048 : Integer
122: .parseInt(value.toString());
123: }
124:
125: public String getCompressableMimeType() {
126: return (String) connector.getAttribute("compressableMimeType");
127: }
128:
129: public String getCompression() {
130: return (String) connector.getAttribute("compression");
131: }
132:
133: public int getConnectionLinger() {
134: Object value = connector.getAttribute("connectionLinger");
135: return value == null ? -1 : Integer.parseInt(value.toString());
136: }
137:
138: public int getConnectionTimeout() {
139: Object value = connector.getAttribute("connectionTimeout");
140: return value == null ? 60000 : Integer.parseInt(value
141: .toString());
142: }
143:
144: public boolean getDisableUploadTimeout() {
145: Object value = connector.getAttribute("disableUploadTimeout");
146: return value == null ? true : new Boolean(value.toString())
147: .booleanValue();
148: }
149:
150: public String getExecutor() {
151: Object value = connector.getAttribute("executor");
152: if (value == null)
153: return null;
154:
155: if (value instanceof String)
156: return (String) value;
157:
158: return (String) value.getClass().getName();
159: }
160:
161: public String getHost() {
162: return getAddress();
163: }
164:
165: public InetSocketAddress getListenAddress() {
166: return new InetSocketAddress(getHost(), getPort());
167: }
168:
169: public int getKeepAliveTimeout() {
170: Object value = connector.getAttribute("keepAliveTimeout");
171: return value == null ? getConnectionTimeout() : Integer
172: .parseInt(value.toString());
173: }
174:
175: public int getMaxHttpHeaderSize() {
176: Object value = connector.getAttribute("maxHttpHeaderSize");
177: return value == null ? 4096 : Integer
178: .parseInt(value.toString());
179: }
180:
181: public int getMaxKeepAliveRequests() {
182: Object value = connector.getAttribute("maxKeepAliveRequests");
183: return value == null ? 100 : Integer.parseInt(value.toString());
184: }
185:
186: public int getMaxThreads() {
187: Object value = connector.getAttribute("maxThreads");
188: return value == null ? 200 : Integer.parseInt(value.toString());
189: }
190:
191: public int getMaxSpareThreads() {
192: Object value = connector.getAttribute("maxSpareThreads");
193: return value == null ? 100 : Integer.parseInt(value.toString());
194: }
195:
196: public int getMinSpareThreads() {
197: Object value = connector.getAttribute("minSpareThreads");
198: return value == null ? 10 : Integer.parseInt(value.toString());
199: }
200:
201: public String getNoCompressionUserAgents() {
202: return (String) connector
203: .getAttribute("noCompressionUserAgents");
204: }
205:
206: public int getPort() {
207: return connector.getPort();
208: }
209:
210: public String getRestrictedUserAgents() {
211: return (String) connector.getAttribute("restrictedUserAgents");
212: }
213:
214: public String getServer() {
215: return (String) connector.getAttribute("server");
216: }
217:
218: public int getSocketBuffer() {
219: Object value = connector.getAttribute("socketBuffer");
220: return value == null ? 9000 : Integer
221: .parseInt(value.toString());
222: }
223:
224: public boolean getTcpNoDelay() {
225: Object value = connector.getAttribute("tcpNoDelay");
226: return value == null ? true : new Boolean(value.toString())
227: .booleanValue();
228: }
229:
230: public int getThreadPriority() {
231: Object value = connector.getAttribute("threadPriority");
232: return value == null ? Thread.NORM_PRIORITY : Integer
233: .parseInt(value.toString());
234: }
235:
236: public void setAcceptCount(int acceptCount) {
237: connector.setAttribute("acceptCount", new Integer(acceptCount));
238: }
239:
240: public void setAddress(String address) {
241: connector.setAttribute("address", address);
242: }
243:
244: public void setBufferSize(int bufferSize) {
245: connector.setAttribute("bufferSize", new Integer(bufferSize));
246: }
247:
248: public void setCompressableMimeType(String compressableMimeType) {
249: connector.setAttribute("compressableMimeType",
250: compressableMimeType);
251: }
252:
253: public void setCompression(String compression) {
254: connector.setAttribute("compression", compression);
255: }
256:
257: public void setConnectionLinger(int connectionLinger) {
258: connector.setAttribute("connectionLinger", new Integer(
259: connectionLinger));
260: }
261:
262: public void setConnectionTimeout(int connectionTimeout) {
263: connector.setAttribute("connectionTimeout", new Integer(
264: connectionTimeout));
265: }
266:
267: public void setDisableUploadTimeout(boolean disableUploadTimeout) {
268: connector.setAttribute("disableUploadTimeout", new Boolean(
269: disableUploadTimeout));
270: }
271:
272: public void setExecutor(String executor) {
273: connector.setAttribute("executor", executor);
274: }
275:
276: public void setHost(String host) {
277: setAddress(host);
278: }
279:
280: public void setKeepAliveTimeout(int keepAliveTimeout) {
281: connector.setAttribute("keepAliveTimeout", keepAliveTimeout);
282: }
283:
284: public void setMaxHttpHeaderSize(int maxHttpHeaderSize) {
285: connector.setAttribute("maxHttpHeaderSize", new Integer(
286: maxHttpHeaderSize));
287: }
288:
289: public void setMaxKeepAliveRequests(int maxKeepAliveRequests) {
290: connector.setAttribute("maxKeepAliveRequests", new Integer(
291: maxKeepAliveRequests));
292: }
293:
294: public void setMaxThreads(int maxThreads) {
295: connector.setAttribute("maxThreads", new Integer(maxThreads));
296: }
297:
298: public void setMaxSpareThreads(int maxSpareThreads) {
299: connector.setAttribute("maxSpareThreads", new Integer(
300: maxSpareThreads));
301: }
302:
303: public void setMinSpareThreads(int minSpareThreads) {
304: connector.setAttribute("minSpareThreads", new Integer(
305: minSpareThreads));
306: }
307:
308: public void setNoCompressionUserAgents(
309: String noCompressionUserAgents) {
310: connector.setAttribute("noCompressionUserAgents",
311: noCompressionUserAgents);
312: }
313:
314: public void setPort(int port) {
315: connector.setPort(port);
316: }
317:
318: public void setRestrictedUserAgents(String restrictedUserAgents) {
319: connector.setAttribute("restrictedUserAgents",
320: restrictedUserAgents);
321: }
322:
323: public void setServer(String server) {
324: if (server.equals(""))
325: server = null;
326: connector.setAttribute("server", server);
327: }
328:
329: public void setSocketBuffer(int socketBuffer) {
330: connector.setAttribute("socketBuffer",
331: new Integer(socketBuffer));
332: }
333:
334: public void setTcpNoDelay(boolean tcpNoDelay) {
335: connector.setAttribute("tcpNoDelay", new Boolean(tcpNoDelay));
336: }
337:
338: public void setThreadPriority(int threadPriority) {
339: connector.setAttribute("threadPriority", new Integer(
340: threadPriority));
341: }
342:
343: // Statistics Provider
344:
345: public boolean isStatisticsProvider() {
346: return true;
347: }
348:
349: public Stats getStats() {
350: String port = String.valueOf(getPort());
351: if (reset) {
352: reset = false;
353: return connStatsProvider.getStats(port);
354: } else
355: return connStatsProvider.updateStats(port);
356: }
357:
358: public void resetStats() {
359: reset = true;
360: }
361:
362: public static final GBeanInfo GBEAN_INFO;
363:
364: static {
365: GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(
366: "Tomcat Connector", BaseHttp11ConnectorGBean.class,
367: ConnectorGBean.GBEAN_INFO);
368: infoFactory.addInterface(BaseHttp11Protocol.class,
369: new String[] {
370: //HTTP Attributes
371: "acceptCount", "address", "bufferSize",
372: "compressableMimeType", "compression",
373: "connectionLinger", "connectionTimeout",
374: "executor", "host", "keepAliveTimeout",
375: "disableUploadTimeout", "maxHttpHeaderSize",
376: "maxKeepAliveRequests", "maxThreads",
377: "maxSpareThreads", "minSpareThreads",
378: "noCompressionUserAgents", "port",
379: "restrictedUserAgents", "server",
380: "socketBuffer",
381: "tcpNoDelay",
382: "threadPriority",
383: //SSL Attributes
384: "algorithm", "clientAuth", "keystoreFile",
385: "keystorePass", "keystoreType", "sslProtocol",
386: "ciphers", "keyAlias", "truststoreFile",
387: "truststorePass", "truststoreType" },
388: new String[] {
389: //HTTP Attributes
390: "acceptCount", "address", "bufferSize",
391: "compressableMimeType", "compression",
392: "connectionLinger", "connectionTimeout",
393: "executor", "host", "keepAliveTimeout",
394: "disableUploadTimeout", "maxHttpHeaderSize",
395: "maxKeepAliveRequests", "maxThreads",
396: "maxSpareThreads", "minSpareThreads",
397: "noCompressionUserAgents", "port",
398: "restrictedUserAgents", "server",
399: "socketBuffer",
400: "tcpNoDelay",
401: "threadPriority",
402: //SSL Attributes
403: "algorithm", "clientAuth", "keystoreFile",
404: "keystorePass", "keystoreType", "sslProtocol",
405: "ciphers", "keyAlias", "truststoreFile",
406: "truststorePass", "truststoreType" });
407: infoFactory.setConstructor(new String[] { "name", "initParams",
408: "protocol", "host", "port", "TomcatContainer",
409: "ServerInfo" });
410: GBEAN_INFO = infoFactory.getBeanInfo();
411: }
412:
413: public static GBeanInfo getGBeanInfo() {
414: return GBEAN_INFO;
415: }
416:
417: }
|