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