1 19 20 package org.netbeans.modules.autoupdate; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.net.URL ; 26 import java.net.URLConnection ; 27 import java.net.UnknownHostException ; 28 import java.text.ParseException ; 29 import java.text.SimpleDateFormat ; 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 import java.util.Date ; 33 import java.util.Enumeration ; 34 import java.util.HashSet ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Locale ; 38 import java.util.jar.Attributes ; 39 import java.util.jar.JarEntry ; 40 import java.util.jar.JarFile ; 41 import java.util.jar.Manifest ; 42 import java.util.logging.Level ; 43 import java.util.logging.Logger ; 44 import java.util.regex.Pattern ; 45 import java.util.zip.ZipEntry ; 46 import java.util.zip.ZipException ; 47 import org.netbeans.updater.UpdateTracking; 48 import org.openide.DialogDisplayer; 49 import org.openide.NotifyDescriptor; 50 import org.openide.modules.ModuleInfo; 51 import org.openide.util.Exceptions; 52 import org.openide.util.Lookup; 53 import org.openide.util.NbBundle; 54 import org.w3c.dom.Attr ; 55 import org.w3c.dom.Document ; 56 import org.w3c.dom.Element ; 57 import org.w3c.dom.NamedNodeMap ; 58 import org.w3c.dom.Node ; 59 import org.w3c.dom.NodeList ; 60 import org.xml.sax.InputSource ; 61 62 67 class ModuleUpdate extends Object 68 implements org.openide.nodes.Node.Cookie { 69 70 private static final String ATTR_HOMEPAGE = "homepage"; private static final String ATTR_DISTRIBUTION = "distribution"; private static final String ATTR_CODENAMEBASE = "codenamebase"; private static final String ATTR_DOWNLOAD_SIZE = "downloadsize"; private static final String ATTR_UNPACKED_SIZE = "unpacksize"; private static final String ATTR_LICENSE = "license"; private static final String ATTR_LICENSE_MISSPELLED = "licence"; private static final String ATTR_PURCHASED = "purchased"; private static final String ATTR_NEEDSRESTART = "needsrestart"; 81 private static final String ATTR_AUTHOR = "moduleauthor"; private static final String ATTR_RELEASE_DATE = "releasedate"; private static final String ATTR_IS_GLOBAL = "global"; private static final String ATTR_TARGET_CLUSTER = "targetcluster"; 86 static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat ("yyyy/MM/dd"); 88 private static final String ELEMENT_DESCRIPTION = "description"; private static final String ELEMENT_NOTIFICATION = "module_notification"; private static final String ELEMENT_EXTERNAL = "external_package"; static final String ELEMENT_L10N = "l10n"; 93 private static final String ATTR_EXT_NAME = "name"; private static final String ATTR_EXT_TARGET = "target_name"; private static final String ATTR_EXT_URL = "start_url"; private static final String ATTR_EXT_DESC = "description"; 98 static final String NBM_LIB = "lib"; static final String NBM_CORE = "core"; 102 private static final Logger LOG = Logger.getLogger ("org.netbeans.modules.autoupdate.ModuleUpdate"); 104 105 private URL xmlURL; 106 107 private Node node; 108 109 private Element documentElement; 110 111 112 private File nbmFile; 113 114 115 116 private URL distribution = null; 117 118 private URL homepage = null; 119 120 private String infoCodenamebase = null; 121 122 private long downloadSize = -1; 123 124 private long unpackedSize = -1; 125 126 private boolean downloadOK = false; 127 128 private String description = null; 129 130 private String licenceID = null; 131 132 private String licenceText = null; 133 134 private boolean selected = false; 135 136 private int security = SignVerifier.NOT_CHECKED; 137 138 private Collection certs = null; 139 140 private boolean installApproved = false; 141 142 private ModuleInfo localModule = null; 144 145 private ModuleInfo remoteModule = null; 146 147 148 private boolean purchased = false; 149 150 151 private String notification = null; 152 153 154 private String moduleAuthor = null; 155 156 157 private Date releaseDate = null; 158 159 160 private boolean notificationAccepted = false; 161 162 private String distributionFilename = null; 163 164 165 private List externals; 166 167 168 private boolean depending; 169 170 171 private boolean toInstallDir; 172 173 174 private boolean downloadStarted; 175 176 177 private boolean safeToInstall; 178 179 180 private Boolean isGlobal; 181 182 183 private String targetCluster; 184 185 186 private List jarList = new ArrayList (); 187 188 189 private File target; 190 191 193 195 196 ModuleUpdate( URL xmlURL, Node node, Element documentElement ) { 197 this.xmlURL = xmlURL; 198 this.node = node; 199 this.documentElement = documentElement; 200 } 201 202 203 ModuleUpdate( File nbmFile, Node node, Element documentElement ) { 204 this.nbmFile = nbmFile; 205 this.node = node; 206 this.documentElement = documentElement; 207 } 208 209 static ModuleUpdate getModuleUpdate( URL xmlURL, Node node, Element documentElement, AutoupdateType at ) { 210 ModuleUpdate mod; 211 NodeList nodeList = ((Element )node).getElementsByTagName( ELEMENT_L10N ); if ( nodeList != null && nodeList.getLength() > 0 ) 213 mod = new L10NUpdate( xmlURL, node, documentElement ); 214 else 215 mod = new ModuleUpdate( xmlURL, node, documentElement ); 216 217 if ( mod.readModuleUpdate( at ) ) 218 return mod; 219 else 220 return null; 221 } 222 223 static private String getMessage (String key, String p1) { 224 return NbBundle.getMessage (ModuleUpdate.class, key, p1); 225 } 226 227 static ModuleUpdate getModuleUpdate( File nbmFile ) { 228 ModuleUpdate mod; 229 Document document = null; 230 Node node; 231 Element documentElement; 232 233 JarFile jf = null; 235 try { 236 jf = new JarFile (nbmFile); 237 ZipEntry info = getLocalizedInfo( jf ); if ( info == null ) { 239 DialogDisplayer.getDefault().notify( 240 new NotifyDescriptor.Message( 241 NbBundle.getMessage( ModuleUpdate.class, "MSG_NoInfoXml", 242 nbmFile.getName() 243 ), 244 NotifyDescriptor.ERROR_MESSAGE 245 ) 246 ); 247 return null; 248 } 249 InputStream is = jf.getInputStream( info ); 250 251 InputSource xmlInputSource = new InputSource ( is ); 252 document = org.openide.xml.XMLUtil.parse( xmlInputSource, false, false, 253 new ErrorCatcher(), org.netbeans.updater.XMLUtil.createAUResolver() ); 254 255 documentElement = document.getDocumentElement(); 256 node = documentElement; 257 } catch ( org.xml.sax.SAXException e ) { 258 Exceptions.attachMessage(e, "Bad info : " + nbmFile.getName()); 259 Exceptions.attachLocalizedMessage(e, getMessage("ERR_Bad_Info", 260 nbmFile.getName())); 261 LOG.log(Level.WARNING, null, e); 262 return null; 263 } catch ( ZipException ze ) { 264 Exceptions.attachMessage(ze, "Corrupted nbm file : " + 265 nbmFile.getName()); 266 Exceptions.attachLocalizedMessage(ze, getMessage("ERR_Corrupted_Zip", 267 nbmFile.getName())); 268 LOG.log(Level.WARNING, null, ze); 269 return null; 270 } catch (UnknownHostException e) { 271 String message = "Cannot access required resource (see the message log for more details). " + "Module " + nbmFile.getName() + " will be ommited in the install list."; Exceptions.attachMessage(e, message); 274 Exceptions.attachLocalizedMessage(e, getMessage("ERR_Unavailability_Resource", 275 nbmFile.getName())); 276 LOG.log(Level.WARNING, null, e); 277 return null; 278 } catch ( IOException e ) { 279 Exceptions.attachMessage(e, "Missing info : " + 280 nbmFile.getName()); 281 Exceptions.attachLocalizedMessage(e, getMessage("ERR_Missing_Info", 282 nbmFile.getName())); 283 LOG.log(Level.WARNING, null, e); 284 return null; 285 } finally { 286 try { 287 if ( jf != null ) 288 jf.close(); 289 } catch ( IOException ie ) { 290 } 291 } 292 293 NodeList nodeList = ((Element )node).getElementsByTagName( ELEMENT_L10N ); if ( nodeList != null && nodeList.getLength() > 0 ) 295 mod = new L10NUpdate( nbmFile, node, documentElement ); 296 else 297 mod = new ModuleUpdate( nbmFile, node, documentElement ); 298 299 if ( mod.createFromDistribution() ) 300 return mod; 301 else 302 return null; 303 } 304 305 307 311 boolean readModuleUpdate(AutoupdateType at) { 312 313 315 try { 316 String textURL = getAttribute( ATTR_HOMEPAGE ); 317 if ( textURL != null ) 318 homepage = new URL ( xmlURL, textURL ); 319 } 320 catch ( java.net.MalformedURLException e ) { 321 LOG.log(Level.WARNING, null, e); 322 } 324 325 326 327 setInfoCodenamebase( getAttribute( ATTR_CODENAMEBASE ) ); 328 329 try { 330 setDownloadSize( Long.parseLong( getAttribute( ATTR_DOWNLOAD_SIZE ) ) ); 331 } 332 catch ( NumberFormatException e ) { 333 } 335 336 try { 337 setUnpackedSize( Long.parseLong( getAttribute( ATTR_UNPACKED_SIZE ) ) ); 338 } 339 catch ( NumberFormatException e ) { 340 } 342 343 setDescription( getTextOfElement( ELEMENT_DESCRIPTION ) ); 344 345 setNotification( getTextOfElement( ELEMENT_NOTIFICATION ) ); 346 347 List ext = externalFromXML( ); 348 if ( ext != null ) 349 externals = ext; 350 351 try { 352 String textURL = getAttribute( ATTR_DISTRIBUTION ); 353 if ( textURL != null ) { 354 String sURL = xmlURL.toString(); 355 int qmark = sURL.indexOf('?'); 356 if ((qmark > -1) && (qmark < sURL.lastIndexOf('/'))) 357 distribution = new URL ( new URL (sURL.substring(0, qmark)), textURL ); 359 else 360 distribution = new URL ( xmlURL, textURL ); 361 if ( at instanceof XMLAutoupdateType ) 362 distribution = ((XMLAutoupdateType)at).modifyURL (distribution); 363 } 364 } 365 catch ( java.net.MalformedURLException e ) { 366 LOG.log(Level.WARNING, null, e); 367 } 369 370 purchased = Boolean.valueOf( getAttribute( ATTR_PURCHASED ) ).booleanValue(); 371 372 processNeedsRestart(); 373 processIsGlobal (); 374 processTargetCluster (); 375 376 readReleaseDate (); 377 readModuleAuthor (); 378 379 try { 380 remoteModule = readRemoteInfo(); 381 } 382 catch ( IllegalArgumentException e ) { 383 LOG.log(Level.WARNING, null, e); 384 } 385 386 licenceID = getAttribute( ATTR_LICENSE ); 388 if (licenceID == null) licenceID = getAttribute( ATTR_LICENSE_MISSPELLED ); 389 licenceText = licenseFromXML( licenceID, documentElement ); 390 391 localModule = readLocalInfo(); 393 394 return remoteModule != null; 395 } 396 397 ModuleInfo readRemoteInfo() throws IllegalArgumentException { 398 Manifest mf = manifestFromXML( ); 401 402 if (description == null || description.equals ("")) { String longDesc = mf.getMainAttributes ().getValue ("OpenIDE-Module-Long-Description"); if (longDesc != null) { 406 description = longDesc; 407 } 408 } 409 410 return new DummyModuleInfo( mf.getMainAttributes() ); 411 } 412 413 ModuleInfo readLocalInfo() { 414 ModuleInfo localinfo = null; 415 416 ModuleInfo[] installedModules = Updates.getInstalledModules(); 417 ModuleInfo[] installedPatches = Updates.getInstalledPatches(); 418 419 if ( remoteModule != null ) { 420 421 if ( remoteModule.getCodeName().equals( IdeDescription.getName() ) ) { 423 localinfo = IdeDescription.getIdeDescription(); 424 } 425 426 for ( int i = 0; localModule == null && i < installedModules.length; i++ ) { 428 if ( installedModules[i].getCodeNameBase().equals( remoteModule.getCodeNameBase() ) ) { 429 localinfo = installedModules[i]; 430 break; 431 } 432 } 433 434 for ( int i = 0; localModule == null && i < installedPatches.length; i++ ) { 436 if ( installedPatches[i].getCodeNameBase().equals( remoteModule.getCodeNameBase() ) ) { 437 localinfo = installedPatches[i]; 438 break; 439 } 440 } 441 } 442 return localinfo; 443 } 444 445 private static ZipEntry getLocalizedInfo(JarFile jf) { 446 String locale = Locale.getDefault().getLanguage(); 447 ZipEntry info = jf.getEntry("Info/locale/info_" + locale + ".xml"); if ( info == null ) { 449 info = jf.getEntry("Info/info.xml"); } 451 return info; 452 } 453 454 455 private boolean createFromDistribution() { 456 457 Document document = null; 458 459 JarFile jf = null; 461 try { 462 jf = new JarFile (nbmFile); 463 ZipEntry info = getLocalizedInfo( jf ); if ( info == null ) { 465 DialogDisplayer.getDefault().notify( 466 new NotifyDescriptor.Message( 467 NbBundle.getMessage( ModuleUpdate.class, "MSG_NoInfoXml", 468 nbmFile.getName() 469 ), 470 NotifyDescriptor.ERROR_MESSAGE 471 ) 472 ); 473 return false; 474 } 475 InputStream is = jf.getInputStream( info ); 476 477 InputSource xmlInputSource = new InputSource ( is ); 478 document = org.openide.xml.XMLUtil.parse( xmlInputSource, false, false, 479 new ErrorCatcher(), org.netbeans.updater.XMLUtil.createAUResolver() ); 480 481 documentElement = document.getDocumentElement(); 482 node = documentElement; 483 } 484 catch ( org.xml.sax.SAXException e ) { 485 Exceptions.attachMessage(e, "Bad info : " + nbmFile.getName()); LOG.log(Level.WARNING, null, e); 487 return false; 488 } 489 catch ( IOException e ) { 490 Exceptions.attachMessage(e, "Missing info : " + nbmFile.getName()); LOG.log(Level.WARNING, null, e); 492 return false; 493 } 494 finally { 495 try { 496 if ( jf != null ) 497 jf.close(); 498 } catch ( IOException ie ) { 499 } 500 } 501 502 504 try { 505 String textURL = getAttribute( ATTR_HOMEPAGE ); 506 if ( textURL != null && textURL.length () > 0 ) { 507 homepage = new URL ( textURL ); 508 } 509 } 510 catch ( java.net.MalformedURLException e ) { 511 LOG.log(Level.WARNING, null, e); 512 } 514 515 setInfoCodenamebase( getAttribute( ATTR_CODENAMEBASE ) ); 516 517 setDownloadSize( nbmFile.length() ); 518 519 520 535 536 setDescription( getTextOfElement( ELEMENT_DESCRIPTION ) ); 537 538 setNotification( getTextOfElement( ELEMENT_NOTIFICATION ) ); 539 540 List ext = externalFromXML( ); 541 if ( ext != null ) 542 externals = ext; 543 544 purchased = Boolean.valueOf( getAttribute( ATTR_PURCHASED ) ).booleanValue(); 545 546 processNeedsRestart(); 547 processIsGlobal (); 548 processTargetCluster (); 549 550 readReleaseDate (); 551 readModuleAuthor (); 552 553 try { 554 remoteModule = readRemoteInfo(); 555 } 556 catch ( IllegalArgumentException e ) { 557 LOG.log(Level.WARNING, null, e); 558 } 559 560 licenceID = getAttribute( ATTR_LICENSE ); 562 if (licenceID == null) licenceID = getAttribute( ATTR_LICENSE_MISSPELLED ); 563 licenceText = licenseFromXML( licenceID, documentElement ); 564 565 566 localModule = readLocalInfo(); 568 569 return remoteModule != null; 570 571 } 572 573 574 void resolveInstalledModule( Collection installedModules ) { 575 576 } 577 578 private void readReleaseDate () { 579 String attr = getAttribute (ATTR_RELEASE_DATE); 580 581 if (attr != null) { 582 try { 583 setReleaseDate (DATE_FORMAT.parse (attr)); 584 } catch (ParseException pe) { 585 LOG.log(Level.WARNING, null, pe); 587 } 588 } 589 } 590 591 private void readModuleAuthor () { 592 setModuleAuthor (getAttribute (ATTR_AUTHOR)); 593 } 594 595 private void processNeedsRestart() { 596 String attr = getAttribute( ATTR_NEEDSRESTART ); 597 if ( attr != null && Boolean.FALSE.toString().equals( attr.toLowerCase() ) ) 598 setSafeToInstall( true ); 599 } 600 601 private void processIsGlobal () { 602 String attr = getAttribute( ATTR_IS_GLOBAL ); 603 if (attr != null) { 604 if (Boolean.TRUE.toString ().equals (attr.toLowerCase ())) { 605 isGlobal = Boolean.TRUE; 606 } else if (Boolean.FALSE.toString ().equals (attr.toLowerCase ())) { 607 isGlobal = Boolean.FALSE; 608 } 609 } 610 } 611 612 private void processTargetCluster () { 613 String attr = getAttribute (ATTR_TARGET_CLUSTER); 614 if (attr != null) { 615 Pattern p = Pattern.compile (attr); 616 Iterator en = UpdateTracking.clusters (false).iterator (); 617 while (en.hasNext ()) { 618 File f = (File ) en.next (); 619 if (p.matcher (f.getName ()).matches ()) { 620 targetCluster = f.getName (); 621 return; 622 } 623 } 624 625 for ( 626 Iterator it = Lookup.getDefault().lookupAll(AutoupdateClusterCreator.class).iterator(); 627 it.hasNext() && targetCluster == null; 628 ) { 629 AutoupdateClusterCreator creator = (AutoupdateClusterCreator)it.next(); 630 File f = creator.findCluster(attr); 631 if (f != null) { 632 targetCluster = f.getName(); 633 return; 634 } 635 } 636 637 } 638 } 639 640 642 645 String getCodeNameBase() { 646 return remoteModule.getCodeNameBase(); 647 } 648 649 652 String getName() { 653 return remoteModule.getDisplayName(); 654 } 655 656 659 URL getDistribution() { 660 return distribution; 661 } 662 663 void setRemoteDistributionFilename(URLConnection distrConnection) { 664 if ( distributionFilename == null ) { 665 String s = distrConnection.getURL().getFile(); 666 int i = s.indexOf('?'); 667 if ( i > 0 ) 668 s = s.substring(0,i); 669 i = s.lastIndexOf('/'); 670 if ( (i > 0) && (i < s.length()-1) ) 671 s = s.substring(i+1); 672 distributionFilename = s; 673 } 674 } 675 676 679 String getDistributionFilename() { 680 if ( distributionFilename == null ) { 681 if ( nbmFile != null ) { 682 distributionFilename = nbmFile.getName(); 683 } 684 else { 685 try { 686 URLConnection distrConnection = getDistribution().openConnection(); 687 setRemoteDistributionFilename(distrConnection); 688 } catch (IOException e) { 689 distributionFilename = new File ( getDistribution().getFile() ).getName(); 690 } 691 } 692 } 693 return distributionFilename; 694 } 695 696 699 String getLicenceID() { 700 return licenceID; 701 } 702 703 706 public String getInfoCodenamebase() { 707 return infoCodenamebase; 708 } 709 710 713 void setInfoCodenamebase(String infoCodenamebase) { 714 this.infoCodenamebase = infoCodenamebase; 715 } 716 717 720 long getDownloadSize() { 721 return downloadSize; 722 } 723 724 727 void setDownloadSize(long downloadSize) { 728 this.downloadSize = downloadSize; 729 } 730 731 734 long getUnpackedSize() { 735 return unpackedSize; 736 } 737 738 741 void setUnpackedSize(long unpackedSize) { 742 this.unpackedSize = unpackedSize; 743 } 744 745 748 String getDescription() { 749 return description; 750 } 751 752 755 void setDescription(String description) { 756 this.description = description; 757 } 758 759 762 String getModuleAuthor () { 763 return moduleAuthor; 764 } 765 766 769 void setModuleAuthor (String author) { 770 moduleAuthor = author; 771 } 772 773 776 Date getReleaseDate () { 777 return releaseDate; 778 } 779 780 783 void setReleaseDate (Date date) { 784 releaseDate = date; 785 } 786 787 790 String getNotification() { 791 return notification; 792 } 793 794 797 void setNotification(String notification) { 798 this.notification = notification; 799 } 800 801 804 boolean getNotificationAccepted() { 805 return notificationAccepted; 806 } 807 808 811 void setNotificationAccepted(boolean notificationAccepted) { 812 this.notificationAccepted = notificationAccepted; 813 } 814 815 818 boolean isSelected() { 819 return selected; 820 } 821 822 825 void setSelected( boolean selected ) { 826 this.selected = selected; 827 } 828 829 830 833 boolean isNew() { 834 return localModule == null; 835 } 836 837 840 boolean isPurchased() { 841 return purchased; 842 } 843 844 847 URL getHomePage() { 848 return homepage; 849 } 850 851 854 String getLicenceText() { 855 return licenceText; 856 } 857 858 861 ModuleInfo getRemoteModule() { 862 return remoteModule; 863 } 864 865 868 ModuleInfo getLocalModule() { 869 return localModule; 870 } 871 872 Node getNode() { 873 return node; 874 } 875 876 877 boolean isUpdateAvailable() { 878 if ( getLocalModule() == null ) 879 return true; 880 881 if ( getRemoteModule().getCodeNameRelease() > getLocalModule().getCodeNameRelease() ) { 882 return true; 883 } 884 885 if (getLocalModule().getSpecificationVersion() == null) { 886 return true; 887 } 888 if (getRemoteModule().getSpecificationVersion() == null) { 889 return false; 890 } 891 if (getLocalModule().getSpecificationVersion().compareTo(getRemoteModule().getSpecificationVersion()) < 0) { 892 return true; 893 } 894 895 return false; 896 897 } 898 899 901 boolean isNotChecked() { 902 return security == SignVerifier.NOT_CHECKED; 903 } 904 905 908 boolean isDownloadOK() { 909 return downloadOK; 910 } 911 912 915 void setDownloadOK( boolean downloadOK ) { 916 this.downloadOK = downloadOK; 917 } 918 919 920 923 int getSecurity() { 924 return security; 925 } 926 927 930 void setSecurity( int security ) { 931 this.security = security; 932 } 933 934 937 Collection getCerts() { 938 return certs; 939 } 940 941 944 void setCerts( Collection certs ) { 945 this.certs = certs; 946 } 947 948 949 952 File getDistributionFile() { 953 return nbmFile; 954 } 955 956 959 boolean isInstallApproved() { 960 return installApproved; 961 } 962 963 966 void setInstallApproved( boolean installApproved ) { 967 this.installApproved = installApproved; 968 } 969 970 972 975 private String getAttribute(String attribute) { 976 Node attr = node.getAttributes().getNamedItem( attribute ); 977 return attr == null ? null : attr.getNodeValue(); 978 } 979 980 984 private String getTextOfElement( String name ) { 985 986 if ( node.getNodeType() != Node.ELEMENT_NODE || 987 !( node instanceof Element ) ) { 988 return null; 989 } 990 991 NodeList nodeList = ((Element )node).getElementsByTagName( name ); 992 StringBuffer sb = new StringBuffer (); 993 994 for( int i = 0; i < nodeList.getLength(); i++ ) { 995 996 if ( nodeList.item( i ).getNodeType() != Node.ELEMENT_NODE || 997 !( nodeList.item( i ) instanceof Element ) ) { 998 break; 999 } 1000 1001 NodeList innerList = nodeList.item( i ).getChildNodes(); 1003 1004 for( int j = 0; j < innerList.getLength(); j++ ) { 1005 short type = innerList.item( j ).getNodeType(); 1006 if ( type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE ) { 1007 sb.append( innerList.item( j ).getNodeValue() ); 1008 } 1009 } 1010 } 1011 return sb.toString(); 1012 } 1013 1014 1016 private List externalFromXML() { 1017 if ( node.getNodeType() != Node.ELEMENT_NODE || 1018 !( node instanceof Element ) ) { 1019 return null; 1020 } 1021 List ext_list = null; 1022 1023 NodeList nodeList = ((Element )node).getElementsByTagName( ELEMENT_EXTERNAL ); 1025 for( int i = 0; i < nodeList.getLength(); i++ ) { 1026 1027 if ( i==0 ) 1028 ext_list = new ArrayList (); 1029 1030 if ( nodeList.item( i ).getNodeType() != Node.ELEMENT_NODE || 1031 !( nodeList.item( i ) instanceof Element ) ) { 1032 break; 1033 } 1034 1035 NamedNodeMap attrList = nodeList.item( i ).getAttributes(); 1037 External ext = new External(); 1038 1039 for( int j = 0; j < attrList.getLength(); j++ ) { 1040 Attr attr = (Attr ) attrList.item( j ); 1041 if ( attr.getName().equals( ATTR_EXT_NAME )) 1042 ext.setName( attr.getValue() ); 1043 else if ( attr.getName().equals( ATTR_EXT_TARGET )) 1044 ext.setTarget_name( attr.getValue() ); 1045 else if ( attr.getName().equals( ATTR_EXT_URL )) 1046 ext.setStart_url( attr.getValue() ); 1047 else if ( attr.getName().equals( ATTR_EXT_DESC )) 1048 ext.setDescription( attr.getValue() ); 1049 } 1050 1051 ext_list.add( ext ); 1052 } 1053 return ext_list; 1054 } 1055 1056 1058 private Manifest manifestFromXML() { 1059 if ( node.getNodeType() != Node.ELEMENT_NODE || 1060 !( node instanceof Element ) ) { 1061 return null; 1062 } 1063 1064 NodeList nodeList = ((Element )node).getElementsByTagName( "manifest" ); 1066 for( int i = 0; i < nodeList.getLength(); i++ ) { 1067 1068 if ( nodeList.item( i ).getNodeType() != Node.ELEMENT_NODE || 1069 !( nodeList.item( i ) instanceof Element ) ) { 1070 break; 1071 } 1072 1073 NamedNodeMap attrList = nodeList.item( i ).getAttributes(); 1075 Manifest mf = new Manifest (); 1076 Attributes mfAttrs = mf.getMainAttributes(); 1077 1078 1079 for( int j = 0; j < attrList.getLength(); j++ ) { 1080 Attr attr = (Attr ) attrList.item( j ); 1081 mfAttrs.put( new Attributes.Name ( attr.getName() ), attr.getValue() ); 1082 } 1083 1084 return mf; 1085 } 1086 return null; 1087 } 1088 1089 1091 private String licenseFromXML( String name, Element docElement ) { 1092 NodeList nodeList = docElement.getElementsByTagName( "license" ); 1094 for( int i = 0; i < nodeList.getLength(); i++ ) { 1095 1096 Node nameAttr = nodeList.item(i).getAttributes().getNamedItem( "name" ); 1098 if ( nameAttr == null ) 1099 continue; 1100 1101 if ( nameAttr.getNodeValue().equals( name ) ) { StringBuffer sb = new StringBuffer (); 1103 1104 NodeList innerList = nodeList.item( i ).getChildNodes(); 1106 1107 for( int j = 0; j < innerList.getLength(); j++ ) { 1108 short type = innerList.item( j ).getNodeType(); 1109 if ( type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE ) { 1110 sb.append( innerList.item( j ).getNodeValue() ); 1111 } 1112 } 1113 return sb.toString(); 1114 } 1115 } 1116 1117 if (name != null) { 1119 Logger.getAnonymousLogger().log(Level.WARNING, 1120 "[AutoUpdate] warning: no license found with name " + 1121 name); 1122 } 1123 return null; 1124 } 1125 1126 1129 List getExternals() { 1130 return externals; 1131 } 1132 1133 1136 public boolean isForcedGlobal () { 1137 return depending || ! isAbleToInstallToUserDir (); 1138 } 1139 1140 1143 public void setDepending(boolean depending) { 1144 this.depending = depending; 1145 } 1146 1147 1150 public boolean isToInstallDir() { 1151 if (isForcedGlobal ()) { 1152 return true; 1153 } else { 1154 1155 if (isGlobal () != null) { 1157 return isGlobal ().booleanValue (); 1158 } else { 1159 return this.toInstallDir; 1160 } 1161 1162 } 1163 } 1164 1165 1168 public void setToInstallDir(boolean toInstallDir) throws IllegalArgumentException { 1169 if (isGlobal () != null && toInstallDir != isGlobal ().booleanValue ()) { 1170 throw new IllegalArgumentException ("Cannot setToInstallDir (" + toInstallDir + ") when isGlobal is set to " + isGlobal ()); } else if (isForcedGlobal () && ! toInstallDir) { 1172 throw new IllegalArgumentException ("Cannot setToInstallDir (" + toInstallDir + ") when isForderGlobal is " + isForcedGlobal ()); } 1174 1175 this.toInstallDir = toInstallDir; 1176 } 1177 1178 1181 public boolean isDownloadStarted() { 1182 return this.downloadStarted; 1183 } 1184 1185 1188 public void setDownloadStarted(boolean downloadStarted) { 1189 this.downloadStarted = downloadStarted; 1190 } 1191 1192 1195 boolean isSafeToInstall() { 1196 return safeToInstall && isNew() && !isForcedGlobal () ; 1197 } 1198 1199 1202 void setSafeToInstall(boolean safeToInstall) { 1203 this.safeToInstall = safeToInstall; 1204 } 1205 1206 1209 Boolean isGlobal () { 1210 return isGlobal; 1211 } 1212 1213 1216 String getTargetCluster () { 1217 return targetCluster; 1218 } 1219 1220 1223 List getJarList() { 1224 return this.jarList; 1225 } 1226 1227 1230 void setJarList(List jarList) { 1231 this.jarList = jarList; 1232 } 1233 1234 void addToJarList(String jarName) { 1235 jarList.add( jarName ); 1236 } 1237 1238 1248 final File findInstallDirectory () { 1249 if (target == null) { 1250 1251 File firstInstallable = null; 1252 File declaredTarget = null; 1253 File updateTarget = null; 1254 1255 java.util.Iterator en = org.netbeans.updater.UpdateTracking.clusters (false).iterator (); 1256 while (en.hasNext ()) { 1257 File f = (File )en.next (); 1258 1259 if (firstInstallable == null) { 1261 1262 if (! f.isDirectory () || UpdateTracking.getTracking (f, false) != null) { 1264 firstInstallable = f; 1265 } 1266 1267 } 1268 1269 if (f.getName ().equals (getTargetCluster ())) { 1270 declaredTarget = f; 1271 } 1272 1273 UpdateTracking ut = org.netbeans.updater.UpdateTracking.getTracking (f, false); 1274 if (ut != null && ut.isModuleInstalled (this.getInfoCodenamebase ())) { 1275 assert declaredTarget == null || f.equals (declaredTarget) : "Update's target cluster must be placed into as same cluster as original module."; 1278 updateTarget = f; 1279 break; 1280 } 1281 } 1282 if (updateTarget != null) { 1283 target = updateTarget; 1284 } else { 1285 if (declaredTarget != null) { 1286 target = declaredTarget; 1287 } 1288 if (getTargetCluster() != null) { 1289 for ( 1290 Iterator it = Lookup.getDefault().lookupAll(AutoupdateClusterCreator.class).iterator(); 1291 it.hasNext() && target == null; 1292 ) { 1293 AutoupdateClusterCreator creator = (AutoupdateClusterCreator)it.next(); 1294 target = creator.findCluster(getTargetCluster()); 1295 } 1296 } 1297 if (target == null) { 1298 target = firstInstallable; 1299 } 1300 if (target != null && ! target.exists ()) { 1301 new File (target, UpdateTracking.TRACKING_FILE_NAME).mkdirs (); 1302 } 1303 } 1304 } 1305 1306 return target; 1307 } 1308 1309 1313 final boolean isAbleToInstallToUserDir () { 1314 HashSet files = new HashSet (); 1315 if (nbmFile == null) { 1316 files.addAll (jarList); 1317 } else { 1318 try { 1319 JarFile f = new JarFile (nbmFile); 1320 Enumeration en = f.entries (); 1321 while (en.hasMoreElements ()) { 1322 JarEntry e = (JarEntry )en.nextElement (); 1323 String s = e.getName (); 1324 if (s.startsWith ("netbeans/")) { 1325 s = s.substring (9); 1326 } 1327 files.add (s); 1328 } 1329 } catch (IOException ex) { 1330 assert false : "IOException caught: " + ex.getMessage (); 1331 return false; 1332 } 1333 } 1334 1335 Iterator en = org.netbeans.updater.UpdateTracking.clusters (false).iterator (); 1336 while (en.hasNext ()) { 1337 File f = (File )en.next (); 1338 1339 Iterator it = files.iterator (); 1340 while (it.hasNext ()) { 1341 String entry = (String )it.next (); 1342 if (entry.startsWith(NBM_CORE + "/") || entry.startsWith(NBM_LIB + "/")) { 1343 File my = new File (f, entry); 1344 if (! my.isDirectory () && my.exists ()) { 1345 return false; 1346 } 1347 } 1348 } 1349 } 1350 return true; 1351 } 1352 1353 1355 1383 static class ErrorCatcher implements org.xml.sax.ErrorHandler { 1384 private void message (String level, org.xml.sax.SAXParseException e) { 1385 } 1386 1387 public void error (org.xml.sax.SAXParseException e) { 1388 } 1389 1390 public void warning (org.xml.sax.SAXParseException e) { 1391 LOG.log(Level.WARNING, null, e); 1392 } 1393 1394 public void fatalError (org.xml.sax.SAXParseException e) { 1395 } 1396 } 1398 class External extends Object { 1399 1400 1401 private String name; 1402 1403 1404 private String target_name; 1405 1406 1407 private String start_url; 1408 1409 1410 private String description; 1411 1412 1415 public String getName() { 1416 return name; 1417 } 1418 1419 1422 public void setName(String name) { 1423 this.name = name; 1424 } 1425 1426 1429 public String getTarget_name() { 1430 return target_name; 1431 } 1432 1433 1436 public void setTarget_name(String target_name) { 1437 this.target_name = target_name; 1438 } 1439 1440 1443 public String getStart_url() { 1444 return start_url; 1445 } 1446 1447 1450 public void setStart_url(String start_url) { 1451 this.start_url = start_url; 1452 } 1453 1454 1457 public String getDescription() { 1458 return description; 1459 } 1460 1461 1464 public void setDescription(String description) { 1465 this.description = description; 1466 } 1467 1468 } 1469} 1470 | Popular Tags |