KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > deployment > EarParser


1 /*
2  * ========================================================================
3  *
4  * Copyright 2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.integration.ant.deployment;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
27
28 import org.apache.cactus.integration.ant.deployment.application.ApplicationXml;
29 import org.apache.cactus.integration.ant.deployment.application.DefaultEarArchive;
30 import org.apache.cactus.integration.ant.deployment.application.EarArchive;
31 import org.apache.cactus.integration.ant.deployment.webapp.WarArchive;
32 import org.apache.tools.ant.BuildException;
33 import org.xml.sax.SAXException JavaDoc;
34
35 /**
36  * Parse an EAR descriptor to extract meaninful information for Cactus,
37  * the results being stored in a {@link EarDeployableFile} object.
38  *
39  * @since Cactus 1.5
40  * @version $Id: EarParser.java,v 1.1 2004/05/31 20:05:22 vmassol Exp $
41  */

42 public class EarParser
43 {
44     /**
45      * Parse an EAR descriptor to extract meaninful information for Cactus.
46      *
47      * @param theDeployableFile the file to parse and deploy
48      * @return the parse results as a {@link EarDeployableFile} object
49      */

50     public static final EarDeployableFile parse(File JavaDoc theDeployableFile)
51     {
52         EarDeployableFile deployable = new EarDeployableFile();
53
54         try
55         {
56             deployable.setFile(theDeployableFile);
57
58             EarArchive earArchive = new DefaultEarArchive(theDeployableFile);
59             String JavaDoc webUri = getUriOfCactifiedWebModule(earArchive);
60             if (webUri == null)
61             {
62                 throw new BuildException("Could not find cactified web "
63                     + "module in the [" + theDeployableFile + "] EAR.");
64             }
65
66             WarArchive warArchive = earArchive.getWebModule(webUri);
67             if (warArchive == null)
68             {
69                 throw new BuildException("Could not find the WAR [" + webUri
70                     + "] in the [" + theDeployableFile + "] EAR.");
71             }
72             
73             deployable.setWarArchive(warArchive);
74             deployable.setTestContext(parseTestContext(earArchive, webUri));
75             deployable.setServletRedirectorMapping(
76                 WarParser.parseServletRedirectorMapping(
77                     deployable.getWarArchive()));
78             deployable.setFilterRedirectorMapping(
79                 WarParser.parseFilterRedirectorMapping(
80                     deployable.getWarArchive()));
81             deployable.setJspRedirectorMapping(
82                 WarParser.parseJspRedirectorMapping(
83                     deployable.getWarArchive()));
84         }
85         catch (IOException JavaDoc e)
86         {
87             throw new BuildException("Failed to parse deployment descriptor "
88                 + "for EAR file [" + theDeployableFile + "].", e);
89         }
90         catch (ParserConfigurationException JavaDoc e)
91         {
92             throw new BuildException("Failed to parse deployment descriptor "
93                 + "for EAR file [" + theDeployableFile + "].", e);
94         }
95         catch (SAXException JavaDoc e)
96         {
97             throw new BuildException("Failed to parse deployment descriptor "
98                 + "for EAR file [" + theDeployableFile + "].", e);
99         }
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 JavaDoc parseTestContext(EarArchive theEar,
119         String JavaDoc theWebUri)
120         throws ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc
121     {
122         String JavaDoc context = theEar.getApplicationXml()
123             .getWebModuleContextRoot(theWebUri);
124         if (context == null)
125         {
126             // The application.xml does not define a <context-root> element.
127
// This is wrong!
128
throw new BuildException("Your application.xml must define a "
129                 + "<context-root> element in the <web> module definition.");
130         }
131
132         // Remove leading "/" if there is one.
133
if (context.startsWith("/"))
134         {
135             context = context.substring(1);
136         }
137
138         return context;
139     }
140     
141     /**
142      * Finds the web module in the EAR that contains the servlet test
143      * redirector, and returns the web-uri of the module found.
144      *
145      * <em>A web-app is considered cactified when it contains at least a
146      * mapping for the Cactus servlet test redirector</em>
147      *
148      * @return The URI of the cactified web-module, or <code>null</code> if no
149      * cactified web-app could be found
150      * @param theEar the EAR archive from which to extract the web URI
151      * @throws IOException If there was a problem reading the deployment
152      * descriptor in the WAR
153      * @throws SAXException If the deployment descriptor of the WAR could not
154      * be parsed
155      * @throws ParserConfigurationException If there is an XML parser
156      * configration problem
157      */

158     protected static final String JavaDoc getUriOfCactifiedWebModule(EarArchive theEar)
159         throws SAXException JavaDoc, IOException JavaDoc, ParserConfigurationException JavaDoc
160     {
161         ApplicationXml applicationXml = theEar.getApplicationXml();
162         for (Iterator JavaDoc i = applicationXml.getWebModuleUris(); i.hasNext();)
163         {
164             String JavaDoc webUri = (String JavaDoc) i.next();
165             WarArchive war = theEar.getWebModule(webUri);
166             if ((war != null)
167                 && (WarParser.parseServletRedirectorMapping(war) != null))
168             {
169                 return webUri;
170             }
171         }
172         return null;
173     }
174 }
175
Popular Tags