KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > tomcat > EngineGBean


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import org.apache.catalina.Engine;
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.StandardEngine;
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.gbean.ReferenceCollection;
34 import org.apache.geronimo.gbean.ReferenceCollectionEvent;
35 import org.apache.geronimo.gbean.ReferenceCollectionListener;
36 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
37
38 /**
39  * @version $Rev: 486195 $ $Date: 2006-12-12 10:42:02 -0500 (Tue, 12 Dec 2006) $
40  */

41 public class EngineGBean extends BaseGBean implements GBeanLifecycle, ObjectRetriever {
42
43     private static final Log log = LogFactory.getLog(EngineGBean.class);
44
45     private static final String JavaDoc NAME = "name";
46     private static final String JavaDoc DEFAULTHOST = "defaultHost";
47
48     private final Engine engine;
49
50     public EngineGBean(String JavaDoc className,
51             Map JavaDoc initParams,
52             HostGBean defaultHost,
53             Collection JavaDoc hosts,
54             ObjectRetriever realmGBean,
55             ValveGBean tomcatValveChain,
56 // CatalinaClusterGBean clusterGBean,
57
ManagerGBean manager) throws Exception JavaDoc {
58         super(); // TODO: make it an attribute
59

60         if (className == null){
61             className = "org.apache.geronimo.tomcat.TomcatEngine";
62         }
63
64         if (initParams == null){
65             throw new IllegalArgumentException JavaDoc("Must have 'name' value in initParams.");
66         }
67         
68         //Be sure the defaulthost has been declared.
69
if (defaultHost == null){
70             throw new IllegalArgumentException JavaDoc("Must have a 'defaultHost' attribute.");
71         }
72
73         //Be sure the name has been declared.
74
if (!initParams.containsKey(NAME)){
75             throw new IllegalArgumentException JavaDoc("Must have a 'name' value initParams.");
76         }
77         
78         //Deprecate the defaultHost initParam
79
if (initParams.containsKey(DEFAULTHOST)){
80             log.warn("The " + DEFAULTHOST + " initParams value is no longer used and will be ignored.");
81             initParams.remove(DEFAULTHOST);
82         }
83
84         engine = (Engine)Class.forName(className).newInstance();
85
86         //Set the parameters
87
setParameters(engine, initParams);
88         
89         //Set realm (must be before Hosts)
90
if (realmGBean != null){
91             engine.setRealm((Realm)realmGBean.getInternalObject());
92         }
93         
94         //Set the default Host
95
final String JavaDoc defaultHostName = ((Host)defaultHost.getInternalObject()).getName();
96         engine.setDefaultHost(defaultHostName);
97         addHost(defaultHost);
98
99         if (manager != null)
100             engine.setManager((Manager)manager.getInternalObject());
101
102         //Add the valve list
103
if (engine instanceof StandardEngine){
104             if (tomcatValveChain != null){
105                 ValveGBean valveGBean = tomcatValveChain;
106                 while(valveGBean != null){
107                     ((StandardEngine)engine).addValve((Valve)valveGBean.getInternalObject());
108                     valveGBean = valveGBean.getNextValve();
109                 }
110             }
111         }
112
113         //Add the hosts
114
if (hosts instanceof ReferenceCollection) {
115             ReferenceCollection refs = (ReferenceCollection)hosts;
116             refs.addReferenceCollectionListener(new ReferenceCollectionListener() {
117
118                 public void memberAdded(ReferenceCollectionEvent event) {
119                     Object JavaDoc o = event.getMember();
120                     ObjectRetriever objectRetriever = (ObjectRetriever) o;
121                     String JavaDoc hostName = ((Host)objectRetriever.getInternalObject()).getName();
122                     if (!hostName.equals(defaultHostName))
123                         addHost(objectRetriever);
124                 }
125
126                 public void memberRemoved(ReferenceCollectionEvent event) {
127                     Object JavaDoc o = event.getMember();
128                     ObjectRetriever objectRetriever = (ObjectRetriever) o;
129                     String JavaDoc hostName = ((Host)objectRetriever.getInternalObject()).getName();
130                     if (!hostName.equals(defaultHostName))
131                         removeHost(objectRetriever);
132                 }
133             });
134             Iterator JavaDoc iterator = refs.iterator();
135             while (iterator.hasNext()){
136                 ObjectRetriever objRetriever = (ObjectRetriever)iterator.next();
137                 String JavaDoc hostName = ((Host)objRetriever.getInternalObject()).getName();
138                 if (!hostName.equals(defaultHostName))
139                     addHost(objRetriever);
140             }
141         }
142
143
144         //Add clustering
145
// if (clusterGBean != null){
146
// engine.setCluster((Cluster)clusterGBean.getInternalObject());
147
// }
148
}
149
150     private void removeHost(ObjectRetriever objRetriever) {
151         Host host = (Host)objRetriever.getInternalObject();
152         engine.removeChild(host);
153     }
154
155     private void addHost(ObjectRetriever objRetriever) {
156         Host host = (Host)objRetriever.getInternalObject();
157
158         //If we didn't set a realm, then use the default
159
if (host.getRealm() == null) {
160             host.setRealm(engine.getRealm());
161         }
162         engine.addChild(host);
163     }
164
165     public Object JavaDoc getInternalObject() {
166         return engine;
167     }
168
169     public void doFail() {
170         log.warn("Failed");
171     }
172
173     public void doStart() throws Exception JavaDoc {
174         log.debug("Started");
175     }
176
177     public void doStop() throws Exception JavaDoc {
178         log.debug("Stopped");
179     }
180
181     public static final GBeanInfo GBEAN_INFO;
182
183     static {
184         GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("TomcatEngine", EngineGBean.class);
185         infoFactory.addAttribute("className", String JavaDoc.class, true);
186         infoFactory.addAttribute("initParams", Map JavaDoc.class, true);
187         infoFactory.addReference("DefaultHost", HostGBean.class, HostGBean.J2EE_TYPE);
188         infoFactory.addReference("Hosts", ObjectRetriever.class, HostGBean.J2EE_TYPE);
189         infoFactory.addReference("RealmGBean", ObjectRetriever.class, NameFactory.GERONIMO_SERVICE);
190         infoFactory.addReference("TomcatValveChain", ValveGBean.class, ValveGBean.J2EE_TYPE);
191 // infoFactory.addReference("CatalinaCluster", CatalinaClusterGBean.class, CatalinaClusterGBean.J2EE_TYPE);
192
infoFactory.addReference("Manager", ManagerGBean.class, ManagerGBean.J2EE_TYPE);
193         infoFactory.addOperation("getInternalObject");
194         infoFactory.setConstructor(new String JavaDoc[] {
195                 "className",
196                 "initParams",
197                 "DefaultHost",
198                 "Hosts",
199                 "RealmGBean",
200                 "TomcatValveChain",
201 // "CatalinaCluster",
202
"Manager"});
203         GBEAN_INFO = infoFactory.getBeanInfo();
204     }
205
206     public static GBeanInfo getGBeanInfo() {
207         return GBEAN_INFO;
208     }
209 }
210
Popular Tags