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