1 23 package com.sun.enterprise.util; 24 25 import java.io.*; 26 import java.io.IOException ; 27 import java.io.FilenameFilter ; 28 import java.util.*; 29 import java.net.URL ; 30 import java.net.MalformedURLException ; 31 import java.nio.channels.*; 32 import java.util.jar.*; 33 import java.util.zip.ZipEntry ; 34 import com.sun.logging.LogDomains; 35 36 import java.util.logging.*; 38 import com.sun.logging.*; 39 41 44 public class FileUtil { 45 46 static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 48 50 private static final boolean debug = com.sun.enterprise.util.logging.Debug.enabled; 53 private static final String JAR_FILE_NAME = "j2ee.jar"; 55 private static final String HOME_DIR_PROP="com.sun.enterprise.home"; 56 private static final String DEFAULT_HOME_DIR=System.getProperty("user.dir"); 57 58 public static final char JAR_SEPARATOR_CHAR = '/'; 60 61 private static final long JAR_ENTRY_UNKNOWN_VALUE = -1; 62 private static final int BYTE_READ_ERROR = -1; 63 64 private static String basedir = null; 65 66 68 69 public static File getTempDirectory() { 70 String temp = System.getProperty("java.io.tmpdir"); 71 String home = System.getProperty("user.name"); 72 if (home == null) { 73 home = ""; } 75 File tmp = null; 76 if (temp == null) { 77 tmp = new File(home, "tmp"); 78 } else { 79 tmp = new File(temp, "j2ee-ri-" + home); 80 } 81 if (!tmp.exists()) { 82 tmp.mkdirs(); 83 } 84 return tmp; 85 } 86 87 98 99 public static String getAbsolutePath(String relativePath) 100 { 101 if(isAbsolute(relativePath)) 102 return relativePath; 103 104 String rpath = relativePath.replace('/', File.separatorChar); 105 if (basedir == null) setBaseDir(); 106 String path = basedir + File.separator + relativePath; 107 108 return new File(path).getAbsolutePath(); 109 } 110 111 private static void setBaseDir() { 112 basedir = System.getProperty(HOME_DIR_PROP); 114 if (basedir != null) { 115 return; 116 } else { 117 basedir = DEFAULT_HOME_DIR; 118 } 119 String classPath = System.getProperty("java.class.path"); 121 if (classPath == null) { 122 return; 123 } else { 124 StringTokenizer st = 125 new StringTokenizer(classPath, File.pathSeparator); 126 while (st.hasMoreTokens()) { 127 String filename = st.nextToken(); 128 if (filename.endsWith(JAR_FILE_NAME)) { 129 try { 130 String parent = 132 (new File(filename)).getAbsoluteFile(). 133 getParentFile().getParent(); 134 if (parent != null) { 135 basedir = parent; 136 } 137 return; 138 } catch (NullPointerException ex) { 139 return; 141 } 142 } 143 } 144 } 145 } 146 147 private static boolean isAbsolute(String fpath) 148 { 149 return new File(fpath).isAbsolute(); 150 } 151 152 173 174 178 public static Set getAllFilesUnder(File directory, FilenameFilter filenameFilter) throws IOException { 179 if (!directory.exists() || !directory.isDirectory()) { 180 throw new IOException ("Problem with: " + directory + ". You must supply a directory that exists"); 181 } 182 return getAllFilesUnder(directory, filenameFilter, true); 183 } 184 185 public static Set getAllFilesUnder(File directory, FilenameFilter filenameFilter, boolean relativize) throws IOException { 186 Set allFiles = new TreeSet(); 187 File relativizingDir = relativize ? directory : null; 188 recursiveGetFilesUnder( relativizingDir, directory, filenameFilter, 189 allFiles, false ); 190 return allFiles; 191 } 192 193 public static Set getAllFilesAndDirectoriesUnder(File directory) throws IOException { 194 if (!directory.exists() || !directory.isDirectory()) { 195 throw new IOException ("Problem with: " + directory + ". You must supply a directory that exists"); 196 } 197 Set allFiles = new TreeSet(); 198 recursiveGetFilesUnder(directory, directory, null, allFiles, true); 199 return allFiles; 200 } 201 202 private static void recursiveGetFilesUnder(File relativizingRoot, File directory, FilenameFilter filenameFilter, Set set, boolean returnDirectories) { 205 File[] files = directory.listFiles(filenameFilter); 206 for (int i = 0; i < files.length; i++) { 207 if (files[i].isDirectory()) { 208 recursiveGetFilesUnder(relativizingRoot, files[i], filenameFilter, set, returnDirectories); 209 if (returnDirectories) { 210 if( relativizingRoot != null ) { 211 set.add(relativize(relativizingRoot, files[i])); 212 } else { 213 set.add(files[i]); 214 } 215 } 216 } else { 217 if( relativizingRoot != null ) { 218 set.add(relativize(relativizingRoot, files[i])); 219 } else { 220 set.add(files[i]); 221 } 222 } 223 } 224 } 225 226 231 public static File relativize(File parent, File child) { 232 String baseDir = parent.getAbsolutePath(); 233 String baseDirAndChild = child.getAbsolutePath(); 234 235 String relative = baseDirAndChild.substring(baseDir.length(), 236 baseDirAndChild.length()); 237 238 if( relative.startsWith(File.separator) ) { 240 relative = relative.substring(1); 241 } 242 243 return new File(relative); 244 } 245 246 254 public static String classNameFromEntryName(String entryName) { 255 String className = entryName; 256 if( entryName.endsWith(".class") ) { 257 int dotClassIndex = entryName.indexOf(".class"); 258 className = entryName.substring(0, dotClassIndex); 259 className = className.replace(JAR_SEPARATOR_CHAR , '.'); 260 } 261 return className; 262 } 263 264 272 public static String classNameFromFile(File file) { 273 String className = file.toString(); 274 if ( className.endsWith(".class") ) { 275 String contentFileStr = className.replace(File.separatorChar, '.'); 276 int cutOffPoint = contentFileStr.lastIndexOf(".class"); 277 className = contentFileStr.substring(0, cutOffPoint); 278 } 279 return className; 280 } 281 282 283 public static void copyFile(File sourceFile, File destFile) 284 throws IOException { 285 286 File parent = new File(destFile.getParent()); 287 if (!parent.exists()) { 288 parent.mkdirs(); 289 } 290 291 FileInputStream fis = new FileInputStream(sourceFile); 292 FileChannel in = fis.getChannel(); 293 FileOutputStream fos = new FileOutputStream(destFile); 294 FileChannel out = fos.getChannel(); 295 in.transferTo(0, in.size(), out); 296 in.close();out.close();fis.close();fos.close(); 297 } 298 299 303 public static String [] parseFileList(String files) { 304 305 Vector fileNames = new Vector(); 306 boolean checkDriveLetter = !(File.pathSeparator.equals(":")); 307 StringTokenizer st = new StringTokenizer(files, ":"); 308 while(st.hasMoreTokens()) { 309 String name = st.nextToken(); 310 if (checkDriveLetter && name.length() == 1) { 311 if (st.hasMoreTokens()) { 314 name = name + ":" + st.nextToken(); 315 } 316 } 317 fileNames.addElement(name); 318 } 319 int size = fileNames.size(); 320 String [] result = new String [size]; 321 for (int i=0; i< size; i++) { 322 result[i] = (String ) fileNames.elementAt(i); 323 } 324 return result; 325 } 326 327 330 public static boolean jarEntriesEqual(File file1, String entry1Name, 331 File file2, String entry2Name) 332 throws IOException { 333 boolean identical = false; 334 JarFile jarFile1 = null; 335 JarFile jarFile2 = null; 336 337 try { 338 jarFile1 = new JarFile(file1); 339 jarFile2 = new JarFile(file2); 340 341 String jarEntry1Name = entry1Name.replace(File.separatorChar, 343 JAR_SEPARATOR_CHAR); 344 String jarEntry2Name = entry2Name.replace(File.separatorChar, 345 JAR_SEPARATOR_CHAR); 346 347 JarEntry entry1 = jarFile1.getJarEntry(jarEntry1Name); 348 JarEntry entry2 = jarFile2.getJarEntry(jarEntry2Name); 349 350 if( entry1 == null ) { 351 356 if(debug && _logger.isLoggable(Level.FINE)) { 358 _logger.log(Level.FINE,file1 + ":" + entry1Name + " not found"); 359 } 360 362 } 363 else if( entry2 == null ) { 364 369 if(debug && _logger.isLoggable(Level.FINE)) { 371 _logger.log(Level.FINE,file2 + ":" + entry2Name + " not found"); 372 } 373 } 375 else { 376 identical = jarEntriesEqual(jarFile1, entry1, jarFile2, entry2); 377 } 378 if( debug ) { 379 383 if(_logger.isLoggable(Level.FINE)) { 385 _logger.log(Level.FINE, "Are " + entry1Name + " and " 386 + entry2Name + " identical? " + ( identical ? "YES" : "NO")); 387 } 388 } 390 } catch(IOException e) { 391 if( debug ) { 392 395 if(_logger.isLoggable(Level.WARNING)) 397 _logger.log(Level.WARNING,"enterprise_util.excep_in_fileutil",e); 398 } 400 throw e; 401 } 402 finally { 403 if( jarFile1 != null ) { 404 jarFile1.close(); 405 } 406 if( jarFile2 != null ) { 407 jarFile2.close(); 408 } 409 } 410 411 return identical; 412 } 413 414 419 public static boolean jarEntriesEqual(JarFile jarFile1, JarEntry entry1, 420 JarFile jarFile2, JarEntry entry2) 421 throws IOException 422 { 423 boolean identical = false; 424 int entry1Size = (int) entry1.getSize(); 425 int entry2Size = (int) entry2.getSize(); 426 427 if( (entry1Size == JAR_ENTRY_UNKNOWN_VALUE) || 428 (entry2Size == JAR_ENTRY_UNKNOWN_VALUE) || 429 (entry1Size == entry2Size) ) { 430 431 if( entry1Size == 0 ) { 433 return true; 434 } 435 436 InputStream inputStream1 = null; 437 InputStream inputStream2 = null; 438 try { 439 inputStream1 = jarFile1.getInputStream(entry1); 440 inputStream2 = jarFile2.getInputStream(entry2); 441 442 byte[] file1Bytes = new byte[entry1Size]; 443 byte[] file2Bytes = new byte[entry2Size]; 444 445 int read=0; 446 int numBytesRead1=0; 447 do { 448 read = inputStream1.read(file1Bytes, numBytesRead1, entry1Size-numBytesRead1); 449 numBytesRead1 += read; 450 } while (read!=BYTE_READ_ERROR & numBytesRead1!=entry1Size); 451 452 int numBytesRead2=0; 453 do { 454 read = inputStream2.read(file2Bytes, numBytesRead2, entry2Size-numBytesRead2); 455 numBytesRead2 += read; 456 } while (read!=BYTE_READ_ERROR & numBytesRead2!=entry2Size); 457 458 459 if( ( numBytesRead1 == BYTE_READ_ERROR ) || 460 ( numBytesRead2 == BYTE_READ_ERROR ) ) { 461 throw new IOException ("Byte read error " + numBytesRead1 + " " + numBytesRead2); 462 } 463 else if( Arrays.equals(file1Bytes, file2Bytes) ) { 464 identical = true; 465 } 466 else { 467 470 if(debug) { 472 _logger.log(Level.FINE,"bytes not equal"); 473 } 474 476 } 477 } catch(IOException e) { 478 481 if (debug && _logger.isLoggable(Level.WARNING)) { 483 _logger.log(Level.WARNING,"enterprise_util.excep_in_fileutil",e); 484 } 485 487 throw e; 488 } finally { 489 if( inputStream1 != null ) { inputStream1.close(); } 490 if( inputStream2 != null ) { inputStream2.close(); } 491 } 492 } 493 else { 494 499 if(debug && _logger.isLoggable(Level.FINE)) { 501 _logger.log(Level.FINE,"sz: " + entry1Size + " , " + entry2Size); 502 } 503 505 } 506 return identical; 507 } 508 509 511 512 public static boolean isEARFile(File file) { 513 try { 514 JarFile jar = new JarFile(file); 515 ZipEntry result = jar.getEntry("META-INF/application.xml"); 516 jar.close(); 517 return result != null; 518 } catch (IOException ex) { 519 return false; 520 } 521 } 522 523 524 public static boolean isWARFile(File file) { 525 try { 526 JarFile jar = new JarFile(file); 527 ZipEntry result = jar.getEntry("WEB-INF/web.xml"); 528 jar.close(); 529 return result != null; 530 } catch (IOException ex) { 531 return false; 532 } 533 } 534 535 public static boolean isEJBJar(File file) { 536 try { 537 JarFile jar = new JarFile(file); 538 ZipEntry result = jar.getEntry("META-INF/ejb-jar.xml"); 539 jar.close(); 540 return result != null; 541 } catch (IOException ex) { 542 return false; 543 } 544 } 545 546 public static boolean isRARFile(File file) { 547 try { 548 JarFile jar = new JarFile(file); 549 ZipEntry result = jar.getEntry("META-INF/ra.xml"); 550 jar.close(); 551 return result != null; 552 } catch (IOException ex) { 553 return false; 554 } 555 } 556 557 public static boolean isAppClientJar(File file) { 558 try { 559 JarFile jar = new JarFile(file); 560 ZipEntry result = jar.getEntry("META-INF/application-client.xml"); 561 jar.close(); 562 return result != null; 563 } catch (IOException ex) { 564 return false; 565 } 566 } 567 568 573 public static boolean deleteDirContents(File dir) 574 { 575 576 if (dir.isDirectory()) { 577 578 try { 579 boolean ok = true; 580 File dirCon = dir.getCanonicalFile(); 581 String ch[] = dirCon.list(); 582 for (int i = 0; i < ch.length; i++) { 583 File file = new File(dir, ch[i]); 584 try { 585 File fileCon = file.getCanonicalFile(); if (fileCon.getParentFile().equals(dirCon)) { 587 if (!FileUtil.delete(fileCon)) { ok = false; } 589 } else { 590 Print.dprintln("Symbolic link? " + file); 595 if (!file.delete()) { ok = false; } } 599 } catch (IOException ioe) { 600 Print.dprintStackTrace("Can't delete: " + file, ioe); 602 ok = false; 603 } 604 } 605 return ok; 606 } catch (IOException ioe) { 607 Print.dprintStackTrace("Can't delete dir contents: " + dir, ioe); 609 return false; 610 } 611 612 } 613 614 return false; 615 616 } 617 618 623 public static boolean deleteDir(File fileDir) { 624 return FileUtil.delete(fileDir); 625 } 626 627 628 public static URL getEntryAsUrl(File moduleLocation, String uri) 629 throws MalformedURLException , IOException { 630 URL url = null; 631 try { 632 url = new URL (uri); 633 } catch(java.net.MalformedURLException e) { 634 url = null; 636 } 637 if (url!=null) { 638 return url; 639 } 640 if( moduleLocation != null ) { 641 if( moduleLocation.isFile() ) { 642 url = FileUtil.createJarUrl(moduleLocation, uri); 643 } else { 644 String path = uri.replace('/', File.separatorChar); 645 url = new File(moduleLocation, path).toURI().toURL(); 646 } 647 } 648 return url; 649 } 650 651 public static URL createJarUrl(File jarFile, String entry) 652 throws MalformedURLException , IOException { 653 return new URL ("jar:" + jarFile.toURI().toURL() + "!/" + entry); 654 } 655 656 660 public static boolean delete(File dir) { 661 if (dir.isDirectory()) { 662 FileUtil.deleteDirContents(dir); 663 return dir.delete(); } else { 671 return dir.delete(); } 676 } 677 678 680 681 private static final boolean DeleteOnExit_ShutdownHook = true; 682 683 private static Vector deleteOnExit_normal = null; 684 private static Vector deleteOnExit_forced = null; 685 686 691 public static File deleteOnExit(File file) { 692 return deleteOnExit(file, false); 693 } 694 695 700 public static File deleteOnExit(File file, boolean forceDeleteDir) { 701 if (DeleteOnExit_ShutdownHook) { 702 if (!file.isAbsolute()) { 703 Print.dprintStackTrace("File is not Absolute! " + file.toString()); 704 } else { 705 if (deleteOnExit_forced == null) { 706 deleteOnExit_forced = new Vector(); 707 deleteOnExit_normal = new Vector(); 708 Runtime.getRuntime().addShutdownHook(new Thread () { 709 public void run() { 710 _delFiles(deleteOnExit_forced, true); 711 _delFiles(deleteOnExit_normal, false); 712 } 713 }); 714 } 715 if (forceDeleteDir && file.isDirectory()) { 716 if (!deleteOnExit_forced.contains(file)) { 717 deleteOnExit_forced.add(file); 718 } 719 } else { 720 if (!deleteOnExit_normal.contains(file)) { 721 deleteOnExit_normal.add(file); 722 } 723 } 724 } 725 } else { 726 file.deleteOnExit(); 727 } 728 return file; 729 } 730 731 private static void _delFiles(java.util.List list, boolean forceDelete) { 732 if (list == null) { 733 return; 734 } 735 736 737 if (!forceDelete) { 738 Comparator fileComparator = new Comparator() { 749 public int compare(Object o1, Object o2) { 750 String n1 = o1.toString(), n2 = o2.toString(); 751 return n2.length() - n1.length(); 752 } 753 public boolean equals(Object o) { 754 return super.equals(o); 755 } 756 }; 757 Collections.sort(list, fileComparator); 758 } 759 760 761 for (Iterator i = list.iterator(); i.hasNext();) { 762 File file = (File)i.next(); 763 if (!file.isAbsolute()) { 764 Print.dprintln("[Not Absolute!] " + file); 765 } else 766 if (file.exists()) { 767 if (forceDelete) { 768 if (!FileUtil.delete(file)) { 769 Print.dprintln("[Not Deleted!] " + file); 770 } 771 } else { 772 if (!file.delete()) { Print.dprintln("[Not Deleted!] " + file); 775 } 776 } 777 } else { 778 } 780 } 781 782 } 783 784 785 public static String getClassNameFromFile(File f) throws IOException , ClassFormatError { 786 FileClassLoader fcl = new FileClassLoader(f.toString()); 787 return fcl.getClassName(f); 788 } 789 790 } 791 | Popular Tags |