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