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.mortbay.jetty.servlet.FilterHolder; 026 027 /** 028 * @version $Rev: 522036 $ $Date: 2007-03-24 10:38:03 -0400 (Sat, 24 Mar 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, Map initParams, JettyServletRegistration jettyServletRegistration) throws Exception { 040 filterHolder = new InternalFilterHolder(jettyServletRegistration); 041 if (jettyServletRegistration != null) { 042 filterHolder.setName(filterName); 043 filterHolder.setClassName(filterClass); 044 filterHolder.setInitParameters(initParams); 045 (jettyServletRegistration.getServletHandler()).addFilter(filterHolder); 046 } 047 } 048 049 public String getFilterName() { 050 return filterHolder.getName(); 051 } 052 053 public void doStart() throws Exception { 054 filterHolder.start(); 055 } 056 057 public void doStop() throws Exception { 058 filterHolder.stop(); 059 } 060 061 public void doFail() { 062 try { 063 filterHolder.stop(); 064 } catch (Exception e) { 065 //ignore? 066 } 067 } 068 069 private static class InternalFilterHolder extends FilterHolder { 070 private final JettyServletRegistration servletRegistration; 071 private boolean destroyed; 072 073 public InternalFilterHolder(JettyServletRegistration servletRegistration) { 074 this.servletRegistration = servletRegistration; 075 } 076 077 078 public synchronized Object newInstance() throws InstantiationException, IllegalAccessException { 079 return servletRegistration.newInstance(_className); 080 } 081 082 public void destroyInstance(Object o) throws Exception { 083 if (!destroyed) { 084 super.destroyInstance(o); 085 servletRegistration.destroyInstance(o); 086 destroyed = true; 087 } 088 } 089 090 } 091 092 public static final GBeanInfo GBEAN_INFO; 093 094 static { 095 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(JettyFilterHolder.class, NameFactory.WEB_FILTER); 096 infoBuilder.addAttribute("filterName", String.class, true); 097 infoBuilder.addAttribute("filterClass", String.class, true); 098 infoBuilder.addAttribute("initParams", Map.class, true); 099 100 infoBuilder.addReference("JettyServletRegistration", JettyServletRegistration.class, NameFactory.WEB_MODULE); 101 102 infoBuilder.setConstructor(new String[]{"filterName", "filterClass", "initParams", "JettyServletRegistration"}); 103 104 GBEAN_INFO = infoBuilder.getBeanInfo(); 105 106 } 107 108 public static GBeanInfo getGBeanInfo() { 109 return GBEAN_INFO; 110 } 111 112 }