1 19 20 package org.netbeans.updater; 21 22 import java.io.BufferedInputStream ; 23 import java.io.File ; 24 import java.io.FileFilter ; 25 import java.io.FileInputStream ; 26 import java.io.InputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.OutputStream ; 29 import java.io.IOException ; 30 import java.io.PrintWriter ; 31 import java.util.*; 32 import java.util.zip.CRC32 ; 33 import org.w3c.dom.*; 34 import org.xml.sax.InputSource ; 35 36 40 public final class UpdateTracking { 41 private static final String ELEMENT_MODULES = "installed_modules"; private static final String ELEMENT_MODULE = "module"; private static final String ATTR_CODENAMEBASE = "codename"; private static final String ELEMENT_VERSION = "module_version"; private static final String ATTR_VERSION = "specification_version"; private static final String ATTR_ORIGIN = "origin"; private static final String ATTR_LAST = "last"; private static final String ATTR_INSTALL = "install_time"; private static final String ELEMENT_FILE = "file"; private static final String ATTR_FILE_NAME = "name"; private static final String ATTR_CRC = "crc"; 53 private static final String NBM_ORIGIN = "nbm"; private static final String INST_ORIGIN = "updater"; 57 58 private static final String FILE_SEPARATOR = System.getProperty ("file.separator"); 59 private static final String LOCALE_DIR = FILE_SEPARATOR + "locale" + FILE_SEPARATOR; 61 62 public static final String TRACKING_FILE_NAME = "update_tracking"; private static final String XML_EXT = ".xml"; 65 66 private static final Map trackings = new HashMap (); 67 68 71 private LinkedHashMap installedModules = new LinkedHashMap (); 72 73 private boolean pError = false; 74 private boolean fromUser = false; 75 private final File directory; 76 private final File trackingFile; 77 private String origin = NBM_ORIGIN; 78 79 80 public UpdateTracking () { 81 this (null); 83 } 84 85 87 private UpdateTracking( File nbPath ) { 88 assert nbPath != null : "Path cannot be null"; 89 90 trackingFile = new File ( nbPath + FILE_SEPARATOR + TRACKING_FILE_NAME); 91 directory = nbPath; 92 origin = INST_ORIGIN; 93 } 94 95 96 100 104 static UpdateTracking getTracking( boolean fromuser ) { 105 UpdateTracking ut = getTracking ( 106 System.getProperty (fromuser ? "netbeans.user" : "netbeans.home"), fromuser 108 ); 109 return ut; 110 } 111 112 117 private static UpdateTracking getTracking(String cluster, boolean createIfDoesNotExists) { 118 File f = new File (cluster); 119 120 UpdateTracking ut = getTracking (f, createIfDoesNotExists); 121 return ut; 122 } 123 124 130 public static UpdateTracking getTracking (File path) { 131 path = new File (path.toURI ().normalize ()).getAbsoluteFile (); 133 134 File userDir = new File (System.getProperty ("netbeans.user")); 137 userDir = new File (userDir.toURI ().normalize ()).getAbsoluteFile (); 138 139 return getTracking (path, path.toString().equals (userDir.getPath ())); 140 } 141 142 147 public static UpdateTracking getTracking (File path, boolean createIfDoesNotExists) { 148 try { 149 path = path.getCanonicalFile (); 150 } catch (java.io.IOException ex) { 151 IllegalStateException ill = new IllegalStateException (ex.getMessage ()); 152 ill.initCause (ex); 153 throw ill; 154 } 155 156 synchronized (trackings) { 157 UpdateTracking track = (UpdateTracking)trackings.get (path); 158 if (track == null) { 159 File utFile = new File (path, TRACKING_FILE_NAME); 160 if (!createIfDoesNotExists && !utFile.isDirectory ()) { 161 return null; 164 } 165 File noAU = new File (path, ".noautoupdate"); if (noAU.exists()) { 167 return null; 170 } 171 172 173 track = new UpdateTracking (path); 174 trackings.put (path, track); 175 track.read (); 176 track.scanDir (); 177 } 178 return track; 179 } 180 } 181 182 183 186 public static File getPlatformDir () { 187 return new File (System.getProperty ("netbeans.home")); } 189 190 195 public static List clusters (boolean includeUserDir) { 196 ArrayList files = new ArrayList (); 197 198 if (includeUserDir) { 199 File ud = new File (System.getProperty ("netbeans.user")); files.add (ud); 201 } 202 203 204 String dirs = System.getProperty("netbeans.dirs"); if (dirs != null) { 206 Enumeration en = new StringTokenizer (dirs, File.pathSeparator); 207 while (en.hasMoreElements ()) { 208 File f = new File ((String )en.nextElement ()); 209 files.add (f); 210 } 211 } 212 213 214 File id = getPlatformDir (); 215 files.add (id); 216 217 return java.util.Collections.unmodifiableList (files); 218 } 219 220 224 228 public boolean isModuleInstalled (String codeBase) { 229 Iterator it = installedModules.values ().iterator (); 230 while (it.hasNext ()) { 231 Module m = (Module)it.next (); 232 String mm = m.codenamebase; 233 int indx = mm.indexOf ('/'); 234 if (indx >= 0) { 235 mm = mm.substring (0, indx); 236 } 237 if (codeBase.equals (mm)) { 238 return true; 239 } 240 } 241 return false; 242 } 243 244 248 public List getModulesToInstall () { 249 class NbmFilter implements java.io.FilenameFilter { 250 public boolean accept( File dir, String name ) { 251 return name.endsWith (ModuleUpdater.NBM_EXTENSION); 252 } 253 } 254 255 File idir = new File (directory, ModuleUpdater.DOWNLOAD_DIR); 256 File [] arr = idir.listFiles (new NbmFilter ()); 257 if (arr == null) { 258 return Collections.EMPTY_LIST; 259 } else { 260 return Arrays.asList (arr); 261 } 262 } 263 264 265 269 270 271 private void read() { 272 273 org.w3c.dom.Document document; 274 275 File file; 276 InputStream is; 277 try { 278 file = trackingFile; 279 280 if ( ! file.isFile () ) 281 return; 282 283 is = new FileInputStream ( file ); 284 285 InputSource xmlInputSource = new InputSource ( is ); 286 document = XMLUtil.parse( xmlInputSource, false, false, new ErrorCatcher(), XMLUtil.createAUResolver() ); 287 if (is != null) 288 is.close(); 289 } 290 catch ( org.xml.sax.SAXException e ) { 291 System.out.println("Bad update_tracking" ); e.printStackTrace (); 293 return; 294 } 295 catch ( java.io.IOException e ) { 296 System.out.println("Missing update_tracking" ); e.printStackTrace (); 298 return; 299 } 300 301 org.w3c.dom.Element element = document.getDocumentElement(); 302 if ((element != null) && element.getTagName().equals(ELEMENT_MODULES)) { 303 scanElement_installed_modules(element, fromUser); 304 } 305 } 306 307 308 void scanElement_installed_modules(org.w3c.dom.Element element, boolean fromuser) { org.w3c.dom.NodeList nodes = element.getChildNodes(); 311 for (int i = 0; i < nodes.getLength(); i++) { 312 org.w3c.dom.Node node = nodes.item(i); 313 if ( node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE ) { 314 org.w3c.dom.Element nodeElement = (org.w3c.dom.Element )node; 315 if (nodeElement.getTagName().equals(ELEMENT_MODULE)) { 316 if (true) throw new IllegalStateException ("What now!?"); 317 } 320 } 321 } 322 } 323 324 325 Module scanElement_module(org.w3c.dom.Element element, boolean fromuser) { Module module = new Module( fromuser ); 327 org.w3c.dom.NamedNodeMap attrs = element.getAttributes(); 328 for (int i = 0; i < attrs.getLength(); i++) { 329 org.w3c.dom.Attr attr = (org.w3c.dom.Attr )attrs.item(i); 330 if (attr.getName().startsWith(ATTR_CODENAMEBASE)) { 331 module.setCodenamebase( attr.getValue() ); 333 } 334 } 335 org.w3c.dom.NodeList nodes = element.getChildNodes(); 336 for (int i = 0; i < nodes.getLength(); i++) { 337 org.w3c.dom.Node node = nodes.item(i); 338 if ( node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE ) { 339 org.w3c.dom.Element nodeElement = (org.w3c.dom.Element )node; 340 if (nodeElement.getTagName().equals(ELEMENT_VERSION)) { 341 scanElement_module_version(nodeElement, module); 342 } 343 } 344 } 345 return module; 346 } 347 348 349 void scanElement_module_version(org.w3c.dom.Element element, Module module) { Version version = new Version(); 351 org.w3c.dom.NamedNodeMap attrs = element.getAttributes(); 352 for (int i = 0; i < attrs.getLength(); i++) { 353 org.w3c.dom.Attr attr = (org.w3c.dom.Attr )attrs.item(i); 354 if (attr.getName().equals(ATTR_VERSION)) { version.setVersion( attr.getValue() ); 356 } 357 if (attr.getName().equals(ATTR_ORIGIN)) { version.setOrigin( attr.getValue() ); 359 } 360 if (attr.getName().equals(ATTR_LAST)) { version.setLast( Boolean.valueOf(attr.getValue() ).booleanValue()); 362 } 363 if (attr.getName().equals(ATTR_INSTALL)) { long li = 0; 365 try { 366 li = Long.parseLong( attr.getValue() ); 367 } catch ( NumberFormatException nfe ) { 368 } 369 version.setInstall_time( li ); 370 } 371 } 372 org.w3c.dom.NodeList nodes = element.getChildNodes(); 373 for (int i = 0; i < nodes.getLength(); i++) { 374 org.w3c.dom.Node node = nodes.item(i); 375 if ( node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE ) { 376 org.w3c.dom.Element nodeElement = (org.w3c.dom.Element )node; 377 if (nodeElement.getTagName().equals(ELEMENT_FILE)) { 378 scanElement_file(nodeElement, version); 379 } 380 } 381 } 382 module.addOldVersion( version ); 383 } 384 385 386 void scanElement_file(org.w3c.dom.Element element, Version version) { ModuleFile file = new ModuleFile(); 388 org.w3c.dom.NamedNodeMap attrs = element.getAttributes(); 389 for (int i = 0; i < attrs.getLength(); i++) { 390 org.w3c.dom.Attr attr = (org.w3c.dom.Attr )attrs.item(i); 391 if (attr.getName().equals(ATTR_FILE_NAME)) { file.setName( attr.getValue() ); 393 } 394 if (attr.getName().equals(ATTR_CRC)) { file.setCrc( attr.getValue() ); 396 } 397 if (attr.getName().equals(ATTR_VERSION)) { 398 file.setLocaleversion( attr.getValue() ); 399 } 400 } 401 version.addFile (file ); 402 } 403 404 Module readModuleTracking( boolean fromuser, String codename, boolean create ) { 405 new File (directory, TRACKING_FILE_NAME).mkdirs(); 406 File file = new File ( 407 new File (directory, TRACKING_FILE_NAME), 408 getTrackingName( codename ) + XML_EXT 409 ); 410 411 try { 413 if ( file.exists() && file.length()==0 ) 414 file.delete(); 415 } catch (Exception e) { 416 } 418 419 if ( ! file.exists() ) { 420 if ( create ) 421 return new Module( codename, file, fromuser ); 422 else 423 return null; 424 } 425 426 return readModuleFromFile( file, codename, fromuser, create ); 427 } 428 429 Version createVersion(String specversion) { 430 Version ver = new Version(); 431 ver.setVersion( specversion ); 432 return ver; 433 } 434 435 private Module readModuleFromFile( File file, String codename, boolean fromuser, boolean create ) { 436 437 438 org.w3c.dom.Document document; 439 InputStream is; 440 try { 441 is = new FileInputStream ( file ); 442 443 InputSource xmlInputSource = new InputSource ( is ); 444 document = XMLUtil.parse( xmlInputSource, false, false, new ErrorCatcher(), XMLUtil.createAUResolver() ); 445 if (is != null) 446 is.close(); 447 } catch ( org.xml.sax.SAXException e ) { 448 System.out.println("Bad update_tracking" ); e.printStackTrace (); 450 return null; 451 } 452 catch ( java.io.IOException e ) { 453 if ( create ) 454 return new Module( codename, file, fromuser ); 455 else 456 return null; 457 } 458 459 org.w3c.dom.Element element = document.getDocumentElement(); 460 if ((element != null) && element.getTagName().equals(ELEMENT_MODULE)) { 461 462 Module m = scanElement_module(element, fromuser); 463 m.setFile( file ); 464 installedModules.put (file, m); 465 return m; 466 } 467 if ( create ) 468 return new Module( codename, file, fromuser ); 469 else 470 return null; 471 } 472 473 private static String getTrackingName(String codename) { 474 String trackingName = codename; 475 int pos = trackingName.indexOf('/'); if ( pos > -1 ) 477 trackingName = trackingName.substring( 0, pos ); 478 return trackingName.replace( '.', '-' ); } 480 481 public static void convertOldFormat(File oldfile, String path, boolean fromUserDir) { 482 new File ( path + FILE_SEPARATOR + TRACKING_FILE_NAME ).mkdirs(); 483 UpdateTracking track = getTracking( fromUserDir ); 484 Iterator it = track.installedModules.values ().iterator(); 485 while ( it.hasNext() ) { 486 Module mod = (Module)it.next(); 487 File newfile = new File ( path + FILE_SEPARATOR + TRACKING_FILE_NAME + FILE_SEPARATOR 488 + getTrackingName( mod.getCodenamebase() ) + XML_EXT ); 489 mod.setFile( newfile ); 490 mod.write(); 491 } 492 oldfile.delete(); 493 } 494 495 public String getL10NSpecificationVersion(String codenamebase, boolean fromUserDir, String jarpath) { 496 Module module = readModuleTracking( fromUserDir, codenamebase, false ); 497 if ( module == null ) 498 return null; 499 500 return module.getL10NSpecificationVersion( jarpath ); 501 } 502 503 void deleteUnusedFiles() { 504 List newModules = new ArrayList (installedModules.values ()); 505 Iterator it = newModules.iterator(); 506 while ( it.hasNext() ) { 507 Module mod = (Module)it.next(); 508 mod.deleteUnusedFiles(); 509 } 510 scanDir (); 511 } 512 513 public static long getFileCRC(File file) throws IOException { 514 BufferedInputStream bsrc = null; 515 CRC32 crc = new CRC32 (); 516 try { 517 bsrc = new BufferedInputStream ( new FileInputStream ( file ) ); 518 byte[] bytes = new byte[1024]; 519 int i; 520 while( (i = bsrc.read()) != -1 ) { 521 crc.update( (byte)i ); 522 } 523 } 524 finally { 525 if ( bsrc != null ) 526 bsrc.close(); 527 } 528 return crc.getValue(); 529 } 530 531 private void scanDir () { 532 File dir = new File (directory, TRACKING_FILE_NAME); 533 File [] files = dir.listFiles( new FileFilter () { 534 public boolean accept( File file ) { 535 if ( !file.isDirectory() && file.getName().toUpperCase().endsWith(".XML") ) return true; 537 else 538 return false; 539 } 540 } ); 541 542 if (files == null) { 543 return; 544 } 545 546 for ( int i = 0; i < files.length; i++ ) { 547 if (!installedModules.containsKey (files[i])) { 548 readModuleFromFile( files[i], null, fromUser, true ); 549 } 550 551 } 552 } 553 554 class Module extends Object { 555 556 557 private String codenamebase; 558 559 560 private List versions = new ArrayList(); 561 562 private File file = null; 563 564 private boolean fromUser = true; 565 566 public Module() { 567 } 568 569 public Module(boolean fromUser) { 570 this.fromUser = fromUser; 571 } 572 573 public Module(String codenamebase, File file, boolean fromUser) { 574 this.codenamebase = codenamebase; 575 this.file = file; 576 this.fromUser = fromUser; 577 } 578 579 private Version lastVersion = null; 580 private Version newVersion = null; 581 582 585 String getCodenamebase() { 586 return codenamebase; 587 } 588 589 592 void setCodenamebase(String codenamebase) { 593 this.codenamebase = codenamebase; 594 } 595 596 599 List getVersions() { 600 return versions; 601 } 602 603 606 void setVersions(List versions) { 607 this.versions = versions; 608 } 609 610 boolean isFromUser() { 611 return fromUser; 612 } 613 614 void setFromUser(boolean fromUser) { 615 this.fromUser = fromUser; 616 } 617 618 private Version getNewOrLastVersion() { 619 if ( newVersion != null ) 620 return newVersion; 621 else 622 return lastVersion; 623 } 624 625 boolean hasNewVersion() { 626 return newVersion != null; 627 } 628 629 void setFile(File file) { 630 this.file = file; 631 } 632 633 public Version addNewVersion( String spec_version ) { 634 if ( lastVersion != null ) 635 lastVersion.setLast ( false ); 636 Version version = new Version(); 637 newVersion = version; 638 version.setVersion( spec_version ); 639 version.setOrigin( origin ); 640 version.setLast( true ); 641 version.setInstall_time( System.currentTimeMillis() ); 642 versions.add( version ); 643 return version; 644 } 645 646 void addOldVersion( Version version ) { 647 if ( version.isLast() ) 648 lastVersion = version; 649 650 versions.add( version ); 651 } 652 653 void addL10NVersion( Version l_version ) { 654 if ( lastVersion != null ) 655 lastVersion.addL10NFiles( l_version.getFiles() ); 656 else { 657 l_version.setOrigin( origin ); 658 l_version.setLast( true ); 659 l_version.setInstall_time( System.currentTimeMillis() ); 660 versions.add( l_version ); 661 } 662 } 663 664 void writeConfigModuleXMLIfMissing () { 665 File configDir = new File (new File (directory, "config"), "Modules"); 667 String candidate = null; 668 String oldCandidate = null; 669 String newCandidate = null; 670 671 String name = codenamebase; 672 int indx = name.indexOf ('/'); 673 if (indx > 0) { 674 name = name.substring (0, indx); 675 } 676 677 String replaced = name.replace ('.', '-'); String searchFor; 680 681 if (replaced.indexOf ("modules") > 0) { searchFor = replaced + ".jar"; } else { 685 searchFor = replaced.substring (replaced.lastIndexOf ('-') > 0 ? replaced.lastIndexOf ('-') + 1 : 0) + ".jar"; } 688 689 String dash = name.replace ('.', '-'); 690 691 File config = new File (configDir, dash + ".xml"); if (config.isFile ()) { 693 return; 695 } 696 697 config.getParentFile ().mkdirs (); 698 699 Boolean isAutoload = null; 700 Boolean isEager = null; 701 702 java.util.Iterator it = newVersion.getFiles ().iterator (); 703 boolean needToWrite = false; 704 705 while (it.hasNext ()) { 706 ModuleFile f = (ModuleFile)it.next (); 707 708 String n = f.getName (); 709 String parentDir = new File (f.getName ()).getParentFile ().getName (); 710 711 needToWrite = needToWrite || n.indexOf ("modules") >= 0; 712 713 if (n.endsWith (".jar")) { candidate = f.getName (); 716 717 if (searchFor.endsWith (candidate) || candidate.endsWith (searchFor)) { 720 newCandidate = candidate; 721 oldCandidate = null; 722 723 if ("autoload".equals (parentDir)) { isAutoload = Boolean.TRUE; 726 } else { 727 isAutoload = Boolean.FALSE; 728 } 729 if ("eager".equals (parentDir)) { isEager = Boolean.TRUE; 731 } else { 732 isEager = Boolean.FALSE; 733 } 734 } else { 735 if (newCandidate == null) { 736 oldCandidate = (oldCandidate == null ? "" : oldCandidate + ", ") + candidate; } 738 } 739 } 740 741 if (isAutoload == null && "autoload".equals (parentDir)) { isAutoload = Boolean.TRUE; 744 } 745 if (isEager == null && "eager".equals (parentDir)) { isEager = Boolean.TRUE; 747 } 748 } 749 750 if (! needToWrite) { 751 System.out.println("Warning: No config file written for module " + codenamebase + ". No jar file present in \"modules\" directory."); 752 return ; 753 } 754 755 assert newCandidate != null || oldCandidate != null : "No jar file present!"; 756 if (newCandidate == null) { 757 assert oldCandidate.equals (candidate) : "More files look as module: " + oldCandidate; 759 if (!oldCandidate.equals (candidate)) { 761 System.out.println("NBM Error: More files look as module: " + oldCandidate); 762 oldCandidate = candidate; 763 } 764 } 766 767 String moduleName = newCandidate == null ? oldCandidate : newCandidate; 768 769 boolean autoload = isAutoload != null && isAutoload.booleanValue (); 770 boolean eager = isEager != null && isEager.booleanValue (); 771 boolean isEnabled = !autoload && !eager; 772 773 String spec = newVersion.getVersion (); 774 OutputStream os; 775 try { 776 os = new FileOutputStream (config); 777 PrintWriter pw = new PrintWriter (new java.io.OutputStreamWriter (os, "UTF-8")); 778 pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 781 pw.println("<!DOCTYPE module PUBLIC \"-//NetBeans//DTD Module Status 1.0//EN\""); 782 pw.println(" \"http://www.netbeans.org/dtds/module-status-1_0.dtd\">"); 783 pw.println("<module name=\"" + name + "\">"); 784 pw.println(" <param name=\"autoload\">" + autoload + "</param>"); 785 pw.println(" <param name=\"eager\">" + eager + "</param>"); 786 if (isEnabled) { 787 pw.println(" <param name=\"enabled\">" + isEnabled + "</param>"); 788 } 789 pw.println(" <param name=\"jar\">" + moduleName + "</param>"); 790 pw.println(" <param name=\"reloadable\">false</param>"); 791 pw.println(" <param name=\"specversion\">" + spec + "</param>"); 792 pw.println("</module>"); 793 pw.flush(); 794 pw.close(); 795 } catch (IOException ex) { 796 ex.printStackTrace(); 797 } 798 799 } 800 801 void write( ) { 802 Document document = XMLUtil.createDocument(ELEMENT_MODULE); 803 804 Element e_module = document.getDocumentElement(); 805 Element e_version = null; 806 Element e_file = null; 807 808 e_module.setAttribute(ATTR_CODENAMEBASE, getCodenamebase()); 809 Iterator it2 = getVersions().iterator(); 810 while ( it2.hasNext() ) { 811 Version ver = (Version)it2.next(); 812 e_version = document.createElement(ELEMENT_VERSION); 813 if ( ver.getVersion() != null ) 814 e_version.setAttribute(ATTR_VERSION, ver.getVersion()); 815 e_version.setAttribute(ATTR_ORIGIN, ver.getOrigin()); 816 e_version.setAttribute(ATTR_LAST, new Boolean ( ver.isLast() ).toString()); 817 e_version.setAttribute(ATTR_INSTALL, Long.toString(ver.getInstall_time())); 818 e_module.appendChild( e_version ); 819 Iterator it3 = ver.getFiles().iterator(); 820 while ( it3.hasNext() ) { 821 ModuleFile file = (ModuleFile)it3.next(); 822 e_file = document.createElement(ELEMENT_FILE); 823 e_file.setAttribute(ATTR_FILE_NAME, file.getName()); 824 e_file.setAttribute(ATTR_CRC, file.getCrc()); 825 if ( file.getLocaleversion() != null ) 826 e_file.setAttribute(ATTR_VERSION, file.getLocaleversion()); 827 e_version.appendChild( e_file ); 828 } 829 } 830 831 document.getDocumentElement().normalize(); 832 833 try { 834 OutputStream os = new FileOutputStream ( file ); 835 XMLUtil.write(document, os); 836 os.close(); 837 } catch (Exception e) { 838 e.printStackTrace(); 839 } 840 } 841 842 void deleteUnusedFiles() { 843 if ( lastVersion == null || newVersion == null ) 844 return; 845 Iterator it = lastVersion.getFiles().iterator(); 846 while ( it.hasNext() ) { 847 ModuleFile modFile = (ModuleFile)it.next(); 848 if ( ! newVersion.containsFile( modFile ) && modFile.getName().indexOf( LOCALE_DIR ) == -1 ) 849 safeDelete( modFile ); 850 } 851 } 852 853 private void safeDelete(ModuleFile modFile) { 854 File f = new File ( file.getParentFile().getParent() + FILE_SEPARATOR + modFile.getName() ); 856 if ( f.exists() ) { 857 try { 859 if ( ! Long.toString( getFileCRC( f ) ).equals( modFile.getCrc() ) ) 860 return; 861 } catch ( IOException ioe ) { 862 return; 863 } 864 865 scanDir(); 867 boolean found = false; 868 Iterator it = installedModules.values ().iterator(); 869 while ( !found && it.hasNext() ) { 870 Module mod = (Module)it.next(); 871 if ( ! mod.equals( this ) ) { 872 Version v = mod.getNewOrLastVersion(); 873 if ( v != null && v.containsFile( modFile ) ) 874 found = true; 875 } 876 } 877 if ( ! found ) 878 f.delete(); 879 } 880 } 881 882 String getL10NSpecificationVersion(String jarpath) { 883 String localever = null; 884 Collections.sort( versions ); 885 Iterator it = versions.iterator(); 886 while ( it.hasNext() ) { 887 Version ver = (Version) it.next(); 888 localever = ver.getLocaleVersion( jarpath ); 889 if ( localever != null ) 890 return localever; 891 } 892 return null; 893 } 894 } 895 896 public class Version extends Object implements Comparable { 897 898 899 private String version; 900 901 902 private String origin; 903 904 905 private boolean last; 906 907 908 private long install_time = 0; 909 910 911 private List files = new ArrayList(); 912 913 916 String getVersion() { 917 return version; 918 } 919 920 923 void setVersion(String version) { 924 this.version = version; 925 } 926 927 930 String getOrigin() { 931 return origin; 932 } 933 934 937 void setOrigin(String origin) { 938 this.origin = origin; 939 } 940 941 944 boolean isLast() { 945 return last; 946 } 947 948 951 void setLast(boolean last) { 952 this.last = last; 953 } 954 955 958 long getInstall_time() { 959 return install_time; 960 } 961 962 965 void setInstall_time(long install_time) { 966 this.install_time = install_time; 967 } 968 969 972 List getFiles() { 973 return files; 974 } 975 976 979 void addL10NFiles(List l10nfiles) { 980 Iterator it = l10nfiles.iterator(); 981 while ( it.hasNext() ) { 982 ModuleFile lf = (ModuleFile) it.next(); 983 String lname = lf.getName(); 984 for ( int i = files.size() - 1; i >=0; i-- ) { 985 ModuleFile f = (ModuleFile) files.get( i ); 986 if ( f.getName().equals( lname ) ) 987 files.remove( i ); 988 } 989 } 990 files.addAll( l10nfiles ); 991 } 992 993 void addFile( ModuleFile file ) { 994 files.add( file ); 995 } 996 997 public void addFileWithCrc( String filename, String crc ) { 998 ModuleFile file = new ModuleFile(); 999 file.setName( filename ); 1000 file.setCrc( crc ); 1001 files.add( file ); 1002 } 1003 1004 public void addL10NFileWithCrc( String filename, String crc, String specver ) { 1005 ModuleFile file = new ModuleFile(); 1006 file.setName( filename ); 1007 file.setCrc( crc ); 1008 file.setLocaleversion( specver ); 1009 files.add( file ); 1010 } 1011 1012 boolean containsFile( ModuleFile file ) { 1013 Iterator it = files.iterator(); 1014 while ( it.hasNext() ) { 1015 ModuleFile f = (ModuleFile)it.next(); 1016 if ( f.getName().equals( file.getName() ) ) 1017 return true; 1018 } 1019 return false; 1020 } 1021 1022 ModuleFile findFile(String filename) { 1023 Iterator it = files.iterator(); 1024 while ( it.hasNext() ) { 1025 ModuleFile f = (ModuleFile)it.next(); 1026 if ( f.getName().equals( filename ) ) 1027 return f; 1028 } 1029 return null; 1030 } 1031 1032 String getLocaleVersion(String filename) { 1033 String locver = null; 1034 ModuleFile f = findFile( filename ); 1035 if ( f != null ) { 1036 locver = f.getLocaleversion(); 1037 if ( locver == null ) 1038 locver = version; 1039 } 1040 return locver; 1041 } 1042 1043 public int compareTo(Object obj) { 1044 Version oth = (Version)obj; 1045 if ( install_time < oth.getInstall_time() ) 1046 return 1; 1047 else if ( install_time > oth.getInstall_time() ) 1048 return -1; 1049 else 1050 return 0; 1051 } 1052 } 1053 1054 class ModuleFile extends Object { 1055 1056 1057 private String name; 1058 1059 1060 private String crc; 1061 1062 1063 private String localeversion = null; 1064 1065 1068 String getName() { 1069 return name; 1070 } 1071 1072 1075 void setName(String name) { 1076 this.name = name; 1077 } 1078 1079 1082 String getCrc() { 1083 return crc; 1084 } 1085 1086 1089 void setCrc(String crc) { 1090 this.crc = crc; 1091 } 1092 1093 1097 public String getLocaleversion() { 1098 return this.localeversion; 1099 } 1100 1101 1105 public void setLocaleversion(String localeversion) { 1106 this.localeversion = localeversion; 1107 } 1108 1109 } 1110 1111 class ErrorCatcher implements org.xml.sax.ErrorHandler { 1112 private void message (String level, org.xml.sax.SAXParseException e) { 1113 pError = true; 1114 } 1115 1116 public void error (org.xml.sax.SAXParseException e) { 1117 pError = true; 1119 } 1120 1121 public void warning (org.xml.sax.SAXParseException e) { 1122 } 1124 1125 public void fatalError (org.xml.sax.SAXParseException e) { 1126 pError = true; 1127 } 1128 } 1129 1130} 1131 | Popular Tags |