1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
|
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2012 Oracle and/or its affiliates. All rights reserved.
*
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
package org.netbeans.updater;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.zip.CRC32;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/** This class represents module updates tracking
*
* @author Ales Kemr
*/
public final class UpdateTracking {
/** Platform dependent file name separator */
public static final String FILE_SEPARATOR = System.getProperty ("file.separator");
public static final String PATH_SEPARATOR = System.getProperty ("path.separator");
public static final String ELEMENT_MODULES = "installed_modules"; // NOI18N
public static final String ELEMENT_MODULE = "module"; // NOI18N
public static final String ATTR_CODENAMEBASE = "codename"; // NOI18N
public static final String ELEMENT_VERSION = "module_version"; // NOI18N
public static final String ATTR_VERSION = "specification_version"; // NOI18N
public static final String ATTR_LAST = "last"; // NOI18N
public static final String ATTR_INSTALL = "install_time"; // NOI18N
public static final String ELEMENT_FILE = "file"; // NOI18N
public static final String ATTR_FILE_NAME = "name"; // NOI18N
public static final String ATTR_ORIGIN = "origin"; // NOI18N
public static final String UPDATER_ORIGIN = "updater"; // NOI18N
public static final String INSTALLER_ORIGIN = "installer"; // NOI18N
private static final String ATTR_CRC = "crc"; // NOI18N
private static final String NBM_ORIGIN = "nbm"; // NOI18N
public static final String ELEMENT_ADDITIONAL = "module_additional"; // NOI18N
public static final String ELEMENT_ADDITIONAL_MODULE = "module"; // NOI18N
public static final String ATTR_ADDITIONAL_NBM_NAME = "nbm_name"; // NOI18N
public static final String ATTR_ADDITIONAL_SOURCE = "source-display-name"; // NOI18N
public static final String EXTRA_CLUSTER_NAME = "extra";
private static final String LOCALE_DIR = FILE_SEPARATOR + "locale" + FILE_SEPARATOR; // NOI18N
public static final String TRACKING_FILE_NAME = "update_tracking"; // NOI18N
public static final String ADDITIONAL_INFO_FILE_NAME = "additional_information.xml"; // NOI18N
private static final String XML_EXT = ".xml"; // NOI18N
private static final String FORBID_AUTOUPDATE = ".noautoupdate"; // NOI18N
/** maps root of clusters to tracking files. (File -> UpdateTracking) */
private static final Map<File, UpdateTracking> trackings = new HashMap<File, UpdateTracking> ();
private static final Map<File, UpdateTracking.AdditionalInfo> infos = new HashMap<File, UpdateTracking.AdditionalInfo> ();
/** Mapping from files defining modules to appropriate modules objects.
*/
private LinkedHashMap<File, Module> installedModules = new LinkedHashMap<File, Module> ();
private final File directory;
private final File trackingFile;
private String origin = NBM_ORIGIN;
private final UpdatingContext context;
/** Private constructor.
*/
private UpdateTracking( File nbPath, UpdatingContext context ) {
assert nbPath != null : "Path cannot be null";
trackingFile = new File( nbPath + FILE_SEPARATOR + TRACKING_FILE_NAME);
directory = nbPath;
origin = UPDATER_ORIGIN;
this.context = context;
}
//
// Various factory and utility methods
//
/** Finds update tracking for given cluster root.
* @path root of a cluster
* @param createIfDoesNotExists should new tracking be created if it does not exists
* @return the tracking for that cluster
*/
static UpdateTracking getTracking (File path, boolean createIfDoesNotExists, UpdatingContext context) {
synchronized (trackings) {
UpdateTracking track = trackings.get (path);
if (track == null) {
File utFile = new File (path, TRACKING_FILE_NAME);
if (!createIfDoesNotExists && !utFile.isDirectory ()) {
// if the update_tracking directory is missing
// do not allow creation at all (only in userdir)
return null;
}
File noAU = new File(path, FORBID_AUTOUPDATE); // NOI18N
if (noAU.exists()) {
// ok, this prevents autoupdate from accessing this
// directory completely
return null;
}
track = new UpdateTracking (path, context);
trackings.put (path, track);
track.read ();
track.scanDir ();
}
return track;
}
}
/** Finds update tracking for given cluster root.
* @path root of a cluster
* @return the additional information for that cluster
*/
static UpdateTracking.AdditionalInfo getAdditionalInformation (File path, UpdatingContext context) {
synchronized (infos) {
UpdateTracking.AdditionalInfo additionalInfo = infos.get (path);
if (additionalInfo == null) {
getTracking (path, false, context);
File downloadDir = new File (path, ModuleUpdater.DOWNLOAD_DIR);
if (downloadDir.exists () && downloadDir.isDirectory ()) {
File addInfo = new File (downloadDir, ADDITIONAL_INFO_FILE_NAME);
if (addInfo.exists ()) {
additionalInfo = new UpdateTracking.AdditionalInfo (addInfo);
}
}
}
return additionalInfo;
}
}
/** Returns the platform installatiion directory.
* @return the File directory.
*/
public static File getPlatformDir () {
String platform = System.getProperty ("netbeans.home");
return platform == null ? null : new File (platform); // NOI18N
}
public static File getUserDir () {
// bugfix #50242: the property "netbeans.user" can return dir with non-normalized file e.g. duplicate //
// and path and value of this property wrongly differs
String user = System.getProperty ("netbeans.user");
File userDir = null;
if (user != null) {
// XXX cannot use FileUtil.normalizeFile from here
userDir = new File (user);
if (userDir.getPath ().startsWith ("\\\\")) {
// Could use URI.normalize but only on userDir.getPath().toUri() in JDK 7 (#4723726 breaks UNC for userDir.toURI())
try {
userDir = userDir.getCanonicalFile ();
} catch (IOException ex) {
// fallback when getCanonicalFile fails
userDir = userDir.getAbsoluteFile ();
}
} else {
userDir = new File (userDir.toURI ().normalize ()).getAbsoluteFile ();
}
}
return userDir;
}
/** Returns enumeration of Files that represent each possible install
* directory.
* @param includeUserDir whether to include also user dir
* @return List<File>
*/
public static List<File> clusters (boolean includeUserDir) {
List<File> files = new ArrayList<File> ();
if (includeUserDir) {
File ud = getUserDir ();
if (ud != null) {
// this prevents autoupdate from accessing this
// directory completely
File noAU = new File (ud, FORBID_AUTOUPDATE); // NOI18N
if (! noAU.exists ()) {
files.add (ud);
}
}
}
String dirs = System.getProperty("netbeans.dirs"); // NOI18N
if (dirs != null) {
Enumeration en = new StringTokenizer (dirs, File.pathSeparator);
while (en.hasMoreElements ()) {
File f = new File ((String)en.nextElement ());
// this prevents autoupdate from accessing this
// directory completely
File noAU = new File (f, FORBID_AUTOUPDATE); // NOI18N
if (! noAU.exists ()) {
files.add (f);
}
}
}
File id = getPlatformDir ();
if (id != null) {
// this prevents autoupdate from accessing this
// directory completely
File noAU = new File (id, FORBID_AUTOUPDATE); // NOI18N
if (! noAU.exists ()) {
files.add (id);
}
}
return java.util.Collections.unmodifiableList (files);
}
//
// Useful search methods
//
/** Returns true if module with given code base is installed here
* @param codeBase name of the module
* @return true or false
*/
public boolean isModuleInstalled (String codeBase) {
for (Module m: installedModules.values ()) {
String mm = m.codenamebase;
int indx = mm.indexOf ('/');
if (indx >= 0) {
mm = mm.substring (0, indx);
}
if (codeBase.equals (mm)) {
return true;
}
}
return false;
}
//
// Private impls
//
private static ErrorHandler DUMMY_ERROR_HANDLER = new ErrorHandler() {
@Override
public void warning(SAXParseException exception) throws SAXException {
}
@Override
public void error(SAXParseException exception) throws SAXException {
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
}
};
/** Scan through org.w3c.dom.Document document. */
private void read() {
/** org.w3c.dom.Document document */
org.w3c.dom.Document document;
File file;
InputStream is;
int avail = 0;
try {
file = trackingFile;
if ( ! file.isFile () ) {
return;
}
is = new FileInputStream( file );
avail = is.available();
InputSource xmlInputSource = new InputSource( is );
document = XMLUtil.parse(xmlInputSource, false, false, DUMMY_ERROR_HANDLER, XMLUtil.createAUResolver());
if (is != null) {
is.close();
}
}
catch ( org.xml.sax.SAXException e ) {
XMLUtil.LOG.log(Level.SEVERE, "Bad update_tracking: " + trackingFile + ", available bytes: " + avail, e); // NOI18N
return;
}
catch ( java.io.IOException e ) {
XMLUtil.LOG.log(Level.SEVERE, "Missing update_tracking: " + trackingFile + ", available bytes: " + avail, e); // NOI18N
return;
}
org.w3c.dom.Element element = document.getDocumentElement();
if ((element != null) && element.getTagName().equals(ELEMENT_MODULES)) {
scanElement_installed_modules(element);
}
}
/** Scan through org.w3c.dom.Element named installed_modules. */
void scanElement_installed_modules(org.w3c.dom.Element element) { // <installed_modules>
// element.getValue();
org.w3c.dom.NodeList nodes = element.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
org.w3c.dom.Node node = nodes.item(i);
if ( node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE ) {
org.w3c.dom.Element nodeElement = (org.w3c.dom.Element)node;
if (nodeElement.getTagName().equals(ELEMENT_MODULE)) {
if (true) {
throw new IllegalStateException ("What now!?");
}
// XXX - should put the module into installedModules but do not know the key
// modules.add( scanElement_module(nodeElement, fromuser) );
}
}
}
}
/** Scan through org.w3c.dom.Element named module. */
Module scanElement_module(org.w3c.dom.Element element) { // <module>
Module module = new Module ();
org.w3c.dom.NamedNodeMap attrs = element.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
org.w3c.dom.Attr attr = (org.w3c.dom.Attr)attrs.item(i);
if (attr.getName().startsWith(ATTR_CODENAMEBASE)) {
// <module codename="???"> or old version <module codenamebase="???">
module.setCodenamebase( attr.getValue() );
}
}
org.w3c.dom.NodeList nodes = element.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
org.w3c.dom.Node node = nodes.item(i);
if ( node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE ) {
org.w3c.dom.Element nodeElement = (org.w3c.dom.Element)node;
if (nodeElement.getTagName().equals(ELEMENT_VERSION)) {
scanElement_module_version(nodeElement, module);
}
}
}
return module;
}
/** Scan through org.w3c.dom.Element named module_version. */
private void scanElement_module_version(org.w3c.dom.Element element, Module module) { // <module_version>
Version version = new Version(module);
org.w3c.dom.NamedNodeMap attrs = element.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
org.w3c.dom.Attr attr = (org.w3c.dom.Attr)attrs.item(i);
if (attr.getName().equals(ATTR_VERSION)) { // <module_version specification_version="???">
version.setVersion( attr.getValue() );
}
if (attr.getName().equals(ATTR_ORIGIN)) { // <module_version origin="???">
version.setOrigin( attr.getValue() );
}
if (attr.getName().equals(ATTR_LAST)) { // <module_version last="???">
version.setLast( Boolean.valueOf(attr.getValue() ).booleanValue());
}
if (attr.getName().equals(ATTR_INSTALL)) { // <module_version install_time="???">
long li = 0;
try {
li = Long.parseLong( attr.getValue() );
} catch ( NumberFormatException nfe ) {
}
version.setInstall_time( li );
}
}
org.w3c.dom.NodeList nodes = element.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
org.w3c.dom.Node node = nodes.item(i);
if ( node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE ) {
org.w3c.dom.Element nodeElement = (org.w3c.dom.Element)node;
if (nodeElement.getTagName().equals(ELEMENT_FILE)) {
scanElement_file(nodeElement, version);
}
}
}
module.addOldVersion( version );
}
/** Scan through org.w3c.dom.Element named file. */
void scanElement_file(org.w3c.dom.Element element, Version version) { // <file>
ModuleFile file = new ModuleFile();
org.w3c.dom.NamedNodeMap attrs = element.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
org.w3c.dom.Attr attr = (org.w3c.dom.Attr)attrs.item(i);
if (attr.getName().equals(ATTR_FILE_NAME)) { // <file name="???">
file.setName( attr.getValue() );
}
if (attr.getName().equals(ATTR_CRC)) { // <file crc="???">
file.setCrc( attr.getValue() );
}
if (attr.getName().equals(ATTR_VERSION)) {
file.setLocaleversion( attr.getValue() );
}
}
version.addFile (file );
}
Module readModuleTracking (String codename, boolean create ) {
new File(directory, TRACKING_FILE_NAME).mkdirs();
File file = new File (
new File(directory, TRACKING_FILE_NAME),
getTrackingName( codename ) + XML_EXT
);
// fix for #34355
try {
if ( file.exists() && file.length()==0 ) {
file.delete();
}
} catch (Exception e) {
// ignore
}
if ( ! file.exists() ) {
if ( create ) {
return new Module( codename, file);
} else {
return null;
}
}
return readModuleFromFile( file, codename, create );
}
Version createVersion(String specversion) {
Version ver = new Version(null);
ver.setVersion( specversion );
return ver;
}
private Module readModuleFromFile( File file, String codename, boolean create ) {
/** org.w3c.dom.Document document */
org.w3c.dom.Document document;
InputStream is;
try {
is = new FileInputStream( file );
InputSource xmlInputSource = new InputSource( is );
document = XMLUtil.parse(xmlInputSource, false, false, DUMMY_ERROR_HANDLER, XMLUtil.createAUResolver());
if (is != null) {
is.close();
}
} catch ( org.xml.sax.SAXException e ) {
XMLUtil.LOG.log(Level.SEVERE, "Bad update_tracking", e); // NOI18N
return null;
}
catch ( java.io.IOException e ) {
if ( create ) {
return new Module (codename, file);
} else {
return null;
}
}
org.w3c.dom.Element element = document.getDocumentElement();
if ((element != null) && element.getTagName().equals(ELEMENT_MODULE)) {
Module m = scanElement_module (element);
m.setFile( file );
installedModules.put (file, m);
return m;
}
if ( create ) {
return new Module (codename, file);
} else {
return null;
}
}
private static String getTrackingName(String codename) {
String trackingName = codename;
int pos = trackingName.indexOf('/'); // NOI18N
if ( pos > -1 ) {
trackingName = trackingName.substring( 0, pos );
}
return trackingName.replace( '.', '-' ); // NOI18N
}
void deleteUnusedFiles() {
List<Module> newModules = new ArrayList<Module> (installedModules.values ());
for (Module mod: newModules) {
mod.deleteUnusedFiles();
}
scanDir ();
}
public static long getFileCRC(File file) throws IOException {
BufferedInputStream bsrc = null;
CRC32 crc = new CRC32();
try {
bsrc = new BufferedInputStream( new FileInputStream( file ) );
byte[] bytes = new byte[1024];
int i;
while( (i = bsrc.read(bytes)) != -1 ) {
crc.update(bytes, 0, i );
}
}
finally {
if ( bsrc != null ) {
bsrc.close();
}
}
return crc.getValue();
}
private void scanDir () {
File dir = new File (directory, TRACKING_FILE_NAME);
File[] files = dir.listFiles( new FileFilter() {
@Override
public boolean accept( File file ) {
if ( !file.isDirectory() && file.getName().toUpperCase().endsWith(".XML") ) {
return true;
} else {
return false;
}
}
} );
if (files == null) {
return;
}
for ( int i = 0; i < files.length; i++ ) {
if (!installedModules.containsKey (files[i])) {
readModuleFromFile( files[i], null, true );
}
}
}
@Override
public String toString() {
return "UpdateTracing[" + this.directory + ", origin: " + this.origin + "]";
}
class Module extends Object {
/** Holds value of property codenamebase. */
private String codenamebase;
/** Holds value of property versions. */
private List<Version> versions = new ArrayList<Version>();
private File file = null;
public Module() {
}
public Module(String codenamebase, File file) {
this.codenamebase = codenamebase;
this.file = file;
}
private Version lastVersion = null;
private Version newVersion = null;
private boolean osgi = false;
/** Getter for property codenamebase.
* @return Value of property codenamebase.
*/
String getCodenamebase() {
return codenamebase;
}
/** Setter for property codenamebase.
* @param codenamebase New value of property codenamebase.
*/
void setCodenamebase(String codenamebase) {
this.codenamebase = codenamebase;
}
void setOSGi(boolean isOSGi) {
this.osgi = isOSGi;
}
boolean isOSGi() {
return osgi;
}
/** Getter for property versions.
* @return Value of property versions.
*/
List<Version> getVersions() {
return versions;
}
/** Setter for property versions.
* @param versions New value of property versions.
*/
void setVersions(List<Version> versions) {
this.versions = versions;
}
private Version getNewOrLastVersion() {
if ( newVersion != null ) {
return newVersion;
} else {
return lastVersion;
}
}
boolean hasNewVersion() {
return newVersion != null;
}
void setFile(File file) {
this.file = file;
}
public Version addNewVersion( String spec_version, String origin ) {
if ( lastVersion != null ) {
lastVersion.setLast ( false );
}
Version version = new Version(this);
newVersion = version;
version.setVersion( spec_version );
version.setOrigin( origin );
version.setLast( true );
version.setInstall_time( System.currentTimeMillis() );
versions.add( version );
return version;
}
void addOldVersion( Version version ) {
if ( version.isLast() ) {
lastVersion = version;
}
versions.add( version );
}
void addL10NVersion( Version l_version ) {
if ( lastVersion != null ) {
lastVersion.addL10NFiles( l_version.getFiles() );
} else {
l_version.setOrigin( origin );
l_version.setLast( true );
l_version.setInstall_time( System.currentTimeMillis() );
versions.add( l_version );
}
}
void writeConfigModuleXMLIfMissing () {
File configDir = new File (new File (directory, ModuleDeactivator.CONFIG), ModuleDeactivator.MODULES); // NOI18N
String candidate = null;
String oldCandidate = null;
String newCandidate = null;
String name = codenamebase;
int indx = name.indexOf ('/');
if (indx > 0) {
name = name.substring (0, indx);
}
// check module name from config file
String replaced = name.replace ('.', '-'); // NOI18N
String searchFor;
if (replaced.indexOf (ModuleDeactivator.MODULES) > 0) { // NOI18N
// standard module
searchFor = replaced + ".jar"; // NOI18N
} else if(osgi) {
searchFor = replaced + ".jar"; // NOI18N
} else {
// core module
searchFor = replaced.substring (replaced.lastIndexOf ('-') > 0 ? replaced.lastIndexOf ('-') + 1 : 0) + ".jar"; // NOI18N
}
String dash = name.replace ('.', '-');
{
boolean needInfoInUserDir = false;
boolean afterNBMsCluster = false;
for (File c : clusters(true)) {
File hidden = new File(new File(new File(c, "config"), "Modules"), dash + ".xml_hidden");
if (hidden.exists()) {
hidden.delete();
XMLUtil.LOG.info("File " + hidden + " deleted.");
}
if (directory.equals(c)) {
afterNBMsCluster = true;
continue;
}
if (afterNBMsCluster) {
continue;
}
File customConfigs = new File(new File(new File(c, "config"), "Modules"), dash + ".xml");
if (customConfigs.exists()) {
needInfoInUserDir = lastVersion == null;
}
}
if (needInfoInUserDir) {
// there is a definition for the same XML file in some cluster
// already and
File userConfig = new File(new File(new File(getUserDir(), "config"), "Modules"), dash + ".xml");
writeModulesConfig(userConfig, searchFor, candidate, newCandidate, oldCandidate, name);
return;
}
}
File config = new File (configDir, dash + ".xml"); // NOI18N
if (config.isFile ()) {
// already written
return;
}
writeModulesConfig(config, searchFor, candidate, newCandidate, oldCandidate, name);
}
void write( ) {
Document document = XMLUtil.createDocument(ELEMENT_MODULE);
Element e_module = document.getDocumentElement();
Element e_version;
Element e_file;
e_module.setAttribute(ATTR_CODENAMEBASE, getCodenamebase());
for (Version ver : getVersions()) {
e_version = document.createElement(ELEMENT_VERSION);
if (ver.getVersion() != null) {
e_version.setAttribute(ATTR_VERSION, ver.getVersion());
}
e_version.setAttribute(ATTR_ORIGIN, ver.getOrigin());
e_version.setAttribute(ATTR_LAST, Boolean.valueOf(ver.isLast()).toString());
e_version.setAttribute(ATTR_INSTALL, Long.toString(ver.getInstall_time()));
e_module.appendChild(e_version);
for (ModuleFile moduleFile : ver.getFiles()) {
e_file = document.createElement(ELEMENT_FILE);
e_file.setAttribute(ATTR_FILE_NAME, moduleFile.getName());
e_file.setAttribute(ATTR_CRC, moduleFile.getCrc());
if (moduleFile.getLocaleversion() != null) {
e_file.setAttribute(ATTR_VERSION, moduleFile.getLocaleversion());
}
e_version.appendChild(e_file);
}
}
document.getDocumentElement().normalize();
OutputStream os = null;
try {
os = context.createOS(file);
} catch (Exception e) {
XMLUtil.LOG.log(Level.WARNING, "Cannot read " + file, e);
//#154904
if (!file.delete()) {
XMLUtil.LOG.log(Level.SEVERE, null, new IOException("Corresponding update would not be installed since it is not possible to modify or delete update tracking file " + file));
} else {
XMLUtil.LOG.log(Level.SEVERE, null, new IOException("Update tracking file was deleted since permissions does not allow to modify it: " + file));
try {
os = context.createOS(file);
} catch (Exception ex) {
XMLUtil.LOG.log(Level.WARNING, "Cannot read", ex);
}
}
}
if (os != null) {
try {
XMLUtil.write(document, os);
XMLUtil.LOG.info("File " + file + " modified.");
} catch (IOException e) {
XMLUtil.LOG.log(Level.WARNING, "Cannot write " + file, e);
} finally {
try {
os.close();
} catch (IOException e) {
XMLUtil.LOG.log(Level.WARNING, "Cannot close " + file, e);
}
}
}
}
void deleteUnusedFiles() {
if ( lastVersion == null || newVersion == null ) {
return;
}
for (ModuleFile modFile : lastVersion.getFiles()) {
if ( ! newVersion.containsFile( modFile ) && modFile.getName().indexOf( LOCALE_DIR ) == -1 ) {
safeDelete( modFile );
}
}
}
private void safeDelete(ModuleFile modFile) {
// test file existence
File f = new File( file.getParentFile().getParent() + FILE_SEPARATOR + modFile.getName() );
if ( f.exists() ) {
// test crc
try {
if (! Long.toString(getFileCRC(f)).equals(modFile.getCrc())) {
return;
}
} catch ( IOException ioe ) {
return;
}
// test if file is referenced from other module
scanDir();
boolean found = false;
Iterator<Module> it = installedModules.values ().iterator();
while ( !found && it.hasNext() ) {
Module mod = it.next();
if ( ! mod.equals( this ) ) {
Version v = mod.getNewOrLastVersion();
if ( v != null && v.containsFile( modFile ) ) {
found = true;
}
}
}
if ( ! found ) {
XMLUtil.LOG.info("Deleting file: " + f);
boolean deleted = f.delete();
XMLUtil.LOG.info(".... " + f + " was deleted? " + deleted);
}
}
}
String getL10NSpecificationVersion(String jarpath) {
String localever;
Collections.<Version>sort( versions );
for (Version ver: versions) {
localever = ver.getLocaleVersion( jarpath );
if ( localever != null ) {
return localever;
}
}
return null;
}
private void writeModulesConfig(File config, String searchFor, String candidate, String newCandidate, String oldCandidate, String name) {
config.getParentFile().mkdirs();
Boolean isAutoload = null;
Boolean isEager = null;
java.util.Iterator it = newVersion.getFiles().iterator();
boolean needToWrite = false;
while (it.hasNext()) {
ModuleFile f = (ModuleFile) it.next ();
String n = f.getName();
String parentDir;
{
File p = new File(f.getName()).getParentFile();
parentDir = p != null ? p.getName() : "";
}
needToWrite = needToWrite || n.indexOf(ModuleDeactivator.MODULES) >= 0 || osgi;
if (n.endsWith(".jar") && ! parentDir.equals("ext")) { // NOI18N
// ok, module candidate
candidate = f.getName();
// the correct candidate looks as e.g. org.netbeans.modules.mymodule
// if no jar looks as codenamebase then the jar file will be found as module's jar
if (searchFor.endsWith(candidate) || candidate.endsWith(searchFor)) {
newCandidate = candidate;
oldCandidate = null;
// autoload and eager will set by module's jar
if ("autoload".equals(parentDir)) {
// NOI18N
isAutoload = Boolean.TRUE;
} else {
isAutoload = Boolean.FALSE;
}
if ("eager".equals(parentDir)) {
// NOI18N
isEager = Boolean.TRUE;
} else {
isEager = Boolean.FALSE;
}
} else {
if (newCandidate == null) {
oldCandidate = (oldCandidate == null ? "" : oldCandidate + ", ") + candidate; // NOI18N
}
}
}
// if no correct name found => set autoload/eager by the last jar file
if (isAutoload == null && "autoload".equals(parentDir)) {
// NOI18N
isAutoload = Boolean.TRUE;
}
if (isEager == null && "eager".equals(parentDir)) {
// NOI18N
isEager = Boolean.TRUE;
}
}
if (!needToWrite) {
XMLUtil.LOG.log(Level.WARNING, "No config file written for module {0}. No jar file present in \"modules\" directory.", codenamebase);
return;
}
assert newCandidate != null || oldCandidate != null : "No jar file present!";
if (newCandidate == null) {
// PENDING: should check but some NBM assumed wrong behaviour before bugfix 53316
assert oldCandidate.equals(candidate) : "More files look as module: " + oldCandidate;
// only temporary
if (!oldCandidate.equals(candidate)) {
XMLUtil.LOG.log(Level.WARNING, "More files look as module: {0}", oldCandidate);
oldCandidate = candidate;
}
// end of temp
}
String moduleName = newCandidate == null ? oldCandidate : newCandidate;
boolean autoload = isAutoload != null && isAutoload.booleanValue();
boolean eager = isEager != null && isEager.booleanValue();
boolean isEnabled = !autoload && !eager;
String spec = newVersion.getVersion();
OutputStream os;
try {
os = context.createOS(config);
PrintWriter pw = new PrintWriter(new java.io.OutputStreamWriter(os, "UTF-8"));
// Please make sure formatting matches what the IDE actually spits
// out; it could matter.
pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
pw.println("<!DOCTYPE module PUBLIC \"-//NetBeans//DTD Module Status 1.0//EN\"");
pw.println(" \"http://www.netbeans.org/dtds/module-status-1_0.dtd\">");
pw.println("<module name=\"" + name + "\">");
pw.println(" <param name=\"autoload\">" + autoload + "</param>");
pw.println(" <param name=\"eager\">" + eager + "</param>");
if (isEnabled) {
pw.println(" <param name=\"enabled\">" + isEnabled + "</param>");
}
pw.println(" <param name=\"jar\">" + moduleName + "</param>");
pw.println(" <param name=\"reloadable\">false</param>");
pw.println(" <param name=\"specversion\">" + spec + "</param>");
pw.println("</module>");
pw.flush();
pw.close();
XMLUtil.LOG.info("New config was written in " + config);
} catch (IOException ex) {
XMLUtil.LOG.log(Level.INFO, null, ex);
}
}
@Override
public String toString() {
return "UpdateTracing.Module[" + this.codenamebase + "(" + this.file + "), OSGI? " + this.osgi + "]";
}
}
public class Version extends Object implements Comparable<Version> {
private final Module module;
Version(Module m) {
this.module = m;
}
/** Holds value of property version. */
private String version;
/** Holds value of property origin. */
private String origin;
/** Holds value of property last. */
private boolean last;
/** Holds value of property install_time. */
private long install_time = 0;
/** Holds value of property files. */
private List<ModuleFile> files = new ArrayList<ModuleFile>();
/** Getter for property version.
* @return Value of property version.
*/
String getVersion() {
return version;
}
/** Setter for property version.
* @param version New value of property version.
*/
void setVersion(String version) {
this.version = version;
}
/** Getter for property origin.
* @return Value of property origin.
*/
String getOrigin() {
return origin;
}
/** Setter for property origin.
* @param origin New value of property origin.
*/
void setOrigin(String origin) {
this.origin = origin;
}
/** Getter for property last.
* @return Value of property last.
*/
boolean isLast() {
return last;
}
/** Setter for property last.
* @param last New value of property last.
*/
void setLast(boolean last) {
this.last = last;
}
/** Getter for property install_time.
* @return Value of property install_time.
*/
long getInstall_time() {
return install_time;
}
/** Setter for property install_time.
* @param install_time New value of property install_time.
*/
void setInstall_time(long install_time) {
this.install_time = install_time;
}
/** Getter for property files.
* @return Value of property files.
*/
List<ModuleFile> getFiles() {
return files;
}
/** Setter for property files.
* @param files New value of property files.
*/
void addL10NFiles(List<ModuleFile> l10nfiles) {
for (ModuleFile lf : l10nfiles) {
String lname = lf.getName();
for ( int i = files.size() - 1; i >=0; i-- ) {
ModuleFile f = files.get( i );
if ( f.getName().equals( lname ) ) {
files.remove( i );
}
}
}
files.addAll( l10nfiles );
}
void addFile( ModuleFile file ) {
files.add( file );
}
public void addFileWithCrc( String filename, String crc ) {
ModuleFile file = new ModuleFile();
file.setName( filename );
file.setCrc( crc );
files.add( file );
}
public void addL10NFileWithCrc( String filename, String crc, String specver ) {
ModuleFile file = new ModuleFile();
file.setName( filename );
file.setCrc( crc );
file.setLocaleversion( specver );
files.add( file );
}
boolean containsFile( ModuleFile file ) {
for (ModuleFile f : files) {
if ( f.getName().equals( file.getName() ) ) {
return true;
}
}
return false;
}
ModuleFile findFile(String filename) {
for (ModuleFile f : files) {
if ( f.getName().equals( filename ) ) {
return f;
}
}
return null;
}
String getLocaleVersion(String filename) {
String locver = null;
ModuleFile f = findFile( filename );
if ( f != null ) {
locver = f.getLocaleversion();
if ( locver == null ) {
locver = version;
}
}
return locver;
}
@Override
public int compareTo (Version oth) {
if ( install_time < oth.getInstall_time() ) {
return 1;
}
else if ( install_time > oth.getInstall_time() ) {
return -1;
} else {
return 0;
}
}
@Override
public String toString() {
return "UpdateTracing.Version[" + this.module + "/" + this.version + ", last? " + this.isLast() + "]";
}
}
class ModuleFile extends Object {
/** Holds value of property name. */
private String name;
/** Holds value of property crc. */
private String crc;
/** Holds value of property localeversion. */
private String localeversion = null;
/** Getter for property name.
* @return Value of property name.
*/
String getName() {
return name;
}
/** Setter for property name.
* @param name New value of property name.
*/
void setName(String name) {
this.name = name;
}
/** Getter for property crc.
* @return Value of property crc.
*/
String getCrc() {
return crc;
}
/** Setter for property crc.
* @param crc New value of property crc.
*/
void setCrc(String crc) {
this.crc = crc;
}
/** Getter for property localeversion.
* @return Value of property localeversion.
*
*/
public String getLocaleversion() {
return this.localeversion;
}
/** Setter for property localeversion.
* @param localeversion New value of property localeversion.
*
*/
public void setLocaleversion(String localeversion) {
this.localeversion = localeversion;
}
@Override
public String toString() {
return "UpdateTracing.ModuleFile[" + this.name + "(" + this.crc + ")" + "]";
}
}
public static class AdditionalInfo extends Object {
private Map<String, String> sources;
private AdditionalInfo (File additionalInfoFile) {
sources = readAdditionalInfoFile (additionalInfoFile);
}
public String getSource (String nbmFileName) {
return sources != null ? sources.get (nbmFileName) : null;
}
private Map<String, String> readAdditionalInfoFile (File f) {
if (f == null || ! f.exists ()) {
throw new IllegalArgumentException ("AdditionalInfo file " + f + " must exists.");
}
Map<String, String> res = null;
/** org.w3c.dom.Document document */
org.w3c.dom.Document document;
InputStream is = null;
try {
is = new FileInputStream (f);
document = XMLUtil.parse (new InputSource (is), false, false, null, null);
} catch (org.xml.sax.SAXException e) {
XMLUtil.LOG.log (Level.WARNING,"Bad " + UpdateTracking.ADDITIONAL_INFO_FILE_NAME + f, e); // NOI18N
return res;
} catch (java.io.IOException e) {
XMLUtil.LOG.log (Level.WARNING,"Missing " + UpdateTracking.ADDITIONAL_INFO_FILE_NAME + f, e); // NOI18N
return res;
} finally {
if (is != null) {
try {
is.close ();
} catch (IOException ioe) {
XMLUtil.LOG.log (Level.INFO, "Cannot close stream for file " + f, ioe); // NOI18N
return res;
}
}
}
org.w3c.dom.Element element = document.getDocumentElement ();
if ((element != null) && element.getTagName ().equals (ELEMENT_ADDITIONAL)) {
res = scanModuleAdditional (element);
}
return res;
}
private Map<String, String> scanModuleAdditional (org.w3c.dom.Element element) {
Map<String, String> res = new HashMap<String, String> ();
org.w3c.dom.NodeList nodes = element.getChildNodes ();
for (int i = 0; i < nodes.getLength (); i++) {
org.w3c.dom.Node node = nodes.item (i);
if (node.getNodeType () == org.w3c.dom.Node.ELEMENT_NODE) {
org.w3c.dom.Element nodeElement = (org.w3c.dom.Element) node;
if (nodeElement.getTagName ().equals (ELEMENT_ADDITIONAL_MODULE)) {
String fileSpec = nodeElement.getAttribute (ATTR_ADDITIONAL_NBM_NAME);
String source = nodeElement.getAttribute (ATTR_ADDITIONAL_SOURCE);
res.put (fileSpec, source);
}
}
}
return res;
}
}
}
|