KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
32
33 /**
34  * Parse an WAR descriptor to extract meaninful information for Cactus,
35  * the results being stored in a {@link WarDeployableFile} object.
36  *
37  * @since Cactus 1.5
38  * @version $Id: WarParser.java,v 1.1 2004/05/31 20:05:22 vmassol Exp $
39  */

40 public class WarParser
41 {
42     /**
43      * Parse an WAR descriptor to extract meaninful information for Cactus.
44      *
45      * @param theDeployableFile the file to parse and deploy
46      * @return the parse results as a {@link WarDeployableFile} object
47      */

48     public static final WarDeployableFile parse(File JavaDoc 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 JavaDoc e)
65         {
66             throw new BuildException("Failed to parse deployment descriptor "
67                 + "for WAR file [" + theDeployableFile + "].", e);
68         }
69         catch (ParserConfigurationException JavaDoc e)
70         {
71             throw new BuildException("Failed to parse deployment descriptor "
72                 + "for WAR file [" + theDeployableFile + "].", e);
73         }
74         catch (SAXException JavaDoc 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     /**
84      * @param theDeployableFile the file to parse and deploy
85      * @return the test context that will be used to verify if the container
86      * is started or not
87      */

88     protected static String JavaDoc parseWebContext(File JavaDoc theDeployableFile)
89     {
90         String JavaDoc 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     /**
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 JavaDoc parseServletRedirectorMapping(WarArchive theWar)
115         throws SAXException JavaDoc, IOException JavaDoc, ParserConfigurationException JavaDoc
116     {
117         Iterator JavaDoc servletNames = theWar.getWebXml().getServletNamesForClass(
118             "org.apache.cactus.server.ServletTestRedirector");
119         if (servletNames.hasNext())
120         {
121             // we only care about the first definition and the first mapping
122
String JavaDoc name = (String JavaDoc) servletNames.next();
123             Iterator JavaDoc mappings = theWar.getWebXml().getServletMappings(name);
124             if (mappings.hasNext())
125             {
126                 return (String JavaDoc) mappings.next();
127             }
128         }
129         return null;
130     }
131
132     /**
133      * Find the first URL-pattern to which the Cactus filter redirector is
134      * mapped in the deployment descriptor.
135      *
136      * @return the filter redirector mapping if found or <code>null</code>
137      * if not found
138      * @param theWar the WAR descriptor that is parsed when looking for
139      * a Cactus filter redirector mapping
140      * @throws IOException If there was a problem reading the deployment
141      * descriptor in the WAR
142      * @throws SAXException If the deployment descriptor of the WAR could not
143      * be parsed
144      * @throws ParserConfigurationException If there is an XML parser
145      * configration problem
146      */

147     static String JavaDoc parseFilterRedirectorMapping(WarArchive theWar)
148         throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc
149     {
150         Iterator JavaDoc filterNames = theWar.getWebXml().getFilterNamesForClass(
151             "org.apache.cactus.server.FilterTestRedirector");
152         if (filterNames.hasNext())
153         {
154             // we only care about the first definition and the first mapping
155
String JavaDoc name = (String JavaDoc) filterNames.next();
156             Iterator JavaDoc mappings = theWar.getWebXml().getFilterMappings(name);
157             if (mappings.hasNext())
158             {
159                 return (String JavaDoc) mappings.next();
160             }
161         }
162         return null;
163     }
164
165     /**
166      * Find the first URL-pattern to which the Cactus JSP redirector is
167      * mapped in the deployment descriptor.
168      *
169      * @return the JSP redirector mapping if found or <code>null</code>
170      * if not found
171      * @param theWar the WAR descriptor that is parsed when looking for
172      * a Cactus JSP redirector mapping
173      * @throws IOException If there was a problem reading the deployment
174      * descriptor in the WAR
175      * @throws SAXException If the deployment descriptor of the WAR could not
176      * be parsed
177      * @throws ParserConfigurationException If there is an XML parser
178      * configration problem
179      */

180     static String JavaDoc parseJspRedirectorMapping(WarArchive theWar)
181         throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc
182     {
183         // To get the JSP redirector mapping, we must first get the full path to
184
// the corresponding JSP file in the WAR
185
String JavaDoc jspRedirectorPath = theWar.findResource("jspRedirector.jsp");
186         if (jspRedirectorPath != null)
187         {
188             jspRedirectorPath = "/" + jspRedirectorPath;
189             Iterator JavaDoc jspNames = theWar.getWebXml().getServletNamesForJspFile(
190                 jspRedirectorPath);
191             if (jspNames.hasNext())
192             {
193                 // we only care about the first definition and the first
194
// mapping
195
String JavaDoc name = (String JavaDoc) jspNames.next();
196                 Iterator JavaDoc mappings =
197                     theWar.getWebXml().getServletMappings(name);
198                 if (mappings.hasNext())
199                 {
200                     return (String JavaDoc) mappings.next();
201                 }
202             }
203         }
204         return null;
205     }
206 }
207
Popular Tags