1 20 package org.apache.cactus.integration.ant.container; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 25 import org.apache.cactus.integration.ant.deployment.DeployableFile; 26 import org.apache.cactus.integration.ant.util.AntLog; 27 import org.apache.cactus.integration.ant.util.AntTaskFactory; 28 import org.apache.commons.logging.Log; 29 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.ProjectComponent; 31 import org.apache.tools.ant.Task; 32 import org.apache.tools.ant.filters.ReplaceTokens; 33 import org.apache.tools.ant.taskdefs.Delete; 34 import org.apache.tools.ant.types.FileSet; 35 import org.apache.tools.ant.types.FilterChain; 36 import org.apache.tools.ant.types.Path; 37 import org.apache.tools.ant.types.PatternSet; 38 import org.apache.tools.ant.types.Environment.Variable; 39 import org.apache.tools.ant.types.selectors.SelectorUtils; 40 41 47 public abstract class AbstractContainer extends ProjectComponent 48 implements Container 49 { 50 52 55 protected static final String RESOURCE_PATH = 56 "/org/apache/cactus/integration/ant/container/resources/"; 57 58 60 63 private DeployableFile deployableFile; 64 65 69 private PatternSet patternSet = new PatternSet(); 70 71 74 private File toDir; 75 76 80 private String ifCondition; 81 82 86 private String unlessCondition; 87 88 91 private AntTaskFactory antTaskFactory; 92 93 96 private Log log = AntLog.NULL; 97 98 101 private Variable[] systemProperties; 102 103 106 private long startUpWait = 1000; 107 108 112 private Path containerClasspath; 113 114 116 119 public String getTestContext() 120 { 121 return null; 122 } 123 124 137 public void setStartUpWait(long theStartUpWait) 138 { 139 this.startUpWait = theStartUpWait; 140 } 141 142 147 public final PatternSet.NameEntry createExclude() 148 { 149 return this.patternSet.createExclude(); 150 } 151 152 157 public final String [] getExcludePatterns() 158 { 159 return this.patternSet.getExcludePatterns(getProject()); 160 } 161 162 168 public final void setIf(String theIfCondition) 169 { 170 this.ifCondition = theIfCondition; 171 } 172 173 178 public final void setToDir(File theToDir) 179 { 180 this.toDir = theToDir; 181 } 182 183 189 public final void setUnless(String theUnlessCondition) 190 { 191 this.unlessCondition = theUnlessCondition; 192 } 193 194 196 199 public long getStartUpWait() 200 { 201 return this.startUpWait; 202 } 203 204 207 public final File getToDir() 208 { 209 return this.toDir; 210 } 211 212 217 public void init() 218 { 219 } 221 222 225 public final boolean isEnabled() 226 { 227 return (testIfCondition() && testUnlessCondition()); 228 } 229 230 233 public final boolean isExcluded(String theTestName) 234 { 235 String [] excludePatterns = 236 this.patternSet.getExcludePatterns(getProject()); 237 if (excludePatterns != null) 238 { 239 String testPath = theTestName.replace('.', '/'); 240 for (int i = 0; i < excludePatterns.length; i++) 241 { 242 String excludePattern = excludePatterns[i]; 243 if (excludePattern.endsWith(".java") 244 || excludePattern.endsWith(".class")) 245 { 246 excludePattern = excludePattern.substring( 247 0, excludePattern.lastIndexOf('.')); 248 } 249 if (SelectorUtils.matchPath(excludePattern, testPath)) 250 { 251 return true; 252 } 253 } 254 } 255 return false; 256 } 257 258 261 public final void setAntTaskFactory(AntTaskFactory theFactory) 262 { 263 this.antTaskFactory = theFactory; 264 } 265 266 269 public final void setDeployableFile(DeployableFile theDeployableFile) 270 { 271 this.deployableFile = theDeployableFile; 272 } 273 274 277 public final void setLog(Log theLog) 278 { 279 this.log = theLog; 280 } 281 282 285 public void setSystemProperties(Variable[] theProperties) 286 { 287 this.systemProperties = theProperties; 288 } 289 290 293 public Variable[] getSystemProperties() 294 { 295 return this.systemProperties; 296 } 297 298 302 public void setContainerClasspath(Path theClasspath) 303 { 304 this.containerClasspath = theClasspath; 305 } 306 307 311 public Path getContainerClasspath() 312 { 313 return this.containerClasspath; 314 } 315 316 318 327 protected final Task createAntTask(String theName) 328 { 329 return this.antTaskFactory.createTask(theName); 330 } 331 332 341 protected final File createDirectory(File theParentDir, String theName) 342 throws IOException 343 { 344 File dir = new File (theParentDir, theName); 345 dir.mkdirs(); 346 if (!dir.isDirectory()) 347 { 348 throw new IOException ( 349 "Couldn't create directory " + dir.getAbsolutePath()); 350 } 351 return dir; 352 } 353 354 364 protected final FilterChain createFilterChain() 365 { 366 ReplaceTokens.Token token = null; 367 FilterChain filterChain = new FilterChain(); 368 369 ReplaceTokens replacePort = new ReplaceTokens(); 371 token = new ReplaceTokens.Token(); 372 token.setKey("cactus.port"); 373 token.setValue(String.valueOf(getPort())); 374 replacePort.addConfiguredToken(token); 375 filterChain.addReplaceTokens(replacePort); 376 377 if (getDeployableFile() != null) 379 { 380 ReplaceTokens replaceContext = new ReplaceTokens(); 381 token = new ReplaceTokens.Token(); 382 token.setKey("cactus.context"); 383 token.setValue(getDeployableFile().getTestContext()); 384 replaceContext.addConfiguredToken(token); 385 filterChain.addReplaceTokens(replaceContext); 386 } 387 388 return filterChain; 389 } 390 391 396 protected void cleanTempDirectory(File theTmpDir) 397 { 398 Delete delete = (Delete) createAntTask("delete"); 400 FileSet fileSet = new FileSet(); 401 fileSet.setDir(theTmpDir); 402 fileSet.createInclude().setName("**/*"); 403 delete.addFileset(fileSet); 404 delete.setIncludeEmptyDirs(true); 405 delete.setFailOnError(false); 406 delete.execute(); 407 } 408 409 419 protected File setupTempDirectory(File theCustomTmpDir, String theName) 420 { 421 File tmpDir; 422 423 if (theCustomTmpDir == null) 424 { 425 tmpDir = new File (System.getProperty("java.io.tmpdir"), theName); 426 } 427 else 428 { 429 tmpDir = theCustomTmpDir; 430 } 431 432 if (!tmpDir.exists()) 433 { 434 if (!tmpDir.mkdirs()) 435 { 436 throw new BuildException("Could not create temporary " 437 + "directory [" + tmpDir + "]"); 438 } 439 } 440 441 if (!tmpDir.isDirectory()) 443 { 444 throw new BuildException("[" + tmpDir + "] is not a directory"); 445 } 446 447 return tmpDir; 448 } 449 450 455 protected final Log getLog() 456 { 457 return this.log; 458 } 459 460 466 protected final DeployableFile getDeployableFile() 467 { 468 return this.deployableFile; 469 } 470 471 473 480 private boolean testIfCondition() 481 { 482 if (ifCondition == null || ifCondition.length() == 0) 483 { 484 return true; 485 } 486 487 return (getProject().getProperty(ifCondition) != null); 488 } 489 490 497 private boolean testUnlessCondition() 498 { 499 if (unlessCondition == null || unlessCondition.length() == 0) 500 { 501 return true; 502 } 503 return (getProject().getProperty(unlessCondition) == null); 504 } 505 506 } 507 | Popular Tags |