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.util.Map;
020    
021    import org.apache.catalina.LifecycleListener;
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: 486195 $ $Date: 2006-12-12 08:42:02 -0700 (Tue, 12 Dec 2006) $
030     */
031    public class LifecycleListenerGBean extends BaseGBean implements GBeanLifecycle, ObjectRetriever {
032    
033        private static final Log log = LogFactory.getLog(LifecycleListenerGBean.class);
034    
035        public static final String J2EE_TYPE = "TomcatLifecycleListener";
036            
037        private final LifecycleListener listener;
038        private final LifecycleListenerGBean nextListener;
039        private final String className;
040     
041        
042        public LifecycleListenerGBean(){      
043            listener = null;
044            nextListener = null;
045            className = null;
046        }
047        
048        public LifecycleListenerGBean(String className, Map initParams, LifecycleListenerGBean nextListener) throws Exception{
049    
050            //Validate
051            if (className == null){
052                throw new IllegalArgumentException("className cannot be null.");
053            }
054            
055            if (nextListener != null){
056                if (!(nextListener.getInternalObject() instanceof LifecycleListener)){
057                    throw new IllegalArgumentException("The class given as the NextListener attribute does not wrap an object of org.apache.catalina.LifecycleListener type.");                
058                }
059                this.nextListener = nextListener;
060            } else {
061                this.nextListener = null;
062            }
063            
064            this.className = className;
065            
066            //Create the Listener object
067            listener = (LifecycleListener)Class.forName(className).newInstance();
068    
069            //Set the parameters
070            setParameters(listener, 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 listener;
088        }
089    
090        public LifecycleListenerGBean getNextListener() {
091            return nextListener;
092        }
093        
094        public static final GBeanInfo GBEAN_INFO;
095    
096        static {
097            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(LifecycleListenerGBean.class, J2EE_TYPE);
098            infoFactory.addAttribute("className", String.class, true);
099            infoFactory.addAttribute("initParams", Map.class, true);
100            infoFactory.addReference("NextListener", LifecycleListenerGBean.class, J2EE_TYPE);
101            infoFactory.addOperation("getInternalObject");
102            infoFactory.addOperation("getNextListener");
103            infoFactory.setConstructor(new String[] { "className", "initParams", "NextListener" });
104            GBEAN_INFO = infoFactory.getBeanInfo();
105        }
106    
107        public static GBeanInfo getGBeanInfo() {
108            return GBEAN_INFO;
109        }
110    
111    }