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.Valve;
022    import org.apache.catalina.ha.CatalinaCluster;
023    import org.apache.catalina.ha.ClusterDeployer;
024    import org.apache.catalina.ha.ClusterListener;
025    import org.apache.catalina.tribes.Channel;
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    import org.apache.geronimo.gbean.GBeanInfo;
029    import org.apache.geronimo.gbean.GBeanInfoBuilder;
030    import org.apache.geronimo.gbean.GBeanLifecycle;
031    import org.apache.geronimo.tomcat.BaseGBean;
032    import org.apache.geronimo.tomcat.ObjectRetriever;
033    import org.apache.geronimo.tomcat.ValveGBean;
034    
035    /**
036    * @version $Rev: 511136 $ $Date: 2007-02-23 17:12:09 -0500 (Fri, 23 Feb 2007) $
037    */
038    public class CatalinaClusterGBean extends BaseGBean implements GBeanLifecycle, ObjectRetriever {
039    
040       private static final Log log = LogFactory.getLog(CatalinaClusterGBean.class);
041       
042       public static final String J2EE_TYPE = "Cluster";
043       
044       private final CatalinaCluster cluster;
045       
046       public CatalinaClusterGBean(){
047           cluster=null;
048       }
049    
050       public CatalinaClusterGBean(String className, 
051               Map initParams,            
052               ClusterListenerGBean clusterListenerChain,
053               ValveGBean tomcatValveChain,
054               ClusterDeployerGBean deployer,
055               ChannelGBean channel) throws Exception {
056           
057           super(); // TODO: make it an attribute
058           
059           //Validate
060           if (className == null){
061               throw new IllegalArgumentException("Must have a 'className' attribute.");
062           }
063                  
064           //Create the CatalinaCluster object
065           cluster = (CatalinaCluster)Class.forName(className).newInstance();
066           
067           //Set the parameters
068           setParameters(cluster, initParams);
069           
070           //Add the cluster listeners list
071           if (clusterListenerChain != null){
072               ClusterListenerGBean clusterListenerGBean = clusterListenerChain;
073               while(clusterListenerGBean != null){
074                   cluster.addClusterListener((ClusterListener)clusterListenerGBean.getInternalObject());
075                   clusterListenerGBean = clusterListenerGBean.getNextValve();
076               }
077           }
078    
079           //Add the valve list
080           if (tomcatValveChain != null){
081               ValveGBean valveGBean = tomcatValveChain;
082               while(valveGBean != null){
083                   cluster.addValve((Valve)valveGBean.getInternalObject());
084                   valveGBean = valveGBean.getNextValve();
085               }
086           }
087           
088           //Add deployer
089           if (deployer != null){
090               cluster.setClusterDeployer((ClusterDeployer)deployer.getInternalObject());
091           }
092    
093           //Add channel
094           if (channel != null){
095               cluster.setChannel((Channel)channel.getInternalObject());
096           }
097       }
098    
099       public Object getInternalObject() {
100           return cluster;
101       }
102    
103       public void doFail() {
104           log.warn("Failed");
105       }
106    
107       public void doStart() throws Exception {
108           log.debug("Started cluster gbean.");
109       }
110    
111       public void doStop() throws Exception {
112           log.debug("Stopped cluster gbean.");
113       }
114    
115       public static final GBeanInfo GBEAN_INFO;
116    
117       static {
118           GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("CatalinaCluster", CatalinaClusterGBean.class, J2EE_TYPE);
119           infoFactory.addAttribute("className", String.class, true);
120           infoFactory.addAttribute("initParams", Map.class, true);
121           infoFactory.addReference("ClusterListenerChain", ClusterListenerGBean.class, ClusterListenerGBean.J2EE_TYPE);
122           infoFactory.addReference("TomcatValveChain", ValveGBean.class, ValveGBean.J2EE_TYPE);
123           infoFactory.addReference("ClusterDeployer", ClusterDeployerGBean.class, ClusterDeployerGBean.J2EE_TYPE);
124           infoFactory.addReference("Channel", ChannelGBean.class, ChannelGBean.J2EE_TYPE);
125           infoFactory.addOperation("getInternalObject", "Object");
126           infoFactory.setConstructor(new String[] { 
127                   "className", 
128                   "initParams", 
129                   "ClusterListenerChain",
130                   "TomcatValveChain",
131                   "ClusterDeployer",
132                   "Channel"});
133           GBEAN_INFO = infoFactory.getBeanInfo();
134       }
135    
136       public static GBeanInfo getGBeanInfo() {
137           return GBEAN_INFO;
138       }
139    }