|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| CactifyWarTask.java | 83.3% | 88% | 94.6% | 87.9% |
|
||||||||||||||
| 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;
|
|
| 21 |
|
|
| 22 |
import java.io.File;
|
|
| 23 |
import java.io.IOException;
|
|
| 24 |
import java.util.ArrayList;
|
|
| 25 |
import java.util.Iterator;
|
|
| 26 |
import java.util.List;
|
|
| 27 |
import java.util.StringTokenizer;
|
|
| 28 |
|
|
| 29 |
import javax.xml.parsers.ParserConfigurationException;
|
|
| 30 |
|
|
| 31 |
import org.apache.cactus.integration.ant.util.ResourceUtils;
|
|
| 32 |
import org.apache.tools.ant.BuildException;
|
|
| 33 |
import org.apache.tools.ant.Project;
|
|
| 34 |
import org.apache.tools.ant.taskdefs.War;
|
|
| 35 |
import org.apache.tools.ant.types.EnumeratedAttribute;
|
|
| 36 |
import org.apache.tools.ant.types.FileSet;
|
|
| 37 |
import org.apache.tools.ant.types.XMLCatalog;
|
|
| 38 |
import org.apache.tools.ant.types.ZipFileSet;
|
|
| 39 |
import org.apache.tools.ant.util.FileUtils;
|
|
| 40 |
import org.codehaus.cargo.module.webapp.DefaultWarArchive;
|
|
| 41 |
import org.codehaus.cargo.module.webapp.WarArchive;
|
|
| 42 |
import org.codehaus.cargo.module.webapp.WebXml;
|
|
| 43 |
import org.codehaus.cargo.module.webapp.WebXmlIo;
|
|
| 44 |
import org.codehaus.cargo.module.webapp.WebXmlMerger;
|
|
| 45 |
import org.codehaus.cargo.module.webapp.WebXmlVersion;
|
|
| 46 |
import org.codehaus.cargo.util.monitor.AntMonitor;
|
|
| 47 |
import org.xml.sax.SAXException;
|
|
| 48 |
|
|
| 49 |
/**
|
|
| 50 |
* An Ant task that injects elements necessary to run Cactus tests into an
|
|
| 51 |
* existing WAR file.
|
|
| 52 |
*
|
|
| 53 |
* @version $Id: CactifyWarTask.java 239172 2005-05-17 09:14:26Z grimsell $
|
|
| 54 |
*/
|
|
| 55 |
public class CactifyWarTask extends War |
|
| 56 |
{
|
|
| 57 |
|
|
| 58 |
// Constants ---------------------------------------------------------------
|
|
| 59 |
|
|
| 60 |
/**
|
|
| 61 |
* The name of the Cactus filter redirector class.
|
|
| 62 |
*/
|
|
| 63 |
private static final String FILTER_REDIRECTOR_CLASS = |
|
| 64 |
"org.apache.cactus.server.FilterTestRedirector";
|
|
| 65 |
|
|
| 66 |
/**
|
|
| 67 |
* The default mapping of the Cactus filter redirector.
|
|
| 68 |
*/
|
|
| 69 |
private static final String DEFAULT_FILTER_REDIRECTOR_MAPPING = |
|
| 70 |
"/FilterRedirector";
|
|
| 71 |
|
|
| 72 |
/**
|
|
| 73 |
* The default mapping of the Cactus JSP redirector.
|
|
| 74 |
*/
|
|
| 75 |
private static final String DEFAULT_JSP_REDIRECTOR_MAPPING = |
|
| 76 |
"/JspRedirector";
|
|
| 77 |
|
|
| 78 |
/**
|
|
| 79 |
* The name of the Cactus servlet redirector class.
|
|
| 80 |
*/
|
|
| 81 |
private static final String SERVLET_REDIRECTOR_CLASS = |
|
| 82 |
"org.apache.cactus.server.ServletTestRedirector";
|
|
| 83 |
|
|
| 84 |
/**
|
|
| 85 |
* The default mapping of the Cactus servlet redirector.
|
|
| 86 |
*/
|
|
| 87 |
private static final String DEFAULT_SERVLET_REDIRECTOR_MAPPING = |
|
| 88 |
"/ServletRedirector";
|
|
| 89 |
|
|
| 90 |
// Inner Classes -----------------------------------------------------------
|
|
| 91 |
|
|
| 92 |
/**
|
|
| 93 |
* Abstract base class for nested redirector elements.
|
|
| 94 |
*/
|
|
| 95 |
public abstract static class Redirector |
|
| 96 |
{
|
|
| 97 |
|
|
| 98 |
// Instance Variables --------------------------------------------------
|
|
| 99 |
|
|
| 100 |
/**
|
|
| 101 |
* The name of the redirector.
|
|
| 102 |
*/
|
|
| 103 |
protected String name;
|
|
| 104 |
|
|
| 105 |
/**
|
|
| 106 |
* The URL pattern that the redirector will be mapped to.
|
|
| 107 |
*/
|
|
| 108 |
protected String mapping;
|
|
| 109 |
|
|
| 110 |
/**
|
|
| 111 |
* Comma-separated list of role names that should be granted access to
|
|
| 112 |
* the redirector.
|
|
| 113 |
*/
|
|
| 114 |
protected String roles;
|
|
| 115 |
|
|
| 116 |
// Abstract Methods ----------------------------------------------------
|
|
| 117 |
|
|
| 118 |
/**
|
|
| 119 |
* Merges the definition of the redirector into the provided deployment
|
|
| 120 |
* descriptor.
|
|
| 121 |
*
|
|
| 122 |
* @param theWebXml The deployment descriptor into which the redirector
|
|
| 123 |
* definition should be merged
|
|
| 124 |
*/
|
|
| 125 |
public abstract void mergeInto(WebXml theWebXml); |
|
| 126 |
|
|
| 127 |
// Public Methods ------------------------------------------------------
|
|
| 128 |
|
|
| 129 |
/**
|
|
| 130 |
* Sets the name of the redirector.
|
|
| 131 |
*
|
|
| 132 |
* @param theName The name to set
|
|
| 133 |
*/
|
|
| 134 | 8 |
public final void setName(String theName) |
| 135 |
{
|
|
| 136 | 8 |
this.name = theName;
|
| 137 |
} |
|
| 138 |
|
|
| 139 |
/**
|
|
| 140 |
* Sets the URL pattern that the redirector should be mapped to.
|
|
| 141 |
*
|
|
| 142 |
* @param theMapping The URL pattern to set
|
|
| 143 |
*/
|
|
| 144 | 12 |
public final void setMapping(String theMapping) |
| 145 |
{
|
|
| 146 | 12 |
this.mapping = theMapping;
|
| 147 |
} |
|
| 148 |
|
|
| 149 |
/**
|
|
| 150 |
* Sets the comma-separated list of role names that should be granted
|
|
| 151 |
* access to the redirector.
|
|
| 152 |
*
|
|
| 153 |
* @param theRoles The roles to set
|
|
| 154 |
*/
|
|
| 155 | 2 |
public final void setRoles(String theRoles) |
| 156 |
{
|
|
| 157 | 2 |
this.roles = theRoles;
|
| 158 |
} |
|
| 159 |
|
|
| 160 |
// Protected Methods ---------------------------------------------------
|
|
| 161 |
|
|
| 162 |
/**
|
|
| 163 |
* Adds the comma-separated list of security roles to a deployment
|
|
| 164 |
* descriptor.
|
|
| 165 |
*
|
|
| 166 |
* @param theWebXml The deployment descriptor
|
|
| 167 |
*/
|
|
| 168 | 2 |
protected final void addSecurity(WebXml theWebXml) |
| 169 |
{
|
|
| 170 | 2 |
StringTokenizer tokenizer = new StringTokenizer(this.roles, ","); |
| 171 | 2 |
List roles = new ArrayList();
|
| 172 | 2 |
while (tokenizer.hasMoreTokens())
|
| 173 |
{
|
|
| 174 | 3 |
String role = tokenizer.nextToken().trim(); |
| 175 | 3 |
if (!theWebXml.hasSecurityRole(role))
|
| 176 |
{
|
|
| 177 | 3 |
theWebXml.addSecurityRole(role); |
| 178 |
} |
|
| 179 | 3 |
roles.add(role); |
| 180 |
} |
|
| 181 | 2 |
if (!roles.isEmpty())
|
| 182 |
{
|
|
| 183 | 2 |
if (!theWebXml.hasLoginConfig())
|
| 184 |
{
|
|
| 185 | 1 |
theWebXml.setLoginConfig("BASIC", "myrealm"); |
| 186 |
} |
|
| 187 | 2 |
if (!theWebXml.hasSecurityConstraint(this.mapping)) |
| 188 |
{
|
|
| 189 | 2 |
theWebXml.addSecurityConstraint("Cactus Test Redirector",
|
| 190 |
this.mapping, roles);
|
|
| 191 |
} |
|
| 192 |
} |
|
| 193 |
} |
|
| 194 |
|
|
| 195 |
} |
|
| 196 |
|
|
| 197 |
/**
|
|
| 198 |
* Implementation of <code>Redirector</code> for filter test redirectors.
|
|
| 199 |
*/
|
|
| 200 |
public static final class FilterRedirector extends Redirector |
|
| 201 |
{
|
|
| 202 |
|
|
| 203 |
/**
|
|
| 204 |
* Default constructor.
|
|
| 205 |
*/
|
|
| 206 | 23 |
public FilterRedirector()
|
| 207 |
{
|
|
| 208 | 23 |
this.name = "FilterRedirector"; |
| 209 | 23 |
this.mapping = DEFAULT_FILTER_REDIRECTOR_MAPPING;
|
| 210 |
} |
|
| 211 |
|
|
| 212 |
/**
|
|
| 213 |
* @see CactifyWarTask.Redirector#mergeInto
|
|
| 214 |
*/
|
|
| 215 | 23 |
public void mergeInto(WebXml theWebXml) |
| 216 |
{
|
|
| 217 | 23 |
if (WebXmlVersion.V2_3.compareTo(theWebXml.getVersion()) <= 0)
|
| 218 |
{
|
|
| 219 | 14 |
theWebXml.addFilter(this.name, FILTER_REDIRECTOR_CLASS);
|
| 220 | 14 |
theWebXml.addFilterMapping(this.name, this.mapping); |
| 221 | 14 |
if (this.roles != null) |
| 222 |
{
|
|
| 223 | 0 |
addSecurity(theWebXml); |
| 224 |
} |
|
| 225 |
} |
|
| 226 |
} |
|
| 227 |
|
|
| 228 |
} |
|
| 229 |
|
|
| 230 |
/**
|
|
| 231 |
* Implementation of <code>Redirector</code> for JSP test redirectors.
|
|
| 232 |
*/
|
|
| 233 |
public static final class JspRedirector extends Redirector |
|
| 234 |
{
|
|
| 235 |
|
|
| 236 |
/**
|
|
| 237 |
* Default constructor.
|
|
| 238 |
*/
|
|
| 239 | 23 |
public JspRedirector()
|
| 240 |
{
|
|
| 241 | 23 |
this.name = "JspRedirector"; |
| 242 | 23 |
this.mapping = DEFAULT_JSP_REDIRECTOR_MAPPING;
|
| 243 |
} |
|
| 244 |
|
|
| 245 |
/**
|
|
| 246 |
* @see CactifyWarTask.Redirector#mergeInto
|
|
| 247 |
*/
|
|
| 248 | 23 |
public void mergeInto(WebXml theWebXml) |
| 249 |
{
|
|
| 250 | 23 |
theWebXml.addJspFile(this.name, "/jspRedirector.jsp"); |
| 251 | 23 |
theWebXml.addServletMapping(this.name, this.mapping); |
| 252 | 23 |
if (this.roles != null) |
| 253 |
{
|
|
| 254 | 0 |
addSecurity(theWebXml); |
| 255 |
} |
|
| 256 |
} |
|
| 257 |
|
|
| 258 |
} |
|
| 259 |
|
|
| 260 |
/**
|
|
| 261 |
* Implementation of <code>Redirector</code> for servlet test redirectors.
|
|
| 262 |
*/
|
|
| 263 |
public static final class ServletRedirector extends Redirector |
|
| 264 |
{
|
|
| 265 |
|
|
| 266 |
/**
|
|
| 267 |
* Default constructor.
|
|
| 268 |
*/
|
|
| 269 | 23 |
public ServletRedirector()
|
| 270 |
{
|
|
| 271 | 23 |
this.name = "ServletRedirector"; |
| 272 | 23 |
this.mapping = DEFAULT_SERVLET_REDIRECTOR_MAPPING;
|
| 273 |
} |
|
| 274 |
|
|
| 275 |
/**
|
|
| 276 |
* @see CactifyWarTask.Redirector#mergeInto
|
|
| 277 |
*/
|
|
| 278 | 23 |
public void mergeInto(WebXml theWebXml) |
| 279 |
{
|
|
| 280 | 23 |
theWebXml.addServlet(this.name, SERVLET_REDIRECTOR_CLASS);
|
| 281 | 23 |
theWebXml.addServletMapping(this.name, this.mapping); |
| 282 | 23 |
if (this.roles != null) |
| 283 |
{
|
|
| 284 | 2 |
addSecurity(theWebXml); |
| 285 |
} |
|
| 286 |
} |
|
| 287 |
|
|
| 288 |
} |
|
| 289 |
|
|
| 290 |
/**
|
|
| 291 |
* Enumeration for the <em>version</em> attribute.
|
|
| 292 |
*/
|
|
| 293 |
public static final class Version extends EnumeratedAttribute |
|
| 294 |
{
|
|
| 295 |
|
|
| 296 |
/**
|
|
| 297 |
* @see org.apache.tools.ant.types.EnumeratedAttribute#getValues()
|
|
| 298 |
*/
|
|
| 299 | 6 |
public String[] getValues()
|
| 300 |
{
|
|
| 301 | 6 |
return new String[] {"2.2", "2.3"}; |
| 302 |
} |
|
| 303 |
|
|
| 304 |
} |
|
| 305 |
|
|
| 306 |
/**
|
|
| 307 |
* Implements the nested element ejbref
|
|
| 308 |
*/
|
|
| 309 |
public static final class EjbRef |
|
| 310 |
{
|
|
| 311 |
/**
|
|
| 312 |
* The name
|
|
| 313 |
*/
|
|
| 314 |
private String name;
|
|
| 315 |
/**
|
|
| 316 |
* The local interface
|
|
| 317 |
*/
|
|
| 318 |
private String localInterface;
|
|
| 319 |
/**
|
|
| 320 |
* The local home interface
|
|
| 321 |
*/
|
|
| 322 |
private String localHomeInterface;
|
|
| 323 |
/**
|
|
| 324 |
* The jndi name
|
|
| 325 |
*/
|
|
| 326 |
private String jndiName;
|
|
| 327 |
/**
|
|
| 328 |
* The type
|
|
| 329 |
*/
|
|
| 330 |
private String type;
|
|
| 331 |
|
|
| 332 |
/**
|
|
| 333 |
* Returns the jndi name
|
|
| 334 |
*
|
|
| 335 |
* @return Returns the jndiName.
|
|
| 336 |
*/
|
|
| 337 | 3 |
public String getJndiName()
|
| 338 |
{
|
|
| 339 | 3 |
return jndiName;
|
| 340 |
} |
|
| 341 |
/**
|
|
| 342 |
* Sets the jndiName
|
|
| 343 |
*
|
|
| 344 |
* @param theJndiName The jndiName to set.
|
|
| 345 |
*/
|
|
| 346 | 3 |
public void setJndiName(String theJndiName) |
| 347 |
{
|
|
| 348 | 3 |
this.jndiName = theJndiName;
|
| 349 |
} |
|
| 350 |
|
|
| 351 |
/**
|
|
| 352 |
* Returns the local home interface
|
|
| 353 |
*
|
|
| 354 |
* @return Returns the localHomeInterface.
|
|
| 355 |
*/
|
|
| 356 | 3 |
public String getLocalHomeInterface()
|
| 357 |
{
|
|
| 358 | 3 |
return localHomeInterface;
|
| 359 |
} |
|
| 360 |
|
|
| 361 |
/**
|
|
| 362 |
* Sets the local home interface
|
|
| 363 |
*
|
|
| 364 |
* @param theLocalHomeInterface The localHomeInterface to set.
|
|
| 365 |
*/
|
|
| 366 | 3 |
public void setLocalHomeInterface(String theLocalHomeInterface) |
| 367 |
{
|
|
| 368 | 3 |
this.localHomeInterface = theLocalHomeInterface;
|
| 369 |
} |
|
| 370 |
|
|
| 371 |
/**
|
|
| 372 |
* Return the local interface
|
|
| 373 |
*
|
|
| 374 |
* @return Returns the localInterface.
|
|
| 375 |
*/
|
|
| 376 | 3 |
public String getLocalInterface()
|
| 377 |
{
|
|
| 378 | 3 |
return localInterface;
|
| 379 |
} |
|
| 380 |
|
|
| 381 |
/**
|
|
| 382 |
* Sets the local interface
|
|
| 383 |
*
|
|
| 384 |
* @param theLocalInterface The localInterface to set.
|
|
| 385 |
*/
|
|
| 386 | 3 |
public void setLocalInterface(String theLocalInterface) |
| 387 |
{
|
|
| 388 | 3 |
this.localInterface = theLocalInterface;
|
| 389 |
} |
|
| 390 |
|
|
| 391 |
/**
|
|
| 392 |
* Returns the name
|
|
| 393 |
*
|
|
| 394 |
* @return Returns the name.
|
|
| 395 |
*/
|
|
| 396 | 3 |
public String getName()
|
| 397 |
{
|
|
| 398 | 3 |
return name;
|
| 399 |
} |
|
| 400 |
|
|
| 401 |
/**
|
|
| 402 |
* Sets the name
|
|
| 403 |
*
|
|
| 404 |
* @param theName The name to set.
|
|
| 405 |
*/
|
|
| 406 | 3 |
public void setName(String theName) |
| 407 |
{
|
|
| 408 | 3 |
this.name = theName;
|
| 409 |
} |
|
| 410 |
|
|
| 411 |
/**
|
|
| 412 |
* Returns the type
|
|
| 413 |
*
|
|
| 414 |
* @return Returns the type.
|
|
| 415 |
*/
|
|
| 416 | 4 |
public String getType()
|
| 417 |
{
|
|
| 418 | 4 |
return type;
|
| 419 |
} |
|
| 420 |
|
|
| 421 |
/**
|
|
| 422 |
* Sets the type
|
|
| 423 |
*
|
|
| 424 |
* @param theType The type to set.
|
|
| 425 |
*/
|
|
| 426 | 3 |
public void setType(String theType) |
| 427 |
{
|
|
| 428 | 3 |
this.type = theType;
|
| 429 |
} |
|
| 430 |
} |
|
| 431 |
// Instance Variables ------------------------------------------------------
|
|
| 432 |
|
|
| 433 |
/**
|
|
| 434 |
* The archive that contains the web-app that should be cactified.
|
|
| 435 |
*/
|
|
| 436 |
private File srcFile;
|
|
| 437 |
|
|
| 438 |
/**
|
|
| 439 |
* Location of the descriptor of which the content should be merged into
|
|
| 440 |
* the descriptor of the cactified archive.
|
|
| 441 |
*/
|
|
| 442 |
private File mergeWebXml;
|
|
| 443 |
|
|
| 444 |
/**
|
|
| 445 |
* The Cactus test redirectors.
|
|
| 446 |
*/
|
|
| 447 |
private List redirectors = new ArrayList(); |
|
| 448 |
|
|
| 449 |
/**
|
|
| 450 |
* For resolving entities such as DTDs.
|
|
| 451 |
*/
|
|
| 452 |
private XMLCatalog xmlCatalog = null; |
|
| 453 |
|
|
| 454 |
/**
|
|
| 455 |
* The web-app version to use when creating a WAR from scratch.
|
|
| 456 |
*/
|
|
| 457 |
private String version = null; |
|
| 458 |
|
|
| 459 |
/**
|
|
| 460 |
* List of ejb-refs to add to the deployment descriptor of the cactified war
|
|
| 461 |
*/
|
|
| 462 |
private List ejbRefs = new ArrayList(); |
|
| 463 |
|
|
| 464 |
// Public Methods ----------------------------------------------------------
|
|
| 465 |
|
|
| 466 |
/**
|
|
| 467 |
* @see org.apache.tools.ant.Task#execute()
|
|
| 468 |
*/
|
|
| 469 | 24 |
public void execute() throws BuildException |
| 470 |
{
|
|
| 471 | 24 |
WebXml webXml = null;
|
| 472 | 24 |
if (this.srcFile != null) |
| 473 |
{
|
|
| 474 | 19 |
log("Analyzing war: " + this.srcFile.getAbsolutePath(), |
| 475 |
Project.MSG_INFO); |
|
| 476 |
|
|
| 477 |
// Add everything that's in the source WAR to the destination WAR
|
|
| 478 | 19 |
ZipFileSet currentFiles = new ZipFileSet();
|
| 479 | 19 |
currentFiles.setSrc(this.srcFile);
|
| 480 | 19 |
currentFiles.createExclude().setName("WEB-INF/web.xml");
|
| 481 | 19 |
currentFiles.createExclude().setName("WEB-INF/weblogic.xml");
|
| 482 | 19 |
currentFiles.createExclude().setName("WEB-INF/orion-web.xml");
|
| 483 | 19 |
currentFiles.createExclude().setName("WEB-INF/ibm-web-bnd.xmi");
|
| 484 | 19 |
addZipfileset(currentFiles); |
| 485 |
|
|
| 486 |
// Parse the original deployment descriptor
|
|
| 487 | 19 |
webXml = getOriginalWebXml(); |
| 488 |
} |
|
| 489 | 24 |
if (this.srcFile == null || webXml == null) |
| 490 |
{
|
|
| 491 | 8 |
if (this.version == null) |
| 492 |
{
|
|
| 493 | 2 |
throw new BuildException("You need to specify either the " |
| 494 |
+ "[srcfile] or the [version] attribute");
|
|
| 495 |
} |
|
| 496 | 6 |
WebXmlVersion webXmlVersion = null;
|
| 497 | 6 |
if (this.version.equals("2.2")) |
| 498 |
{
|
|
| 499 | 2 |
webXmlVersion = WebXmlVersion.V2_2; |
| 500 |
} |
|
| 501 |
else
|
|
| 502 |
{
|
|
| 503 | 4 |
webXmlVersion = WebXmlVersion.V2_3; |
| 504 |
} |
|
| 505 | 6 |
try
|
| 506 |
{
|
|
| 507 | 6 |
webXml = WebXmlIo.newWebXml(webXmlVersion); |
| 508 |
} |
|
| 509 |
catch (ParserConfigurationException pce)
|
|
| 510 |
{
|
|
| 511 | 0 |
throw new BuildException( |
| 512 |
"Could not create deployment descriptor", pce);
|
|
| 513 |
} |
|
| 514 |
} |
|
| 515 |
|
|
| 516 | 22 |
File tmpWebXml = cactifyWebXml(webXml); |
| 517 | 22 |
setWebxml(tmpWebXml); |
| 518 |
|
|
| 519 | 22 |
addCactusJars(); |
| 520 |
|
|
| 521 | 22 |
try
|
| 522 |
{
|
|
| 523 | 22 |
super.execute();
|
| 524 |
} |
|
| 525 |
finally
|
|
| 526 |
{
|
|
| 527 |
// Even though the temporary descriptor will get deleted
|
|
| 528 |
// automatically when the VM exits, delete it explicitly here just
|
|
| 529 |
// to be a better citizen
|
|
| 530 | 22 |
tmpWebXml.delete(); |
| 531 |
} |
|
| 532 |
} |
|
| 533 |
|
|
| 534 |
/**
|
|
| 535 |
* Adds a Cactus filter test redirector.
|
|
| 536 |
*
|
|
| 537 |
* @param theFilterRedirector The redirector to add
|
|
| 538 |
*/
|
|
| 539 | 4 |
public final void addFilterRedirector(FilterRedirector theFilterRedirector) |
| 540 |
{
|
|
| 541 | 4 |
this.redirectors.add(theFilterRedirector);
|
| 542 |
} |
|
| 543 |
|
|
| 544 |
/**
|
|
| 545 |
* Adds a Cactus JSP test redirector.
|
|
| 546 |
*
|
|
| 547 |
* @param theJspRedirector The redirector to add
|
|
| 548 |
*/
|
|
| 549 | 3 |
public final void addJspRedirector(JspRedirector theJspRedirector) |
| 550 |
{
|
|
| 551 | 3 |
this.redirectors.add(theJspRedirector);
|
| 552 |
} |
|
| 553 |
|
|
| 554 |
/**
|
|
| 555 |
* Adds a Cactus servlet test redirector.
|
|
| 556 |
*
|
|
| 557 |
* @param theServletRedirector The redirector to add
|
|
| 558 |
*/
|
|
| 559 | 5 |
public final void addServletRedirector( |
| 560 |
ServletRedirector theServletRedirector) |
|
| 561 |
{
|
|
| 562 | 5 |
this.redirectors.add(theServletRedirector);
|
| 563 |
} |
|
| 564 |
|
|
| 565 |
/**
|
|
| 566 |
* Adds an XML catalog to the internal catalog.
|
|
| 567 |
*
|
|
| 568 |
* @param theXmlCatalog the XMLCatalog instance to use to look up DTDs
|
|
| 569 |
*/
|
|
| 570 | 0 |
public final void addConfiguredXMLCatalog(XMLCatalog theXmlCatalog) |
| 571 |
{
|
|
| 572 | 0 |
if (this.xmlCatalog == null) |
| 573 |
{
|
|
| 574 | 0 |
this.xmlCatalog = new XMLCatalog(); |
| 575 | 0 |
this.xmlCatalog.setProject(getProject());
|
| 576 |
} |
|
| 577 | 0 |
this.xmlCatalog.addConfiguredXMLCatalog(theXmlCatalog);
|
| 578 |
} |
|
| 579 |
|
|
| 580 |
/**
|
|
| 581 |
* Adds a configured EjbRef instance. Called by Ant.
|
|
| 582 |
*
|
|
| 583 |
* @param theEjbRef the EjbRef to add
|
|
| 584 |
*/
|
|
| 585 | 3 |
public final void addConfiguredEjbref(EjbRef theEjbRef) |
| 586 |
{
|
|
| 587 | 3 |
ejbRefs.add(theEjbRef); |
| 588 |
} |
|
| 589 |
|
|
| 590 |
/**
|
|
| 591 |
* The descriptor to merge into the original file.
|
|
| 592 |
*
|
|
| 593 |
* @param theMergeFile the <code>web.xml</code> to merge
|
|
| 594 |
*/
|
|
| 595 | 0 |
public final void setMergeWebXml(File theMergeFile) |
| 596 |
{
|
|
| 597 | 0 |
this.mergeWebXml = theMergeFile;
|
| 598 |
} |
|
| 599 |
|
|
| 600 |
/**
|
|
| 601 |
* Sets the web application archive that should be cactified.
|
|
| 602 |
*
|
|
| 603 |
* @param theSrcFile The WAR file to set
|
|
| 604 |
*/
|
|
| 605 | 19 |
public final void setSrcFile(File theSrcFile) |
| 606 |
{
|
|
| 607 | 19 |
this.srcFile = theSrcFile;
|
| 608 |
} |
|
| 609 |
|
|
| 610 |
/**
|
|
| 611 |
* Sets the web-app version to use when creating a WAR file from scratch.
|
|
| 612 |
*
|
|
| 613 |
* @param theVersion The version
|
|
| 614 |
*/
|
|
| 615 | 6 |
public final void setVersion(Version theVersion) |
| 616 |
{
|
|
| 617 | 6 |
this.version = theVersion.getValue();
|
| 618 |
} |
|
| 619 |
|
|
| 620 |
// Private Methods ---------------------------------------------------------
|
|
| 621 |
|
|
| 622 |
/**
|
|
| 623 |
* Adds the libraries required by Cactus on the server side.
|
|
| 624 |
*/
|
|
| 625 | 22 |
private void addCactusJars() |
| 626 |
{
|
|
| 627 | 22 |
addJarWithClass("org.aspectj.lang.JoinPoint", "AspectJ Runtime"); |
| 628 | 22 |
addJarWithClass("org.apache.cactus.ServletTestCase",
|
| 629 |
"Cactus Framework");
|
|
| 630 | 22 |
addJarWithClass("org.apache.commons.logging.Log",
|
| 631 |
"Commons-Logging");
|
|
| 632 | 22 |
addJarWithClass("org.apache.commons.httpclient.HttpClient",
|
| 633 |
"Commons-HttpClient");
|
|
| 634 | 22 |
addJarWithClass("junit.framework.TestCase", "JUnit"); |
| 635 |
} |
|
| 636 |
|
|
| 637 |
/**
|
|
| 638 |
* Adds the JAR file containing the specified resource to the WEB-INF/lib
|
|
| 639 |
* folder of a web-application archive.
|
|
| 640 |
*
|
|
| 641 |
* @param theClassName The name of the class that the JAR contains
|
|
| 642 |
* @param theDescription A description of the JAR that should be displayed
|
|
| 643 |
* to the user in log messages
|
|
| 644 |
*/
|
|
| 645 | 110 |
private void addJarWithClass(String theClassName, String theDescription) |
| 646 |
{
|
|
| 647 | 110 |
String resourceName = "/" + theClassName.replace('.', '/') + ".class"; |
| 648 | 110 |
if (this.srcFile != null) |
| 649 |
{
|
|
| 650 | 90 |
try
|
| 651 |
{
|
|
| 652 | 90 |
WarArchive srcWar = new DefaultWarArchive(srcFile);
|
| 653 | 90 |
if (srcWar.containsClass(theClassName))
|
| 654 |
{
|
|
| 655 | 5 |
log("The " + theDescription + " JAR is already present in " |
| 656 |
+ "the WAR", Project.MSG_VERBOSE);
|
|
| 657 | 5 |
return;
|
| 658 |
} |
|
| 659 |
} |
|
| 660 |
catch (IOException ioe)
|
|
| 661 |
{
|
|
| 662 | 0 |
log("Problem reading source WAR to when trying to detect "
|
| 663 |
+ "already present JAR files (" + ioe + ")", |
|
| 664 |
Project.MSG_WARN); |
|
| 665 |
} |
|
| 666 |
} |
|
| 667 | 105 |
ZipFileSet jar = new ZipFileSet();
|
| 668 | 105 |
File file = ResourceUtils.getResourceLocation(resourceName); |
| 669 | 105 |
if (file != null) |
| 670 |
{
|
|
| 671 | 42 |
jar.setFile(file); |
| 672 | 42 |
addLib(jar); |
| 673 |
} |
|
| 674 |
else
|
|
| 675 |
{
|
|
| 676 | 63 |
log("Could not find the " + theDescription + " JAR", |
| 677 |
Project.MSG_WARN); |
|
| 678 | 63 |
log("You need to add the JAR to the classpath of the task",
|
| 679 |
Project.MSG_INFO); |
|
| 680 | 63 |
log("(Searched for class " + theClassName + ")", Project.MSG_DEBUG); |
| 681 |
} |
|
| 682 |
} |
|
| 683 |
|
|
| 684 |
/**
|
|
| 685 |
* Adds the Cactus JSP redirector file to the web application.
|
|
| 686 |
*/
|
|
| 687 | 22 |
private void addJspRedirector() |
| 688 |
{
|
|
| 689 |
// Now copy the actual JSP redirector file into the web application
|
|
| 690 | 22 |
File jspRedirectorFile = new File(
|
| 691 |
new File(System.getProperty("java.io.tmpdir")), |
|
| 692 |
"jspRedirector.jsp");
|
|
| 693 | 22 |
jspRedirectorFile.deleteOnExit(); |
| 694 | 22 |
try
|
| 695 |
{
|
|
| 696 | 22 |
ResourceUtils.copyResource(getProject(), |
| 697 |
"/org/apache/cactus/server/jspRedirector.jsp",
|
|
| 698 |
jspRedirectorFile); |
|
| 699 |
} |
|
| 700 |
catch (IOException e)
|
|
| 701 |
{
|
|
| 702 | 22 |
log("Could not copy the JSP redirector (" + e.getMessage() + ")", |
| 703 |
Project.MSG_WARN); |
|
| 704 |
} |
|
| 705 | 22 |
FileSet fileSet = new FileSet();
|
| 706 | 22 |
fileSet.setFile(jspRedirectorFile); |
| 707 | 22 |
addFileset(fileSet); |
| 708 |
} |
|
| 709 |
|
|
| 710 |
/**
|
|
| 711 |
* Adds the definitions corresponding to the nested redirector elements to
|
|
| 712 |
* the provided deployment descriptor.
|
|
| 713 |
*
|
|
| 714 |
* @param theWebXml The deployment descriptor
|
|
| 715 |
*/
|
|
| 716 | 22 |
private void addRedirectorDefinitions(WebXml theWebXml) |
| 717 |
{
|
|
| 718 | 22 |
boolean filterRedirectorDefined = false; |
| 719 | 22 |
boolean jspRedirectorDefined = false; |
| 720 | 22 |
boolean servletRedirectorDefined = false; |
| 721 |
|
|
| 722 |
// add the user defined redirectors
|
|
| 723 | 22 |
for (Iterator i = this.redirectors.iterator(); i.hasNext();) |
| 724 |
{
|
|
| 725 | 12 |
Redirector redirector = (Redirector) i.next(); |
| 726 | 12 |
if (redirector instanceof FilterRedirector) |
| 727 |
{
|
|
| 728 | 4 |
filterRedirectorDefined = true;
|
| 729 |
} |
|
| 730 | 8 |
else if (redirector instanceof JspRedirector) |
| 731 |
{
|
|
| 732 | 3 |
jspRedirectorDefined = true;
|
| 733 |
} |
|
| 734 | 5 |
else if (redirector instanceof ServletRedirector) |
| 735 |
{
|
|
| 736 | 5 |
servletRedirectorDefined = true;
|
| 737 |
} |
|
| 738 | 12 |
redirector.mergeInto(theWebXml); |
| 739 |
} |
|
| 740 |
|
|
| 741 |
// now add the default redirectors if they haven't been provided by
|
|
| 742 |
// the user
|
|
| 743 | 22 |
if (!filterRedirectorDefined)
|
| 744 |
{
|
|
| 745 | 19 |
new FilterRedirector().mergeInto(theWebXml);
|
| 746 |
} |
|
| 747 | 22 |
if (!servletRedirectorDefined)
|
| 748 |
{
|
|
| 749 | 18 |
new ServletRedirector().mergeInto(theWebXml);
|
| 750 |
} |
|
| 751 | 22 |
if (!jspRedirectorDefined)
|
| 752 |
{
|
|
| 753 | 20 |
new JspRedirector().mergeInto(theWebXml);
|
| 754 |
} |
|
| 755 |
} |
|
| 756 |
|
|
| 757 |
/**
|
|
| 758 |
* Enhances the provided web deployment descriptor with the definitions
|
|
| 759 |
* required for testing with Cactus.
|
|
| 760 |
*
|
|
| 761 |
* @param theWebXml The original deployment descriptor
|
|
| 762 |
* @return A temporary file containing the cactified descriptor
|
|
| 763 |
*/
|
|
| 764 | 22 |
private File cactifyWebXml(WebXml theWebXml)
|
| 765 |
{
|
|
| 766 | 22 |
addRedirectorDefinitions(theWebXml); |
| 767 | 22 |
addJspRedirector(); |
| 768 | 22 |
addEjbRefs(theWebXml); |
| 769 |
|
|
| 770 |
// If the user has specified a deployment descriptor to merge into the
|
|
| 771 |
// cactified descriptor, perform the merge
|
|
| 772 | 22 |
if (this.mergeWebXml != null) |
| 773 |
{
|
|
| 774 | 0 |
try
|
| 775 |
{
|
|
| 776 | 0 |
WebXml parsedMergeWebXml = WebXmlIo.parseWebXmlFromFile( |
| 777 |
this.mergeWebXml, this.xmlCatalog); |
|
| 778 | 0 |
WebXmlMerger merger = new WebXmlMerger(theWebXml);
|
| 779 | 0 |
merger.setMonitor(new AntMonitor(this)); |
| 780 | 0 |
merger.merge(parsedMergeWebXml); |
| 781 |
} |
|
| 782 |
catch (IOException e)
|
|
| 783 |
{
|
|
| 784 | 0 |
throw new BuildException( |
| 785 |
"Could not merge deployment descriptors", e);
|
|
| 786 |
} |
|
| 787 |
catch (SAXException e)
|
|
| 788 |
{
|
|
| 789 | 0 |
throw new BuildException("Parsing of merge file failed", e); |
| 790 |
} |
|
| 791 |
catch (ParserConfigurationException e)
|
|
| 792 |
{
|
|
| 793 | 0 |
throw new BuildException("XML parser configuration error", e); |
| 794 |
} |
|
| 795 |
} |
|
| 796 |
|
|
| 797 |
// Serialize the cactified deployment descriptor into a temporary file,
|
|
| 798 |
// so that it can get picked up by the War task
|
|
| 799 | 22 |
FileUtils fileUtils = FileUtils.newFileUtils(); |
| 800 | 22 |
File tmpDir = fileUtils.createTempFile("cactus", "tmp.dir", |
| 801 |
getProject().getBaseDir()); |
|
| 802 | 22 |
tmpDir.mkdirs(); |
| 803 | 22 |
tmpDir.deleteOnExit(); |
| 804 | 22 |
File webXmlFile = null;
|
| 805 | 22 |
try
|
| 806 |
{
|
|
| 807 | 22 |
ZipFileSet fileSet = new ZipFileSet();
|
| 808 | 22 |
fileSet.setDir(tmpDir); |
| 809 | 22 |
File[] files = WebXmlIo.writeAll(theWebXml, tmpDir); |
| 810 | 22 |
for (int i = 0; i < files.length; i++) |
| 811 |
{
|
|
| 812 | 24 |
File f = files[i]; |
| 813 | 24 |
f.deleteOnExit(); |
| 814 | 24 |
if (f.getName().equals("web.xml")) |
| 815 |
{
|
|
| 816 | 22 |
webXmlFile = f; |
| 817 |
} |
|
| 818 |
else
|
|
| 819 |
{
|
|
| 820 | 2 |
fileSet.createInclude().setName(f.getName()); |
| 821 |
} |
|
| 822 |
} |
|
| 823 | 22 |
addWebinf(fileSet); |
| 824 |
} |
|
| 825 |
catch (IOException ioe)
|
|
| 826 |
{
|
|
| 827 | 0 |
throw new BuildException( |
| 828 |
"Could not write temporary deployment descriptor", ioe);
|
|
| 829 |
} |
|
| 830 | 22 |
return webXmlFile;
|
| 831 |
} |
|
| 832 |
|
|
| 833 |
/**
|
|
| 834 |
* Extracts and parses the original web deployment descriptor from the
|
|
| 835 |
* web-app.
|
|
| 836 |
*
|
|
| 837 |
* @return The parsed descriptor or null if not found
|
|
| 838 |
* @throws BuildException If the descriptor could not be
|
|
| 839 |
* parsed
|
|
| 840 |
*/
|
|
| 841 | 19 |
private WebXml getOriginalWebXml() throws BuildException |
| 842 |
{
|
|
| 843 |
// Open the archive as JAR file and extract the deployment descriptor
|
|
| 844 | 19 |
WarArchive war = null;
|
| 845 | 19 |
try
|
| 846 |
{
|
|
| 847 | 19 |
war = new DefaultWarArchive(this.srcFile); |
| 848 | 19 |
WebXml webXml = war.getWebXml(); |
| 849 | 19 |
return webXml;
|
| 850 |
} |
|
| 851 |
catch (SAXException e)
|
|
| 852 |
{
|
|
| 853 | 0 |
throw new BuildException( |
| 854 |
"Parsing of web.xml deployment descriptor failed", e);
|
|
| 855 |
} |
|
| 856 |
catch (IOException e)
|
|
| 857 |
{
|
|
| 858 | 0 |
throw new BuildException("Failed to open WAR", e); |
| 859 |
} |
|
| 860 |
catch (ParserConfigurationException e)
|
|
| 861 |
{
|
|
| 862 | 0 |
throw new BuildException("XML parser configuration error", e); |
| 863 |
} |
|
| 864 |
} |
|
| 865 |
|
|
| 866 |
/**
|
|
| 867 |
* Add ejb references to a web.xml.
|
|
| 868 |
*
|
|
| 869 |
* @param theWebXml the web.xml to modify
|
|
| 870 |
*/
|
|
| 871 | 22 |
private void addEjbRefs(WebXml theWebXml) |
| 872 |
{
|
|
| 873 | 22 |
Iterator i = ejbRefs.iterator(); |
| 874 | 22 |
while (i.hasNext())
|
| 875 |
{
|
|
| 876 | 3 |
EjbRef ref = (EjbRef) i.next(); |
| 877 | 3 |
if ("Session".equals(ref.getType())) |
| 878 |
{
|
|
| 879 | 2 |
theWebXml.addLocalSessionEjbRef(ref.getName(), |
| 880 |
ref.getLocalInterface(), |
|
| 881 |
ref.getLocalHomeInterface(), |
|
| 882 |
ref.getJndiName()); |
|
| 883 |
} |
|
| 884 | 1 |
else if ("Entity".equals(ref.getType())) |
| 885 |
{
|
|
| 886 | 1 |
theWebXml.addLocalEntityEjbRef(ref.getName(), |
| 887 |
ref.getLocalInterface(), |
|
| 888 |
ref.getLocalHomeInterface(), |
|
| 889 |
ref.getJndiName()); |
|
| 890 |
} |
|
| 891 |
} |
|
| 892 |
} |
|
| 893 |
} |
|
| 894 |
|
|
||||||||||