View Javadoc

1   /**
2    *
3    * Copyright 2003-2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.geronimo.tomcat;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.Map;
22  import org.apache.catalina.Cluster;
23  import org.apache.catalina.Host;
24  import org.apache.catalina.Manager;
25  import org.apache.catalina.Realm;
26  import org.apache.catalina.Valve;
27  import org.apache.catalina.core.StandardHost;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.geronimo.gbean.GBeanInfo;
31  import org.apache.geronimo.gbean.GBeanInfoBuilder;
32  import org.apache.geronimo.gbean.GBeanLifecycle;
33  import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
34  import org.apache.geronimo.tomcat.cluster.CatalinaClusterGBean;
35  
36  /**
37   * @version $Rev: 357408 $ $Date: 2005-12-17 16:20:34 -0800 (Sat, 17 Dec 2005) $
38   */
39  public class HostGBean extends BaseGBean implements GBeanLifecycle, ObjectRetriever {
40  
41      private static final Log log = LogFactory.getLog(HostGBean.class);
42      
43      public static final String J2EE_TYPE = "Host";
44      private static final String WORKDIR = "workDir";
45      private static final String NAME = "name";
46      
47      private final Host host;
48      
49      public HostGBean(){
50          host = null;
51      }
52  
53      public HostGBean(String className, 
54              Map initParams, 
55              ArrayList aliases,
56              ObjectRetriever realmGBean,            
57              ValveGBean tomcatValveChain,
58              CatalinaClusterGBean clusterGBean,
59              ManagerGBean manager) throws Exception {
60          super(); // TODO: make it an attribute
61          
62          //Validate
63          if (className == null){
64              className = "org.apache.catalina.core.StandardHost";
65          }
66          
67          if (initParams == null){
68              throw new IllegalArgumentException("Must have a 'name' value in initParams.");
69          }
70          
71          //Be sure the name has been declared.
72          if (!initParams.containsKey(NAME)){
73              throw new IllegalArgumentException("Must have a 'name' value initParams.");
74          }
75          
76          //Be sure we have a default working directory
77          if (!initParams.containsKey(WORKDIR)){
78              initParams.put(WORKDIR, "work");
79          }
80  
81          //Create the Host object
82          host = (Host)Class.forName(className).newInstance();
83          
84          //Set the parameters
85          setParameters(host, initParams);
86          
87          //Add aliases, if any
88          if (aliases != null){
89              for (Iterator iter = aliases.iterator(); iter.hasNext();) {
90                  String alias = (String) iter.next();
91                  host.addAlias(alias);
92              }
93          }
94          
95          if (realmGBean != null)
96              host.setRealm((Realm)realmGBean.getInternalObject());
97  
98          //Add the valve list
99          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 }