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 */ 017 package org.apache.geronimo.jetty6; 018 019 import java.util.Map; 020 021 import org.apache.geronimo.gbean.GBeanInfo; 022 import org.apache.geronimo.gbean.GBeanInfoBuilder; 023 import org.apache.geronimo.gbean.GBeanLifecycle; 024 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 025 import org.apache.geronimo.jetty6.handler.LifecycleCommand; 026 import org.mortbay.jetty.servlet.FilterHolder; 027 028 /** 029 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $ 030 */ 031 public class JettyFilterHolder implements GBeanLifecycle { 032 033 private final FilterHolder filterHolder; 034 035 //todo consider an interface instead of this constructor for endpoint use. 036 public JettyFilterHolder() { 037 filterHolder = null; 038 } 039 040 public JettyFilterHolder(String filterName, String filterClass, Map initParams, JettyServletRegistration jettyServletRegistration) throws Exception { 041 filterHolder = new InternalFilterHolder(jettyServletRegistration); 042 if (jettyServletRegistration != null) { 043 filterHolder.setName(filterName); 044 filterHolder.setClassName(filterClass); 045 filterHolder.setInitParameters(initParams); 046 (jettyServletRegistration.getServletHandler()).addFilter(filterHolder); 047 } 048 } 049 050 public String getFilterName() { 051 return filterHolder.getName(); 052 } 053 054 public void doStart() throws Exception { 055 filterHolder.start(); 056 } 057 058 public void doStop() throws Exception { 059 filterHolder.stop(); 060 } 061 062 public void doFail() { 063 try { 064 filterHolder.stop(); 065 } catch (Exception e) { 066 //ignore? 067 } 068 } 069 070 private static class InternalFilterHolder extends FilterHolder { 071 private final JettyServletRegistration servletRegistration; 072 private boolean destroyed; 073 074 public InternalFilterHolder(JettyServletRegistration servletRegistration) { 075 this.servletRegistration = servletRegistration; 076 } 077 078 079 public synchronized Object newInstance() throws InstantiationException, IllegalAccessException { 080 return servletRegistration.newInstance(_className); 081 } 082 083 public void destroyInstance(Object o) throws Exception { 084 if (!destroyed) { 085 super.destroyInstance(o); 086 servletRegistration.destroyInstance(o); 087 destroyed = true; 088 } 089 } 090 091 public void doStop() { 092 LifecycleCommand lifecycleCommand = (new LifecycleCommand() { 093 public void lifecycleMethod() throws Exception { 094 internalDoStop(); 095 } 096 }); 097 try { 098 this.servletRegistration.getLifecycleChain().lifecycleCommand(lifecycleCommand); 099 } catch (Exception e) { 100 //ignore???? 101 } 102 } 103 104 public void doStart() throws Exception { 105 LifecycleCommand lifecycleCommand = (new LifecycleCommand() { 106 public void lifecycleMethod() throws Exception { 107 internalDoStart(); 108 } 109 }); 110 this.servletRegistration.getLifecycleChain().lifecycleCommand(lifecycleCommand); 111 } 112 113 private void internalDoStart() throws Exception { 114 super.doStart(); 115 } 116 117 private void internalDoStop() { 118 super.doStop(); 119 } 120 121 } 122 123 public static final GBeanInfo GBEAN_INFO; 124 125 static { 126 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(JettyFilterHolder.class, NameFactory.WEB_FILTER); 127 infoBuilder.addAttribute("filterName", String.class, true); 128 infoBuilder.addAttribute("filterClass", String.class, true); 129 infoBuilder.addAttribute("initParams", Map.class, true); 130 131 infoBuilder.addReference("JettyServletRegistration", JettyServletRegistration.class, NameFactory.WEB_MODULE); 132 133 infoBuilder.setConstructor(new String[]{"filterName", "filterClass", "initParams", "JettyServletRegistration"}); 134 135 GBEAN_INFO = infoBuilder.getBeanInfo(); 136 137 } 138 139 public static GBeanInfo getGBeanInfo() { 140 return GBEAN_INFO; 141 } 142 143 }