001: /*
002: * ========================================================================
003: *
004: * Copyright 2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.integration.ant.deployment;
021:
022: import java.io.File;
023: import java.io.IOException;
024: import java.util.Iterator;
025:
026: import javax.xml.parsers.ParserConfigurationException;
027:
028: import org.apache.cactus.integration.ant.deployment.webapp.DefaultWarArchive;
029: import org.apache.cactus.integration.ant.deployment.webapp.WarArchive;
030: import org.apache.tools.ant.BuildException;
031: import org.xml.sax.SAXException;
032:
033: /**
034: * Parse an WAR descriptor to extract meaninful information for Cactus,
035: * the results being stored in a {@link WarDeployableFile} object.
036: *
037: * @since Cactus 1.5
038: * @version $Id: WarParser.java 239003 2004-05-31 20:05:27Z vmassol $
039: */
040: public class WarParser {
041: /**
042: * Parse an WAR descriptor to extract meaninful information for Cactus.
043: *
044: * @param theDeployableFile the file to parse and deploy
045: * @return the parse results as a {@link WarDeployableFile} object
046: */
047: public static final WarDeployableFile parse(File theDeployableFile) {
048: WarDeployableFile deployable = new WarDeployableFile();
049:
050: try {
051: deployable.setFile(theDeployableFile);
052: deployable.setWarArchive(new DefaultWarArchive(
053: theDeployableFile));
054: deployable
055: .setTestContext(parseWebContext(theDeployableFile));
056: deployable
057: .setServletRedirectorMapping(parseServletRedirectorMapping(deployable
058: .getWarArchive()));
059: deployable
060: .setFilterRedirectorMapping(parseFilterRedirectorMapping(deployable
061: .getWarArchive()));
062: deployable
063: .setJspRedirectorMapping(parseJspRedirectorMapping(deployable
064: .getWarArchive()));
065: } catch (IOException e) {
066: throw new BuildException(
067: "Failed to parse deployment descriptor "
068: + "for WAR file [" + theDeployableFile
069: + "].", e);
070: } catch (ParserConfigurationException e) {
071: throw new BuildException(
072: "Failed to parse deployment descriptor "
073: + "for WAR file [" + theDeployableFile
074: + "].", e);
075: } catch (SAXException e) {
076: throw new BuildException(
077: "Failed to parse deployment descriptor "
078: + "for WAR file [" + theDeployableFile
079: + "].", e);
080: }
081:
082: return deployable;
083: }
084:
085: /**
086: * @param theDeployableFile the file to parse and deploy
087: * @return the test context that will be used to verify if the container
088: * is started or not
089: */
090: protected static String parseWebContext(File theDeployableFile) {
091: String context = theDeployableFile.getName();
092: int warIndex = context.toLowerCase().lastIndexOf(".war");
093: if (warIndex >= 0) {
094: context = context.substring(0, warIndex);
095: }
096: return context;
097: }
098:
099: /**
100: * Find the first URL-pattern to which the Cactus servlet redirector is
101: * mapped in the deployment descriptor.
102: *
103: * @return the servlet redirector mapping if found or <code>null</code>
104: * if not found
105: * @param theWar the WAR descriptor that is parsed when looking for
106: * a Cactus servlet redirector mapping
107: * @throws IOException If there was a problem reading the deployment
108: * descriptor in the WAR
109: * @throws SAXException If the deployment descriptor of the WAR could not
110: * be parsed
111: * @throws ParserConfigurationException If there is an XML parser
112: * configration problem
113: */
114: static String parseServletRedirectorMapping(WarArchive theWar)
115: throws SAXException, IOException,
116: ParserConfigurationException {
117: Iterator servletNames = theWar
118: .getWebXml()
119: .getServletNamesForClass(
120: "org.apache.cactus.server.ServletTestRedirector");
121: if (servletNames.hasNext()) {
122: // we only care about the first definition and the first mapping
123: String name = (String) servletNames.next();
124: Iterator mappings = theWar.getWebXml().getServletMappings(
125: name);
126: if (mappings.hasNext()) {
127: return (String) mappings.next();
128: }
129: }
130: return null;
131: }
132:
133: /**
134: * Find the first URL-pattern to which the Cactus filter redirector is
135: * mapped in the deployment descriptor.
136: *
137: * @return the filter redirector mapping if found or <code>null</code>
138: * if not found
139: * @param theWar the WAR descriptor that is parsed when looking for
140: * a Cactus filter redirector mapping
141: * @throws IOException If there was a problem reading the deployment
142: * descriptor in the WAR
143: * @throws SAXException If the deployment descriptor of the WAR could not
144: * be parsed
145: * @throws ParserConfigurationException If there is an XML parser
146: * configration problem
147: */
148: static String parseFilterRedirectorMapping(WarArchive theWar)
149: throws IOException, SAXException,
150: ParserConfigurationException {
151: Iterator filterNames = theWar
152: .getWebXml()
153: .getFilterNamesForClass(
154: "org.apache.cactus.server.FilterTestRedirector");
155: if (filterNames.hasNext()) {
156: // we only care about the first definition and the first mapping
157: String name = (String) filterNames.next();
158: Iterator mappings = theWar.getWebXml().getFilterMappings(
159: name);
160: if (mappings.hasNext()) {
161: return (String) mappings.next();
162: }
163: }
164: return null;
165: }
166:
167: /**
168: * Find the first URL-pattern to which the Cactus JSP redirector is
169: * mapped in the deployment descriptor.
170: *
171: * @return the JSP redirector mapping if found or <code>null</code>
172: * if not found
173: * @param theWar the WAR descriptor that is parsed when looking for
174: * a Cactus JSP redirector mapping
175: * @throws IOException If there was a problem reading the deployment
176: * descriptor in the WAR
177: * @throws SAXException If the deployment descriptor of the WAR could not
178: * be parsed
179: * @throws ParserConfigurationException If there is an XML parser
180: * configration problem
181: */
182: static String parseJspRedirectorMapping(WarArchive theWar)
183: throws IOException, SAXException,
184: ParserConfigurationException {
185: // To get the JSP redirector mapping, we must first get the full path to
186: // the corresponding JSP file in the WAR
187: String jspRedirectorPath = theWar
188: .findResource("jspRedirector.jsp");
189: if (jspRedirectorPath != null) {
190: jspRedirectorPath = "/" + jspRedirectorPath;
191: Iterator jspNames = theWar.getWebXml()
192: .getServletNamesForJspFile(jspRedirectorPath);
193: if (jspNames.hasNext()) {
194: // we only care about the first definition and the first
195: // mapping
196: String name = (String) jspNames.next();
197: Iterator mappings = theWar.getWebXml()
198: .getServletMappings(name);
199: if (mappings.hasNext()) {
200: return (String) mappings.next();
201: }
202: }
203: }
204: return null;
205: }
206: }
|