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.ArrayList;
019: import java.util.Iterator;
020: import java.util.Map;
021:
022: import org.apache.catalina.Cluster;
023: import org.apache.catalina.Host;
024: import org.apache.catalina.LifecycleListener;
025: import org.apache.catalina.Manager;
026: import org.apache.catalina.Realm;
027: import org.apache.catalina.Valve;
028: import org.apache.catalina.core.StandardEngine;
029: import org.apache.catalina.core.StandardHost;
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.j2ee.j2eeobjectnames.NameFactory;
036: import org.apache.geronimo.tomcat.cluster.CatalinaClusterGBean;
037:
038: /**
039: * @version $Rev: 551692 $ $Date: 2007-06-28 13:36:09 -0700 (Thu, 28 Jun 2007) $
040: */
041: public class HostGBean extends BaseGBean implements GBeanLifecycle,
042: 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, Map initParams,
057: ArrayList aliases, ObjectRetriever realmGBean,
058: ValveGBean tomcatValveChain,
059: LifecycleListenerGBean listenerChain,
060: CatalinaClusterGBean clusterGBean, ManagerGBean manager)
061: throws Exception {
062: super (); // TODO: make it an attribute
063:
064: //Validate
065: if (className == null) {
066: className = "org.apache.catalina.core.StandardHost";
067: }
068:
069: if (initParams == null) {
070: throw new IllegalArgumentException(
071: "Must have a 'name' value in initParams.");
072: }
073:
074: //Be sure the name has been declared.
075: if (!initParams.containsKey(NAME)) {
076: throw new IllegalArgumentException(
077: "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
109: .getInternalObject());
110: valveGBean = valveGBean.getNextValve();
111: }
112: }
113:
114: if (listenerChain != null) {
115: LifecycleListenerGBean listenerGBean = listenerChain;
116: while (listenerGBean != null) {
117: ((StandardHost) host)
118: .addLifecycleListener((LifecycleListener) listenerGBean
119: .getInternalObject());
120: listenerGBean = listenerGBean.getNextListener();
121: }
122: }
123:
124: }
125:
126: //Add clustering
127: if (clusterGBean != null) {
128: host.setCluster((Cluster) clusterGBean.getInternalObject());
129: }
130:
131: //Add manager
132: if (manager != null)
133: host.setManager((Manager) manager.getInternalObject());
134: }
135:
136: public Object getInternalObject() {
137: return host;
138: }
139:
140: public void doFail() {
141: log.warn("Failed");
142: }
143:
144: public void doStart() throws Exception {
145: log.debug("Started host name '" + host.getName() + "'");
146: }
147:
148: public void doStop() throws Exception {
149: log.debug("Stopped host '" + host.getName() + "'");
150: }
151:
152: public static final GBeanInfo GBEAN_INFO;
153:
154: static {
155: GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(
156: "TomcatHost", HostGBean.class, J2EE_TYPE);
157: infoFactory.addAttribute("className", String.class, true);
158: infoFactory.addAttribute("initParams", Map.class, true);
159: infoFactory.addAttribute("aliases", ArrayList.class, true);
160: infoFactory.addReference("RealmGBean", ObjectRetriever.class,
161: NameFactory.GERONIMO_SERVICE);
162: infoFactory.addReference("TomcatValveChain", ValveGBean.class,
163: ValveGBean.J2EE_TYPE);
164: infoFactory.addReference("LifecycleListenerChain",
165: LifecycleListenerGBean.class,
166: LifecycleListenerGBean.J2EE_TYPE);
167: infoFactory.addReference("CatalinaCluster",
168: CatalinaClusterGBean.class,
169: CatalinaClusterGBean.J2EE_TYPE);
170: infoFactory.addReference("Manager", ManagerGBean.class,
171: ManagerGBean.J2EE_TYPE);
172: infoFactory.addOperation("getInternalObject");
173: infoFactory.setConstructor(new String[] { "className",
174: "initParams", "aliases", "RealmGBean",
175: "TomcatValveChain", "LifecycleListenerChain",
176: "CatalinaCluster", "Manager" });
177: GBEAN_INFO = infoFactory.getBeanInfo();
178: }
179:
180: public static GBeanInfo getGBeanInfo() {
181: return GBEAN_INFO;
182: }
183: }
|