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.application.ApplicationXml;
029: import org.apache.cactus.integration.ant.deployment.application.DefaultEarArchive;
030: import org.apache.cactus.integration.ant.deployment.application.EarArchive;
031: import org.apache.cactus.integration.ant.deployment.webapp.WarArchive;
032: import org.apache.tools.ant.BuildException;
033: import org.xml.sax.SAXException;
034:
035: /**
036: * Parse an EAR descriptor to extract meaninful information for Cactus,
037: * the results being stored in a {@link EarDeployableFile} object.
038: *
039: * @since Cactus 1.5
040: * @version $Id: EarParser.java 239003 2004-05-31 20:05:27Z vmassol $
041: */
042: public class EarParser {
043: /**
044: * Parse an EAR descriptor to extract meaninful information for Cactus.
045: *
046: * @param theDeployableFile the file to parse and deploy
047: * @return the parse results as a {@link EarDeployableFile} object
048: */
049: public static final EarDeployableFile parse(File theDeployableFile) {
050: EarDeployableFile deployable = new EarDeployableFile();
051:
052: try {
053: deployable.setFile(theDeployableFile);
054:
055: EarArchive earArchive = new DefaultEarArchive(
056: theDeployableFile);
057: String webUri = getUriOfCactifiedWebModule(earArchive);
058: if (webUri == null) {
059: throw new BuildException(
060: "Could not find cactified web "
061: + "module in the [" + theDeployableFile
062: + "] EAR.");
063: }
064:
065: WarArchive warArchive = earArchive.getWebModule(webUri);
066: if (warArchive == null) {
067: throw new BuildException("Could not find the WAR ["
068: + webUri + "] in the [" + theDeployableFile
069: + "] EAR.");
070: }
071:
072: deployable.setWarArchive(warArchive);
073: deployable.setTestContext(parseTestContext(earArchive,
074: webUri));
075: deployable.setServletRedirectorMapping(WarParser
076: .parseServletRedirectorMapping(deployable
077: .getWarArchive()));
078: deployable.setFilterRedirectorMapping(WarParser
079: .parseFilterRedirectorMapping(deployable
080: .getWarArchive()));
081: deployable.setJspRedirectorMapping(WarParser
082: .parseJspRedirectorMapping(deployable
083: .getWarArchive()));
084: } catch (IOException e) {
085: throw new BuildException(
086: "Failed to parse deployment descriptor "
087: + "for EAR file [" + theDeployableFile
088: + "].", e);
089: } catch (ParserConfigurationException e) {
090: throw new BuildException(
091: "Failed to parse deployment descriptor "
092: + "for EAR file [" + theDeployableFile
093: + "].", e);
094: } catch (SAXException e) {
095: throw new BuildException(
096: "Failed to parse deployment descriptor "
097: + "for EAR file [" + theDeployableFile
098: + "].", e);
099: }
100:
101: return deployable;
102: }
103:
104: /**
105: * Find the test context from the EAR archive.
106: *
107: * @return the test context
108: * @param theEar the EAR archive from which to extract the test context
109: * @param theWebUri the WAR URI of the WAR file in the EAR from which to
110: * extract the test context
111: * @throws IOException If there was a problem reading the deployment
112: * descriptor in the WAR
113: * @throws SAXException If the deployment descriptor of the WAR could not
114: * be parsed
115: * @throws ParserConfigurationException If there is an XML parser
116: * configration problem
117: */
118: protected static final String parseTestContext(EarArchive theEar,
119: String theWebUri) throws ParserConfigurationException,
120: IOException, SAXException {
121: String context = theEar.getApplicationXml()
122: .getWebModuleContextRoot(theWebUri);
123: if (context == null) {
124: // The application.xml does not define a <context-root> element.
125: // This is wrong!
126: throw new BuildException(
127: "Your application.xml must define a "
128: + "<context-root> element in the <web> module definition.");
129: }
130:
131: // Remove leading "/" if there is one.
132: if (context.startsWith("/")) {
133: context = context.substring(1);
134: }
135:
136: return context;
137: }
138:
139: /**
140: * Finds the web module in the EAR that contains the servlet test
141: * redirector, and returns the web-uri of the module found.
142: *
143: * <em>A web-app is considered cactified when it contains at least a
144: * mapping for the Cactus servlet test redirector</em>
145: *
146: * @return The URI of the cactified web-module, or <code>null</code> if no
147: * cactified web-app could be found
148: * @param theEar the EAR archive from which to extract the web URI
149: * @throws IOException If there was a problem reading the deployment
150: * descriptor in the WAR
151: * @throws SAXException If the deployment descriptor of the WAR could not
152: * be parsed
153: * @throws ParserConfigurationException If there is an XML parser
154: * configration problem
155: */
156: protected static final String getUriOfCactifiedWebModule(
157: EarArchive theEar) throws SAXException, IOException,
158: ParserConfigurationException {
159: ApplicationXml applicationXml = theEar.getApplicationXml();
160: for (Iterator i = applicationXml.getWebModuleUris(); i
161: .hasNext();) {
162: String webUri = (String) i.next();
163: WarArchive war = theEar.getWebModule(webUri);
164: if ((war != null)
165: && (WarParser.parseServletRedirectorMapping(war) != null)) {
166: return webUri;
167: }
168: }
169: return null;
170: }
171: }
|