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.util.HashMap;
022: import java.util.Map;
023:
024: import javax.management.j2ee.statistics.Stats;
025:
026: import org.apache.catalina.LifecycleException;
027: import org.apache.catalina.connector.Connector;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.geronimo.gbean.GBeanInfo;
031: import org.apache.geronimo.gbean.GBeanInfoBuilder;
032: import org.apache.geronimo.gbean.GBeanLifecycle;
033: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
034: import org.apache.geronimo.management.geronimo.NetworkConnector;
035: import org.apache.geronimo.system.serverinfo.ServerInfo;
036: import org.apache.geronimo.tomcat.BaseGBean;
037: import org.apache.geronimo.tomcat.ObjectRetriever;
038: import org.apache.geronimo.tomcat.TomcatContainer;
039:
040: public abstract class ConnectorGBean extends BaseGBean implements
041: CommonProtocol, GBeanLifecycle, ObjectRetriever,
042: TomcatWebConnector {
043:
044: private static final Log log = LogFactory
045: .getLog(ConnectorGBean.class);
046:
047: public final static String CONNECTOR_CONTAINER_REFERENCE = "TomcatContainer";
048:
049: protected final ServerInfo serverInfo;
050:
051: protected final Connector connector;
052:
053: private final TomcatContainer container;
054:
055: private String name;
056:
057: public ConnectorGBean(String name, Map initParams,
058: String tomcatProtocol, TomcatContainer container,
059: ServerInfo serverInfo) throws Exception {
060:
061: //Relief for new Tomcat-only parameters that may come in the future
062: if (initParams == null) {
063: initParams = new HashMap();
064: }
065:
066: // Do we really need this?? For Tomcat I don't think so...
067: // validateProtocol(protocol);
068:
069: if (name == null) {
070: throw new IllegalArgumentException("name cannot be null.");
071: }
072:
073: if (container == null) {
074: throw new IllegalArgumentException(
075: "container cannot be null.");
076: }
077:
078: if (serverInfo == null) {
079: throw new IllegalArgumentException(
080: "serverInfo cannot be null.");
081: }
082:
083: tomcatProtocol = validateProtocol(tomcatProtocol);
084:
085: this .name = name;
086: this .container = container;
087: this .serverInfo = serverInfo;
088:
089: // Create the Connector object
090: connector = new Connector(tomcatProtocol);
091:
092: setParameters(connector, initParams);
093:
094: }
095:
096: public void doFail() {
097: log.warn(name + " connector failed");
098: doStop();
099: }
100:
101: public void doStart() throws LifecycleException {
102: container.addConnector(connector);
103: connector.start();
104: if (log.isDebugEnabled())
105: log.debug(name + " connector started");
106: }
107:
108: public void doStop() {
109: try {
110: connector.stop();
111: } catch (LifecycleException e) {
112: log.error(e);
113: }
114:
115: container.removeConnector(connector);
116:
117: if (log.isDebugEnabled())
118: log.debug(name + " connector stopped");
119: }
120:
121: /**
122: * Ensures that this implementation can handle the requested protocol.
123: * @param protocol
124: */
125: protected String validateProtocol(String tomcatProtocol) {
126: return tomcatProtocol;
127: }
128:
129: public abstract int getDefaultPort();
130:
131: public abstract String getGeronimoProtocol();
132:
133: public abstract Stats getStats();
134:
135: public abstract void resetStats();
136:
137: public Object getInternalObject() {
138: return connector;
139: }
140:
141: public String getName() {
142: return name;
143: }
144:
145: public void setAllowTrace(boolean allow) {
146: connector.setAllowTrace(allow);
147: }
148:
149: public boolean getAllowTrace() {
150: return connector.getAllowTrace();
151: }
152:
153: public void setEmptySessionPath(boolean emptySessionPath) {
154: connector.setEmptySessionPath(emptySessionPath);
155: }
156:
157: public void setEnableLookups(boolean enabled) {
158: connector.setEnableLookups(enabled);
159: }
160:
161: public int getMaxPostSize() {
162: int value = connector.getMaxPostSize();
163: return value == 0 ? 2097152 : value;
164: }
165:
166: public void setMaxPostSize(int bytes) {
167: connector.setMaxPostSize(bytes);
168: }
169:
170: public String getProtocol() {
171: //This is totally wrong on the Geronimo side and needs to be re-thought out.
172: //This was done to shoe horn in gerneric Geronimo protocols which should have no relation
173: //to the container's scheme. This whole idea needs rework.
174: return getGeronimoProtocol();
175: }
176:
177: public String getTomcatProtocol() {
178: return connector.getProtocol();
179: }
180:
181: public String getProxyName() {
182: return connector.getProxyName();
183: }
184:
185: public int getProxyPort() {
186: return connector.getProxyPort();
187: }
188:
189: public int getRedirectPort() {
190: return connector.getRedirectPort();
191: }
192:
193: public String getScheme() {
194: return connector.getScheme();
195: }
196:
197: public boolean getSecure() {
198: return connector.getSecure();
199: }
200:
201: public String getUriEncoding() {
202: return connector.getURIEncoding();
203: }
204:
205: public boolean getUseBodyEncodingForURI() {
206: return connector.getUseBodyEncodingForURI();
207: }
208:
209: public boolean getUseIPVHosts() {
210: return connector.getUseIPVHosts();
211: }
212:
213: public void setMaxSavePostSize(int maxSavePostSize) {
214: connector.setMaxSavePostSize(maxSavePostSize);
215: }
216:
217: public void setProxyName(String proxyName) {
218: if (proxyName.equals(""))
219: proxyName = null;
220: connector.setProxyName(proxyName);
221: }
222:
223: public void setProxyPort(int port) {
224: connector.setProxyPort(port);
225: }
226:
227: public void setRedirectPort(int port) {
228: connector.setRedirectPort(port);
229: }
230:
231: public void setScheme(String scheme) {
232: connector.setScheme(scheme);
233: }
234:
235: public void setSecure(boolean secure) {
236: connector.setSecure(secure);
237: }
238:
239: public boolean getSslEnabled() {
240: Object value = connector.getAttribute("SSLEnabled");
241: return value == null ? false : new Boolean(value.toString())
242: .booleanValue();
243: }
244:
245: public void setSslEnabled(boolean sslEnabled) {
246: connector.setAttribute("SSLEnabled", sslEnabled);
247: }
248:
249: public void setUriEncoding(String uriEncoding) {
250: connector.setURIEncoding(uriEncoding);
251: }
252:
253: public void setUseBodyEncodingForURI(boolean useBodyEncodingForURI) {
254: connector.setUseBodyEncodingForURI(useBodyEncodingForURI);
255: }
256:
257: public void setUseIPVHosts(boolean useIPVHosts) {
258: connector.setUseIPVHosts(useIPVHosts);
259: }
260:
261: public void setXpoweredBy(boolean xpoweredBy) {
262: connector.setXpoweredBy(xpoweredBy);
263: }
264:
265: public boolean getEnableLookups() {
266: return connector.getEnableLookups();
267: }
268:
269: public int getMaxSavePostSize() {
270: int value = connector.getMaxSavePostSize();
271: return value == 0 ? 4096 : value;
272: }
273:
274: public boolean getEmptySessionPath() {
275: return connector.getEmptySessionPath();
276: }
277:
278: public boolean getXpoweredBy() {
279: return connector.getXpoweredBy();
280: }
281:
282: public static final GBeanInfo GBEAN_INFO;
283:
284: static {
285: GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(
286: "Tomcat Connector", ConnectorGBean.class);
287:
288: infoFactory.addAttribute("name", String.class, true);
289: infoFactory.addAttribute("initParams", Map.class, true);
290: infoFactory.addAttribute("protocol", String.class, true);
291: infoFactory.addReference(CONNECTOR_CONTAINER_REFERENCE,
292: TomcatContainer.class, NameFactory.GERONIMO_SERVICE);
293: infoFactory.addReference("ServerInfo", ServerInfo.class,
294: "GBean");
295: infoFactory.addInterface(ObjectRetriever.class);
296: infoFactory.addInterface(TomcatWebConnector.class);
297: infoFactory.addInterface(CommonProtocol.class,
298:
299: new String[] { "allowTrace", "emptySessionPath",
300: "enableLookups", "maxPostSize", "maxSavePostSize",
301: "protocol", "tomcatProtocol", "proxyName", "proxyPort",
302: "redirectPort", "scheme", "secure", "sslEnabled",
303: "uriEncoding", "useBodyEncodingForURI", "useIPVHosts",
304: "xpoweredBy" },
305:
306: new String[] { "allowTrace", "emptySessionPath",
307: "enableLookups", "maxPostSize", "maxSavePostSize",
308: "protocol", "tomcatProtocol", "proxyName", "proxyPort",
309: "redirectPort", "scheme", "secure", "sslEnabled",
310: "uriEncoding", "useBodyEncodingForURI", "useIPVHosts",
311: "xpoweredBy" });
312: infoFactory.setConstructor(new String[] { "name", "initParams",
313: "protocol", "TomcatContainer", "ServerInfo" });
314: GBEAN_INFO = infoFactory.getBeanInfo();
315: }
316:
317: public static GBeanInfo getGBeanInfo() {
318: return GBEAN_INFO;
319: }
320:
321: }
|