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.cluster;
018    
019    import java.util.Map;
020    
021    import org.apache.catalina.ha.ClusterListener;
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    import org.apache.geronimo.tomcat.BaseGBean;
028    import org.apache.geronimo.tomcat.ObjectRetriever;
029    
030    public class ClusterListenerGBean extends BaseGBean implements GBeanLifecycle, ObjectRetriever {
031    
032        private static final Log log = LogFactory.getLog(ClusterListenerGBean.class);
033    
034        public static final String J2EE_TYPE = "ClusterListener";
035            
036        private final ClusterListener listener;
037        private final ClusterListenerGBean nextListener;
038        private final String className;
039     
040        
041        public ClusterListenerGBean(){      
042            listener = null;
043            nextListener = null;
044            className = null;
045        }
046        
047        public ClusterListenerGBean(String className, Map initParams, ClusterListenerGBean nextListener) throws Exception{
048    
049            //Validate
050            if (className == null){
051                throw new IllegalArgumentException("className cannot be null.");
052            }
053            
054            if (nextListener != null){
055                if (!(nextListener.getInternalObject() instanceof ClusterListener)){
056                    throw new IllegalArgumentException("nextListener is not of type ClusterListener.");                
057                }
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 = (ClusterListener)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 ClusterListenerGBean getNextValve() {
091            return nextListener;
092        }
093        
094        public static final GBeanInfo GBEAN_INFO;
095    
096        static {
097            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("ClusterListener", ClusterListenerGBean.class, J2EE_TYPE);
098            infoFactory.addAttribute("className", String.class, true);
099            infoFactory.addAttribute("initParams", Map.class, true);
100            infoFactory.addReference("NextListener", ClusterListenerGBean.class, J2EE_TYPE);
101            infoFactory.addOperation("getInternalObject", "Object");
102            infoFactory.addOperation("getNextValve", "ClusterListenerGBean");
103            infoFactory.setConstructor(new String[] { 
104                    "className", 
105                    "initParams", 
106                    "NextListener" });
107            GBEAN_INFO = infoFactory.getBeanInfo();
108        }
109    
110        public static GBeanInfo getGBeanInfo() {
111            return GBEAN_INFO;
112        }
113    
114    }