001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.jetty6;
017:
018: import java.util.Map;
019:
020: import org.apache.geronimo.gbean.GBeanInfo;
021: import org.apache.geronimo.gbean.GBeanInfoBuilder;
022: import org.apache.geronimo.gbean.GBeanLifecycle;
023: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
024: import org.apache.geronimo.jetty6.handler.LifecycleCommand;
025: import org.mortbay.jetty.servlet.FilterHolder;
026:
027: /**
028: * @version $Rev: 601045 $ $Date: 2007-12-04 11:15:27 -0800 (Tue, 04 Dec 2007) $
029: */
030: public class JettyFilterHolder implements GBeanLifecycle {
031:
032: private final FilterHolder filterHolder;
033:
034: //todo consider an interface instead of this constructor for endpoint use.
035: public JettyFilterHolder() {
036: filterHolder = null;
037: }
038:
039: public JettyFilterHolder(String filterName, String filterClass,
040: Map initParams,
041: JettyServletRegistration jettyServletRegistration)
042: throws Exception {
043: filterHolder = new InternalFilterHolder(
044: jettyServletRegistration);
045: if (jettyServletRegistration != null) {
046: filterHolder.setName(filterName);
047: filterHolder.setClassName(filterClass);
048: filterHolder.setInitParameters(initParams);
049: (jettyServletRegistration.getServletHandler())
050: .addFilter(filterHolder);
051: }
052: }
053:
054: public String getFilterName() {
055: return filterHolder.getName();
056: }
057:
058: public void doStart() throws Exception {
059: filterHolder.start();
060: }
061:
062: public void doStop() throws Exception {
063: filterHolder.stop();
064: }
065:
066: public void doFail() {
067: try {
068: filterHolder.stop();
069: } catch (Exception e) {
070: //ignore?
071: }
072: }
073:
074: private static class InternalFilterHolder extends FilterHolder {
075: private final JettyServletRegistration servletRegistration;
076: private boolean destroyed;
077:
078: public InternalFilterHolder(
079: JettyServletRegistration servletRegistration) {
080: this .servletRegistration = servletRegistration;
081: }
082:
083: public synchronized Object newInstance()
084: throws InstantiationException, IllegalAccessException {
085: return servletRegistration.newInstance(_className);
086: }
087:
088: public void destroyInstance(Object o) throws Exception {
089: if (!destroyed) {
090: super .destroyInstance(o);
091: servletRegistration.destroyInstance(o);
092: destroyed = true;
093: }
094: }
095:
096: public void doStop() {
097: LifecycleCommand lifecycleCommand = (new LifecycleCommand() {
098: public void lifecycleMethod() throws Exception {
099: internalDoStop();
100: }
101: });
102: try {
103: this .servletRegistration.getLifecycleChain()
104: .lifecycleCommand(lifecycleCommand);
105: } catch (Exception e) {
106: //ignore????
107: }
108: }
109:
110: public void doStart() throws Exception {
111: LifecycleCommand lifecycleCommand = (new LifecycleCommand() {
112: public void lifecycleMethod() throws Exception {
113: internalDoStart();
114: }
115: });
116: this .servletRegistration.getLifecycleChain()
117: .lifecycleCommand(lifecycleCommand);
118: }
119:
120: private void internalDoStart() throws Exception {
121: super .doStart();
122: }
123:
124: private void internalDoStop() {
125: super .doStop();
126: }
127:
128: }
129:
130: public static final GBeanInfo GBEAN_INFO;
131:
132: static {
133: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
134: JettyFilterHolder.class, NameFactory.WEB_FILTER);
135: infoBuilder.addAttribute("filterName", String.class, true);
136: infoBuilder.addAttribute("filterClass", String.class, true);
137: infoBuilder.addAttribute("initParams", Map.class, true);
138:
139: infoBuilder.addReference("JettyServletRegistration",
140: JettyServletRegistration.class, NameFactory.WEB_MODULE);
141:
142: infoBuilder
143: .setConstructor(new String[] { "filterName",
144: "filterClass", "initParams",
145: "JettyServletRegistration" });
146:
147: GBEAN_INFO = infoBuilder.getBeanInfo();
148:
149: }
150:
151: public static GBeanInfo getGBeanInfo() {
152: return GBEAN_INFO;
153: }
154:
155: }
|