1 17 package org.apache.geronimo.tomcat; 18 19 import java.io.File ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 import java.net.URLStreamHandlerFactory ; 23 import java.net.URL ; 24 25 import org.apache.catalina.Container; 26 import org.apache.catalina.Context; 27 import org.apache.catalina.Engine; 28 import org.apache.catalina.Realm; 29 import org.apache.catalina.connector.Connector; 30 import org.apache.catalina.realm.JAASRealm; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.apache.geronimo.gbean.GBeanInfo; 34 import org.apache.geronimo.gbean.GBeanInfoBuilder; 35 import org.apache.geronimo.gbean.GBeanLifecycle; 36 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 37 import org.apache.geronimo.management.geronimo.NetworkConnector; 38 import org.apache.geronimo.management.geronimo.WebManager; 39 import org.apache.geronimo.system.serverinfo.ServerInfo; 40 import org.apache.geronimo.tomcat.realm.TomcatGeronimoRealm; 41 import org.apache.geronimo.tomcat.realm.TomcatJAASRealm; 42 import org.apache.geronimo.tomcat.util.SecurityHolder; 43 import org.apache.geronimo.webservices.SoapHandler; 44 import org.apache.geronimo.webservices.WebServiceContainer; 45 import org.apache.naming.resources.DirContextURLStreamHandlerFactory; 46 47 48 55 public class TomcatContainer implements SoapHandler, GBeanLifecycle, TomcatWebContainer { 56 57 private static final Log log = LogFactory.getLog(TomcatContainer.class); 58 59 62 private static final String DEFAULT_CATALINA_HOME = "var/catalina"; 63 64 67 private TomcatGeronimoEmbedded embedded; 68 69 72 private Engine engine; 73 74 77 private ClassLoader classLoader; 78 79 private final Map webServices = new HashMap (); 80 private final String objectName; 81 private final WebManager manager; 82 private static boolean first = true; 83 84 public TomcatContainer() { 86 this.objectName = null; setCatalinaHome(DEFAULT_CATALINA_HOME); 88 manager = null; 89 } 90 91 94 public TomcatContainer(ClassLoader classLoader, String catalinaHome, ObjectRetriever engineGBean, ServerInfo serverInfo, String objectName, WebManager manager) { 95 URLStreamHandlerFactory streamHandlerFactory = 97 new DirContextURLStreamHandlerFactory(); 98 if (first) { 99 first = false; 100 try { 101 URL.setURLStreamHandlerFactory(streamHandlerFactory); 102 } catch (Exception e) { 103 log.error("Error registering jndi stream handler", e); 105 } catch (Throwable t) { 106 log.info("Dual registration of jndi stream handler: " 108 + t.getMessage()); 109 } 110 } 111 112 113 if (catalinaHome == null) 114 catalinaHome = DEFAULT_CATALINA_HOME; 115 116 setCatalinaHome(serverInfo.resolveServerPath(catalinaHome)); 117 118 if (classLoader == null) { 119 throw new IllegalArgumentException ("classLoader cannot be null."); 120 } 121 122 if (engineGBean == null) { 123 throw new IllegalArgumentException ("engineGBean cannot be null."); 124 } 125 126 this.classLoader = classLoader; 127 128 this.engine = (Engine) engineGBean.getInternalObject(); 129 130 this.objectName = objectName; 131 this.manager = manager; 132 } 133 134 public String getObjectName() { 135 return objectName; 136 } 137 138 public boolean isStateManageable() { 139 return true; 140 } 141 142 public boolean isStatisticsProvider() { 143 return false; } 145 146 public boolean isEventProvider() { 147 return true; 148 } 149 150 public NetworkConnector[] getConnectors() { 151 return manager.getConnectorsForContainer(this); 152 } 153 154 public NetworkConnector[] getConnectors(String protocol) { 155 return manager.getConnectorsForContainer(this, protocol); 156 } 157 158 public void doFail() { 159 try { 160 doStop(); 161 } catch (Exception ignored) { 162 } 163 } 164 165 170 public void doStart() throws Exception { 171 log.debug("doStart()"); 172 173 log.debug("Endorsed Dirs set to:" + System.getProperty("java.endorsed.dirs")); 174 175 177 if (embedded == null) { 179 embedded = new TomcatGeronimoEmbedded(); 180 } 181 182 187 188 embedded.setUseNaming(false); 192 193 File rootContext = new File (System.getProperty("catalina.home") + "/ROOT"); 195 196 String docBase = ""; 197 if (rootContext.exists()) { 198 docBase = "ROOT"; 199 } 200 201 Container[] hosts = engine.findChildren(); 202 Context defaultContext; 203 for (int i = 0; i < hosts.length; i++) { 204 defaultContext = embedded.createContext("", docBase, classLoader); 205 if (defaultContext instanceof GeronimoStandardContext) { 206 GeronimoStandardContext ctx = (GeronimoStandardContext) defaultContext; 207 ctx.setJ2EEApplication(null); 210 ctx.setJ2EEServer("geronimo"); 212 } 213 hosts[i].addChild(defaultContext); 214 } 215 216 embedded.addEngine(engine); 219 220 embedded.start(); 223 } 224 225 public void doStop() throws Exception { 226 if (embedded != null) { 227 embedded.stop(); 228 embedded = null; 229 } 230 231 } 232 233 242 public void addContext(TomcatContext ctx) throws Exception { 243 Context anotherCtxObj = embedded.createContext(ctx.getContextPath(), ctx.getDocBase(), ctx.getClassLoader()); 244 245 ctx.setContext(anotherCtxObj); 247 248 if (anotherCtxObj instanceof GeronimoStandardContext) { 250 ((GeronimoStandardContext) anotherCtxObj).setContextProperties(ctx); 251 } 252 String virtualServer = ctx.getVirtualServer(); 254 if (virtualServer == null) { 255 virtualServer = engine.getDefaultHost(); 256 } 257 Container host = engine.findChild(virtualServer); 258 if (host == null) { 259 throw new IllegalArgumentException ("Invalid virtual host '" + virtualServer + "'. Do you have a matching Host entry in the plan?"); 260 } 261 262 String securityRealmName = null; 264 SecurityHolder secHolder = ctx.getSecurityHolder(); 265 if (secHolder != null) 266 securityRealmName = secHolder.getSecurityRealm(); 267 268 if (ctx.getRealm() != null) { 270 Realm realm = ctx.getRealm(); 271 272 if (securityRealmName != null) { 275 if (realm instanceof JAASRealm) { 276 ((JAASRealm) realm).setAppName(securityRealmName); 277 } 278 } 279 anotherCtxObj.setRealm(realm); 280 } else { 281 Realm realm = host.getRealm(); 282 if (securityRealmName != null) { 284 String parentRealmName = null; 285 if (realm instanceof JAASRealm) { 286 parentRealmName = ((JAASRealm) realm).getAppName(); 287 } 288 289 if (!securityRealmName.equals(parentRealmName)) { 291 293 if (secHolder.isSecurity()) { 295 realm = new TomcatGeronimoRealm(); 297 } else { 298 realm = new TomcatJAASRealm(); 300 } 301 302 log.debug("The security-realm-name '" + securityRealmName + 303 "' was specified and a parent (Engine/Host) is not named the same or no RealmGBean was configured for this context. " + 304 "Creating a default " + realm.getClass().getName() + 305 " adapter for this context."); 306 307 ((JAASRealm) realm).setUserClassNames("org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal"); 308 ((JAASRealm) realm).setRoleClassNames("org.apache.geronimo.security.realm.providers.GeronimoGroupPrincipal"); 309 ((JAASRealm) realm).setAppName(securityRealmName); 310 311 anotherCtxObj.setRealm(realm); 312 } else { 313 anotherCtxObj.setRealm(realm); 315 } 316 } else { 317 anotherCtxObj.setRealm(realm); 318 } 319 } 320 321 host.addChild(anotherCtxObj); 322 } 323 324 public void removeContext(TomcatContext ctx) { 325 Context context = ctx.getContext(); 326 327 if (context != null) { 328 if (context instanceof GeronimoStandardContext) { 329 GeronimoStandardContext stdctx = (GeronimoStandardContext) context; 330 331 try { 332 stdctx.stop(); 333 stdctx.destroy(); 334 } catch (Exception e) { 335 throw new RuntimeException (e); 336 } 337 338 } 339 context.getParent().removeChild(context); 340 } 341 342 } 343 344 public void setCatalinaHome(String catalinaHome) { 345 System.setProperty("catalina.home", catalinaHome); 346 } 347 348 public void addConnector(Connector connector) { 349 embedded.addConnector(connector); 350 } 351 352 public void removeConnector(Connector connector) { 353 embedded.removeConnector(connector); 354 } 355 356 public void addWebService(String contextPath, String [] virtualHosts, WebServiceContainer webServiceContainer, String securityRealmName, String realmName, String transportGuarantee, String authMethod, ClassLoader classLoader) throws Exception { 357 Context webServiceContext = embedded.createEJBWebServiceContext(contextPath, webServiceContainer, securityRealmName, realmName, transportGuarantee, authMethod, classLoader); 358 359 String virtualServer; 360 if (virtualHosts != null && virtualHosts.length > 0) { 361 virtualServer = virtualHosts[0]; 362 } else { 363 virtualServer = engine.getDefaultHost(); 364 } 365 366 Container host = engine.findChild(virtualServer); 367 if (host == null) { 368 throw new IllegalArgumentException ("Invalid virtual host '" + virtualServer + "'. Do you have a matchiing Host entry in the plan?"); 369 } 370 371 host.addChild(webServiceContext); 372 webServices.put(contextPath, webServiceContext); 373 } 374 375 public void removeWebService(String contextPath) { 376 TomcatEJBWebServiceContext context = (TomcatEJBWebServiceContext) webServices.get(contextPath); 377 try { 378 context.stop(); 379 context.destroy(); 380 } catch (Exception e) { 381 throw new RuntimeException (e); 382 } 383 context.getParent().removeChild(context); 384 webServices.remove(contextPath); 385 } 386 387 public static final GBeanInfo GBEAN_INFO; 388 389 static { 390 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Tomcat Web Container", TomcatContainer.class); 391 392 infoFactory.setConstructor(new String []{"classLoader", "catalinaHome", "EngineGBean", "ServerInfo", "objectName", "WebManager"}); 393 394 infoFactory.addAttribute("classLoader", ClassLoader .class, false); 395 396 infoFactory.addAttribute("catalinaHome", String .class, true); 397 398 infoFactory.addAttribute("objectName", String .class, false); 399 400 infoFactory.addReference("EngineGBean", ObjectRetriever.class, NameFactory.GERONIMO_SERVICE); 401 402 infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean"); 403 infoFactory.addReference("WebManager", WebManager.class); 404 405 infoFactory.addOperation("addContext", new Class []{TomcatContext.class}); 406 infoFactory.addOperation("removeContext", new Class []{TomcatContext.class}); 407 408 infoFactory.addOperation("addConnector", new Class []{Connector.class}); 409 infoFactory.addOperation("removeConnector", new Class []{Connector.class}); 410 411 infoFactory.addInterface(SoapHandler.class); 412 infoFactory.addInterface(TomcatWebContainer.class); 413 414 GBEAN_INFO = infoFactory.getBeanInfo(); 415 } 416 417 public static GBeanInfo getGBeanInfo() { 418 return GBEAN_INFO; 419 } 420 421 } 422 | Popular Tags |