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.ArrayList;
020    import java.util.Iterator;
021    import java.util.Map;
022    
023    import org.apache.catalina.Cluster;
024    import org.apache.catalina.Host;
025    import org.apache.catalina.LifecycleListener;
026    import org.apache.catalina.Manager;
027    import org.apache.catalina.Realm;
028    import org.apache.catalina.Valve;
029    import org.apache.catalina.core.StandardEngine;
030    import org.apache.catalina.core.StandardHost;
031    import org.apache.commons.logging.Log;
032    import org.apache.commons.logging.LogFactory;
033    import org.apache.geronimo.gbean.GBeanInfo;
034    import org.apache.geronimo.gbean.GBeanInfoBuilder;
035    import org.apache.geronimo.gbean.GBeanLifecycle;
036    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
037    import org.apache.geronimo.tomcat.cluster.CatalinaClusterGBean;
038    
039    /**
040     * @version $Rev: 551692 $ $Date: 2007-06-28 16:36:09 -0400 (Thu, 28 Jun 2007) $
041     */
042    public class HostGBean extends BaseGBean implements GBeanLifecycle, ObjectRetriever {
043    
044        private static final Log log = LogFactory.getLog(HostGBean.class);
045        
046        public static final String J2EE_TYPE = "Host";
047        private static final String WORKDIR = "workDir";
048        private static final String NAME = "name";
049        
050        private final Host host;
051        
052        public HostGBean(){
053            host = null;
054        }
055    
056        public HostGBean(String className, 
057                Map initParams, 
058                ArrayList aliases,
059                ObjectRetriever realmGBean,            
060                ValveGBean tomcatValveChain,
061                LifecycleListenerGBean listenerChain,
062                CatalinaClusterGBean clusterGBean,
063                ManagerGBean manager) throws Exception {
064            super(); // TODO: make it an attribute
065            
066            //Validate
067            if (className == null){
068                className = "org.apache.catalina.core.StandardHost";
069            }
070            
071            if (initParams == null){
072                throw new IllegalArgumentException("Must have a 'name' value in initParams.");
073            }
074            
075            //Be sure the name has been declared.
076            if (!initParams.containsKey(NAME)){
077                throw new IllegalArgumentException("Must have a 'name' value initParams.");
078            }
079            
080            //Be sure we have a default working directory
081            if (!initParams.containsKey(WORKDIR)){
082                initParams.put(WORKDIR, "work");
083            }
084    
085            //Create the Host object
086            host = (Host)Class.forName(className).newInstance();
087            
088            //Set the parameters
089            setParameters(host, initParams);
090            
091            //Add aliases, if any
092            if (aliases != null){
093                for (Iterator iter = aliases.iterator(); iter.hasNext();) {
094                    String alias = (String) iter.next();
095                    host.addAlias(alias);
096                }
097            }
098            
099            if (realmGBean != null)
100                host.setRealm((Realm)realmGBean.getInternalObject());
101    
102            //Add the valve list
103            if (host instanceof StandardHost){
104                
105                if (tomcatValveChain != null){
106                    ValveGBean valveGBean = tomcatValveChain;
107                    while(valveGBean != null){
108                        ((StandardHost)host).addValve((Valve)valveGBean.getInternalObject());
109                        valveGBean = valveGBean.getNextValve();
110                    }
111                }
112                
113                if (listenerChain != null){
114                    LifecycleListenerGBean listenerGBean = listenerChain;
115                    while(listenerGBean != null){
116                        ((StandardHost)host).addLifecycleListener((LifecycleListener)listenerGBean.getInternalObject());
117                        listenerGBean = listenerGBean.getNextListener();
118                    }
119                }
120                
121           }
122    
123            //Add clustering
124            if (clusterGBean != null){
125                host.setCluster((Cluster)clusterGBean.getInternalObject());
126            }
127            
128            //Add manager
129            if (manager != null)
130                host.setManager((Manager)manager.getInternalObject());
131        }
132    
133        public Object getInternalObject() {
134            return host;
135        }
136    
137        public void doFail() {
138            log.warn("Failed");
139        }
140    
141        public void doStart() throws Exception {
142            log.debug("Started host name '" + host.getName() + "'");
143        }
144    
145        public void doStop() throws Exception {
146            log.debug("Stopped host '" + host.getName() + "'");
147        }
148    
149        public static final GBeanInfo GBEAN_INFO;
150    
151        static {
152            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("TomcatHost", HostGBean.class, J2EE_TYPE);
153            infoFactory.addAttribute("className", String.class, true);
154            infoFactory.addAttribute("initParams", Map.class, true);
155            infoFactory.addAttribute("aliases", ArrayList.class, true);
156            infoFactory.addReference("RealmGBean", ObjectRetriever.class, NameFactory.GERONIMO_SERVICE);
157            infoFactory.addReference("TomcatValveChain", ValveGBean.class, ValveGBean.J2EE_TYPE);
158            infoFactory.addReference("LifecycleListenerChain", LifecycleListenerGBean.class, LifecycleListenerGBean.J2EE_TYPE);
159            infoFactory.addReference("CatalinaCluster", CatalinaClusterGBean.class, CatalinaClusterGBean.J2EE_TYPE);
160            infoFactory.addReference("Manager", ManagerGBean.class, ManagerGBean.J2EE_TYPE);
161            infoFactory.addOperation("getInternalObject");
162            infoFactory.setConstructor(new String[] { 
163                    "className", 
164                    "initParams", 
165                    "aliases", 
166                    "RealmGBean", 
167                    "TomcatValveChain",
168                    "LifecycleListenerChain",
169                    "CatalinaCluster",
170                    "Manager"});
171            GBEAN_INFO = infoFactory.getBeanInfo();
172        }
173    
174        public static GBeanInfo getGBeanInfo() {
175            return GBEAN_INFO;
176        }
177    }