1 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.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 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 NAME = "name"; 46 private static final String DEFAULTHOST = "defaultHost"; 47 48 private final Engine engine; 49 50 public EngineGBean(String className, 51 Map initParams, 52 HostGBean defaultHost, 53 Collection hosts, 54 ObjectRetriever realmGBean, 55 ValveGBean tomcatValveChain, 56 ManagerGBean manager) throws Exception { 58 super(); 60 if (className == null){ 61 className = "org.apache.geronimo.tomcat.TomcatEngine"; 62 } 63 64 if (initParams == null){ 65 throw new IllegalArgumentException ("Must have 'name' value in initParams."); 66 } 67 68 if (defaultHost == null){ 70 throw new IllegalArgumentException ("Must have a 'defaultHost' attribute."); 71 } 72 73 if (!initParams.containsKey(NAME)){ 75 throw new IllegalArgumentException ("Must have a 'name' value initParams."); 76 } 77 78 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 setParameters(engine, initParams); 88 89 if (realmGBean != null){ 91 engine.setRealm((Realm)realmGBean.getInternalObject()); 92 } 93 94 final String 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 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 if (hosts instanceof ReferenceCollection) { 115 ReferenceCollection refs = (ReferenceCollection)hosts; 116 refs.addReferenceCollectionListener(new ReferenceCollectionListener() { 117 118 public void memberAdded(ReferenceCollectionEvent event) { 119 Object o = event.getMember(); 120 ObjectRetriever objectRetriever = (ObjectRetriever) o; 121 String hostName = ((Host)objectRetriever.getInternalObject()).getName(); 122 if (!hostName.equals(defaultHostName)) 123 addHost(objectRetriever); 124 } 125 126 public void memberRemoved(ReferenceCollectionEvent event) { 127 Object o = event.getMember(); 128 ObjectRetriever objectRetriever = (ObjectRetriever) o; 129 String hostName = ((Host)objectRetriever.getInternalObject()).getName(); 130 if (!hostName.equals(defaultHostName)) 131 removeHost(objectRetriever); 132 } 133 }); 134 Iterator iterator = refs.iterator(); 135 while (iterator.hasNext()){ 136 ObjectRetriever objRetriever = (ObjectRetriever)iterator.next(); 137 String hostName = ((Host)objRetriever.getInternalObject()).getName(); 138 if (!hostName.equals(defaultHostName)) 139 addHost(objRetriever); 140 } 141 } 142 143 144 } 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 (host.getRealm() == null) { 160 host.setRealm(engine.getRealm()); 161 } 162 engine.addChild(host); 163 } 164 165 public Object getInternalObject() { 166 return engine; 167 } 168 169 public void doFail() { 170 log.warn("Failed"); 171 } 172 173 public void doStart() throws Exception { 174 log.debug("Started"); 175 } 176 177 public void doStop() throws Exception { 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 .class, true); 186 infoFactory.addAttribute("initParams", Map .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("Manager", ManagerGBean.class, ManagerGBean.J2EE_TYPE); 193 infoFactory.addOperation("getInternalObject"); 194 infoFactory.setConstructor(new String [] { 195 "className", 196 "initParams", 197 "DefaultHost", 198 "Hosts", 199 "RealmGBean", 200 "TomcatValveChain", 201 "Manager"}); 203 GBEAN_INFO = infoFactory.getBeanInfo(); 204 } 205 206 public static GBeanInfo getGBeanInfo() { 207 return GBEAN_INFO; 208 } 209 } 210 | Popular Tags |