1 20 package org.apache.cactus.integration.ant; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.StringTokenizer ; 28 29 import javax.xml.parsers.ParserConfigurationException ; 30 31 import org.apache.cactus.integration.ant.util.AntLog; 32 import org.apache.cactus.integration.ant.util.ResourceUtils; 33 import org.apache.cactus.integration.ant.deployment.webapp.DefaultWarArchive; 34 import org.apache.cactus.integration.ant.deployment.webapp.WarArchive; 35 import org.apache.cactus.integration.ant.deployment.webapp.WebXml; 36 import org.apache.cactus.integration.ant.deployment.webapp.WebXmlIo; 37 import org.apache.cactus.integration.ant.deployment.webapp.WebXmlMerger; 38 import org.apache.cactus.integration.ant.deployment.webapp.WebXmlVersion; 39 import org.apache.tools.ant.BuildException; 40 import org.apache.tools.ant.Project; 41 import org.apache.tools.ant.taskdefs.War; 42 import org.apache.tools.ant.types.EnumeratedAttribute; 43 import org.apache.tools.ant.types.FileSet; 44 import org.apache.tools.ant.types.XMLCatalog; 45 import org.apache.tools.ant.types.ZipFileSet; 46 import org.apache.tools.ant.util.FileUtils; 47 import org.xml.sax.SAXException ; 48 49 55 public class CactifyWarTask extends War 56 { 57 58 60 63 private static final String FILTER_REDIRECTOR_CLASS = 64 "org.apache.cactus.server.FilterTestRedirector"; 65 66 69 private static final String DEFAULT_FILTER_REDIRECTOR_MAPPING = 70 "/FilterRedirector"; 71 72 75 private static final String DEFAULT_JSP_REDIRECTOR_MAPPING = 76 "/JspRedirector"; 77 78 81 private static final String SERVLET_REDIRECTOR_CLASS = 82 "org.apache.cactus.server.ServletTestRedirector"; 83 84 87 private static final String DEFAULT_SERVLET_REDIRECTOR_MAPPING = 88 "/ServletRedirector"; 89 90 92 95 public abstract static class Redirector 96 { 97 98 100 103 protected String name; 104 105 108 protected String mapping; 109 110 114 protected String roles; 115 116 118 125 public abstract void mergeInto(WebXml theWebXml); 126 127 129 134 public final void setName(String theName) 135 { 136 this.name = theName; 137 } 138 139 144 public final void setMapping(String theMapping) 145 { 146 this.mapping = theMapping; 147 } 148 149 155 public final void setRoles(String theRoles) 156 { 157 this.roles = theRoles; 158 } 159 160 162 168 protected final void addSecurity(WebXml theWebXml) 169 { 170 StringTokenizer tokenizer = new StringTokenizer (this.roles, ","); 171 List roles = new ArrayList (); 172 while (tokenizer.hasMoreTokens()) 173 { 174 String role = tokenizer.nextToken().trim(); 175 if (!theWebXml.hasSecurityRole(role)) 176 { 177 theWebXml.addSecurityRole(role); 178 } 179 roles.add(role); 180 } 181 if (!roles.isEmpty()) 182 { 183 if (!theWebXml.hasLoginConfig()) 184 { 185 theWebXml.setLoginConfig("BASIC", "myrealm"); 186 } 187 if (!theWebXml.hasSecurityConstraint(this.mapping)) 188 { 189 theWebXml.addSecurityConstraint("Cactus Test Redirector", 190 this.mapping, roles); 191 } 192 } 193 } 194 195 } 196 197 200 public static final class FilterRedirector extends Redirector 201 { 202 203 206 public FilterRedirector() 207 { 208 this.name = "FilterRedirector"; 209 this.mapping = DEFAULT_FILTER_REDIRECTOR_MAPPING; 210 } 211 212 215 public void mergeInto(WebXml theWebXml) 216 { 217 if (WebXmlVersion.V2_3.compareTo(theWebXml.getVersion()) <= 0) 218 { 219 theWebXml.addFilter(this.name, FILTER_REDIRECTOR_CLASS); 220 theWebXml.addFilterMapping(this.name, this.mapping); 221 if (this.roles != null) 222 { 223 addSecurity(theWebXml); 224 } 225 } 226 } 227 228 } 229 230 233 public static final class JspRedirector extends Redirector 234 { 235 236 239 public JspRedirector() 240 { 241 this.name = "JspRedirector"; 242 this.mapping = DEFAULT_JSP_REDIRECTOR_MAPPING; 243 } 244 245 248 public void mergeInto(WebXml theWebXml) 249 { 250 theWebXml.addJspFile(this.name, "/jspRedirector.jsp"); 251 theWebXml.addServletMapping(this.name, this.mapping); 252 if (this.roles != null) 253 { 254 addSecurity(theWebXml); 255 } 256 } 257 258 } 259 260 263 public static final class ServletRedirector extends Redirector 264 { 265 266 269 public ServletRedirector() 270 { 271 this.name = "ServletRedirector"; 272 this.mapping = DEFAULT_SERVLET_REDIRECTOR_MAPPING; 273 } 274 275 278 public void mergeInto(WebXml theWebXml) 279 { 280 theWebXml.addServlet(this.name, SERVLET_REDIRECTOR_CLASS); 281 theWebXml.addServletMapping(this.name, this.mapping); 282 if (this.roles != null) 283 { 284 addSecurity(theWebXml); 285 } 286 } 287 288 } 289 290 293 public static final class Version extends EnumeratedAttribute 294 { 295 296 299 public String [] getValues() 300 { 301 return new String [] {"2.2", "2.3"}; 302 } 303 304 } 305 306 308 311 private File srcFile; 312 313 317 private File mergeWebXml; 318 319 322 private List redirectors = new ArrayList (); 323 324 327 private XMLCatalog xmlCatalog = null; 328 329 332 private String version = null; 333 334 336 339 public void execute() throws BuildException 340 { 341 WebXml webXml = null; 342 if (this.srcFile != null) 343 { 344 log("Analyzing war: " + this.srcFile.getAbsolutePath(), 345 Project.MSG_INFO); 346 347 ZipFileSet currentFiles = new ZipFileSet(); 349 currentFiles.setSrc(this.srcFile); 350 currentFiles.createExclude().setName("WEB-INF/web.xml"); 351 addZipfileset(currentFiles); 352 353 webXml = getOriginalWebXml(); 355 } 356 else 357 { 358 if (this.version == null) 359 { 360 throw new BuildException("You need to specify either the " 361 + "[srcfile] or the [version] attribute"); 362 } 363 WebXmlVersion webXmlVersion = null; 364 if (this.version.equals("2.2")) 365 { 366 webXmlVersion = WebXmlVersion.V2_2; 367 } 368 else 369 { 370 webXmlVersion = WebXmlVersion.V2_3; 371 } 372 try 373 { 374 webXml = WebXmlIo.newWebXml(webXmlVersion); 375 } 376 catch (ParserConfigurationException pce) 377 { 378 throw new BuildException( 379 "Could not create deployment descriptor", pce); 380 } 381 } 382 383 File tmpWebXml = cactifyWebXml(webXml); 384 setWebxml(tmpWebXml); 385 386 addCactusJars(); 387 388 try 389 { 390 super.execute(); 391 } 392 finally 393 { 394 tmpWebXml.delete(); 398 } 399 } 400 401 406 public final void addFilterRedirector(FilterRedirector theFilterRedirector) 407 { 408 this.redirectors.add(theFilterRedirector); 409 } 410 411 416 public final void addJspRedirector(JspRedirector theJspRedirector) 417 { 418 this.redirectors.add(theJspRedirector); 419 } 420 421 426 public final void addServletRedirector( 427 ServletRedirector theServletRedirector) 428 { 429 this.redirectors.add(theServletRedirector); 430 } 431 432 437 public final void addConfiguredXMLCatalog(XMLCatalog theXmlCatalog) 438 { 439 if (this.xmlCatalog == null) 440 { 441 this.xmlCatalog = new XMLCatalog(); 442 this.xmlCatalog.setProject(getProject()); 443 } 444 this.xmlCatalog.addConfiguredXMLCatalog(theXmlCatalog); 445 } 446 447 452 public final void setMergeWebXml(File theMergeFile) 453 { 454 this.mergeWebXml = theMergeFile; 455 } 456 457 462 public final void setSrcFile(File theSrcFile) 463 { 464 this.srcFile = theSrcFile; 465 } 466 467 472 public final void setVersion(Version theVersion) 473 { 474 this.version = theVersion.getValue(); 475 } 476 477 479 482 private void addCactusJars() 483 { 484 addJarWithClass("org.aspectj.lang.JoinPoint", "AspectJ Runtime"); 485 addJarWithClass("org.apache.cactus.ServletTestCase", 486 "Cactus Framework"); 487 addJarWithClass("org.apache.commons.logging.Log", 488 "Commons-Logging"); 489 addJarWithClass("org.apache.commons.httpclient.HttpClient", 490 "Commons-HttpClient"); 491 addJarWithClass("junit.framework.TestCase", "JUnit"); 492 } 493 494 502 private void addJarWithClass(String theClassName, String theDescription) 503 { 504 String resourceName = "/" + theClassName.replace('.', '/') + ".class"; 505 if (this.srcFile != null) 506 { 507 try 508 { 509 WarArchive srcWar = new DefaultWarArchive(srcFile); 510 if (srcWar.containsClass(theClassName)) 511 { 512 log("The " + theDescription + " JAR is already present in " 513 + "the WAR", Project.MSG_VERBOSE); 514 return; 515 } 516 } 517 catch (IOException ioe) 518 { 519 log("Problem reading source WAR to when trying to detect " 520 + "already present JAR files (" + ioe + ")", 521 Project.MSG_WARN); 522 } 523 } 524 ZipFileSet jar = new ZipFileSet(); 525 File file = ResourceUtils.getResourceLocation(resourceName); 526 if (file != null) 527 { 528 jar.setFile(file); 529 addLib(jar); 530 } 531 else 532 { 533 log("Could not find the " + theDescription + " JAR", 534 Project.MSG_WARN); 535 log("You need to add the JAR to the classpath of the task", 536 Project.MSG_INFO); 537 log("(Searched for class " + theClassName + ")", Project.MSG_DEBUG); 538 } 539 } 540 541 544 private void addJspRedirector() 545 { 546 File jspRedirectorFile = new File ( 548 new File (System.getProperty("java.io.tmpdir")), 549 "jspRedirector.jsp"); 550 jspRedirectorFile.deleteOnExit(); 551 try 552 { 553 ResourceUtils.copyResource(getProject(), 554 "/org/apache/cactus/server/jspRedirector.jsp", 555 jspRedirectorFile); 556 } 557 catch (IOException e) 558 { 559 log("Could not copy the JSP redirector (" + e.getMessage() + ")", 560 Project.MSG_WARN); 561 } 562 FileSet fileSet = new FileSet(); 563 fileSet.setFile(jspRedirectorFile); 564 addFileset(fileSet); 565 } 566 567 573 private void addRedirectorDefinitions(WebXml theWebXml) 574 { 575 boolean filterRedirectorDefined = false; 576 boolean jspRedirectorDefined = false; 577 boolean servletRedirectorDefined = false; 578 579 for (Iterator i = this.redirectors.iterator(); i.hasNext();) 581 { 582 Redirector redirector = (Redirector) i.next(); 583 if (redirector instanceof FilterRedirector) 584 { 585 filterRedirectorDefined = true; 586 } 587 else if (redirector instanceof JspRedirector) 588 { 589 jspRedirectorDefined = true; 590 } 591 else if (redirector instanceof ServletRedirector) 592 { 593 servletRedirectorDefined = true; 594 } 595 redirector.mergeInto(theWebXml); 596 } 597 598 if (!filterRedirectorDefined) 601 { 602 new FilterRedirector().mergeInto(theWebXml); 603 } 604 if (!servletRedirectorDefined) 605 { 606 new ServletRedirector().mergeInto(theWebXml); 607 } 608 if (!jspRedirectorDefined) 609 { 610 new JspRedirector().mergeInto(theWebXml); 611 } 612 } 613 614 621 private File cactifyWebXml(WebXml theWebXml) 622 { 623 addRedirectorDefinitions(theWebXml); 624 addJspRedirector(); 625 626 if (this.mergeWebXml != null) 629 { 630 try 631 { 632 WebXml parsedMergeWebXml = WebXmlIo.parseWebXmlFromFile( 633 this.mergeWebXml, this.xmlCatalog); 634 WebXmlMerger merger = new WebXmlMerger(theWebXml); 635 merger.setLog(new AntLog(this)); 636 merger = new WebXmlMerger(theWebXml); 637 merger.setLog(new AntLog(this)); 638 merger.merge(parsedMergeWebXml); 639 } 640 catch (IOException e) 641 { 642 throw new BuildException( 643 "Could not merge deployment descriptors", e); 644 } 645 catch (SAXException e) 646 { 647 throw new BuildException("Parsing of merge file failed", e); 648 } 649 catch (ParserConfigurationException e) 650 { 651 throw new BuildException("XML parser configuration error", e); 652 } 653 } 654 655 FileUtils fileUtils = FileUtils.newFileUtils(); 658 File tmpWebXml = fileUtils.createTempFile("cactus", "web.xml", 659 getProject().getBaseDir()); 660 tmpWebXml.deleteOnExit(); 661 try 662 { 663 WebXmlIo.writeWebXml(theWebXml, tmpWebXml, null, true); 664 } 665 catch (IOException ioe) 666 { 667 throw new BuildException( 668 "Could not write temporary deployment descriptor", ioe); 669 } 670 return tmpWebXml; 671 } 672 673 681 private WebXml getOriginalWebXml() throws BuildException 682 { 683 WarArchive war = null; 685 try 686 { 687 war = new DefaultWarArchive(this.srcFile); 688 WebXml webXml = war.getWebXml(); 689 if (webXml == null) 690 { 691 throw new BuildException("The WAR source file does not " 692 + "contain a WEB-INF/web.xml deployment descriptor"); 693 } 694 return webXml; 695 } 696 catch (SAXException e) 697 { 698 throw new BuildException( 699 "Parsing of web.xml deployment descriptor failed", e); 700 } 701 catch (IOException e) 702 { 703 throw new BuildException("Failed to open WAR", e); 704 } 705 catch (ParserConfigurationException e) 706 { 707 throw new BuildException("XML parser configuration error", e); 708 } 709 } 710 711 } 712 | Popular Tags |