001    /**
002     *
003     * Copyright 2003-2005 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  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.util.Map;
020    
021    import org.apache.catalina.Valve;
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.apache.geronimo.gbean.GBeanInfo;
025    import org.apache.geronimo.gbean.GBeanInfoBuilder;
026    import org.apache.geronimo.gbean.GBeanLifecycle;
027    
028    /**
029     * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
030     */
031    public class ValveGBean extends BaseGBean implements GBeanLifecycle, ObjectRetriever {
032    
033        private static final Log log = LogFactory.getLog(ValveGBean.class);
034    
035        public static final String J2EE_TYPE = "TomcatValve";
036            
037        private final Valve valve;
038        private final ValveGBean nextValve;
039        private final String className;
040     
041        
042        public ValveGBean(){      
043            valve = null;
044            nextValve = null;
045            className = null;
046        }
047        
048        public ValveGBean(String className, Map initParams, ValveGBean nextValve) 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)Class.forName(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.addReference("NextValve", ValveGBean.class, J2EE_TYPE);
101            infoFactory.addOperation("getInternalObject");
102            infoFactory.addOperation("getNextValve");
103            infoFactory.setConstructor(new String[] { "className", "initParams", "NextValve" });
104            GBEAN_INFO = infoFactory.getBeanInfo();
105        }
106    
107        public static GBeanInfo getGBeanInfo() {
108            return GBEAN_INFO;
109        }
110    
111    }