1 23 24 package com.sun.enterprise.deployment.node.web; 25 26 import java.util.*; 27 import org.w3c.dom.Node ; 28 29 import com.sun.enterprise.deployment.node.*; 30 31 import com.sun.enterprise.deployment.xml.TagNames; 32 import com.sun.enterprise.deployment.xml.WebTagNames; 33 import com.sun.enterprise.deployment.xml.WebServicesTagNames; 34 import com.sun.enterprise.deployment.Descriptor; 35 import com.sun.enterprise.deployment.WebBundleDescriptor; 36 import com.sun.enterprise.deployment.SecurityRoleDescriptor; 37 38 import com.sun.enterprise.deployment.types.EjbReference; 39 import com.sun.enterprise.deployment.SecurityRoleDescriptor; 40 import com.sun.enterprise.deployment.EntityManagerReferenceDescriptor; 41 import com.sun.enterprise.deployment.EntityManagerFactoryReferenceDescriptor; 42 import com.sun.enterprise.deployment.EnvironmentProperty; 43 import com.sun.enterprise.deployment.ServiceReferenceDescriptor; 44 import com.sun.enterprise.deployment.ResourceReferenceDescriptor; 45 import com.sun.enterprise.deployment.JmsDestinationReferenceDescriptor; 46 import com.sun.enterprise.deployment.WebComponentDescriptor; 47 import com.sun.enterprise.deployment.MimeMappingDescriptor; 48 import com.sun.enterprise.deployment.AppListenerDescriptorImpl; 49 import com.sun.enterprise.deployment.ServletFilterDescriptor; 50 import com.sun.enterprise.deployment.ServletFilterMappingDescriptor; 51 import com.sun.enterprise.deployment.ErrorPageDescriptorImpl; 52 import com.sun.enterprise.deployment.JspConfigDescriptor; 53 import com.sun.enterprise.deployment.TagLibConfigurationDescriptor; 54 import com.sun.enterprise.deployment.LocaleEncodingMappingDescriptor; 55 import com.sun.enterprise.deployment.LocaleEncodingMappingListDescriptor; 56 import com.sun.enterprise.deployment.LifecycleCallbackDescriptor; 57 import com.sun.enterprise.deployment.SecurityConstraintImpl; 58 import com.sun.enterprise.deployment.LoginConfigurationImpl; 59 import com.sun.enterprise.deployment.WebComponentDescriptor; 60 import com.sun.enterprise.deployment.util.DOLUtils; 61 import com.sun.enterprise.deployment.util.DOLLoadingContextFactory; 62 import com.sun.enterprise.deployment.web.LoginConfiguration; 63 64 70 public class WebBundleNode extends BundleNode implements RootXMLNode { 71 72 public final static XMLElement tag = new XMLElement(WebTagNames.WEB_BUNDLE); 73 74 77 public final static String PUBLIC_DTD_ID = "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"; 78 public final static String PUBLIC_DTD_ID_12 = "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"; 79 82 public final static String SYSTEM_ID = "http://java.sun.com/dtd/web-app_2_3.dtd"; 83 public final static String SYSTEM_ID_12 = "http://java.sun.com/dtd/web-app_2_2.dtd"; 84 85 public final static String SCHEMA_ID_24 = "web-app_2_4.xsd"; 86 public final static String SCHEMA_ID = "web-app_2_5.xsd"; 87 public final static String SPEC_VERSION = "2.5"; 88 private static List<String > systemIDs = null; 89 90 private WebBundleDescriptor descriptor; 91 private Map servletMappings; 92 private boolean isFirstWelcomeFile = true; 93 94 100 public static String registerBundle(Map publicIDToDTD) { 101 publicIDToDTD.put(PUBLIC_DTD_ID, SYSTEM_ID); 102 publicIDToDTD.put(PUBLIC_DTD_ID_12, SYSTEM_ID_12); 103 return tag.getQName(); 104 } 105 106 107 public WebBundleNode() { 108 super(); 109 registerElementHandler(new XMLElement(TagNames.ENVIRONMENT_PROPERTY), EnvEntryNode.class); 110 registerElementHandler(new XMLElement(TagNames.EJB_REFERENCE), EjbReferenceNode.class); 111 registerElementHandler(new XMLElement(TagNames.EJB_LOCAL_REFERENCE), EjbLocalReferenceNode.class); 112 registerElementHandler(new XMLElement(WebServicesTagNames.SERVICE_REF), ServiceReferenceNode.class, "addServiceReferenceDescriptor"); 113 registerElementHandler(new XMLElement(TagNames.RESOURCE_REFERENCE), 114 ResourceRefNode.class, "addResourceReferenceDescriptor"); 115 registerElementHandler(new XMLElement(TagNames.RESOURCE_ENV_REFERENCE), 116 ResourceEnvRefNode.class, "addJmsDestinationReferenceDescriptor"); 117 registerElementHandler(new XMLElement(TagNames.MESSAGE_DESTINATION_REFERENCE), MessageDestinationRefNode.class, "addMessageDestinationReferenceDescriptor"); 118 registerElementHandler(new XMLElement(TagNames.PERSISTENCE_CONTEXT_REF), EntityManagerReferenceNode.class, "addEntityManagerReferenceDescriptor"); 119 registerElementHandler(new XMLElement(TagNames.PERSISTENCE_UNIT_REF), EntityManagerFactoryReferenceNode.class, "addEntityManagerFactoryReferenceDescriptor"); 120 registerElementHandler(new XMLElement(TagNames.ROLE), 121 SecurityRoleNode.class, "addRole"); 122 registerElementHandler(new XMLElement(WebTagNames.SERVLET), ServletNode.class); 123 registerElementHandler(new XMLElement(WebTagNames.SERVLET_MAPPING), ServletMappingNode.class); 124 registerElementHandler(new XMLElement(WebTagNames.MIME_MAPPING), 125 MimeMappingNode.class, "addMimeMapping"); 126 registerElementHandler(new XMLElement(WebTagNames.CONTEXT_PARAM), 127 InitParamNode.class, "addContextParameter"); 128 registerElementHandler(new XMLElement(WebTagNames.SECURITY_CONSTRAINT), 129 SecurityConstraintNode.class, "addSecurityConstraint"); 130 registerElementHandler(new XMLElement(WebTagNames.FILTER), 131 FilterNode.class, "addServletFilter"); 132 registerElementHandler(new XMLElement(WebTagNames.FILTER_MAPPING), 133 FilterMappingNode.class, "addServletFilterMapping"); 134 registerElementHandler(new XMLElement(WebTagNames.LISTENER), 135 ListenerNode.class, "addAppListenerDescriptor"); 136 registerElementHandler(new XMLElement(WebTagNames.ERROR_PAGE), 137 ErrorPageNode.class, "addErrorPageDescriptor"); 138 registerElementHandler(new XMLElement(WebTagNames.LOGIN_CONFIG), 139 LoginConfigNode.class); 140 registerElementHandler(new XMLElement(WebTagNames.TAGLIB), 142 TagLibNode.class); 143 registerElementHandler(new XMLElement(WebTagNames.JSPCONFIG), 144 JspConfigNode.class); 145 registerElementHandler(new XMLElement(WebTagNames.LOCALE_ENCODING_MAPPING_LIST), 146 LocaleEncodingMappingListNode.class, "addLocaleEncodingMappingListDescriptor"); 147 registerElementHandler(new XMLElement(TagNames.MESSAGE_DESTINATION), 148 MessageDestinationNode.class, 149 "addMessageDestination"); 150 registerElementHandler(new XMLElement(TagNames.POST_CONSTRUCT), LifecycleCallbackNode.class, "addPostConstructDescriptor"); 151 registerElementHandler(new XMLElement(TagNames.PRE_DESTROY), LifecycleCallbackNode.class, "addPreDestroyDescriptor"); 152 } 153 154 157 public Object getDescriptor() { 158 159 if (descriptor==null && 160 !DOLLoadingContextFactory.isParsingDefaultWebXML()) { 161 descriptor = 162 DOLLoadingContextFactory.getDefaultWebBundleDescriptor(); 163 } 164 165 if (descriptor==null) { 167 descriptor = (WebBundleDescriptor) DescriptorFactory.getDescriptor(getXMLPath()); 168 } 169 return descriptor; 170 } 171 172 178 public void addDescriptor(Object newDescriptor) { 179 if (newDescriptor instanceof EjbReference) { 180 descriptor.addEjbReferenceDescriptor( 181 (EjbReference) newDescriptor); 182 } else if (newDescriptor instanceof EnvironmentProperty) { 183 DOLUtils.getDefaultLogger().fine("Adding env entry" + newDescriptor); 184 descriptor.addEnvironmentProperty((EnvironmentProperty) newDescriptor); 185 } else if (newDescriptor instanceof WebComponentDescriptor) { 186 DOLUtils.getDefaultLogger().fine("Adding web component" + newDescriptor); 187 descriptor.addWebComponentDescriptor((WebComponentDescriptor) newDescriptor); 188 } else if (newDescriptor instanceof TagLibConfigurationDescriptor) { 189 DOLUtils.getDefaultLogger().fine("Adding taglib component " + newDescriptor); 192 if (descriptor.getJspConfigDescriptor()==null) { 193 descriptor.setJspConfigDescriptor(new JspConfigDescriptor()); 194 } 195 196 descriptor.getJspConfigDescriptor().addTagLib((TagLibConfigurationDescriptor) newDescriptor); 197 } else if (newDescriptor instanceof JspConfigDescriptor) { 198 DOLUtils.getDefaultLogger().fine("Adding JSP Config Descriptor" 199 + newDescriptor); 200 if (descriptor.getJspConfigDescriptor()!=null) { 201 throw new RuntimeException ( 202 "Has more than one jsp-config element!"); 203 } 204 descriptor.setJspConfigDescriptor( 205 (JspConfigDescriptor)newDescriptor); 206 } else if (newDescriptor instanceof LoginConfiguration) { 207 DOLUtils.getDefaultLogger().fine("Adding Login Config Descriptor" 208 + newDescriptor); 209 if (descriptor.getLoginConfiguration()!=null) { 210 throw new RuntimeException ( 211 "Has more than one login-config element!"); 212 } 213 descriptor.setLoginConfiguration( 214 (LoginConfiguration)newDescriptor); 215 } else { 216 super.addDescriptor(newDescriptor); 217 } 218 } 219 220 226 public void setElementValue(XMLElement element, String value) { 227 if (WebTagNames.SESSION_TIMEOUT.equals(element.getQName())) { 228 if (descriptor.getSessionTimeout() != 232 WebBundleDescriptor.SESSION_TIMEOUT_DEFAULT) { 233 throw new RuntimeException ( 234 "Has more than one session-config element!"); 235 } 236 descriptor.setSessionTimeout((new Integer (value.trim())).intValue()); 237 } else if (WebTagNames.WELCOME_FILE.equals(element.getQName())) { 238 if (isFirstWelcomeFile && 239 !DOLLoadingContextFactory.isParsingDefaultWebXML()) { 240 descriptor.getWelcomeFilesSet().clear(); 243 isFirstWelcomeFile = false; 244 } 245 descriptor.addWelcomeFile(value); 246 } else { 247 super.setElementValue(element, value); 248 } 249 } 250 251 254 protected XMLElement getXMLRootTag() { 255 return tag; 256 } 257 258 261 public String getDocType() { 262 return null; 263 } 264 265 268 public String getSystemID() { 269 return SCHEMA_ID; 270 } 271 272 275 public List<String > getSystemIDs() { 276 if (systemIDs != null) { 277 return systemIDs; 278 } 279 280 systemIDs = new ArrayList<String >(); 281 systemIDs.add(SCHEMA_ID); 282 systemIDs.add(SCHEMA_ID_24); 283 return systemIDs; 284 } 285 286 292 void addServletMapping(String servletName, String urlPattern) { 293 if (servletMappings==null) { 294 servletMappings = new HashMap(); 295 } 296 if (servletMappings.containsKey(servletName)) { 297 ((Vector) servletMappings.get(servletName)).add(urlPattern); 298 } else { 299 Vector mappings = new Vector(); 300 mappings.add(urlPattern); 301 servletMappings.put(servletName, mappings); 302 } 303 } 304 305 311 public boolean endElement(XMLElement element) { 312 if (WebTagNames.DISTRIBUTABLE.equals(element.getQName())) { 313 descriptor.setDistributable(true); 314 return false; 315 } else { 316 boolean allDone = super.endElement(element); 317 if (allDone && servletMappings!=null) { 318 for (Iterator keys = servletMappings.keySet().iterator(); keys.hasNext();) { 319 String servletName = (String ) keys.next(); 320 Vector mappings = (Vector) servletMappings.get(servletName); 321 WebComponentDescriptor servlet= descriptor.getWebComponentByCanonicalName(servletName); 322 if (servlet!=null) { 323 for (Iterator mapping = mappings.iterator();mapping.hasNext();) { 324 servlet.addUrlPattern((String ) mapping.next()); 325 } 326 } else { 327 throw new RuntimeException ("There is no web component by the name of " + servletName + " here."); 328 } 329 } 330 } 331 return allDone; 332 } 333 } 334 335 342 public Node writeDescriptor(Node parent, Descriptor descriptor) { 343 if (! (descriptor instanceof WebBundleDescriptor)) { 344 throw new IllegalArgumentException (getClass() + " cannot handles descriptors of type " + descriptor.getClass()); 345 } 346 WebBundleDescriptor webBundleDesc = (WebBundleDescriptor) descriptor; 347 Node jarNode = super.writeDescriptor(parent, webBundleDesc); 348 if (webBundleDesc.isDistributable()) { 349 appendChild(jarNode, WebTagNames.DISTRIBUTABLE); 350 } 351 352 addInitParam(jarNode, WebTagNames.CONTEXT_PARAM, webBundleDesc.getContextParametersSet()); 354 355 FilterNode filterNode = new FilterNode(); 357 for (Enumeration filters = webBundleDesc.getServletFilters().elements();filters.hasMoreElements();) { 358 filterNode.writeDescriptor(jarNode, WebTagNames.FILTER, 359 (ServletFilterDescriptor) filters.nextElement()); 360 } 361 362 FilterMappingNode filterMappingNode = new FilterMappingNode(); 364 for (Enumeration mappings = webBundleDesc.getServletFilterMappings().elements(); 365 mappings.hasMoreElements();) { 366 filterMappingNode.writeDescriptor(jarNode, WebTagNames.FILTER_MAPPING, 367 (ServletFilterMappingDescriptor) mappings.nextElement()); 368 } 369 370 Vector appListeners = webBundleDesc.getAppListenerDescriptors(); 372 if (appListeners!=null && !appListeners.isEmpty()) { 373 for (Enumeration e = appListeners.elements();e.hasMoreElements();) { 374 AppListenerDescriptorImpl listener = (AppListenerDescriptorImpl) e.nextElement(); 375 Node listenerNode = appendChild(jarNode, WebTagNames.LISTENER); 376 appendTextChild(listenerNode, WebTagNames.LISTENER_CLASS, listener.getListener()); 377 } 378 } 379 380 Set servlets = webBundleDesc.getWebComponentDescriptorsSet(); 382 if (servlets!=null && !servlets.isEmpty()) { 383 ServletNode servletNode = new ServletNode(); 384 for (Iterator e= servlets.iterator();e.hasNext();) { 385 WebComponentDescriptor aServlet = (WebComponentDescriptor) e.next(); 386 servletNode.writeDescriptor(jarNode, aServlet); 387 } 388 } 389 390 if (servlets!=null && !servlets.isEmpty()) { 392 for (Iterator servletsIterator = servlets.iterator(); servletsIterator.hasNext();) { 393 WebComponentDescriptor aServlet = (WebComponentDescriptor) servletsIterator.next(); 394 for (Iterator patterns = aServlet.getUrlPatternsSet().iterator();patterns.hasNext();) { 395 String pattern = (String ) patterns.next(); 396 Node mappingNode= appendChild(jarNode, WebTagNames.SERVLET_MAPPING); 397 appendTextChild(mappingNode, WebTagNames.SERVLET_NAME, aServlet.getCanonicalName()); 398 399 if (webBundleDesc.getSpecVersion().equals("2.2")) { 402 if (!pattern.startsWith("/") 403 && !pattern.startsWith("*.")) { 404 pattern = "/" + pattern; 405 } 406 } 407 appendTextChild(mappingNode, WebTagNames.URL_PATTERN, pattern); 408 } 409 } 410 } 411 412 if (webBundleDesc.getSessionTimeout() != webBundleDesc.SESSION_TIMEOUT_DEFAULT) { 414 Node config = appendChild(jarNode, WebTagNames.SESSION_CONFIG); 415 appendTextChild(config, WebTagNames.SESSION_TIMEOUT, 416 String.valueOf(webBundleDesc.getSessionTimeout())); 417 } 418 419 MimeMappingNode mimeNode = new MimeMappingNode(); 421 for (Enumeration e = webBundleDesc.getMimeMappings();e.hasMoreElements();) { 422 MimeMappingDescriptor mimeMapping = (MimeMappingDescriptor) e.nextElement(); 423 mimeNode.writeDescriptor(jarNode, WebTagNames.MIME_MAPPING, mimeMapping); 424 } 425 426 Enumeration welcomeFiles = webBundleDesc.getWelcomeFiles(); 428 if (welcomeFiles.hasMoreElements()) { 429 Node welcomeList = appendChild(jarNode, WebTagNames.WELCOME_FILE_LIST); 430 while (welcomeFiles.hasMoreElements()) { 431 appendTextChild(welcomeList, WebTagNames.WELCOME_FILE, 432 (String ) welcomeFiles.nextElement()); 433 } 434 } 435 436 Enumeration errorPages = webBundleDesc.getErrorPageDescriptors(); 438 if (errorPages.hasMoreElements()) { 439 ErrorPageNode errorPageNode = new ErrorPageNode(); 440 while (errorPages.hasMoreElements()) { 441 errorPageNode.writeDescriptor(jarNode, WebTagNames.ERROR_PAGE, 442 (ErrorPageDescriptorImpl) errorPages.nextElement()); 443 } 444 } 445 446 JspConfigDescriptor jspConf = webBundleDesc.getJspConfigDescriptor(); 448 if(jspConf != null) { 449 JspConfigNode ln = new JspConfigNode(); 450 ln.writeDescriptor(jarNode, 451 WebTagNames.JSPCONFIG, 452 jspConf); 453 } 454 455 Enumeration securityConstraints = webBundleDesc.getSecurityConstraints(); 457 if (securityConstraints.hasMoreElements()) { 458 SecurityConstraintNode scNode = new SecurityConstraintNode(); 459 while (securityConstraints.hasMoreElements()) { 460 SecurityConstraintImpl sc = (SecurityConstraintImpl) securityConstraints.nextElement(); 461 scNode.writeDescriptor(jarNode, WebTagNames.SECURITY_CONSTRAINT, sc); 462 } 463 } 464 465 LoginConfigurationImpl lci = (LoginConfigurationImpl) webBundleDesc.getLoginConfiguration(); 467 if (lci!=null) { 468 LoginConfigNode lcn = new LoginConfigNode(); 469 lcn.writeDescriptor(jarNode, WebTagNames.LOGIN_CONFIG, lci); 470 } 471 472 Enumeration roles = webBundleDesc.getSecurityRoles(); 474 if (roles.hasMoreElements()) { 475 SecurityRoleNode srNode = new SecurityRoleNode(); 476 while (roles.hasMoreElements()) { 477 SecurityRoleDescriptor role = (SecurityRoleDescriptor) roles.nextElement(); 478 srNode.writeDescriptor(jarNode, WebTagNames.ROLE, role); 479 } 480 } 481 482 writeEnvEntryDescriptors(jarNode, webBundleDesc.getEnvironmentProperties().iterator()); 484 485 writeEjbReferenceDescriptors(jarNode, webBundleDesc.getEjbReferenceDescriptors().iterator()); 487 488 writeServiceReferenceDescriptors(jarNode, webBundleDesc.getServiceReferenceDescriptors().iterator()); 490 491 writeResourceRefDescriptors(jarNode, webBundleDesc.getResourceReferenceDescriptors().iterator()); 493 494 writeResourceEnvRefDescriptors(jarNode, webBundleDesc.getJmsDestinationReferenceDescriptors().iterator()); 496 497 writeMessageDestinationRefDescriptors(jarNode, webBundleDesc.getMessageDestinationReferenceDescriptors().iterator()); 499 500 writeEntityManagerReferenceDescriptors(jarNode, webBundleDesc.getEntityManagerReferenceDescriptors().iterator()); 502 503 writeEntityManagerFactoryReferenceDescriptors(jarNode, webBundleDesc.getEntityManagerFactoryReferenceDescriptors().iterator()); 505 506 writePostConstructDescriptors(jarNode, webBundleDesc.getPostConstructDescriptors().iterator()); 508 509 writePreDestroyDescriptors(jarNode, webBundleDesc.getPreDestroyDescriptors().iterator()); 511 512 writeMessageDestinations 514 (jarNode, webBundleDesc.getMessageDestinations().iterator()); 515 516 LocaleEncodingMappingListDescriptor lemDesc = webBundleDesc.getLocaleEncodingMappingListDescriptor(); 517 if(lemDesc != null) { 518 LocaleEncodingMappingListNode ln = new LocaleEncodingMappingListNode(); 519 ln.writeDescriptor(jarNode, 520 WebTagNames.LOCALE_ENCODING_MAPPING_LIST, 521 lemDesc); 522 } 523 524 return jarNode; 525 } 526 527 static void addInitParam(Node parentNode, String nodeName, Set initParams) { 528 if (!initParams.isEmpty()) { 529 InitParamNode initParamNode = new InitParamNode(); 530 for (Iterator e=initParams.iterator();e.hasNext();) { 531 EnvironmentProperty ep = (EnvironmentProperty) e.next(); 532 initParamNode.writeDescriptor(parentNode, nodeName, ep); 533 } 534 } 535 } 536 537 static void addInitParam(Node parentNode, String nodeName, Enumeration initParams) { 538 InitParamNode initParamNode = new InitParamNode(); 539 while (initParams.hasMoreElements()) { 540 EnvironmentProperty ep = (EnvironmentProperty) initParams.nextElement(); 541 initParamNode.writeDescriptor(parentNode, nodeName, ep); 542 } 543 } 544 545 548 public String getSpecVersion() { 549 return SPEC_VERSION; 550 } 551 552 } 553 | Popular Tags |