1 23 24 77 78 package org.apache.tools.ant.taskdefs.optional.sun.appserv; 79 80 import org.apache.tools.ant.Task; 81 import org.apache.tools.ant.AntClassLoader; 82 import org.apache.tools.ant.types.Path; 83 import org.apache.tools.ant.Project; 84 import org.apache.tools.ant.Target; 85 import org.apache.tools.ant.BuildException; 86 import org.apache.tools.ant.DirectoryScanner; 87 import org.apache.tools.ant.types.FileSet; 88 import org.apache.tools.ant.types.ZipFileSet; 89 import org.apache.tools.ant.taskdefs.Jar; 90 91 import java.io.*; 92 import java.util.*; 93 import java.lang.reflect.Field ; 94 import java.net.URL ; 95 96 121 public class UpdateTask extends Task { 122 123 private static final boolean debug = false; 124 125 private String file; 126 private String sunonehome = null; 127 private String domain = "domain1"; 128 129 private ArrayList fromFiles; 130 private ArrayList toFiles;; 131 132 LocalStringsManager lsm = new LocalStringsManager(); 133 134 137 public void setFile(String file) { 138 this.file = new File(file).getPath(); 140 } 141 142 145 public void setDomain(String domain) { 146 this.domain = domain; 147 } 148 149 152 public void execute() throws BuildException { 153 154 if ( file == null || file.equals("") ) { 155 throw new BuildException(lsm.getString("AttributeFileNoProvided")); 156 } 157 158 try { 159 if ( sunonehome == null ) { 160 ClassLoader cl = this.getClass().getClassLoader(); 162 URL url = cl.getResource( 163 "org/apache/tools/ant/taskdefs/defaults.properties"); 164 String path = new File(url.getFile()).getPath(); 165 167 if ( path.indexOf("file:") != -1 ) { 169 path = path.substring(5); 170 } 171 172 int jarIndex = path.indexOf("sun-appserv-ant.jar"); 174 sunonehome = path.substring(0, jarIndex - 5); 175 } 176 177 System.out.println(lsm.getString("UpdateMessage", new Object [] {file, sunonehome, domain})); 178 179 Jar[] creatorTasks = findCreatorTasks(file); 181 if ( creatorTasks == null ) { 182 throw new BuildException(lsm.getString("UnableToCreateArchive", new Object [] {file})); 183 } 184 if ( debug ) { 185 System.out.println(lsm.getString("TaskThatCreatedArchive", new Object [] {file})); 186 } 187 188 fromFiles = new ArrayList(); 190 toFiles = new ArrayList(); 191 192 String sep = File.separator; 194 String appsDirName = sunonehome + sep + "domains" + sep + 195 domain + sep + "applications"; 196 String appname = getAppNameFromFile(file); 197 String deployedDir; 198 if ( file.endsWith(".ear") ) { 199 String j2eeAppsDir = appsDirName + sep + "j2ee-apps"; 202 deployedDir = j2eeAppsDir + File.separator + appname; 203 204 String [] moduleFiles = getModuleFiles(creatorTasks, file); 206 207 for ( int i=0; i<moduleFiles.length; i++ ) { 209 String modFile = moduleFiles[i]; 210 211 Jar[] modCreatorTasks = findCreatorTasks(modFile); 213 if ( modCreatorTasks == null ) { 214 System.err.println(lsm.getString("UnableToFindTask", new Object [] {modFile})); 215 continue; 216 } 217 218 String modName = getAppNameFromFile(modFile); 220 String modDeployedDir; 221 if ( modFile.endsWith(".war") ) { 222 modDeployedDir = deployedDir + sep + modName + "_war"; 223 } 224 else if ( modFile.endsWith(".jar") ) { 225 modDeployedDir = deployedDir + sep + modName + "_jar"; 226 } 227 else if ( modFile.endsWith(".rar") ) { 228 modDeployedDir = deployedDir + sep + modName + "_rar"; 229 } 230 else if ( modFile.equals(file) ) { 231 modDeployedDir = deployedDir; 232 } 233 else { 234 System.err.println(lsm.getString("InvalidModule", new Object [] {modFile})); 235 continue; 236 } 237 238 updateModule(modCreatorTasks, modDeployedDir); 239 } 240 } 241 else { 242 String modulesDir = appsDirName + sep + "j2ee-modules"; 247 deployedDir = modulesDir + sep + appname; 248 249 updateModule(creatorTasks, deployedDir); 250 } 251 252 if ( fromFiles.size() > 0 ) { 254 for ( int i=0; i<fromFiles.size(); i++ ) { 255 copyFile((File)fromFiles.get(i), (File)toFiles.get(i)); 256 } 257 258 File reload = new File(deployedDir, ".reload"); 260 if ( !reload.createNewFile() ) { 261 reload.setLastModified(System.currentTimeMillis()); 262 } 263 264 System.out.println(lsm.getString("AplicationUpdated")); 265 } 266 else { 267 System.out.println(lsm.getString("FilesUpdateToDate")); 268 } 269 270 } catch ( Exception ex ) { 271 System.err.println(lsm.getString("UpdateError")); 272 if ( debug ) 273 ex.printStackTrace(); 274 throw new BuildException(ex); 275 } 276 } 277 278 279 282 private String [] getModuleFiles(Jar[] earTasks, String earFile) 283 throws BuildException { 284 ArrayList filesets = getFilesets(earTasks); 286 287 ArrayList modules = new ArrayList(); 289 for ( int i=0; i<filesets.size(); i++ ) { 290 FileSet fs = (FileSet)filesets.get(i); 291 292 if ( !fs.getDir(getProject()).exists() ) 294 continue; 295 296 DirectoryScanner ds = fs.getDirectoryScanner(getProject()); 298 File fromDir = fs.getDir(getProject()); 299 300 String [] files = ds.getIncludedFiles(); 301 for ( int j=0; j<files.length; j++ ) { 302 String f = files[j]; 303 if ( f.endsWith(".jar") || f.endsWith(".war") 304 || f.endsWith(".rar") ) { 305 modules.add(new File(fromDir, f).toString()); 306 } 307 } 308 } 309 modules.add(earFile); 310 311 return (String [])modules.toArray(new String [modules.size()]); 312 } 313 314 315 318 private void updateModule(Jar[] creatorTasks, String deployedDir) 319 throws BuildException { 320 321 try { 322 File deployedDirFile = new File(deployedDir); 323 if ( !deployedDirFile.exists() ) { 324 System.err.println(lsm.getString("ModuleDoesNotExist", new Object [] {file})); 325 return; 326 } 327 328 ArrayList filesets = getFilesets(creatorTasks); 330 331 for ( int i=0; i<filesets.size(); i++ ) { 333 FileSet fs = (FileSet)filesets.get(i); 334 335 if ( !fs.getDir(getProject()).exists() ) 337 continue; 338 339 DirectoryScanner ds = fs.getDirectoryScanner(getProject()); 341 File fromDir = fs.getDir(getProject()); 342 String [] srcFiles = ds.getIncludedFiles(); 343 344 String prefix = ""; 345 String fullpath = ""; 346 if ( fs instanceof ZipFileSet ) { 347 ZipFileSet zfs = (ZipFileSet)fs; 348 String tmpPrefix = zfs.getPrefix(getProject()); 351 String tmpFullpath = zfs.getFullpath(getProject()); 352 if ( tmpPrefix != null && !tmpPrefix.equals("") ) { 353 prefix = tmpPrefix; 354 } 355 else if ( tmpFullpath != null && !tmpFullpath.equals("") ) { 356 fullpath = tmpFullpath; 359 } 360 361 } 364 365 if ( file.endsWith(".ear") ) { 367 if ( fullpath.equals("META-INF/application.xml") 368 || prefix.startsWith("META-INF") ) { 369 compareAndCopy(fromDir, srcFiles, 370 deployedDirFile, prefix, fullpath); 371 } 372 } 373 else { 374 compareAndCopy(fromDir, srcFiles, 375 deployedDirFile, prefix, fullpath); 376 } 377 } 378 379 } catch ( Exception ex ) { 380 System.err.println(lsm.getString("UpdateError")); 381 if ( debug ) 382 ex.printStackTrace(); 383 throw new BuildException(ex); 384 } 385 } 386 387 388 391 private String getAppNameFromFile(String file) { 392 String appname = file.substring(0, file.length()-4); if ( appname.lastIndexOf(File.separator) != -1 ) { 394 appname = appname.substring(appname.lastIndexOf(File.separator)+1); 395 } 396 return appname; 397 } 398 399 400 405 private Jar[] findCreatorTasks(String file) throws IOException { 406 407 if ( debug ) { 408 System.err.println("In findCreatorTasks for file " + file 409 + " canonical path is " + new File(file).getCanonicalPath()); 410 } 411 412 Hashtable targets = project.getTargets(); 413 Enumeration e = targets.elements(); 414 ArrayList creators = new ArrayList(); 415 while ( e.hasMoreElements() ) { 416 Target t = (Target)e.nextElement(); 417 Task[] tasks = t.getTasks(); 418 419 for ( int i=0; i<tasks.length; i++ ) { 422 if ( tasks[i] instanceof org.apache.tools.ant.UnknownElement ) { 423 try { 424 tasks[i].maybeConfigure(); 425 } catch ( Exception ex ) { 426 } 428 } 429 } 430 431 tasks = t.getTasks(); 432 for ( int i=0; i<tasks.length; i++ ) { 433 if ( tasks[i] instanceof org.apache.tools.ant.taskdefs.Jar ) { 435 436 Jar task = (Jar)tasks[i]; 437 438 try { 440 task.maybeConfigure(); 441 } catch ( Exception ex ) { 442 } 444 445 450 if ( task.getDestFile() != null 452 && task.getDestFile().getCanonicalPath().equals( 453 new File(file).getCanonicalPath()) ) { 454 creators.add(task); 455 } 456 } 457 } 458 } 459 460 if ( creators.size() == 0 ) { 461 return null; 462 } 463 else { 464 return (Jar[])creators.toArray(new Jar[creators.size()]); 465 } 466 } 467 468 469 473 private ArrayList getFilesets(Jar[] tasks) throws BuildException { 474 475 try { 476 481 Class zipClass = Class.forName("org.apache.tools.ant.taskdefs.Zip"); 482 Field filesetsField = zipClass.getDeclaredField("filesets"); 483 filesetsField.setAccessible(true); 484 Field basedirField = zipClass.getDeclaredField("baseDir"); 485 basedirField.setAccessible(true); 486 487 Class mtClass = Class.forName( 488 "org.apache.tools.ant.taskdefs.MatchingTask"); 489 Field filesetField = mtClass.getDeclaredField("fileset"); 490 filesetField.setAccessible(true); 491 492 ArrayList filesets = new ArrayList(); 493 494 for ( int i=0; i<tasks.length; i++ ) { 495 496 Vector fs = (Vector)filesetsField.get(tasks[i]); 498 filesets.addAll(fs); 499 500 FileSet implFileset = (FileSet)filesetField.get(tasks[i]); 502 File baseDir = (File)basedirField.get(tasks[i]); 503 if ( implFileset != null && baseDir != null ) { 504 FileSet fileset = (FileSet)implFileset.clone(); 505 fileset.setDir(baseDir); 506 filesets.add(fileset); 507 } 508 } 509 510 return filesets; 511 512 } catch ( Exception ex ) { 513 throw (BuildException)(new BuildException().initCause(ex)); 514 } 515 } 516 517 518 522 private void compareAndCopy(File fromDir, String [] srcFiles, File toDir, 523 String prefix, String fullpath) 524 { 525 if ( prefix != null && !prefix.equals("") ) { 526 toDir = new File(toDir, prefix); 527 } 528 529 for ( int i=0; i<srcFiles.length; i++ ) { 530 String srcFile = srcFiles[i]; 531 532 if ( debug ) { 533 System.out.println("In compareAndCopy, fromDir = " + fromDir 534 + " srcFile = " + srcFile + " toDir = " + toDir 535 + " prefix = " + prefix + " fullpath = " + fullpath); 536 } 537 538 File from = new File(fromDir, srcFile); 539 File to; 540 if ( fullpath != null && !fullpath.equals("") ) { 541 to = new File(toDir, fullpath); 542 } 543 else { 544 to = new File(toDir, srcFile); 545 } 546 if ( from.lastModified() > to.lastModified() ) { 547 fromFiles.add(from); 548 toFiles.add(to); 549 } 550 } 551 } 552 553 private void copyFile(File from, File to) throws BuildException { 554 555 System.out.println("Copying file " + from + " to " + to); 556 557 FileInputStream in = null; 558 FileOutputStream out = null; 559 try { 560 if ( !to.exists() ) { 561 if ( !to.getParentFile().exists() ) { 562 to.getParentFile().mkdirs(); 564 } 565 to.createNewFile(); 566 } 567 568 in = new FileInputStream(from); 569 out = new FileOutputStream(to); 570 571 byte[] buffer = new byte[8 * 1024]; 572 int count = 0; 573 while ( (count = in.read(buffer, 0, buffer.length)) != -1 ) { 574 out.write(buffer, 0, count); 575 } 576 577 } catch ( Exception ex ) { 578 throw new RuntimeException (lsm.getString("UnableToCopy", new Object [] {from}), ex); 579 } finally { 580 try { 581 if (out != null) { 582 out.close(); 583 } 584 if (in != null) { 585 in.close(); 586 } 587 } catch ( Exception ex ) {} 588 } 589 } 590 } 591 | Popular Tags |