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.tomcat; 018 019 import java.lang.ClassLoader; 020 import java.util.Map; 021 022 import org.apache.catalina.Valve; 023 import org.apache.commons.logging.Log; 024 import org.apache.commons.logging.LogFactory; 025 import org.apache.geronimo.gbean.GBeanInfo; 026 import org.apache.geronimo.gbean.GBeanInfoBuilder; 027 import org.apache.geronimo.gbean.GBeanLifecycle; 028 029 /** 030 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $ 031 */ 032 public class ValveGBean extends BaseGBean implements GBeanLifecycle, ObjectRetriever { 033 034 private static final Log log = LogFactory.getLog(ValveGBean.class); 035 036 public static final String J2EE_TYPE = "TomcatValve"; 037 038 private final Valve valve; 039 private final ValveGBean nextValve; 040 private final String className; 041 042 public ValveGBean(){ 043 valve = null; 044 nextValve = null; 045 className = null; 046 } 047 048 public ValveGBean(String className, Map initParams, ValveGBean nextValve, ClassLoader classLoader) throws Exception{ 049 050 //Validate 051 if (className == null){ 052 throw new IllegalArgumentException("className cannot be null."); 053 } 054 055 if (nextValve != null){ 056 if (!(nextValve.getInternalObject() instanceof Valve)){ 057 throw new IllegalArgumentException("The class given as the NextValve attribute does not wrap an object of org.apache.catalina.Valve type."); 058 } 059 this.nextValve = nextValve; 060 } else { 061 this.nextValve = null; 062 } 063 064 this.className = className; 065 066 //Create the Valve object 067 valve = (Valve)classLoader.loadClass(className).newInstance(); 068 069 //Set the parameters 070 setParameters(valve, initParams); 071 072 } 073 074 public void doStart() throws Exception { 075 log.debug(className + " started."); 076 } 077 078 public void doStop() throws Exception { 079 log.debug(className + " stopped."); 080 } 081 082 public void doFail() { 083 log.warn(className + " failed."); 084 } 085 086 public Object getInternalObject() { 087 return valve; 088 } 089 090 public ValveGBean getNextValve() { 091 return nextValve; 092 } 093 094 public static final GBeanInfo GBEAN_INFO; 095 096 static { 097 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(ValveGBean.class, J2EE_TYPE); 098 infoFactory.addAttribute("className", String.class, true); 099 infoFactory.addAttribute("initParams", Map.class, true); 100 infoFactory.addAttribute("classLoader", ClassLoader.class, false); 101 infoFactory.addReference("NextValve", ValveGBean.class, J2EE_TYPE); 102 infoFactory.addOperation("getInternalObject"); 103 infoFactory.addOperation("getNextValve"); 104 infoFactory.setConstructor(new String[] { "className", "initParams", "NextValve", "classLoader" }); 105 GBEAN_INFO = infoFactory.getBeanInfo(); 106 } 107 108 public static GBeanInfo getGBeanInfo() { 109 return GBEAN_INFO; 110 } 111 112 }