|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| CactifyWarTask.java | 82% | 84.1% | 92% | 84.5% |
|
||||||||||||||
| 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;
|
|
| 58 |
|
|
| 59 |
import java.io.File;
|
|
| 60 |
import java.io.IOException;
|
|
| 61 |
import java.util.ArrayList;
|
|
| 62 |
import java.util.Iterator;
|
|
| 63 |
import java.util.List;
|
|
| 64 |
import java.util.StringTokenizer;
|
|
| 65 |
|
|
| 66 |
import javax.xml.parsers.ParserConfigurationException;
|
|
| 67 |
|
|
| 68 |
import org.apache.cactus.integration.ant.util.AntLog;
|
|
| 69 |
import org.apache.cactus.integration.ant.util.ResourceUtils;
|
|
| 70 |
import org.apache.cactus.integration.ant.deployment.DefaultWarArchive;
|
|
| 71 |
import org.apache.cactus.integration.ant.deployment.WarArchive;
|
|
| 72 |
import org.apache.cactus.integration.ant.deployment.WebXml;
|
|
| 73 |
import org.apache.cactus.integration.ant.deployment.WebXmlIo;
|
|
| 74 |
import org.apache.cactus.integration.ant.deployment.WebXmlMerger;
|
|
| 75 |
import org.apache.cactus.integration.ant.deployment.WebXmlVersion;
|
|
| 76 |
import org.apache.tools.ant.BuildException;
|
|
| 77 |
import org.apache.tools.ant.Project;
|
|
| 78 |
import org.apache.tools.ant.taskdefs.War;
|
|
| 79 |
import org.apache.tools.ant.types.EnumeratedAttribute;
|
|
| 80 |
import org.apache.tools.ant.types.FileSet;
|
|
| 81 |
import org.apache.tools.ant.types.XMLCatalog;
|
|
| 82 |
import org.apache.tools.ant.types.ZipFileSet;
|
|
| 83 |
import org.apache.tools.ant.util.FileUtils;
|
|
| 84 |
import org.xml.sax.SAXException;
|
|
| 85 |
|
|
| 86 |
/**
|
|
| 87 |
* An Ant task that injects elements necessary to run Cactus tests into an
|
|
| 88 |
* existing WAR file.
|
|
| 89 |
*
|
|
| 90 |
* @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
|
|
| 91 |
*
|
|
| 92 |
* @version $Id: CactifyWarTask.java,v 1.24.2.3 2003/10/25 17:22:06 vmassol Exp $
|
|
| 93 |
*/
|
|
| 94 |
public class CactifyWarTask extends War |
|
| 95 |
{
|
|
| 96 |
|
|
| 97 |
// Constants ---------------------------------------------------------------
|
|
| 98 |
|
|
| 99 |
/**
|
|
| 100 |
* The name of the Cactus filter redirector class.
|
|
| 101 |
*/
|
|
| 102 |
private static final String FILTER_REDIRECTOR_CLASS = |
|
| 103 |
"org.apache.cactus.server.FilterTestRedirector";
|
|
| 104 |
|
|
| 105 |
/**
|
|
| 106 |
* The default mapping of the Cactus filter redirector.
|
|
| 107 |
*/
|
|
| 108 |
private static final String DEFAULT_FILTER_REDIRECTOR_MAPPING = |
|
| 109 |
"/FilterRedirector";
|
|
| 110 |
|
|
| 111 |
/**
|
|
| 112 |
* The default mapping of the Cactus JSP redirector.
|
|
| 113 |
*/
|
|
| 114 |
private static final String DEFAULT_JSP_REDIRECTOR_MAPPING = |
|
| 115 |
"/JspRedirector";
|
|
| 116 |
|
|
| 117 |
/**
|
|
| 118 |
* The name of the Cactus servlet redirector class.
|
|
| 119 |
*/
|
|
| 120 |
private static final String SERVLET_REDIRECTOR_CLASS = |
|
| 121 |
"org.apache.cactus.server.ServletTestRedirector";
|
|
| 122 |
|
|
| 123 |
/**
|
|
| 124 |
* The default mapping of the Cactus servlet redirector.
|
|
| 125 |
*/
|
|
| 126 |
private static final String DEFAULT_SERVLET_REDIRECTOR_MAPPING = |
|
| 127 |
"/ServletRedirector";
|
|
| 128 |
|
|
| 129 |
// Inner Classes -----------------------------------------------------------
|
|
| 130 |
|
|
| 131 |
/**
|
|
| 132 |
* Abstract base class for nested redirector elements.
|
|
| 133 |
*/
|
|
| 134 |
public abstract static class Redirector |
|
| 135 |
{
|
|
| 136 |
|
|
| 137 |
// Instance Variables --------------------------------------------------
|
|
| 138 |
|
|
| 139 |
/**
|
|
| 140 |
* The name of the redirector.
|
|
| 141 |
*/
|
|
| 142 |
protected String name;
|
|
| 143 |
|
|
| 144 |
/**
|
|
| 145 |
* The URL pattern that the redirector will be mapped to.
|
|
| 146 |
*/
|
|
| 147 |
protected String mapping;
|
|
| 148 |
|
|
| 149 |
/**
|
|
| 150 |
* Comma-separated list of role names that should be granted access to
|
|
| 151 |
* the redirector.
|
|
| 152 |
*/
|
|
| 153 |
protected String roles;
|
|
| 154 |
|
|
| 155 |
// Abstract Methods ----------------------------------------------------
|
|
| 156 |
|
|
| 157 |
/**
|
|
| 158 |
* Merges the definition of the redirector into the provided deployment
|
|
| 159 |
* descriptor.
|
|
| 160 |
*
|
|
| 161 |
* @param theWebXml The deployment descriptor into which the redirector
|
|
| 162 |
* definition should be merged
|
|
| 163 |
*/
|
|
| 164 |
public abstract void mergeInto(WebXml theWebXml); |
|
| 165 |
|
|
| 166 |
// Public Methods ------------------------------------------------------
|
|
| 167 |
|
|
| 168 |
/**
|
|
| 169 |
* Sets the name of the redirector.
|
|
| 170 |
*
|
|
| 171 |
* @param theName The name to set
|
|
| 172 |
*/
|
|
| 173 | 8 |
public final void setName(String theName) |
| 174 |
{
|
|
| 175 | 8 |
this.name = theName;
|
| 176 |
} |
|
| 177 |
|
|
| 178 |
/**
|
|
| 179 |
* Sets the URL pattern that the redirector should be mapped to.
|
|
| 180 |
*
|
|
| 181 |
* @param theMapping The URL pattern to set
|
|
| 182 |
*/
|
|
| 183 | 12 |
public final void setMapping(String theMapping) |
| 184 |
{
|
|
| 185 | 12 |
this.mapping = theMapping;
|
| 186 |
} |
|
| 187 |
|
|
| 188 |
/**
|
|
| 189 |
* Sets the comma-separated list of role names that should be granted
|
|
| 190 |
* access to the redirector.
|
|
| 191 |
*
|
|
| 192 |
* @param theRoles The roles to set
|
|
| 193 |
*/
|
|
| 194 | 2 |
public final void setRoles(String theRoles) |
| 195 |
{
|
|
| 196 | 2 |
this.roles = theRoles;
|
| 197 |
} |
|
| 198 |
|
|
| 199 |
// Protected Methods ---------------------------------------------------
|
|
| 200 |
|
|
| 201 |
/**
|
|
| 202 |
* Adds the comma-separated list of security roles to a deployment
|
|
| 203 |
* descriptor.
|
|
| 204 |
*
|
|
| 205 |
* @param theWebXml The deployment descriptor
|
|
| 206 |
*/
|
|
| 207 | 2 |
protected final void addSecurity(WebXml theWebXml) |
| 208 |
{
|
|
| 209 | 2 |
StringTokenizer tokenizer = new StringTokenizer(this.roles, ","); |
| 210 | 2 |
List roles = new ArrayList();
|
| 211 | 2 |
while (tokenizer.hasMoreTokens())
|
| 212 |
{
|
|
| 213 | 3 |
String role = tokenizer.nextToken().trim(); |
| 214 | 3 |
if (!theWebXml.hasSecurityRole(role))
|
| 215 |
{
|
|
| 216 | 3 |
theWebXml.addSecurityRole(role); |
| 217 |
} |
|
| 218 | 3 |
roles.add(role); |
| 219 |
} |
|
| 220 | 2 |
if (!roles.isEmpty())
|
| 221 |
{
|
|
| 222 | 2 |
if (!theWebXml.hasLoginConfig())
|
| 223 |
{
|
|
| 224 | 1 |
theWebXml.setLoginConfig("BASIC", "myrealm"); |
| 225 |
} |
|
| 226 | 2 |
if (!theWebXml.hasSecurityConstraint(this.mapping)) |
| 227 |
{
|
|
| 228 | 2 |
theWebXml.addSecurityConstraint("Cactus Test Redirector",
|
| 229 |
this.mapping, roles);
|
|
| 230 |
} |
|
| 231 |
} |
|
| 232 |
} |
|
| 233 |
|
|
| 234 |
} |
|
| 235 |
|
|
| 236 |
/**
|
|
| 237 |
* Implementation of <code>Redirector</code> for filter test redirectors.
|
|
| 238 |
*/
|
|
| 239 |
public static final class FilterRedirector extends Redirector |
|
| 240 |
{
|
|
| 241 |
|
|
| 242 |
/**
|
|
| 243 |
* Default constructor.
|
|
| 244 |
*/
|
|
| 245 | 17 |
public FilterRedirector()
|
| 246 |
{
|
|
| 247 | 17 |
this.name = "FilterRedirector"; |
| 248 | 17 |
this.mapping = DEFAULT_FILTER_REDIRECTOR_MAPPING;
|
| 249 |
} |
|
| 250 |
|
|
| 251 |
/**
|
|
| 252 |
* @see CactifyWarTask.Redirector#mergeInto
|
|
| 253 |
*/
|
|
| 254 | 17 |
public void mergeInto(WebXml theWebXml) |
| 255 |
{
|
|
| 256 | 17 |
if (WebXmlVersion.V2_3.compareTo(theWebXml.getVersion()) <= 0)
|
| 257 |
{
|
|
| 258 | 11 |
theWebXml.addFilter(this.name, FILTER_REDIRECTOR_CLASS);
|
| 259 | 11 |
theWebXml.addFilterMapping(this.name, this.mapping); |
| 260 | 11 |
if (this.roles != null) |
| 261 |
{
|
|
| 262 | 0 |
addSecurity(theWebXml); |
| 263 |
} |
|
| 264 |
} |
|
| 265 |
} |
|
| 266 |
|
|
| 267 |
} |
|
| 268 |
|
|
| 269 |
/**
|
|
| 270 |
* Implementation of <code>Redirector</code> for JSP test redirectors.
|
|
| 271 |
*/
|
|
| 272 |
public static final class JspRedirector extends Redirector |
|
| 273 |
{
|
|
| 274 |
|
|
| 275 |
/**
|
|
| 276 |
* Default constructor.
|
|
| 277 |
*/
|
|
| 278 | 17 |
public JspRedirector()
|
| 279 |
{
|
|
| 280 | 17 |
this.name = "JspRedirector"; |
| 281 | 17 |
this.mapping = DEFAULT_JSP_REDIRECTOR_MAPPING;
|
| 282 |
} |
|
| 283 |
|
|
| 284 |
/**
|
|
| 285 |
* @see CactifyWarTask.Redirector#mergeInto
|
|
| 286 |
*/
|
|
| 287 | 17 |
public void mergeInto(WebXml theWebXml) |
| 288 |
{
|
|
| 289 | 17 |
theWebXml.addJspFile(this.name, "/jspRedirector.jsp"); |
| 290 | 17 |
theWebXml.addServletMapping(this.name, this.mapping); |
| 291 | 17 |
if (this.roles != null) |
| 292 |
{
|
|
| 293 | 0 |
addSecurity(theWebXml); |
| 294 |
} |
|
| 295 |
} |
|
| 296 |
|
|
| 297 |
} |
|
| 298 |
|
|
| 299 |
/**
|
|
| 300 |
* Implementation of <code>Redirector</code> for servlet test redirectors.
|
|
| 301 |
*/
|
|
| 302 |
public static final class ServletRedirector extends Redirector |
|
| 303 |
{
|
|
| 304 |
|
|
| 305 |
/**
|
|
| 306 |
* Default constructor.
|
|
| 307 |
*/
|
|
| 308 | 17 |
public ServletRedirector()
|
| 309 |
{
|
|
| 310 | 17 |
this.name = "ServletRedirector"; |
| 311 | 17 |
this.mapping = DEFAULT_SERVLET_REDIRECTOR_MAPPING;
|
| 312 |
} |
|
| 313 |
|
|
| 314 |
/**
|
|
| 315 |
* @see CactifyWarTask.Redirector#mergeInto
|
|
| 316 |
*/
|
|
| 317 | 17 |
public void mergeInto(WebXml theWebXml) |
| 318 |
{
|
|
| 319 | 17 |
theWebXml.addServlet(this.name, SERVLET_REDIRECTOR_CLASS);
|
| 320 | 17 |
theWebXml.addServletMapping(this.name, this.mapping); |
| 321 | 17 |
if (this.roles != null) |
| 322 |
{
|
|
| 323 | 2 |
addSecurity(theWebXml); |
| 324 |
} |
|
| 325 |
} |
|
| 326 |
|
|
| 327 |
} |
|
| 328 |
|
|
| 329 |
/**
|
|
| 330 |
* Enumeration for the <em>version</em> attribute.
|
|
| 331 |
*/
|
|
| 332 |
public static final class Version extends EnumeratedAttribute |
|
| 333 |
{
|
|
| 334 |
|
|
| 335 |
/**
|
|
| 336 |
* @see org.apache.tools.ant.types.EnumeratedAttribute#getValues()
|
|
| 337 |
*/
|
|
| 338 | 2 |
public String[] getValues()
|
| 339 |
{
|
|
| 340 | 2 |
return new String[] {"2.2", "2.3"}; |
| 341 |
} |
|
| 342 |
|
|
| 343 |
} |
|
| 344 |
|
|
| 345 |
// Instance Variables ------------------------------------------------------
|
|
| 346 |
|
|
| 347 |
/**
|
|
| 348 |
* The archive that contains the web-app that should be cactified.
|
|
| 349 |
*/
|
|
| 350 |
private File srcFile;
|
|
| 351 |
|
|
| 352 |
/**
|
|
| 353 |
* Location of the descriptor of which the content should be merged into
|
|
| 354 |
* the descriptor of the cactified archive.
|
|
| 355 |
*/
|
|
| 356 |
private File mergeWebXml;
|
|
| 357 |
|
|
| 358 |
/**
|
|
| 359 |
* The Cactus test redirectors.
|
|
| 360 |
*/
|
|
| 361 |
private List redirectors = new ArrayList(); |
|
| 362 |
|
|
| 363 |
/**
|
|
| 364 |
* For resolving entities such as DTDs.
|
|
| 365 |
*/
|
|
| 366 |
private XMLCatalog xmlCatalog = null; |
|
| 367 |
|
|
| 368 |
/**
|
|
| 369 |
* The web-app version to use when creating a WAR from scratch.
|
|
| 370 |
*/
|
|
| 371 |
private String version = null; |
|
| 372 |
|
|
| 373 |
// Public Methods ----------------------------------------------------------
|
|
| 374 |
|
|
| 375 |
/**
|
|
| 376 |
* @see org.apache.tools.ant.Task#execute()
|
|
| 377 |
*/
|
|
| 378 | 18 |
public void execute() throws BuildException |
| 379 |
{
|
|
| 380 | 18 |
WebXml webXml = null;
|
| 381 | 18 |
if (this.srcFile != null) |
| 382 |
{
|
|
| 383 | 15 |
log("Analyzing war: " + this.srcFile.getAbsolutePath(), |
| 384 |
Project.MSG_INFO); |
|
| 385 |
|
|
| 386 |
// Add everything that's in the source WAR to the destination WAR
|
|
| 387 | 15 |
ZipFileSet currentFiles = new ZipFileSet();
|
| 388 | 15 |
currentFiles.setSrc(this.srcFile);
|
| 389 | 15 |
currentFiles.createExclude().setName("WEB-INF/web.xml");
|
| 390 | 15 |
addZipfileset(currentFiles); |
| 391 |
|
|
| 392 |
// Parse the original deployment descriptor
|
|
| 393 | 15 |
webXml = getOriginalWebXml(); |
| 394 |
} |
|
| 395 |
else
|
|
| 396 |
{
|
|
| 397 | 3 |
if (this.version == null) |
| 398 |
{
|
|
| 399 | 1 |
throw new BuildException("You need to specify either the " |
| 400 |
+ "[srcfile] or the [version] attribute");
|
|
| 401 |
} |
|
| 402 | 2 |
WebXmlVersion webXmlVersion = null;
|
| 403 | 2 |
if (this.version.equals("2.2")) |
| 404 |
{
|
|
| 405 | 1 |
webXmlVersion = WebXmlVersion.V2_2; |
| 406 |
} |
|
| 407 |
else
|
|
| 408 |
{
|
|
| 409 | 1 |
webXmlVersion = WebXmlVersion.V2_3; |
| 410 |
} |
|
| 411 | 2 |
try
|
| 412 |
{
|
|
| 413 | 2 |
webXml = WebXmlIo.newWebXml(webXmlVersion); |
| 414 |
} |
|
| 415 |
catch (ParserConfigurationException pce)
|
|
| 416 |
{
|
|
| 417 | 0 |
throw new BuildException( |
| 418 |
"Could not create deployment descriptor", pce);
|
|
| 419 |
} |
|
| 420 |
} |
|
| 421 |
|
|
| 422 | 16 |
File tmpWebXml = cactifyWebXml(webXml); |
| 423 | 16 |
setWebxml(tmpWebXml); |
| 424 |
|
|
| 425 | 16 |
addCactusJars(); |
| 426 |
|
|
| 427 | 16 |
try
|
| 428 |
{
|
|
| 429 | 16 |
super.execute();
|
| 430 |
} |
|
| 431 |
finally
|
|
| 432 |
{
|
|
| 433 |
// Even though the temporary descriptor will get deleted
|
|
| 434 |
// automatically when the VM exits, delete it explicitly here just
|
|
| 435 |
// to be a better citizen
|
|
| 436 | 16 |
tmpWebXml.delete(); |
| 437 |
} |
|
| 438 |
} |
|
| 439 |
|
|
| 440 |
/**
|
|
| 441 |
* Adds a Cactus filter test redirector.
|
|
| 442 |
*
|
|
| 443 |
* @param theFilterRedirector The redirector to add
|
|
| 444 |
*/
|
|
| 445 | 4 |
public final void addFilterRedirector(FilterRedirector theFilterRedirector) |
| 446 |
{
|
|
| 447 | 4 |
this.redirectors.add(theFilterRedirector);
|
| 448 |
} |
|
| 449 |
|
|
| 450 |
/**
|
|
| 451 |
* Adds a Cactus JSP test redirector.
|
|
| 452 |
*
|
|
| 453 |
* @param theJspRedirector The redirector to add
|
|
| 454 |
*/
|
|
| 455 | 3 |
public final void addJspRedirector(JspRedirector theJspRedirector) |
| 456 |
{
|
|
| 457 | 3 |
this.redirectors.add(theJspRedirector);
|
| 458 |
} |
|
| 459 |
|
|
| 460 |
/**
|
|
| 461 |
* Adds a Cactus servlet test redirector.
|
|
| 462 |
*
|
|
| 463 |
* @param theServletRedirector The redirector to add
|
|
| 464 |
*/
|
|
| 465 | 5 |
public final void addServletRedirector( |
| 466 |
ServletRedirector theServletRedirector) |
|
| 467 |
{
|
|
| 468 | 5 |
this.redirectors.add(theServletRedirector);
|
| 469 |
} |
|
| 470 |
|
|
| 471 |
/**
|
|
| 472 |
* Adds an XML catalog to the internal catalog.
|
|
| 473 |
*
|
|
| 474 |
* @param theXmlCatalog the XMLCatalog instance to use to look up DTDs
|
|
| 475 |
*/
|
|
| 476 | 0 |
public final void addConfiguredXMLCatalog(XMLCatalog theXmlCatalog) |
| 477 |
{
|
|
| 478 | 0 |
if (this.xmlCatalog == null) |
| 479 |
{
|
|
| 480 | 0 |
this.xmlCatalog = new XMLCatalog(); |
| 481 | 0 |
this.xmlCatalog.setProject(getProject());
|
| 482 |
} |
|
| 483 | 0 |
this.xmlCatalog.addConfiguredXMLCatalog(theXmlCatalog);
|
| 484 |
} |
|
| 485 |
|
|
| 486 |
/**
|
|
| 487 |
* The descriptor to merge into the original file.
|
|
| 488 |
*
|
|
| 489 |
* @param theMergeFile the <code>web.xml</code> to merge
|
|
| 490 |
*/
|
|
| 491 | 0 |
public final void setMergeWebXml(File theMergeFile) |
| 492 |
{
|
|
| 493 | 0 |
this.mergeWebXml = theMergeFile;
|
| 494 |
} |
|
| 495 |
|
|
| 496 |
/**
|
|
| 497 |
* Sets the web application archive that should be cactified.
|
|
| 498 |
*
|
|
| 499 |
* @param theSrcFile The WAR file to set
|
|
| 500 |
*/
|
|
| 501 | 15 |
public final void setSrcFile(File theSrcFile) |
| 502 |
{
|
|
| 503 | 15 |
this.srcFile = theSrcFile;
|
| 504 |
} |
|
| 505 |
|
|
| 506 |
/**
|
|
| 507 |
* Sets the web-app version to use when creating a WAR file from scratch.
|
|
| 508 |
*
|
|
| 509 |
* @param theVersion The version
|
|
| 510 |
*/
|
|
| 511 | 2 |
public final void setVersion(Version theVersion) |
| 512 |
{
|
|
| 513 | 2 |
this.version = theVersion.getValue();
|
| 514 |
} |
|
| 515 |
|
|
| 516 |
// Private Methods ---------------------------------------------------------
|
|
| 517 |
|
|
| 518 |
/**
|
|
| 519 |
* Adds the libraries required by Cactus on the server side.
|
|
| 520 |
*/
|
|
| 521 | 16 |
private void addCactusJars() |
| 522 |
{
|
|
| 523 | 16 |
addJarWithClass("org.aspectj.lang.JoinPoint", "AspectJ Runtime"); |
| 524 | 16 |
addJarWithClass("org.apache.cactus.ServletTestCase",
|
| 525 |
"Cactus Framework");
|
|
| 526 | 16 |
addJarWithClass("org.apache.commons.logging.Log",
|
| 527 |
"Commons-Logging");
|
|
| 528 | 16 |
addJarWithClass("org.apache.commons.httpclient.HttpClient",
|
| 529 |
"Commons-HttpClient");
|
|
| 530 | 16 |
addJarWithClass("junit.framework.TestCase", "JUnit"); |
| 531 |
} |
|
| 532 |
|
|
| 533 |
/**
|
|
| 534 |
* Adds the JAR file containing the specified resource to the WEB-INF/lib
|
|
| 535 |
* folder of a web-application archive.
|
|
| 536 |
*
|
|
| 537 |
* @param theClassName The name of the class that the JAR contains
|
|
| 538 |
* @param theDescription A description of the JAR that should be displayed
|
|
| 539 |
* to the user in log messages
|
|
| 540 |
*/
|
|
| 541 | 80 |
private void addJarWithClass(String theClassName, String theDescription) |
| 542 |
{
|
|
| 543 | 80 |
String resourceName = "/" + theClassName.replace('.', '/') + ".class"; |
| 544 | 80 |
if (this.srcFile != null) |
| 545 |
{
|
|
| 546 | 70 |
try
|
| 547 |
{
|
|
| 548 | 70 |
WarArchive srcWar = new DefaultWarArchive(srcFile);
|
| 549 | 70 |
if (srcWar.containsClass(theClassName))
|
| 550 |
{
|
|
| 551 | 5 |
log("The " + theDescription + " JAR is already present in " |
| 552 |
+ "the WAR", Project.MSG_VERBOSE);
|
|
| 553 | 5 |
return;
|
| 554 |
} |
|
| 555 |
} |
|
| 556 |
catch (IOException ioe)
|
|
| 557 |
{
|
|
| 558 | 0 |
log("Problem reading source WAR to when trying to detect "
|
| 559 |
+ "already present JAR files (" + ioe + ")", |
|
| 560 |
Project.MSG_WARN); |
|
| 561 |
} |
|
| 562 |
} |
|
| 563 | 75 |
ZipFileSet jar = new ZipFileSet();
|
| 564 | 75 |
File file = ResourceUtils.getResourceLocation(resourceName); |
| 565 | 75 |
if (file != null) |
| 566 |
{
|
|
| 567 | 30 |
jar.setFile(file); |
| 568 | 30 |
addLib(jar); |
| 569 |
} |
|
| 570 |
else
|
|
| 571 |
{
|
|
| 572 | 45 |
log("Could not find the " + theDescription + " JAR", |
| 573 |
Project.MSG_WARN); |
|
| 574 | 45 |
log("You need to add the JAR to the classpath of the task",
|
| 575 |
Project.MSG_INFO); |
|
| 576 | 45 |
log("(Searched for class " + theClassName + ")", Project.MSG_DEBUG); |
| 577 |
} |
|
| 578 |
} |
|
| 579 |
|
|
| 580 |
/**
|
|
| 581 |
* Adds the Cactus JSP redirector file to the web application.
|
|
| 582 |
*/
|
|
| 583 | 16 |
private void addJspRedirector() |
| 584 |
{
|
|
| 585 |
// Now copy the actual JSP redirector file into the web application
|
|
| 586 | 16 |
File jspRedirectorFile = new File(
|
| 587 |
new File(System.getProperty("java.io.tmpdir")), |
|
| 588 |
"jspRedirector.jsp");
|
|
| 589 | 16 |
jspRedirectorFile.deleteOnExit(); |
| 590 | 16 |
try
|
| 591 |
{
|
|
| 592 | 16 |
ResourceUtils.copyResource(getProject(), |
| 593 |
"/org/apache/cactus/server/jspRedirector.jsp",
|
|
| 594 |
jspRedirectorFile); |
|
| 595 |
} |
|
| 596 |
catch (IOException e)
|
|
| 597 |
{
|
|
| 598 | 16 |
log("Could not copy the JSP redirector (" + e.getMessage() + ")", |
| 599 |
Project.MSG_WARN); |
|
| 600 |
} |
|
| 601 | 16 |
FileSet fileSet = new FileSet();
|
| 602 | 16 |
fileSet.setFile(jspRedirectorFile); |
| 603 | 16 |
addFileset(fileSet); |
| 604 |
} |
|
| 605 |
|
|
| 606 |
/**
|
|
| 607 |
* Adds the definitions corresponding to the nested redirector elements to
|
|
| 608 |
* the provided deployment descriptor.
|
|
| 609 |
*
|
|
| 610 |
* @param theWebXml The deployment descriptor
|
|
| 611 |
*/
|
|
| 612 | 16 |
private void addRedirectorDefinitions(WebXml theWebXml) |
| 613 |
{
|
|
| 614 | 16 |
boolean filterRedirectorDefined = false; |
| 615 | 16 |
boolean jspRedirectorDefined = false; |
| 616 | 16 |
boolean servletRedirectorDefined = false; |
| 617 |
|
|
| 618 |
// add the user defined redirectors
|
|
| 619 | 16 |
for (Iterator i = this.redirectors.iterator(); i.hasNext();) |
| 620 |
{
|
|
| 621 | 12 |
Redirector redirector = (Redirector) i.next(); |
| 622 | 12 |
if (redirector instanceof FilterRedirector) |
| 623 |
{
|
|
| 624 | 4 |
filterRedirectorDefined = true;
|
| 625 |
} |
|
| 626 | 8 |
else if (redirector instanceof JspRedirector) |
| 627 |
{
|
|
| 628 | 3 |
jspRedirectorDefined = true;
|
| 629 |
} |
|
| 630 | 5 |
else if (redirector instanceof ServletRedirector) |
| 631 |
{
|
|
| 632 | 5 |
servletRedirectorDefined = true;
|
| 633 |
} |
|
| 634 | 12 |
redirector.mergeInto(theWebXml); |
| 635 |
} |
|
| 636 |
|
|
| 637 |
// now add the default redirectors if they haven't been provided by
|
|
| 638 |
// the user
|
|
| 639 | 16 |
if (!filterRedirectorDefined)
|
| 640 |
{
|
|
| 641 | 13 |
new FilterRedirector().mergeInto(theWebXml);
|
| 642 |
} |
|
| 643 | 16 |
if (!servletRedirectorDefined)
|
| 644 |
{
|
|
| 645 | 12 |
new ServletRedirector().mergeInto(theWebXml);
|
| 646 |
} |
|
| 647 | 16 |
if (!jspRedirectorDefined)
|
| 648 |
{
|
|
| 649 | 14 |
new JspRedirector().mergeInto(theWebXml);
|
| 650 |
} |
|
| 651 |
} |
|
| 652 |
|
|
| 653 |
/**
|
|
| 654 |
* Enhances the provided web deployment descriptor with the definitions
|
|
| 655 |
* required for testing with Cactus.
|
|
| 656 |
*
|
|
| 657 |
* @param theWebXml The original deployment descriptor
|
|
| 658 |
* @return A temporary file containing the cactified descriptor
|
|
| 659 |
*/
|
|
| 660 | 16 |
private File cactifyWebXml(WebXml theWebXml)
|
| 661 |
{
|
|
| 662 | 16 |
addRedirectorDefinitions(theWebXml); |
| 663 | 16 |
addJspRedirector(); |
| 664 |
|
|
| 665 |
// If the user has specified a deployment descriptor to merge into the
|
|
| 666 |
// cactified descriptor, perform the merge
|
|
| 667 | 16 |
if (this.mergeWebXml != null) |
| 668 |
{
|
|
| 669 | 0 |
try
|
| 670 |
{
|
|
| 671 | 0 |
WebXml parsedMergeWebXml = WebXmlIo.parseWebXmlFromFile( |
| 672 |
this.mergeWebXml, this.xmlCatalog); |
|
| 673 | 0 |
WebXmlMerger merger = new WebXmlMerger(theWebXml);
|
| 674 | 0 |
merger.setLog(new AntLog(this)); |
| 675 | 0 |
merger = new WebXmlMerger(theWebXml);
|
| 676 | 0 |
merger.setLog(new AntLog(this)); |
| 677 | 0 |
merger.merge(parsedMergeWebXml); |
| 678 |
} |
|
| 679 |
catch (IOException e)
|
|
| 680 |
{
|
|
| 681 | 0 |
throw new BuildException( |
| 682 |
"Could not merge deployment descriptors", e);
|
|
| 683 |
} |
|
| 684 |
catch (SAXException e)
|
|
| 685 |
{
|
|
| 686 | 0 |
throw new BuildException("Parsing of merge file failed", e); |
| 687 |
} |
|
| 688 |
catch (ParserConfigurationException e)
|
|
| 689 |
{
|
|
| 690 | 0 |
throw new BuildException("XML parser configuration error", e); |
| 691 |
} |
|
| 692 |
} |
|
| 693 |
|
|
| 694 |
// Serialize the cactified deployment descriptor into a temporary file,
|
|
| 695 |
// so that it can get picked up by the War task
|
|
| 696 | 16 |
FileUtils fileUtils = FileUtils.newFileUtils(); |
| 697 | 16 |
File tmpWebXml = fileUtils.createTempFile("cactus", "web.xml", |
| 698 |
getProject().getBaseDir()); |
|
| 699 | 16 |
tmpWebXml.deleteOnExit(); |
| 700 | 16 |
try
|
| 701 |
{
|
|
| 702 | 16 |
WebXmlIo.writeWebXml(theWebXml, tmpWebXml, null, true); |
| 703 |
} |
|
| 704 |
catch (IOException ioe)
|
|
| 705 |
{
|
|
| 706 | 0 |
throw new BuildException( |
| 707 |
"Could not write temporary deployment descriptor", ioe);
|
|
| 708 |
} |
|
| 709 | 16 |
return tmpWebXml;
|
| 710 |
} |
|
| 711 |
|
|
| 712 |
/**
|
|
| 713 |
* Extracts and parses the original web deployment descriptor from the
|
|
| 714 |
* web-app.
|
|
| 715 |
*
|
|
| 716 |
* @return The parsed descriptor
|
|
| 717 |
* @throws BuildException If the descriptor is not found or could not be
|
|
| 718 |
* parsed
|
|
| 719 |
*/
|
|
| 720 | 15 |
private WebXml getOriginalWebXml() throws BuildException |
| 721 |
{
|
|
| 722 |
// Open the archive as JAR file and extract the deployment descriptor
|
|
| 723 | 15 |
WarArchive war = null;
|
| 724 | 15 |
try
|
| 725 |
{
|
|
| 726 | 15 |
war = new DefaultWarArchive(this.srcFile); |
| 727 | 15 |
WebXml webXml = war.getWebXml(); |
| 728 | 15 |
if (webXml == null) |
| 729 |
{
|
|
| 730 | 1 |
throw new BuildException( |
| 731 |
"The source file does not contain a deployment descriptor");
|
|
| 732 |
} |
|
| 733 | 14 |
return webXml;
|
| 734 |
} |
|
| 735 |
catch (SAXException e)
|
|
| 736 |
{
|
|
| 737 | 0 |
throw new BuildException( |
| 738 |
"Parsing of deployment descriptor failed", e);
|
|
| 739 |
} |
|
| 740 |
catch (IOException e)
|
|
| 741 |
{
|
|
| 742 | 0 |
throw new BuildException("Failed to open WAR", e); |
| 743 |
} |
|
| 744 |
catch (ParserConfigurationException e)
|
|
| 745 |
{
|
|
| 746 | 0 |
throw new BuildException("XML parser configuration error", e); |
| 747 |
} |
|
| 748 |
} |
|
| 749 |
|
|
| 750 |
} |
|
| 751 |
|
|
||||||||||