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: */package org.apache.geronimo.tomcat;
017:
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.Map;
021:
022: import org.apache.catalina.Cluster;
023: import org.apache.catalina.Engine;
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.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.geronimo.gbean.GBeanInfo;
033: import org.apache.geronimo.gbean.GBeanInfoBuilder;
034: import org.apache.geronimo.gbean.GBeanLifecycle;
035: import org.apache.geronimo.gbean.ReferenceCollection;
036: import org.apache.geronimo.gbean.ReferenceCollectionEvent;
037: import org.apache.geronimo.gbean.ReferenceCollectionListener;
038: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
039: import org.apache.geronimo.system.jmx.MBeanServerReference;
040: import org.apache.geronimo.tomcat.cluster.CatalinaClusterGBean;
041: import org.apache.tomcat.util.modeler.Registry;
042:
043: /**
044: * @version $Rev: 554952 $ $Date: 2007-07-10 07:07:16 -0700 (Tue, 10 Jul 2007) $
045: */
046: public class EngineGBean extends BaseGBean implements GBeanLifecycle,
047: ObjectRetriever {
048:
049: private static final Log log = LogFactory.getLog(EngineGBean.class);
050:
051: private static final String NAME = "name";
052: private static final String DEFAULTHOST = "defaultHost";
053:
054: private final Engine engine;
055:
056: public EngineGBean(String className, Map initParams,
057: HostGBean defaultHost, Collection hosts,
058: ObjectRetriever realmGBean, ValveGBean tomcatValveChain,
059: LifecycleListenerGBean listenerChain,
060: CatalinaClusterGBean clusterGBean, ManagerGBean manager,
061: MBeanServerReference mbeanServerReference) throws Exception {
062: super (); // TODO: make it an attribute
063:
064: if (className == null) {
065: className = "org.apache.geronimo.tomcat.TomcatEngine";
066: }
067:
068: if (initParams == null) {
069: throw new IllegalArgumentException(
070: "Must have 'name' value in initParams.");
071: }
072:
073: //Be sure the defaulthost has been declared.
074: if (defaultHost == null) {
075: throw new IllegalArgumentException(
076: "Must have a 'defaultHost' attribute.");
077: }
078:
079: //Be sure the name has been declared.
080: if (!initParams.containsKey(NAME)) {
081: throw new IllegalArgumentException(
082: "Must have a 'name' value initParams.");
083: }
084:
085: //Deprecate the defaultHost initParam
086: if (initParams.containsKey(DEFAULTHOST)) {
087: log
088: .warn("The "
089: + DEFAULTHOST
090: + " initParams value is no longer used and will be ignored.");
091: initParams.remove(DEFAULTHOST);
092: }
093:
094: engine = (Engine) Class.forName(className).newInstance();
095:
096: //Set the parameters
097: setParameters(engine, initParams);
098:
099: //Set realm (must be before Hosts)
100: if (realmGBean != null) {
101: engine.setRealm((Realm) realmGBean.getInternalObject());
102: }
103:
104: //Set the default Host
105: final String defaultHostName = ((Host) defaultHost
106: .getInternalObject()).getName();
107: engine.setDefaultHost(defaultHostName);
108: addHost(defaultHost);
109:
110: if (manager != null)
111: engine.setManager((Manager) manager.getInternalObject());
112:
113: //Add the valve and listener lists
114: if (engine instanceof StandardEngine) {
115: if (tomcatValveChain != null) {
116: ValveGBean valveGBean = tomcatValveChain;
117: while (valveGBean != null) {
118: ((StandardEngine) engine)
119: .addValve((Valve) valveGBean
120: .getInternalObject());
121: valveGBean = valveGBean.getNextValve();
122: }
123: }
124:
125: if (listenerChain != null) {
126: LifecycleListenerGBean listenerGBean = listenerChain;
127: while (listenerGBean != null) {
128: ((StandardEngine) engine)
129: .addLifecycleListener((LifecycleListener) listenerGBean
130: .getInternalObject());
131: listenerGBean = listenerGBean.getNextListener();
132: }
133: }
134: }
135:
136: if (mbeanServerReference != null) {
137: Registry.setServer(mbeanServerReference.getMBeanServer());
138: }
139:
140: //Add the hosts
141: if (hosts instanceof ReferenceCollection) {
142: ReferenceCollection refs = (ReferenceCollection) hosts;
143: refs
144: .addReferenceCollectionListener(new ReferenceCollectionListener() {
145:
146: public void memberAdded(
147: ReferenceCollectionEvent event) {
148: Object o = event.getMember();
149: ObjectRetriever objectRetriever = (ObjectRetriever) o;
150: String hostName = ((Host) objectRetriever
151: .getInternalObject()).getName();
152: if (!hostName.equals(defaultHostName))
153: addHost(objectRetriever);
154: }
155:
156: public void memberRemoved(
157: ReferenceCollectionEvent event) {
158: Object o = event.getMember();
159: ObjectRetriever objectRetriever = (ObjectRetriever) o;
160: String hostName = ((Host) objectRetriever
161: .getInternalObject()).getName();
162: if (!hostName.equals(defaultHostName))
163: removeHost(objectRetriever);
164: }
165: });
166: Iterator iterator = refs.iterator();
167: while (iterator.hasNext()) {
168: ObjectRetriever objRetriever = (ObjectRetriever) iterator
169: .next();
170: String hostName = ((Host) objRetriever
171: .getInternalObject()).getName();
172: if (!hostName.equals(defaultHostName))
173: addHost(objRetriever);
174: }
175: }
176:
177: //Add clustering
178: if (clusterGBean != null) {
179: engine.setCluster((Cluster) clusterGBean
180: .getInternalObject());
181: }
182: }
183:
184: private void removeHost(ObjectRetriever objRetriever) {
185: Host host = (Host) objRetriever.getInternalObject();
186: engine.removeChild(host);
187: }
188:
189: private void addHost(ObjectRetriever objRetriever) {
190: Host host = (Host) objRetriever.getInternalObject();
191:
192: //If we didn't set a realm, then use the default
193: if (host.getRealm() == null) {
194: host.setRealm(engine.getRealm());
195: }
196: engine.addChild(host);
197: }
198:
199: public Object getInternalObject() {
200: return engine;
201: }
202:
203: public void doFail() {
204: log.warn("Failed");
205: }
206:
207: public void doStart() throws Exception {
208: log.debug("Started");
209: }
210:
211: public void doStop() throws Exception {
212: log.debug("Stopped");
213: }
214:
215: public static final GBeanInfo GBEAN_INFO;
216:
217: static {
218: GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(
219: "TomcatEngine", EngineGBean.class);
220: infoFactory.addAttribute("className", String.class, true);
221: infoFactory.addAttribute("initParams", Map.class, true);
222: infoFactory.addReference("DefaultHost", HostGBean.class,
223: HostGBean.J2EE_TYPE);
224: infoFactory.addReference("Hosts", ObjectRetriever.class,
225: HostGBean.J2EE_TYPE);
226: infoFactory.addReference("RealmGBean", ObjectRetriever.class,
227: NameFactory.GERONIMO_SERVICE);
228: infoFactory.addReference("TomcatValveChain", ValveGBean.class,
229: ValveGBean.J2EE_TYPE);
230: infoFactory.addReference("LifecycleListenerChain",
231: LifecycleListenerGBean.class,
232: LifecycleListenerGBean.J2EE_TYPE);
233: infoFactory.addReference("CatalinaCluster",
234: CatalinaClusterGBean.class,
235: CatalinaClusterGBean.J2EE_TYPE);
236: infoFactory.addReference("Manager", ManagerGBean.class,
237: ManagerGBean.J2EE_TYPE);
238: infoFactory.addReference("MBeanServerReference",
239: MBeanServerReference.class);
240: infoFactory.addOperation("getInternalObject");
241: infoFactory.setConstructor(new String[] { "className",
242: "initParams", "DefaultHost", "Hosts", "RealmGBean",
243: "TomcatValveChain", "LifecycleListenerChain",
244: "CatalinaCluster", "Manager", "MBeanServerReference" });
245: GBEAN_INFO = infoFactory.getBeanInfo();
246: }
247:
248: public static GBeanInfo getGBeanInfo() {
249: return GBEAN_INFO;
250: }
251: }
|