1 17 18 package org.apache.geronimo.tomcat; 19 20 import java.net.MalformedURLException ; 21 import java.net.URI ; 22 import java.net.URL ; 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.Hashtable ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 31 import javax.management.MalformedObjectNameException ; 32 import javax.management.ObjectName ; 33 import javax.naming.directory.DirContext ; 34 import javax.transaction.TransactionManager ; 35 import javax.transaction.UserTransaction ; 36 37 import org.apache.catalina.Context; 38 import org.apache.catalina.Manager; 39 import org.apache.catalina.Realm; 40 import org.apache.catalina.Valve; 41 import org.apache.catalina.core.StandardContext; 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 import org.apache.geronimo.connector.outbound.connectiontracking.TrackedConnectionAssociator; 45 import org.apache.geronimo.gbean.AbstractName; 46 import org.apache.geronimo.gbean.GBeanInfo; 47 import org.apache.geronimo.gbean.GBeanInfoBuilder; 48 import org.apache.geronimo.gbean.GBeanLifecycle; 49 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 50 import org.apache.geronimo.j2ee.management.impl.InvalidObjectNameException; 51 import org.apache.geronimo.kernel.Kernel; 52 import org.apache.geronimo.kernel.ObjectNameUtil; 53 import org.apache.geronimo.management.J2EEApplication; 54 import org.apache.geronimo.management.J2EEServer; 55 import org.apache.geronimo.management.geronimo.WebConnector; 56 import org.apache.geronimo.management.geronimo.WebContainer; 57 import org.apache.geronimo.management.geronimo.WebModule; 58 import org.apache.geronimo.tomcat.util.SecurityHolder; 59 import org.apache.geronimo.transaction.GeronimoUserTransaction; 60 import org.apache.geronimo.webservices.WebServiceContainer; 61 import org.apache.geronimo.webservices.WebServiceContainerFactory; 62 import org.apache.naming.resources.DirContextURLStreamHandler; 63 64 69 public class TomcatWebAppContext implements GBeanLifecycle, TomcatContext, WebModule { 70 71 private static Log log = LogFactory.getLog(TomcatWebAppContext.class); 72 73 protected final TomcatContainer container; 74 75 private final ClassLoader classLoader; 76 77 protected Context context = null; 78 79 private String path = null; 80 81 private String docBase = null; 82 83 private String virtualServer = null; 84 85 private final Realm realm; 86 87 private final List valveChain; 88 89 91 private final Manager manager; 92 93 private final boolean crossContext; 94 95 private final boolean disableCookies; 96 97 private final UserTransaction userTransaction; 98 99 private final Map componentContext; 100 101 private final Kernel kernel; 102 103 private final Set unshareableResources; 104 105 private final Set applicationManagedSecurityResources; 106 107 private final TrackedConnectionAssociator trackedConnectionAssociator; 108 109 private final SecurityHolder securityHolder; 110 111 private final J2EEServer server; 112 113 private final Map webServices; 114 115 private final String objectName; 116 117 private final String originalSpecDD; 118 119 private final URL configurationBaseURL; 120 121 123 private final String j2EEServer; 124 125 private final String j2EEApplication; 126 127 public TomcatWebAppContext( 128 ClassLoader classLoader, 129 String objectName, 130 String originalSpecDD, 131 URL configurationBaseUrl, 132 SecurityHolder securityHolder, 133 String virtualServer, 134 Map componentContext, 135 Set unshareableResources, 136 Set applicationManagedSecurityResources, 137 TransactionManager transactionManager, 138 TrackedConnectionAssociator trackedConnectionAssociator, 139 TomcatContainer container, 140 ObjectRetriever tomcatRealm, 141 ValveGBean tomcatValveChain, 142 ManagerGBean manager, 144 boolean crossContext, 145 boolean disableCookies, 146 Map webServices, 147 J2EEServer server, 148 J2EEApplication application, 149 Kernel kernel) 150 throws Exception { 151 152 assert classLoader != null; 153 assert configurationBaseUrl != null; 154 assert transactionManager != null; 155 assert trackedConnectionAssociator != null; 156 assert componentContext != null; 157 assert container != null; 158 159 160 this.objectName = objectName; 161 URI root; 162 if (configurationBaseUrl.getProtocol().equalsIgnoreCase("file")) { 164 root = new URI ("file", configurationBaseUrl.getPath(), null); 165 } else { 166 root = URI.create(configurationBaseUrl.toString()); 167 } 168 this.setDocBase(root.getPath()); 169 this.container = container; 170 this.originalSpecDD = originalSpecDD; 171 172 this.virtualServer = virtualServer; 173 this.securityHolder = securityHolder; 174 175 this.userTransaction = new GeronimoUserTransaction(transactionManager); 176 this.componentContext = componentContext; 177 this.unshareableResources = unshareableResources; 178 this.applicationManagedSecurityResources = applicationManagedSecurityResources; 179 this.trackedConnectionAssociator = trackedConnectionAssociator; 180 181 this.server = server; 182 183 this.configurationBaseURL = configurationBaseUrl; 184 185 if (tomcatRealm != null){ 186 realm = (Realm)tomcatRealm.getInternalObject(); 187 if (realm == null){ 188 throw new IllegalArgumentException ("tomcatRealm must be an instance of org.apache.catalina.Realm."); 189 } 190 } else{ 191 realm = null; 192 } 193 194 if (tomcatValveChain != null){ 196 ArrayList chain = new ArrayList (); 197 ValveGBean valveGBean = tomcatValveChain; 198 while(valveGBean != null){ 199 chain.add((Valve)valveGBean.getInternalObject()); 200 valveGBean = valveGBean.getNextValve(); 201 } 202 valveChain = chain; 203 } else { 204 valveChain = null; 205 } 206 207 214 if (manager != null) { 216 this.manager = (Manager ) manager.getInternalObject(); 217 } else { 218 this.manager = null; 219 } 220 221 this.crossContext = crossContext; 222 223 this.disableCookies = disableCookies; 224 225 this.webServices = createWebServices(webServices, kernel); 226 227 this.classLoader = classLoader; 228 229 this.kernel = kernel; 230 231 if (objectName != null) { 232 ObjectName myObjectName = ObjectNameUtil.getObjectName(objectName); 233 verifyObjectName(myObjectName); 234 j2EEServer = myObjectName.getKeyProperty(NameFactory.J2EE_SERVER); 235 j2EEApplication = myObjectName.getKeyProperty(NameFactory.J2EE_APPLICATION); 236 } else { 237 j2EEServer = null; 239 j2EEApplication = null; 240 } 241 } 242 243 private Map createWebServices(Map webServiceFactoryMap, Kernel kernel) throws Exception { 244 Map webServices = new HashMap (); 245 if (webServiceFactoryMap != null) { 246 for (Iterator iterator = webServiceFactoryMap.entrySet().iterator(); iterator.hasNext();) { 247 Map.Entry entry = (Map.Entry ) iterator.next(); 248 String servletName = (String ) entry.getKey(); 249 AbstractName factoryName = (AbstractName) entry.getValue(); 250 WebServiceContainerFactory webServiceContainerFactory = (WebServiceContainerFactory) kernel.getGBean(factoryName); 251 WebServiceContainer webServiceContainer = webServiceContainerFactory.getWebServiceContainer(); 252 webServices.put(servletName, webServiceContainer); 253 } 254 } 255 return webServices; 256 } 257 258 public String getObjectName() { 259 return objectName; 260 } 261 262 public String getJ2EEApplication() { 263 return j2EEApplication; 264 } 265 266 public String getJ2EEServer() { 267 return j2EEServer; 268 } 269 270 public boolean isStateManageable() { 271 return true; 272 } 273 274 public boolean isStatisticsProvider() { 275 return false; 276 } 277 278 public boolean isEventProvider() { 279 return true; 280 } 281 282 public URL getWARDirectory() { 283 return configurationBaseURL; 284 } 285 286 public String getWARName() { 287 try { 289 return ObjectName.getInstance(objectName).getKeyProperty(NameFactory.J2EE_NAME); 290 } catch (MalformedObjectNameException e) { 291 return null; 292 } 293 } 294 295 public WebContainer getContainer() { 296 return container; 297 } 298 299 public String getServer() { 300 return server == null? null: server.getObjectName(); 301 } 302 303 public String getDocBase() { 304 return docBase; 305 } 306 307 public void setDocBase(String docBase) { 308 this.docBase = docBase; 309 } 310 311 public UserTransaction getUserTransaction() { 312 return userTransaction; 313 } 314 315 public Map getComponentContext() { 316 return componentContext; 317 } 318 319 public String getVirtualServer() { 320 return virtualServer; 321 } 322 323 public ClassLoader getClassLoader() { 324 return classLoader; 325 } 326 327 public Kernel getKernel() { 328 return kernel; 329 } 330 331 public boolean isDisableCookies() { 332 return disableCookies; 333 } 334 335 public Context getContext() { 336 return context; 337 } 338 339 public void setContext(Context context) { 340 this.context = context; 341 } 342 343 public URL getURLFor() { 344 WebConnector[] connectors = (WebConnector[]) container.getConnectors(); 345 Map map = new HashMap (); 346 for (int i = 0; i < connectors.length; i++) { 347 WebConnector connector = connectors[i]; 348 map.put(connector.getProtocol(), connector.getConnectUrl()); 349 } 350 String urlPrefix; 351 if((urlPrefix = (String ) map.get("HTTP")) == null) { 352 if((urlPrefix = (String ) map.get("HTTPS")) == null) { 353 urlPrefix = (String ) map.get("AJP"); 354 } 355 } 356 if(urlPrefix == null) { 357 return null; 358 } 359 try { 360 return new URL (urlPrefix + getContextPath()); 361 } catch (MalformedURLException e) { 362 log.error("Bad URL to connect to web app", e); 363 return null; 364 } 365 } 366 367 public String getContextPath() { 368 return path; 369 } 370 371 public void setContextPath(String path) { 372 this.path = path.startsWith("/") ? path : "/" + path; 373 } 374 375 public SecurityHolder getSecurityHolder() { 376 return securityHolder; 377 } 378 379 380 public Set getApplicationManagedSecurityResources() { 381 return applicationManagedSecurityResources; 382 } 383 384 public TrackedConnectionAssociator getTrackedConnectionAssociator() { 385 return trackedConnectionAssociator; 386 } 387 388 public Set getUnshareableResources() { 389 return unshareableResources; 390 } 391 392 public Realm getRealm() { 393 return realm; 394 } 395 396 public List getValveChain() { 397 return valveChain; 398 } 399 400 404 public Manager getManager() { 405 return manager; 406 } 407 408 public boolean isCrossContext() { 409 return crossContext; 410 } 411 412 public Map getWebServices(){ 413 return webServices; 414 } 415 416 public String [] getServlets(){ 417 String [] result = null; 418 if ((context != null) && (context instanceof StandardContext)) { 419 result = ((StandardContext) context).getServlets(); 420 } 421 422 return result; 423 } 424 425 429 private void verifyObjectName(ObjectName objectName) { 430 if (objectName.isPattern()) { 431 throw new InvalidObjectNameException( 432 "ObjectName can not be a pattern", objectName); 433 } 434 Hashtable keyPropertyList = objectName.getKeyPropertyList(); 435 if (!NameFactory.WEB_MODULE.equals(keyPropertyList.get("j2eeType"))) { 436 throw new InvalidObjectNameException( 437 "WebModule object name j2eeType property must be 'WebModule'", 438 objectName); 439 } 440 if (!keyPropertyList.containsKey(NameFactory.J2EE_NAME)) { 441 throw new InvalidObjectNameException( 442 "WebModule object must contain a name property", objectName); 443 } 444 if (!keyPropertyList.containsKey(NameFactory.J2EE_SERVER)) { 445 throw new InvalidObjectNameException( 446 "WebModule object name must contain a J2EEServer property", 447 objectName); 448 } 449 if (!keyPropertyList.containsKey(NameFactory.J2EE_APPLICATION)) { 450 throw new InvalidObjectNameException( 451 "WebModule object name must contain a J2EEApplication property", 452 objectName); 453 } 454 if (keyPropertyList.size() != 4) { 455 throw new InvalidObjectNameException( 456 "WebModule object name can only have j2eeType, name, J2EEApplication, and J2EEServer properties", 457 objectName); 458 } 459 } 460 461 public String [] getJavaVMs() { 462 return server == null? new String [0]: server.getJavaVMs(); 463 } 464 465 public String getDeploymentDescriptor() { 466 return originalSpecDD; 467 } 468 469 public void doStart() throws Exception { 470 471 container.addContext(this); 473 DirContext resources = context.getResources(); 477 DirContextURLStreamHandler.bind(classLoader, resources); 478 479 log.debug("TomcatWebAppContext started for " + path); 480 } 481 482 public void doStop() throws Exception { 483 container.removeContext(this); 484 DirContextURLStreamHandler.unbind(classLoader); 485 486 489 log.debug("TomcatWebAppContext stopped"); 490 } 491 492 public void doFail() { 493 container.removeContext(this); 494 495 498 log.warn("TomcatWebAppContext failed"); 499 } 500 501 public static final GBeanInfo GBEAN_INFO; 502 503 static { 504 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic("Tomcat WebApplication Context", TomcatWebAppContext.class, NameFactory.WEB_MODULE); 505 506 infoBuilder.addAttribute("classLoader", ClassLoader .class, false); 507 infoBuilder.addAttribute("objectName", String .class, false); 508 infoBuilder.addAttribute("deploymentDescriptor", String .class, true); 509 infoBuilder.addAttribute("configurationBaseUrl", URL .class, true); 510 511 infoBuilder.addAttribute("contextPath", String .class, true); 512 513 infoBuilder.addAttribute("securityHolder", SecurityHolder.class, true); 514 infoBuilder.addAttribute("virtualServer", String .class, true); 515 infoBuilder.addAttribute("componentContext", Map .class, true); 516 infoBuilder.addAttribute("unshareableResources", Set .class, true); 517 infoBuilder.addAttribute("applicationManagedSecurityResources", Set .class, true); 518 infoBuilder.addReference("TransactionManager", TransactionManager .class, NameFactory.TRANSACTION_MANAGER); 519 infoBuilder.addReference("TrackedConnectionAssociator", TrackedConnectionAssociator.class, NameFactory.JCA_CONNECTION_TRACKER); 520 521 infoBuilder.addReference("Container", TomcatContainer.class, NameFactory.GERONIMO_SERVICE); 522 infoBuilder.addReference("TomcatRealm", ObjectRetriever.class); 523 infoBuilder.addReference("TomcatValveChain", ValveGBean.class); 524 infoBuilder.addReference("Manager", ManagerGBean.class); 526 infoBuilder.addAttribute("crossContext", boolean.class, true); 527 infoBuilder.addAttribute("disableCookies", boolean.class, true); 528 infoBuilder.addAttribute("webServices", Map .class, true); 529 infoBuilder.addReference("J2EEServer", J2EEServer.class); 530 infoBuilder.addReference("J2EEApplication", J2EEApplication.class); 531 infoBuilder.addAttribute("kernel", Kernel.class, false); 532 533 infoBuilder.addInterface(WebModule.class); 534 535 infoBuilder.setConstructor(new String [] { 536 "classLoader", 537 "objectName", 538 "deploymentDescriptor", 539 "configurationBaseUrl", 540 "securityHolder", 541 "virtualServer", 542 "componentContext", 543 "unshareableResources", 544 "applicationManagedSecurityResources", 545 "TransactionManager", 546 "TrackedConnectionAssociator", 547 "Container", 548 "TomcatRealm", 549 "TomcatValveChain", 550 "Manager", 552 "crossContext", 553 "disableCookies", 554 "webServices", 555 "J2EEServer", 556 "J2EEApplication", 557 "kernel" 558 } 559 ); 560 561 GBEAN_INFO = infoBuilder.getBeanInfo(); 562 } 563 564 public static GBeanInfo getGBeanInfo() { 565 return GBEAN_INFO; 566 } 567 } 568 | Popular Tags |