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.Collection;
20 import java.util.Iterator;
21 import java.util.Map;
22 import org.apache.catalina.Cluster;
23 import org.apache.catalina.Engine;
24 import org.apache.catalina.Host;
25 import org.apache.catalina.Manager;
26 import org.apache.catalina.Realm;
27 import org.apache.catalina.Valve;
28 import org.apache.catalina.core.StandardEngine;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.geronimo.gbean.GBeanInfo;
32 import org.apache.geronimo.gbean.GBeanInfoBuilder;
33 import org.apache.geronimo.gbean.GBeanLifecycle;
34 import org.apache.geronimo.gbean.ReferenceCollection;
35 import org.apache.geronimo.gbean.ReferenceCollectionEvent;
36 import org.apache.geronimo.gbean.ReferenceCollectionListener;
37 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
38 import org.apache.geronimo.tomcat.cluster.CatalinaClusterGBean;
39
40 /**
41 * @version $Rev: 387050 $ $Date: 2006-03-19 14:43:00 -0800 (Sun, 19 Mar 2006) $
42 */
43 public class EngineGBean extends BaseGBean implements GBeanLifecycle, ObjectRetriever {
44
45 private static final Log log = LogFactory.getLog(EngineGBean.class);
46
47 private static final String NAME = "name";
48 private static final String DEFAULTHOST = "defaultHost";
49
50 private final Engine engine;
51
52 public EngineGBean(String className,
53 Map initParams,
54 HostGBean defaultHost,
55 Collection hosts,
56 ObjectRetriever realmGBean,
57 ValveGBean tomcatValveChain,
58 CatalinaClusterGBean clusterGBean,
59 ManagerGBean manager) throws Exception {
60 super();
61
62 if (className == null){
63 className = "org.apache.geronimo.tomcat.TomcatEngine";
64 }
65
66 if (initParams == null){
67 throw new IllegalArgumentException("Must have 'name' value in initParams.");
68 }
69
70
71 if (defaultHost == null){
72 throw new IllegalArgumentException("Must have a 'defaultHost' attribute.");
73 }
74
75
76 if (!initParams.containsKey(NAME)){
77 throw new IllegalArgumentException("Must have a 'name' value initParams.");
78 }
79
80
81 if (initParams.containsKey(DEFAULTHOST)){
82 log.warn("The " + DEFAULTHOST + " initParams value is no longer used and will be ignored.");
83 initParams.remove(DEFAULTHOST);
84 }
85
86 engine = (Engine)Class.forName(className).newInstance();
87
88
89 setParameters(engine, initParams);
90
91
92 if (realmGBean != null){
93 engine.setRealm((Realm)realmGBean.getInternalObject());
94 }
95
96
97 final String defaultHostName = ((Host)defaultHost.getInternalObject()).getName();
98 engine.setDefaultHost(defaultHostName);
99 addHost(defaultHost);
100
101 if (manager != null)
102 engine.setManager((Manager)manager.getInternalObject());
103
104
105 if (engine instanceof StandardEngine){
106 if (tomcatValveChain != null){
107 ValveGBean valveGBean = tomcatValveChain;
108 while(valveGBean != null){
109 ((StandardEngine)engine).addValve((Valve)valveGBean.getInternalObject());
110 valveGBean = valveGBean.getNextValve();
111 }
112 }
113 }
114
115
116 if (hosts instanceof ReferenceCollection) {
117 ReferenceCollection refs = (ReferenceCollection)hosts;
118 refs.addReferenceCollectionListener(new ReferenceCollectionListener() {
119
120 public void memberAdded(ReferenceCollectionEvent event) {
121 Object o = event.getMember();
122 ObjectRetriever objectRetriever = (ObjectRetriever) o;
123 String hostName = ((Host)objectRetriever.getInternalObject()).getName();
124 if (!hostName.equals(defaultHostName))
125 addHost(objectRetriever);
126 }
127
128 public void memberRemoved(ReferenceCollectionEvent event) {
129 Object o = event.getMember();
130 ObjectRetriever objectRetriever = (ObjectRetriever) o;
131 String hostName = ((Host)objectRetriever.getInternalObject()).getName();
132 if (!hostName.equals(defaultHostName))
133 removeHost(objectRetriever);
134 }
135 });
136 Iterator iterator = refs.iterator();
137 while (iterator.hasNext()){
138 ObjectRetriever objRetriever = (ObjectRetriever)iterator.next();
139 String hostName = ((Host)objRetriever.getInternalObject()).getName();
140 if (!hostName.equals(defaultHostName))
141 addHost(objRetriever);
142 }
143 }
144
145
146
147 if (clusterGBean != null){
148 engine.setCluster((Cluster)clusterGBean.getInternalObject());
149 }
150 }
151
152 private void removeHost(ObjectRetriever objRetriever) {
153 Host host = (Host)objRetriever.getInternalObject();
154 engine.removeChild(host);
155 }
156
157 private void addHost(ObjectRetriever objRetriever) {
158 Host host = (Host)objRetriever.getInternalObject();
159
160
161 if (host.getRealm() == null) {
162 host.setRealm(engine.getRealm());
163 }
164 engine.addChild(host);
165 }
166
167 public Object getInternalObject() {
168 return engine;
169 }
170
171 public void doFail() {
172 log.warn("Failed");
173 }
174
175 public void doStart() throws Exception {
176 log.debug("Started");
177 }
178
179 public void doStop() throws Exception {
180 log.debug("Stopped");
181 }
182
183 public static final GBeanInfo GBEAN_INFO;
184
185 static {
186 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("TomcatEngine", EngineGBean.class);
187 infoFactory.addAttribute("className", String.class, true);
188 infoFactory.addAttribute("initParams", Map.class, true);
189 infoFactory.addReference("DefaultHost", HostGBean.class, HostGBean.J2EE_TYPE);
190 infoFactory.addReference("Hosts", ObjectRetriever.class, HostGBean.J2EE_TYPE);
191 infoFactory.addReference("RealmGBean", ObjectRetriever.class, NameFactory.GERONIMO_SERVICE);
192 infoFactory.addReference("TomcatValveChain", ValveGBean.class, ValveGBean.J2EE_TYPE);
193 infoFactory.addReference("CatalinaCluster", CatalinaClusterGBean.class, CatalinaClusterGBean.J2EE_TYPE);
194 infoFactory.addReference("Manager", ManagerGBean.class, ManagerGBean.J2EE_TYPE);
195 infoFactory.addOperation("getInternalObject");
196 infoFactory.setConstructor(new String[] {
197 "className",
198 "initParams",
199 "DefaultHost",
200 "Hosts",
201 "RealmGBean",
202 "TomcatValveChain",
203 "CatalinaCluster",
204 "Manager"});
205 GBEAN_INFO = infoFactory.getBeanInfo();
206 }
207
208 public static GBeanInfo getGBeanInfo() {
209 return GBEAN_INFO;
210 }
211 }