|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
WarParser.java | 37.5% | 68.3% | 100% | 62.9% |
|
1 |
/*
|
|
2 |
* ====================================================================
|
|
3 |
*
|
|
4 |
* The Apache Software License, Version 1.1
|
|
5 |
*
|
|
6 |
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
|
7 |
* reserved.
|
|
8 |
*
|
|
9 |
* Redistribution and use in source and binary forms, with or without
|
|
10 |
* modification, are permitted provided that the following conditions
|
|
11 |
* are met:
|
|
12 |
*
|
|
13 |
* 1. Redistributions of source code must retain the above copyright
|
|
14 |
* notice, this list of conditions and the following disclaimer.
|
|
15 |
*
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
17 |
* notice, this list of conditions and the following disclaimer in
|
|
18 |
* the documentation and/or other materials provided with the
|
|
19 |
* distribution.
|
|
20 |
*
|
|
21 |
* 3. The end-user documentation included with the redistribution, if
|
|
22 |
* any, must include the following acknowlegement:
|
|
23 |
* "This product includes software developed by the
|
|
24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
27 |
*
|
|
28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
29 |
* Foundation" must not be used to endorse or promote products
|
|
30 |
* derived from this software without prior written permission. For
|
|
31 |
* written permission, please contact apache@apache.org.
|
|
32 |
*
|
|
33 |
* 5. Products derived from this software may not be called "Apache"
|
|
34 |
* nor may "Apache" appear in their names without prior written
|
|
35 |
* permission of the Apache Group.
|
|
36 |
*
|
|
37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
48 |
* SUCH DAMAGE.
|
|
49 |
* ====================================================================
|
|
50 |
*
|
|
51 |
* This software consists of voluntary contributions made by many
|
|
52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
53 |
* information on the Apache Software Foundation, please see
|
|
54 |
* <http://www.apache.org/>.
|
|
55 |
*
|
|
56 |
*/
|
|
57 |
package org.apache.cactus.integration.ant.container;
|
|
58 |
|
|
59 |
import java.io.File;
|
|
60 |
import java.io.IOException;
|
|
61 |
import java.util.Iterator;
|
|
62 |
|
|
63 |
import javax.xml.parsers.ParserConfigurationException;
|
|
64 |
|
|
65 |
import org.apache.cactus.integration.ant.deployment.DefaultWarArchive;
|
|
66 |
import org.apache.cactus.integration.ant.deployment.WarArchive;
|
|
67 |
import org.apache.tools.ant.BuildException;
|
|
68 |
import org.xml.sax.SAXException;
|
|
69 |
|
|
70 |
/**
|
|
71 |
* Parse an WAR descriptor to extract meaninful information for Cactus,
|
|
72 |
* the results being stored in a {@link WarDeployableFile} object.
|
|
73 |
*
|
|
74 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
75 |
*
|
|
76 |
* @since Cactus 1.5
|
|
77 |
* @version $Id: WarParser.java,v 1.1.2.1 2003/10/25 17:22:05 vmassol Exp $
|
|
78 |
*/
|
|
79 |
public class WarParser |
|
80 |
{ |
|
81 |
/**
|
|
82 |
* Parse an WAR descriptor to extract meaninful information for Cactus.
|
|
83 |
*
|
|
84 |
* @param theDeployableFile the file to parse and deploy
|
|
85 |
* @return the parse results as a {@link WarDeployableFile} object
|
|
86 |
*/
|
|
87 | 3 |
public static final WarDeployableFile parse(File theDeployableFile) |
88 |
{ |
|
89 | 3 |
WarDeployableFile deployable = new WarDeployableFile();
|
90 |
|
|
91 | 3 |
try
|
92 |
{ |
|
93 | 3 |
deployable.setFile(theDeployableFile); |
94 | 3 |
deployable.setWarArchive(new DefaultWarArchive(theDeployableFile));
|
95 | 2 |
deployable.setTestContext(parseWebContext(theDeployableFile)); |
96 | 2 |
deployable.setServletRedirectorMapping( |
97 |
parseServletRedirectorMapping(deployable.getWarArchive())); |
|
98 | 2 |
deployable.setFilterRedirectorMapping( |
99 |
parseFilterRedirectorMapping(deployable.getWarArchive())); |
|
100 | 2 |
deployable.setJspRedirectorMapping( |
101 |
parseJspRedirectorMapping(deployable.getWarArchive())); |
|
102 |
} |
|
103 |
catch (IOException e)
|
|
104 |
{ |
|
105 | 1 |
throw new BuildException("Failed to parse deployment descriptor " |
106 |
+ "for WAR file [" + theDeployableFile + "].", e); |
|
107 |
} |
|
108 |
catch (ParserConfigurationException e)
|
|
109 |
{ |
|
110 | 0 |
throw new BuildException("Failed to parse deployment descriptor " |
111 |
+ "for WAR file [" + theDeployableFile + "].", e); |
|
112 |
} |
|
113 |
catch (SAXException e)
|
|
114 |
{ |
|
115 | 0 |
throw new BuildException("Failed to parse deployment descriptor " |
116 |
+ "for WAR file [" + theDeployableFile + "].", e); |
|
117 |
} |
|
118 |
|
|
119 | 2 |
return deployable;
|
120 |
} |
|
121 |
|
|
122 |
/**
|
|
123 |
* @param theDeployableFile the file to parse and deploy
|
|
124 |
* @return the test context that will be used to verify if the container
|
|
125 |
* is started or not
|
|
126 |
*/
|
|
127 | 2 |
protected static String parseWebContext(File theDeployableFile) |
128 |
{ |
|
129 | 2 |
String context = theDeployableFile.getName(); |
130 | 2 |
int warIndex = context.toLowerCase().lastIndexOf(".war"); |
131 | 2 |
if (warIndex >= 0)
|
132 |
{ |
|
133 | 2 |
context = context.substring(0, warIndex); |
134 |
} |
|
135 | 2 |
return context;
|
136 |
} |
|
137 |
|
|
138 |
/**
|
|
139 |
* Find the first URL-pattern to which the Cactus servlet redirector is
|
|
140 |
* mapped in the deployment descriptor.
|
|
141 |
*
|
|
142 |
* @return the servlet redirector mapping if found or <code>null</code>
|
|
143 |
* if not found
|
|
144 |
* @param theWar the WAR descriptor that is parsed when looking for
|
|
145 |
* a Cactus servlet redirector mapping
|
|
146 |
* @throws IOException If there was a problem reading the deployment
|
|
147 |
* descriptor in the WAR
|
|
148 |
* @throws SAXException If the deployment descriptor of the WAR could not
|
|
149 |
* be parsed
|
|
150 |
* @throws ParserConfigurationException If there is an XML parser
|
|
151 |
* configration problem
|
|
152 |
*/
|
|
153 | 7 |
static String parseServletRedirectorMapping(WarArchive theWar)
|
154 |
throws SAXException, IOException, ParserConfigurationException
|
|
155 |
{ |
|
156 | 7 |
Iterator servletNames = theWar.getWebXml().getServletNamesForClass( |
157 |
"org.apache.cactus.server.ServletTestRedirector");
|
|
158 | 7 |
if (servletNames.hasNext())
|
159 |
{ |
|
160 |
// we only care about the first definition and the first mapping
|
|
161 | 5 |
String name = (String) servletNames.next(); |
162 | 5 |
Iterator mappings = theWar.getWebXml().getServletMappings(name); |
163 | 5 |
if (mappings.hasNext())
|
164 |
{ |
|
165 | 5 |
return (String) mappings.next();
|
166 |
} |
|
167 |
} |
|
168 | 2 |
return null; |
169 |
} |
|
170 |
|
|
171 |
/**
|
|
172 |
* Find the first URL-pattern to which the Cactus filter redirector is
|
|
173 |
* mapped in the deployment descriptor.
|
|
174 |
*
|
|
175 |
* @return the filter redirector mapping if found or <code>null</code>
|
|
176 |
* if not found
|
|
177 |
* @param theWar the WAR descriptor that is parsed when looking for
|
|
178 |
* a Cactus filter redirector mapping
|
|
179 |
* @throws IOException If there was a problem reading the deployment
|
|
180 |
* descriptor in the WAR
|
|
181 |
* @throws SAXException If the deployment descriptor of the WAR could not
|
|
182 |
* be parsed
|
|
183 |
* @throws ParserConfigurationException If there is an XML parser
|
|
184 |
* configration problem
|
|
185 |
*/
|
|
186 | 4 |
static String parseFilterRedirectorMapping(WarArchive theWar)
|
187 |
throws IOException, SAXException, ParserConfigurationException
|
|
188 |
{ |
|
189 | 4 |
Iterator filterNames = theWar.getWebXml().getFilterNamesForClass( |
190 |
"org.apache.cactus.server.FilterTestRedirector");
|
|
191 | 4 |
if (filterNames.hasNext())
|
192 |
{ |
|
193 |
// we only care about the first definition and the first mapping
|
|
194 | 0 |
String name = (String) filterNames.next(); |
195 | 0 |
Iterator mappings = theWar.getWebXml().getFilterMappings(name); |
196 | 0 |
if (mappings.hasNext())
|
197 |
{ |
|
198 | 0 |
return (String) mappings.next();
|
199 |
} |
|
200 |
} |
|
201 | 4 |
return null; |
202 |
} |
|
203 |
|
|
204 |
/**
|
|
205 |
* Find the first URL-pattern to which the Cactus JSP redirector is
|
|
206 |
* mapped in the deployment descriptor.
|
|
207 |
*
|
|
208 |
* @return the JSP redirector mapping if found or <code>null</code>
|
|
209 |
* if not found
|
|
210 |
* @param theWar the WAR descriptor that is parsed when looking for
|
|
211 |
* a Cactus JSP redirector mapping
|
|
212 |
* @throws IOException If there was a problem reading the deployment
|
|
213 |
* descriptor in the WAR
|
|
214 |
* @throws SAXException If the deployment descriptor of the WAR could not
|
|
215 |
* be parsed
|
|
216 |
* @throws ParserConfigurationException If there is an XML parser
|
|
217 |
* configration problem
|
|
218 |
*/
|
|
219 | 4 |
static String parseJspRedirectorMapping(WarArchive theWar)
|
220 |
throws IOException, SAXException, ParserConfigurationException
|
|
221 |
{ |
|
222 |
// To get the JSP redirector mapping, we must first get the full path to
|
|
223 |
// the corresponding JSP file in the WAR
|
|
224 | 4 |
String jspRedirectorPath = theWar.findResource("jspRedirector.jsp");
|
225 | 4 |
if (jspRedirectorPath != null) |
226 |
{ |
|
227 | 0 |
jspRedirectorPath = "/" + jspRedirectorPath;
|
228 | 0 |
Iterator jspNames = theWar.getWebXml().getServletNamesForJspFile( |
229 |
jspRedirectorPath); |
|
230 | 0 |
if (jspNames.hasNext())
|
231 |
{ |
|
232 |
// we only care about the first definition and the first
|
|
233 |
// mapping
|
|
234 | 0 |
String name = (String) jspNames.next(); |
235 | 0 |
Iterator mappings = |
236 |
theWar.getWebXml().getServletMappings(name); |
|
237 | 0 |
if (mappings.hasNext())
|
238 |
{ |
|
239 | 0 |
return (String) mappings.next();
|
240 |
} |
|
241 |
} |
|
242 |
} |
|
243 | 4 |
return null; |
244 |
} |
|
245 |
} |
|
246 |
|
|