1 20 package org.apache.cactus.integration.ant.deployment.webapp; 21 22 import java.util.Iterator ; 23 24 import org.apache.cactus.integration.ant.util.AntLog; 25 import org.apache.commons.logging.Log; 26 import org.w3c.dom.Element ; 27 28 34 public class WebXmlMerger 35 { 36 37 39 42 private WebXml webXml; 43 44 47 private Log log = AntLog.NULL; 48 49 51 56 public WebXmlMerger(WebXml theWebXml) 57 { 58 this.webXml = theWebXml; 59 } 60 61 63 68 public final void merge(WebXml theMergeWebXml) 69 { 70 checkServletVersions(theMergeWebXml); 71 mergeContextParams(theMergeWebXml); 72 if (WebXmlVersion.V2_3.compareTo(this.webXml.getVersion()) <= 0) 73 { 74 mergeFilters(theMergeWebXml); 75 } 76 mergeServlets(theMergeWebXml); 77 if (WebXmlVersion.V2_3.compareTo(this.webXml.getVersion()) <= 0) 78 { 79 mergeResourceEnvironmentReferences(theMergeWebXml); 80 } 81 mergeResourceReferences(theMergeWebXml); 82 mergeSecurityConstraints(theMergeWebXml); 83 mergeLoginConfig(theMergeWebXml); 84 mergeSecurityRoles(theMergeWebXml); 85 mergeEnvironmentEntries(theMergeWebXml); 86 mergeEjbRefs(theMergeWebXml); 87 if (WebXmlVersion.V2_3.compareTo(this.webXml.getVersion()) <= 0) 88 { 89 mergeEjbLocalRefs(theMergeWebXml); 90 } 91 } 92 93 100 public final void setLog(Log theLog) 101 { 102 this.log = theLog; 103 } 104 105 107 113 protected final void checkServletVersions(WebXml theWebXml) 114 { 115 if ((this.webXml.getVersion() != null) 116 && (this.webXml.getVersion().compareTo(theWebXml.getVersion()) < 0)) 117 { 118 this.log.warn( 119 "Merging elements from a version " + theWebXml.getVersion() 120 + " descriptor into a version " + this.webXml.getVersion() 121 + ", some elements may be skipped"); 122 } 123 } 124 125 132 protected final void mergeContextParams(WebXml theWebXml) 133 { 134 Iterator contextParams = theWebXml.getElements(WebXmlTag.CONTEXT_PARAM); 135 int count = 0; 136 while (contextParams.hasNext()) 137 { 138 String paramName = theWebXml.getContextParamName( 139 (Element ) contextParams.next()); 140 if (!webXml.hasContextParam(paramName)) 141 { 142 webXml.addContextParam(theWebXml.getContextParam(paramName)); 143 } 144 count++; 145 } 146 this.log.trace("Merged " + count + " context-param definition" 147 + (count != 1 ? "s " : " ") + "into the descriptor"); 148 } 149 150 157 protected final void mergeFilters(WebXml theWebXml) 158 { 159 Iterator filterNames = theWebXml.getFilterNames(); 160 int count = 0; 161 while (filterNames.hasNext()) 162 { 163 String filterName = (String ) filterNames.next(); 164 if (!webXml.hasFilter(filterName)) 165 { 166 webXml.addFilter(theWebXml.getFilter(filterName)); 167 } 168 else 169 { 170 Iterator filterInitParamNames = 172 theWebXml.getFilterInitParamNames(filterName); 173 while (filterInitParamNames.hasNext()) 174 { 175 String paramName = (String ) filterInitParamNames.next(); 176 String paramValue = 177 theWebXml.getFilterInitParam(filterName, paramName); 178 webXml.addFilterInitParam( 179 filterName, paramName, paramValue); 180 } 181 } 182 Iterator filterMappings = theWebXml.getFilterMappings(filterName); 184 while (filterMappings.hasNext()) 185 { 186 String urlPattern = (String ) filterMappings.next(); 187 webXml.addFilterMapping(filterName, urlPattern); 188 } 189 count++; 190 } 191 this.log.trace("Merged " + count + " filter definition" 192 + (count != 1 ? "s " : " ") + "into the descriptor"); 193 } 194 195 202 protected final void mergeServlets(WebXml theWebXml) 203 { 204 Iterator servletNames = theWebXml.getServletNames(); 205 int count = 0; 206 while (servletNames.hasNext()) 207 { 208 String servletName = (String ) servletNames.next(); 209 if (!webXml.hasServlet(servletName)) 210 { 211 webXml.addServlet(theWebXml.getServlet(servletName)); 212 } 213 else 214 { 215 Iterator servletInitParamNames = 217 theWebXml.getServletInitParamNames(servletName); 218 while (servletInitParamNames.hasNext()) 219 { 220 String paramName = (String ) servletInitParamNames.next(); 221 String paramValue = 222 theWebXml.getServletInitParam(servletName, paramName); 223 webXml.addServletInitParam( 224 servletName, paramName, paramValue); 225 } 226 } 227 Iterator servletMappings = 229 theWebXml.getServletMappings(servletName); 230 while (servletMappings.hasNext()) 231 { 232 String urlPattern = (String ) servletMappings.next(); 233 webXml.addServletMapping(servletName, urlPattern); 234 } 235 count++; 236 } 237 this.log.trace("Merged " + count + " servlet definition" 238 + (count != 1 ? "s " : " ") + "into the descriptor"); 239 } 240 241 248 protected final void mergeResourceEnvironmentReferences(WebXml theWebXml) 249 { 250 int count = insertElements(theWebXml, WebXmlTag.RESOURCE_ENV_REF); 251 if (count > 0) 252 { 253 this.log.trace("Merged " + count + " resource environment " 254 + "reference" + (count != 1 ? "s " : " ") + "into the " 255 + "descriptor"); 256 } 257 } 258 259 266 protected final void mergeResourceReferences(WebXml theWebXml) 267 { 268 int count = insertElements(theWebXml, WebXmlTag.RESOURCE_REF); 269 if (count > 0) 270 { 271 this.log.trace("Merged " + count + " resource reference" 272 + (count != 1 ? "s " : " ") + "into the descriptor"); 273 } 274 } 275 276 282 protected final void mergeSecurityConstraints(WebXml theWebXml) 283 { 284 int count = insertElements(theWebXml, WebXmlTag.SECURITY_CONSTRAINT); 285 if (count > 0) 286 { 287 this.log.trace("Merged " + count + " security constraint" 288 + (count != 1 ? "s " : " ") + "into the descriptor"); 289 } 290 } 291 292 300 protected final void mergeLoginConfig(WebXml theWebXml) 301 { 302 boolean replaced = replaceElement(theWebXml, WebXmlTag.LOGIN_CONFIG); 303 if (replaced) 304 { 305 this.log.trace( 306 "Merged the login configuration into the descriptor"); 307 } 308 } 309 310 317 protected final void mergeSecurityRoles(WebXml theWebXml) 318 { 319 Iterator securityRoleNames = theWebXml.getSecurityRoleNames(); 320 int count = 0; 321 while (securityRoleNames.hasNext()) 322 { 323 String securityRoleName = (String ) securityRoleNames.next(); 324 if (!this.webXml.hasSecurityRole(securityRoleName)) 325 { 326 this.webXml.addSecurityRole(securityRoleName); 327 } 328 } 329 if (count > 0) 330 { 331 this.log.trace("Merged " + count + " security role" 332 + (count != 1 ? "s " : " ") + "into the descriptor"); 333 } 334 } 335 336 343 protected final void mergeEnvironmentEntries(WebXml theWebXml) 344 { 345 int count = insertElements(theWebXml, WebXmlTag.ENV_ENTRY); 346 if (count > 0) 347 { 348 this.log.trace("Merged " + count + " environment entr" 349 + (count != 1 ? "ies " : "y ") + "into the descriptor"); 350 } 351 } 352 353 360 protected final void mergeEjbRefs(WebXml theWebXml) 361 { 362 int count = insertElements(theWebXml, WebXmlTag.EJB_REF); 363 if (count > 0) 364 { 365 this.log.trace("Merged " + count + " EJB reference" 366 + (count != 1 ? "s " : "y ") + "into the descriptor"); 367 } 368 } 369 370 377 protected final void mergeEjbLocalRefs(WebXml theWebXml) 378 { 379 int count = insertElements(theWebXml, WebXmlTag.EJB_LOCAL_REF); 380 if (count > 0) 381 { 382 this.log.trace("Merged " + count + " EJB local reference" 383 + (count != 1 ? "s " : "y ") + "into the descriptor"); 384 } 385 } 386 387 389 399 private int insertElements(WebXml theWebXml, WebXmlTag theTag) 400 { 401 Iterator elements = theWebXml.getElements(theTag); 402 int count = 0; 403 while (elements.hasNext()) 404 { 405 Element element = (Element ) elements.next(); 406 webXml.addElement(theTag, element); 407 count++; 408 } 409 return count; 410 } 411 412 422 private boolean replaceElement(WebXml theWebXml, WebXmlTag theTag) 423 { 424 Iterator elements = theWebXml.getElements(theTag); 425 if (elements.hasNext()) 426 { 427 webXml.replaceElement(theTag, (Element ) elements.next()); 428 return true; 429 } 430 return false; 431 } 432 433 } 434 | Popular Tags |