1 20 package org.apache.cactus.integration.ant.deployment; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Iterator ; 25 26 import javax.xml.parsers.ParserConfigurationException ; 27 28 import org.apache.cactus.integration.ant.deployment.webapp.DefaultWarArchive; 29 import org.apache.cactus.integration.ant.deployment.webapp.WarArchive; 30 import org.apache.tools.ant.BuildException; 31 import org.xml.sax.SAXException ; 32 33 40 public class WarParser 41 { 42 48 public static final WarDeployableFile parse(File theDeployableFile) 49 { 50 WarDeployableFile deployable = new WarDeployableFile(); 51 52 try 53 { 54 deployable.setFile(theDeployableFile); 55 deployable.setWarArchive(new DefaultWarArchive(theDeployableFile)); 56 deployable.setTestContext(parseWebContext(theDeployableFile)); 57 deployable.setServletRedirectorMapping( 58 parseServletRedirectorMapping(deployable.getWarArchive())); 59 deployable.setFilterRedirectorMapping( 60 parseFilterRedirectorMapping(deployable.getWarArchive())); 61 deployable.setJspRedirectorMapping( 62 parseJspRedirectorMapping(deployable.getWarArchive())); 63 } 64 catch (IOException e) 65 { 66 throw new BuildException("Failed to parse deployment descriptor " 67 + "for WAR file [" + theDeployableFile + "].", e); 68 } 69 catch (ParserConfigurationException e) 70 { 71 throw new BuildException("Failed to parse deployment descriptor " 72 + "for WAR file [" + theDeployableFile + "].", e); 73 } 74 catch (SAXException e) 75 { 76 throw new BuildException("Failed to parse deployment descriptor " 77 + "for WAR file [" + theDeployableFile + "].", e); 78 } 79 80 return deployable; 81 } 82 83 88 protected static String parseWebContext(File theDeployableFile) 89 { 90 String context = theDeployableFile.getName(); 91 int warIndex = context.toLowerCase().lastIndexOf(".war"); 92 if (warIndex >= 0) 93 { 94 context = context.substring(0, warIndex); 95 } 96 return context; 97 } 98 99 114 static String parseServletRedirectorMapping(WarArchive theWar) 115 throws SAXException , IOException , ParserConfigurationException 116 { 117 Iterator servletNames = theWar.getWebXml().getServletNamesForClass( 118 "org.apache.cactus.server.ServletTestRedirector"); 119 if (servletNames.hasNext()) 120 { 121 String name = (String ) servletNames.next(); 123 Iterator mappings = theWar.getWebXml().getServletMappings(name); 124 if (mappings.hasNext()) 125 { 126 return (String ) mappings.next(); 127 } 128 } 129 return null; 130 } 131 132 147 static String parseFilterRedirectorMapping(WarArchive theWar) 148 throws IOException , SAXException , ParserConfigurationException 149 { 150 Iterator filterNames = theWar.getWebXml().getFilterNamesForClass( 151 "org.apache.cactus.server.FilterTestRedirector"); 152 if (filterNames.hasNext()) 153 { 154 String name = (String ) filterNames.next(); 156 Iterator mappings = theWar.getWebXml().getFilterMappings(name); 157 if (mappings.hasNext()) 158 { 159 return (String ) mappings.next(); 160 } 161 } 162 return null; 163 } 164 165 180 static String parseJspRedirectorMapping(WarArchive theWar) 181 throws IOException , SAXException , ParserConfigurationException 182 { 183 String jspRedirectorPath = theWar.findResource("jspRedirector.jsp"); 186 if (jspRedirectorPath != null) 187 { 188 jspRedirectorPath = "/" + jspRedirectorPath; 189 Iterator jspNames = theWar.getWebXml().getServletNamesForJspFile( 190 jspRedirectorPath); 191 if (jspNames.hasNext()) 192 { 193 String name = (String ) jspNames.next(); 196 Iterator mappings = 197 theWar.getWebXml().getServletMappings(name); 198 if (mappings.hasNext()) 199 { 200 return (String ) mappings.next(); 201 } 202 } 203 } 204 return null; 205 } 206 } 207 | Popular Tags |