KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > tomcat > ConnectorGBean


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 package org.apache.geronimo.tomcat;
18
19 import java.net.InetSocketAddress JavaDoc;
20 import java.net.UnknownHostException JavaDoc;
21 import java.net.InetAddress JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 import org.apache.catalina.LifecycleException;
26 import org.apache.catalina.connector.Connector;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.geronimo.gbean.GBeanInfo;
30 import org.apache.geronimo.gbean.GBeanInfoBuilder;
31 import org.apache.geronimo.gbean.GBeanLifecycle;
32 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
33 import org.apache.geronimo.management.geronimo.WebManager;
34
35 /**
36  * @version $Rev: 486195 $ $Date: 2006-12-12 10:42:02 -0500 (Tue, 12 Dec 2006) $
37  */

38 public class ConnectorGBean extends BaseGBean implements GBeanLifecycle, ObjectRetriever, TomcatWebConnector {
39     private static final Log log = LogFactory.getLog(ConnectorGBean.class);
40     public final static String JavaDoc CONNECTOR_CONTAINER_REFERENCE = "TomcatContainer";
41
42     protected final Connector connector;
43     private final TomcatContainer container;
44     private String JavaDoc name;
45     private String JavaDoc connectHost;
46
47     public ConnectorGBean(String JavaDoc name, String JavaDoc protocol, String JavaDoc host, int port, TomcatContainer container) throws Exception JavaDoc {
48         super(); // TODO: make it an attribute
49

50         Map JavaDoc initParams = new HashMap JavaDoc();
51
52         validateProtocol(protocol);
53         
54         //Default the host to listen on all address is one was not specified
55
if (host == null){
56             host = "0.0.0.0";
57         }
58         
59         //Must have a port
60
if (port == 0){
61             throw new IllegalArgumentException JavaDoc("Must declare a port.");
62         }
63
64         initParams.put("address", host);
65         initParams.put("port", Integer.toString(port));
66         initializeParams(protocol, initParams);
67
68         // Convert Geronimo standard values to Tomcat standard values
69
// Only AJP requires an explicit protocol setting
70
if(protocol != null && protocol.equals(WebManager.PROTOCOL_AJP)) {
71             protocol = "AJP/1.3";
72         } else {
73             protocol = null;
74         }
75
76         if (name == null){
77             throw new IllegalArgumentException JavaDoc("name cannot be null.");
78         }
79
80         if (container == null){
81             throw new IllegalArgumentException JavaDoc("container cannot be null.");
82         }
83  
84         this.name = name;
85         this.container = container;
86
87         //Create the Connector object
88
connector = new Connector(protocol);
89
90
91         //Set the parameters
92
setParameters(connector, initParams);
93         
94     }
95
96     /**
97      * Adds any special parameters before constructing the connector. Note:
98      * all keys and values must be Strings.
99      *
100      * @param protocol Should be one of the constants from WebContainer.
101      * @param params The map of parameters that will be used to initialize the connector.
102      */

103     protected void initializeParams(String JavaDoc protocol, Map JavaDoc params) {}
104
105     /**
106      * Ensures that this implementation can handle the requested protocol.
107      * @param protocol
108      */

109     protected void validateProtocol(String JavaDoc protocol) {
110         if(protocol == null) {
111             return;
112         }
113         if(protocol.equals(WebManager.PROTOCOL_HTTPS)) {
114             throw new IllegalArgumentException JavaDoc("Use a HttpsConnectorGBean for an HTTPS connector");
115         } else if(!protocol.equals(WebManager.PROTOCOL_HTTP) && !protocol.equals(WebManager.PROTOCOL_AJP)) {
116             throw new IllegalArgumentException JavaDoc("Unrecognized protocol '"+protocol+"' (use the values of the PROTOCOL_* constants in WebConnector)");
117         }
118     }
119
120     public String JavaDoc getName() {
121         return name;
122     }
123
124     public Object JavaDoc getInternalObject() {
125         return connector;
126     }
127
128     public void doStart() throws LifecycleException {
129         container.addConnector(connector);
130         connector.start();
131         log.debug(name + " connector started");
132    }
133
134     public void doStop() {
135         try{
136             connector.stop();
137         } catch (LifecycleException e){
138             log.error(e);
139         }
140         container.removeConnector(connector);
141         log.debug(name + " connector stopped");
142     }
143
144     public void doFail() {
145         log.warn(name + " connector failed");
146         doStop();
147     }
148
149     public int getDefaultPort() {
150         return getProtocol().equals(WebManager.PROTOCOL_AJP) ? -1 :
151                 getProtocol().equals(WebManager.PROTOCOL_HTTP) ? 80 :
152                 getProtocol().equals(WebManager.PROTOCOL_HTTPS) ? 443 : -1;
153     }
154
155     public String JavaDoc getConnectUrl() {
156         if(connectHost == null) {
157             String JavaDoc host = getHost();
158             if(host == null || host.equals("0.0.0.0")) {
159                 InetAddress JavaDoc address = null;
160                 try {
161                     address = InetAddress.getLocalHost();
162                 } catch (UnknownHostException JavaDoc e) {
163                     host = "unknown-host";
164                 }
165                 if(address != null) {
166                     host = address.getCanonicalHostName();
167                     if(host == null || host.equals("")) {
168                         host = address.getHostAddress();
169                     }
170                 }
171             }
172             connectHost = host;
173         }
174         return getProtocol().toLowerCase()+"://"+connectHost+(getPort() == getDefaultPort() ? "" : ":"+getPort());
175     }
176
177     public boolean isEmptySessionPath(){
178         return connector.getEmptySessionPath();
179     }
180     
181     public void setEmptySessionPath(boolean emptySessionPath){
182        connector.setEmptySessionPath(emptySessionPath);
183     }
184
185     /**
186      * Gets the network protocol that this connector handles.
187      */

188     public String JavaDoc getProtocol() {
189         String JavaDoc protocol = connector.getProtocol();
190         if(protocol.indexOf("AJP") > -1) {
191             return WebManager.PROTOCOL_AJP;
192         } else if(connector.getScheme().equalsIgnoreCase("http")) {
193             return WebManager.PROTOCOL_HTTP;
194         } else if(connector.getScheme().equalsIgnoreCase("https")) {
195             return WebManager.PROTOCOL_HTTPS;
196         }
197         throw new IllegalStateException JavaDoc("Unknown protocol '"+protocol+"' and scheme '"+connector.getScheme()+"'");
198     }
199
200     /**
201      * Gets the network port that this connector listens on.
202      */

203     public int getPort() {
204         return connector.getPort();
205     }
206
207     /**
208      * Sets the network port that this connector listens on.
209      */

210     public void setPort(int port) {
211         connector.setPort(port);
212     }
213
214     /**
215      * Gets the hostname/IP that this connector listens on.
216      */

217     public String JavaDoc getHost() {
218         Object JavaDoc value = connector.getAttribute("address");
219         if(value == null) {
220             return "0.0.0.0";
221         } else if(value instanceof InetAddress JavaDoc) {
222             return ((InetAddress JavaDoc)value).getHostAddress();
223         } else return value.toString();
224     }
225
226     /**
227      * Sets the hostname/IP that this connector listens on. This is typically
228      * most useful for machines with multiple network cards, but can be used
229      * to limit a connector to only listen for connections from the local
230      * machine (127.0.0.1). To listen on all available network interfaces,
231      * specify an address of 0.0.0.0.
232      */

233     public void setHost(String JavaDoc host) throws UnknownHostException JavaDoc {
234         connector.setAttribute("address", host);
235     }
236
237     /**
238      * Every connector must specify a property of type InetSocketAddress
239      * because we use that to identify the network services to print a list
240      * during startup. However, this can be read-only since the host and port
241      * are set separately using setHost and setPort.
242      */

243     public InetSocketAddress JavaDoc getListenAddress() {
244         return new InetSocketAddress JavaDoc(getHost(), getPort());
245     }
246
247     /**
248      * Gets the size of the buffer used to handle network data for this
249      * connector.
250      */

251     public int getBufferSizeBytes() {
252         Object JavaDoc value = connector.getAttribute("bufferSize");
253         return value == null ? 2048 : Integer.parseInt(value.toString());
254     }
255
256     /**
257      * Gets the size of the buffer used to handle network data for this
258      * connector.
259      */

260     public void setBufferSizeBytes(int bytes) {
261         connector.setAttribute("bufferSize", new Integer JavaDoc(bytes));
262     }
263
264     /**
265      * Gets the maximum number of threads used to service connections from
266      * this connector.
267      */

268     public int getMaxThreads() {
269         Object JavaDoc value = connector.getAttribute("maxThreads");
270         return value == null ? 200 : Integer.parseInt(value.toString());
271     }
272
273     /**
274      * Sets the maximum number of threads used to service connections from
275      * this connector.
276      */

277     public void setMaxThreads(int threads) {
278         connector.setAttribute("maxThreads", new Integer JavaDoc(threads));
279     }
280
281     /**
282      * Gets the maximum number of connections that may be queued while all
283      * threads are busy. Any requests received while the queue is full will
284      * be rejected.
285      */

286     public int getAcceptQueueSize() {
287         Object JavaDoc value = connector.getAttribute("acceptCount");
288         return value == null ? 10 : Integer.parseInt(value.toString());
289     }
290
291     /**
292      * Sets the maximum number of connections that may be queued while all
293      * threads are busy. Any requests received while the queue is full will
294      * be rejected.
295      */

296     public void setAcceptQueueSize(int size) {
297         connector.setAttribute("acceptCount", new Integer JavaDoc(size));
298     }
299
300     /**
301      * Gets the amount of time the socket used by this connector will linger
302      * after being closed. -1 indicates that socket linger is disabled.
303      */

304     public int getLingerMillis() {
305         Object JavaDoc value = connector.getAttribute("connectionLinger");
306         return value == null ? -1 : Integer.parseInt(value.toString());
307     }
308
309     /**
310      * Sets the amount of time the socket used by this connector will linger
311      * after being closed. Use -1 to disable socket linger.
312      */

313     public void setLingerMillis(int millis) {
314         connector.setAttribute("connectionLinger", new Integer JavaDoc(millis));
315     }
316
317     /**
318      * Gets whether the TCP_NODELAY flag is set for the sockets used by this
319      * connector. This usually enhances performance, so it should typically
320      * be set.
321      */

322     public boolean isTcpNoDelay() {
323         Object JavaDoc value = connector.getAttribute("tcpNoDelay");
324         return value == null ? true : new Boolean JavaDoc(value.toString()).booleanValue();
325     }
326
327     /**
328      * Sets whether the TCP_NODELAY flag is set for the sockets used by this
329      * connector. This usually enhances performance, so it should typically
330      * be set.
331      */

332     public void setTcpNoDelay(boolean enable) {
333         connector.setAttribute("tcpNoDelay", new Boolean JavaDoc(enable));
334     }
335
336     /**
337      * Gets the network port to which traffic will be redirected if this
338      * connector handles insecure traffic and the request requires a secure
339      * connection. Needless to say, this should point to another connector
340      * configured for SSL.
341      */

342     public int getRedirectPort() {
343         return connector.getRedirectPort();
344     }
345
346     /**
347      * Gets the network port to which traffic will be redirected if this
348      * connector handles insecure traffic and the request requires a secure
349      * connection. Needless to say, this should point to another connector
350      * configured for SSL. If no SSL connector is available, any port can
351      * be used as they all fail equally well. :)
352      */

353     public void setRedirectPort(int port) {
354         connector.setRedirectPort(port);
355     }
356
357     public int getMinSpareThreads() {
358         Object JavaDoc value = connector.getAttribute("minSpareThreads");
359         return value == null ? 4 : Integer.parseInt(value.toString());
360     }
361
362     public void setMinSpareThreads(int threads) {
363         connector.setAttribute("minSpareThreads", new Integer JavaDoc(threads));
364     }
365
366     public int getMaxSpareThreads() {
367         Object JavaDoc value = connector.getAttribute("maxSpareThreads");
368         return value == null ? 50 : Integer.parseInt(value.toString());
369     }
370
371     public void setMaxSpareThreads(int threads) {
372         connector.setAttribute("maxSpareThreads", new Integer JavaDoc(threads));
373     }
374
375     public int getMaxHttpHeaderSizeBytes() {
376         Object JavaDoc value = connector.getAttribute("maxHttpHeaderSize");
377         return value == null ? 4096 : Integer.parseInt(value.toString());
378     }
379
380     public void setMaxHttpHeaderSizeBytes(int bytes) {
381         connector.setAttribute("maxHttpHeaderSize", new Integer JavaDoc(bytes));
382     }
383
384     public boolean isHostLookupEnabled() {
385         return connector.getEnableLookups();
386     }
387
388     public void setHostLookupEnabled(boolean enabled) {
389         connector.setEnableLookups(enabled);
390     }
391
392     public int getConnectionTimeoutMillis() {
393         Object JavaDoc value = connector.getAttribute("connectionTimeout");
394         return value == null ? 60000 : Integer.parseInt(value.toString());
395     }
396
397     public void setConnectionTimeoutMillis(int millis) {
398         connector.setAttribute("connectionTimeout", new Integer JavaDoc(millis));
399     }
400
401     public boolean isUploadTimeoutEnabled() {
402         Object JavaDoc value = connector.getAttribute("disableUploadTimeout");
403         return value == null ? true : !new Boolean JavaDoc(value.toString()).booleanValue();
404     }
405
406     public void setUploadTimeoutEnabled(boolean enabled) {
407         connector.setAttribute("disableUploadTimeout", new Boolean JavaDoc(!enabled));
408     }
409
410     public int getMaxPostSize() {
411         int value = connector.getMaxPostSize();
412         return value == 0 ? 2097152 : value;
413     }
414
415     public void setMaxPostSize(int bytes) {
416         connector.setMaxPostSize(bytes);
417     }
418
419     public int getMaxSavePostSize() {
420         int value = connector.getMaxSavePostSize();
421         return value == 0 ? 4096 : value;
422     }
423
424     public void setMaxSavePostSize(int kbytes) {
425         connector.setMaxSavePostSize(kbytes);
426     }
427
428     public int getMaxKeepAliveRequests() {
429         Object JavaDoc value = connector.getAttribute("maxKeepAliveRequests");
430         return value == null ? 100 : Integer.parseInt(value.toString());
431     }
432
433     public void setMaxKeepAliveRequests(int maxKeepAliveRequests) {
434         connector.setAttribute("maxKeepAliveRequests", new Integer JavaDoc(maxKeepAliveRequests));
435     }
436
437     public int getSocketBuffer() {
438         Object JavaDoc value = connector.getAttribute("socketBuffer");
439         return value == null ? 9000 : Integer.parseInt(value.toString());
440     }
441
442     public void setSocketBuffer(int kbytes) {
443         connector.setAttribute("socketBuffer", new Integer JavaDoc(kbytes));
444     }
445
446     public boolean getUseBodyEncodingForURI() {
447         return connector.getUseBodyEncodingForURI();
448     }
449
450     public void setUseBodyEncodingForURI(boolean enabled) {
451         connector.setUseBodyEncodingForURI(enabled);
452     }
453
454     public void setAllowTrace(boolean allow) {
455         connector.setAllowTrace(allow);
456     }
457
458     public boolean getAllowTrace() {
459         return connector.getAllowTrace();
460     }
461
462     public void setProxyName(String JavaDoc proxyName) {
463         connector.setProxyName(proxyName);
464     }
465
466     public String JavaDoc getProxyName() {
467         return connector.getProxyName();
468     }
469
470     public void setProxyPort(int port) {
471         connector.setProxyPort(port);
472     }
473
474     public int getProxyPort() {
475         return connector.getProxyPort();
476     }
477
478     public void setScheme(String JavaDoc scheme) {
479         connector.setScheme(scheme);
480     }
481
482     public String JavaDoc getScheme() {
483         return connector.getScheme();
484     }
485
486     public void setUriEncoding(String JavaDoc encoding) {
487         connector.setURIEncoding(encoding);
488     }
489
490     public String JavaDoc getUriEncoding() {
491         return connector.getURIEncoding();
492     }
493
494     public void setUseIPVHosts(boolean useIPVHosts) {
495         connector.setUseIPVHosts(useIPVHosts);
496     }
497
498     public boolean getUseIPVHosts() {
499         return connector.getUseIPVHosts();
500     }
501
502     public void setXpoweredBy(boolean xpoweredBy) {
503         connector.setXpoweredBy(xpoweredBy);
504     }
505
506     public boolean getXpoweredBy() {
507         return connector.getXpoweredBy();
508     }
509
510     public void setCompressableMimeType(String JavaDoc compressableMimeType) {
511         connector.setAttribute("compressableMimeType", compressableMimeType);
512     }
513
514     public String JavaDoc getCompressableMimeType() {
515         return (String JavaDoc) connector.getAttribute("compressableMimeType");
516     }
517
518     public void setCompression(String JavaDoc compression) {
519         connector.setAttribute("compression", compression);
520     }
521
522     public String JavaDoc getCompression() {
523         return (String JavaDoc) connector.getAttribute("compression");
524     }
525
526     public void setNoCompressionUserAgents(String JavaDoc noCompressionUserAgents) {
527         connector.setAttribute("noCompressionUserAgents", noCompressionUserAgents);
528     }
529
530     public String JavaDoc getNoCompressionUserAgents() {
531         return (String JavaDoc) connector.getAttribute("noCompressionUserAgents");
532     }
533
534     public void setRestrictedUserAgents(String JavaDoc restrictedUserAgents) {
535         connector.setAttribute("restrictedUserAgents", restrictedUserAgents);
536     }
537
538     public String JavaDoc getRestrictedUserAgents() {
539         return (String JavaDoc) connector.getAttribute("restrictedUserAgents");
540     }
541
542     public void setThreadPriority(int threadPriority) {
543         connector.setAttribute("threadPriority", new Integer JavaDoc(threadPriority));
544     }
545
546     public int getThreadPriority() {
547         Object JavaDoc value = connector.getAttribute("threadPriority");
548         return value == null ? 5 :Integer.parseInt(value.toString());
549     }
550
551     public void setServer(String JavaDoc server) {
552         connector.setAttribute("server", server);
553     }
554
555     public String JavaDoc getServer() {
556         return (String JavaDoc) connector.getAttribute("server");
557     }
558
559     public void setStrategy(String JavaDoc strategy) {
560         connector.setAttribute("strategy", strategy);
561     }
562
563     public String JavaDoc getStrategy() {
564         return (String JavaDoc) connector.getAttribute("strategy");
565     }
566
567     public static final GBeanInfo GBEAN_INFO;
568
569     static {
570         GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Tomcat Connector", ConnectorGBean.class);
571         infoFactory.addAttribute("name", String JavaDoc.class, true);
572         infoFactory.addAttribute("protocol", String JavaDoc.class, true);
573         infoFactory.addReference(CONNECTOR_CONTAINER_REFERENCE, TomcatContainer.class, NameFactory.GERONIMO_SERVICE);
574         infoFactory.addOperation("getInternalObject");
575         infoFactory.addInterface(TomcatWebConnector.class,
576                 new String JavaDoc[]{
577                         "host",
578                         "port",
579                         "bufferSizeBytes",
580                         "maxThreads",
581                         "acceptQueueSize",
582                         "lingerMillis",
583                         "tcpNoDelay",
584                         "redirectPort",
585                         "minSpareThreads",
586                         "maxSpareThreads",
587                         "maxHttpHeaderSizeBytes",
588                         "hostLookupEnabled",
589                         "connectionTimeoutMillis",
590                         "uploadTimeoutEnabled",
591                         "connectUrl",
592                         "maxPostSize",
593                         "maxSavePostSize",
594                         "emptySessionPath",
595                         "maxKeepAliveRequests",
596                         "socketBuffer",
597                         "useBodyEncodingForURI",
598                         "allowTrace",
599                         "proxyName",
600                         "proxyPort",
601                         "scheme",
602                         "secure",
603                         "uriEncoding",
604                         "useIPVHosts",
605                         "xpoweredBy",
606                         "compressableMimeType",
607                         "compression",
608                         "noCompressionUserAgents",
609                         "restrictedUserAgents",
610                         "threadPriority",
611                         "server",
612                         "strategy"
613                 },
614
615                 new String JavaDoc[]{
616                         "host",
617                         "port",
618                         "redirectPort",
619                         "maxThreads"});
620         infoFactory.setConstructor(new String JavaDoc[] { "name", "protocol", "host", "port", "TomcatContainer"});
621         GBEAN_INFO = infoFactory.getBeanInfo();
622     }
623
624     public static GBeanInfo getGBeanInfo() {
625         return GBEAN_INFO;
626     }
627
628 }
629
Popular Tags