1 19 20 package org.netbeans.modules.apisupport.project.universe; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.net.MalformedURLException ; 29 import java.net.URI ; 30 import java.net.URL ; 31 import java.text.MessageFormat ; 32 import java.util.ArrayList ; 33 import java.util.Arrays ; 34 import java.util.Collection ; 35 import java.util.HashSet ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.Locale ; 39 import java.util.Map ; 40 import java.util.Properties ; 41 import java.util.Set ; 42 import java.util.SortedSet ; 43 import java.util.TreeSet ; 44 import java.util.jar.JarFile ; 45 import java.util.zip.ZipEntry ; 46 import org.netbeans.api.project.ProjectManager; 47 import org.netbeans.modules.apisupport.project.ManifestManager; 48 import org.netbeans.modules.apisupport.project.Util; 49 import org.netbeans.spi.project.support.ant.EditableProperties; 50 import org.netbeans.spi.project.support.ant.PropertyProvider; 51 import org.netbeans.spi.project.support.ant.PropertyUtils; 52 import org.openide.ErrorManager; 53 import org.openide.filesystems.FileUtil; 54 import org.openide.modules.InstalledFileLocator; 55 import org.openide.modules.SpecificationVersion; 56 import org.openide.util.Mutex; 57 import org.openide.util.MutexException; 58 import org.openide.util.NbBundle; 59 60 67 public final class NbPlatform { 68 69 private static final String PLATFORM_PREFIX = "nbplatform."; private static final String PLATFORM_DEST_DIR_SUFFIX = ".netbeans.dest.dir"; private static final String PLATFORM_LABEL_SUFFIX = ".label"; private static final String PLATFORM_SOURCES_SUFFIX = ".sources"; private static final String PLATFORM_JAVADOC_SUFFIX = ".javadoc"; private static final String PLATFORM_HARNESS_DIR_SUFFIX = ".harness.dir"; public static final String PLATFORM_ID_DEFAULT = "default"; 77 public static final String PROP_SOURCE_ROOTS = "sourceRoots"; 79 private static Set <NbPlatform> platforms; 80 81 private final PropertyChangeSupport pcs = new PropertyChangeSupport (this); 82 83 85 public static final int HARNESS_VERSION_UNKNOWN = 0; 86 87 public static final int HARNESS_VERSION_50 = 1; 88 89 public static final int HARNESS_VERSION_50u1 = 2; 90 91 public static final int HARNESS_VERSION_55u1 = 3; 92 93 public static final int HARNESS_VERSION_60 = 4; 94 95 98 public static void reset() { 99 platforms = null; 100 } 101 102 105 public static synchronized Set <NbPlatform> getPlatforms() { 106 return new HashSet (getPlatformsInternal()); 107 } 108 109 private static Set <NbPlatform> getPlatformsInternal() { 110 if (platforms == null) { 111 platforms = new HashSet (); 112 Map <String ,String > p = PropertyUtils.sequentialPropertyEvaluator(null, new PropertyProvider[] {PropertyUtils.globalPropertyProvider()}).getProperties(); 113 boolean foundDefault = false; 114 Iterator entries = p.entrySet().iterator(); 115 while (entries.hasNext()) { 116 Map.Entry entry = (Map.Entry ) entries.next(); 117 String key = (String ) entry.getKey(); 118 if (key.startsWith(PLATFORM_PREFIX) && key.endsWith(PLATFORM_DEST_DIR_SUFFIX)) { 119 String id = key.substring(PLATFORM_PREFIX.length(), key.length() - PLATFORM_DEST_DIR_SUFFIX.length()); 120 String label = (String ) p.get(PLATFORM_PREFIX + id + PLATFORM_LABEL_SUFFIX); 121 String destdir = (String ) entry.getValue(); 122 String harnessdir = (String ) p.get(PLATFORM_PREFIX + id + PLATFORM_HARNESS_DIR_SUFFIX); 123 String sources = (String ) p.get(PLATFORM_PREFIX + id + PLATFORM_SOURCES_SUFFIX); 124 String javadoc = (String ) p.get(PLATFORM_PREFIX + id + PLATFORM_JAVADOC_SUFFIX); 125 File destdirF = FileUtil.normalizeFile(new File (destdir)); 126 File harness; 127 if (harnessdir != null) { 128 harness = FileUtil.normalizeFile(new File (harnessdir)); 129 } else { 130 harness = findHarness(destdirF); 131 } 132 platforms.add(new NbPlatform(id, label, destdirF, harness, findURLs(sources), findURLs(javadoc))); 133 foundDefault |= id.equals(PLATFORM_ID_DEFAULT); 134 } 135 } 136 if (!foundDefault) { 137 File loc = defaultPlatformLocation(); 138 if (loc != null) { 139 platforms.add(new NbPlatform(PLATFORM_ID_DEFAULT, null, loc, findHarness(loc), new URL [0], new URL [0])); 140 } 141 } 142 if (Util.err.isLoggable(ErrorManager.INFORMATIONAL)) { 143 Util.err.log("NbPlatform initial list: " + platforms); 144 } 145 } 146 return platforms; 147 } 148 149 153 public static NbPlatform getDefaultPlatform() { 154 return NbPlatform.getPlatformByID(PLATFORM_ID_DEFAULT); 155 } 156 157 160 public static File defaultPlatformLocation() { 161 File bootJar = InstalledFileLocator.getDefault().locate("core/core.jar", "org.netbeans.core.startup", false); if (bootJar == null) { 165 if (Util.err.isLoggable(ErrorManager.INFORMATIONAL)) { 166 Util.err.log("no core/core.jar"); 167 } 168 return null; 169 } 170 File harnessJar = InstalledFileLocator.getDefault().locate("modules/org-netbeans-modules-apisupport-harness.jar", "org.netbeans.modules.apisupport.harness", false); if (harnessJar == null) { 173 ErrorManager.getDefault().log(ErrorManager.WARNING, "Cannot resolve default platform. " + "Probably either \"org.netbeans.modules.apisupport.harness\" module is missing or is corrupted."); return null; 176 } 177 File loc = harnessJar.getParentFile().getParentFile().getParentFile(); 178 try { 179 if (!loc.getCanonicalFile().equals(bootJar.getParentFile().getParentFile().getParentFile().getCanonicalFile())) { 180 if (Util.err.isLoggable(ErrorManager.INFORMATIONAL)) { 182 Util.err.log("core.jar & harness.jar locations do not match: " + bootJar + " vs. " + harnessJar); 183 } 184 return null; 185 } 186 } catch (IOException x) { 187 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, x); 188 } 189 return FileUtil.normalizeFile(loc); 191 } 192 193 196 private static URL [] defaultPlatformSources(File loc) { 197 if (loc.getName().equals("netbeans") && loc.getParentFile().getName().equals("nbbuild")) { try { 199 return new URL [] {loc.getParentFile().getParentFile().toURI().toURL()}; 200 } catch (MalformedURLException e) { 201 assert false : e; 202 } 203 } 204 return new URL [0]; 205 } 206 207 210 private static URL [] defaultPlatformJavadoc() { 211 File apidocsZip = InstalledFileLocator.getDefault().locate("docs/NetBeansAPIs.zip", "org.netbeans.modules.apisupport.apidocs", true); if (apidocsZip != null) { 213 return new URL [] {Util.urlForJar(apidocsZip)}; 214 } else { 215 return new URL [0]; 216 } 217 } 218 219 224 public static synchronized NbPlatform getPlatformByID(String id) { 225 Iterator it = getPlatformsInternal().iterator(); 226 while (it.hasNext()) { 227 NbPlatform p = (NbPlatform) it.next(); 228 if (p.getID().equals(id)) { 229 return p; 230 } 231 } 232 return null; 233 } 234 235 244 public static synchronized NbPlatform getPlatformByDestDir(File destDir) { 245 Iterator it = getPlatformsInternal().iterator(); 246 while (it.hasNext()) { 247 NbPlatform p = (NbPlatform) it.next(); 248 if (p.getDestDir().equals(destDir)) { 249 return p; 250 } 251 } 252 URL [] sources = new URL [0]; 253 if (destDir.getName().equals("netbeans")) { File parent = destDir.getParentFile(); 255 if (parent != null && parent.getName().equals("nbbuild")) { File superparent = parent.getParentFile(); 257 if (superparent != null && ModuleList.isNetBeansOrg(superparent)) { 258 sources = new URL [] {Util.urlForDir(superparent)}; 259 } 260 } 261 } 262 return new NbPlatform(null, null, destDir, findHarness(destDir), sources, new URL [0]); 266 } 267 268 272 private static File findHarness(File destDir) { 273 File [] kids = destDir.listFiles(); 274 if (kids != null) { 275 for (int i = 0; i < kids.length; i++) { 276 if (isHarness(kids[i])) { 277 return kids[i]; 278 } 279 } 280 } 281 return new File (destDir, "harness"); } 283 284 287 public static boolean isHarness(File dir) { 288 return new File (dir, "modules" + File.separatorChar + "org-netbeans-modules-apisupport-harness.jar").isFile(); } 290 291 295 public static synchronized boolean contains(File destDir) { 296 boolean contains = false; 297 Iterator it = getPlatformsInternal().iterator(); 298 while (it.hasNext()) { 299 NbPlatform p = (NbPlatform) it.next(); 300 if (p.getDestDir().equals(destDir)) { 301 contains = true; 302 break; 303 } 304 } 305 return contains; 306 } 307 308 316 public static NbPlatform addPlatform(final String id, final File destdir, final String label) throws IOException { 317 return addPlatform(id, destdir, findHarness(destdir), label); 318 } 319 320 329 public static NbPlatform addPlatform(final String id, final File destdir, final File harness, final String label) throws IOException { 330 try { 331 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() { 332 public Object run() throws IOException { 333 EditableProperties props = PropertyUtils.getGlobalProperties(); 334 String plafDestDir = PLATFORM_PREFIX + id + PLATFORM_DEST_DIR_SUFFIX; 335 props.setProperty(plafDestDir, destdir.getAbsolutePath()); 336 if (!destdir.isDirectory()) { 337 throw new FileNotFoundException (destdir.getAbsolutePath()); 338 } 339 storeHarnessLocation(id, destdir, harness, props); 340 props.setProperty(PLATFORM_PREFIX + id + PLATFORM_LABEL_SUFFIX, label); 341 PropertyUtils.putGlobalProperties(props); 342 return null; 343 } 344 }); 345 } catch (MutexException e) { 346 throw (IOException ) e.getException(); 347 } 348 NbPlatform plaf = new NbPlatform(id, label, FileUtil.normalizeFile(destdir), harness, 349 findURLs(null), findURLs(null)); 350 synchronized (NbPlatform.class) { 351 getPlatformsInternal().add(plaf); 352 } 353 if (Util.err.isLoggable(ErrorManager.INFORMATIONAL)) { 354 Util.err.log("NbPlatform added: " + plaf); 355 } 356 return plaf; 357 } 358 359 private static void storeHarnessLocation(String id, File destdir, File harness, EditableProperties props) { 360 String harnessDirKey = PLATFORM_PREFIX + id + PLATFORM_HARNESS_DIR_SUFFIX; 361 if (harness.equals(findHarness(destdir))) { 362 String plafDestDir = PLATFORM_PREFIX + id + PLATFORM_DEST_DIR_SUFFIX; 364 props.setProperty(harnessDirKey, "${" + plafDestDir + "}/" + harness.getName()); } else if (harness.equals(getDefaultPlatform().getHarnessLocation())) { 366 props.setProperty(harnessDirKey, "${" + PLATFORM_PREFIX + PLATFORM_ID_DEFAULT + PLATFORM_HARNESS_DIR_SUFFIX + "}"); } else { 369 props.setProperty(harnessDirKey, harness.getAbsolutePath()); 371 } 372 } 373 374 public static void removePlatform(final NbPlatform plaf) throws IOException { 375 try { 376 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() { 377 public Object run() throws IOException { 378 EditableProperties props = PropertyUtils.getGlobalProperties(); 379 props.remove(PLATFORM_PREFIX + plaf.getID() + PLATFORM_DEST_DIR_SUFFIX); 380 props.remove(PLATFORM_PREFIX + plaf.getID() + PLATFORM_HARNESS_DIR_SUFFIX); 381 props.remove(PLATFORM_PREFIX + plaf.getID() + PLATFORM_LABEL_SUFFIX); 382 props.remove(PLATFORM_PREFIX + plaf.getID() + PLATFORM_SOURCES_SUFFIX); 383 props.remove(PLATFORM_PREFIX + plaf.getID() + PLATFORM_JAVADOC_SUFFIX); 384 PropertyUtils.putGlobalProperties(props); 385 return null; 386 } 387 }); 388 } catch (MutexException e) { 389 throw (IOException ) e.getException(); 390 } 391 synchronized (NbPlatform.class) { 392 getPlatformsInternal().remove(plaf); 393 } 394 if (Util.err.isLoggable(ErrorManager.INFORMATIONAL)) { 395 Util.err.log("NbPlatform removed: " + plaf); 396 } 397 } 398 399 private final String id; 400 private String label; 401 private File nbdestdir; 402 private File harness; 403 private URL [] sourceRoots; 404 private URL [] javadocRoots; 405 private List <ModuleList> listsForSources; 406 private int harnessVersion = -1; 407 408 private NbPlatform(String id, String label, File nbdestdir, File harness, URL [] sources, URL [] javadoc) { 409 this.id = id; 410 this.label = label; 411 this.nbdestdir = nbdestdir; 412 this.harness = harness; 413 this.sourceRoots = sources; 414 this.javadocRoots = javadoc; 415 } 416 417 static URL [] findURLs(final String path) { 418 if (path == null) { 419 return new URL [0]; 420 } 421 String [] pieces = PropertyUtils.tokenizePath(path); 422 URL [] urls = new URL [pieces.length]; 423 for (int i = 0; i < pieces.length; i++) { 424 urls[i] = Util.urlForDirOrJar(FileUtil.normalizeFile(new File (pieces[i]))); 426 } 427 return urls; 428 } 429 430 436 public String getID() { 437 return id; 438 } 439 440 444 public boolean isDefault() { 445 return PLATFORM_ID_DEFAULT.equals(id); 446 } 447 448 454 public String getLabel() { 455 if (label == null) { 456 try { 457 label = isValid() ? computeDisplayName(nbdestdir) : 458 NbBundle.getMessage(NbPlatform.class, "MSG_InvalidPlatform", getDestDir().getAbsolutePath()); 460 } catch (IOException e) { 461 Util.err.notify(ErrorManager.INFORMATIONAL, e); 462 label = nbdestdir.getAbsolutePath(); 463 } 464 } 465 if (isDefault()) { 466 return NbBundle.getMessage(NbPlatform.class, "LBL_default_platform", label); 467 } else { 468 return label; 469 } 470 } 471 472 476 public File getDestDir() { 477 return nbdestdir; 478 } 479 480 public void setDestDir(File destdir) { 481 this.nbdestdir = destdir; 482 } 484 485 490 public URL [] getSourceRoots() { 491 if (sourceRoots.length == 0 && isDefault()) { 492 return defaultPlatformSources(getDestDir()); 493 } else { 494 return sourceRoots; 495 } 496 } 497 498 private void maybeUpdateDefaultPlatformSources() { 499 if (sourceRoots.length == 0 && isDefault()) { 500 sourceRoots = defaultPlatformSources(getDestDir()); 501 pcs.firePropertyChange(PROP_SOURCE_ROOTS, null, null); 502 } 503 } 504 505 510 public void addSourceRoot(URL root) throws IOException { 511 maybeUpdateDefaultPlatformSources(); 512 URL [] newSourceRoots = new URL [sourceRoots.length + 1]; 513 System.arraycopy(sourceRoots, 0, newSourceRoots, 0, sourceRoots.length); 514 newSourceRoots[sourceRoots.length] = root; 515 setSourceRoots(newSourceRoots); 516 } 517 518 523 public void removeSourceRoots(URL [] urlsToRemove) throws IOException { 524 maybeUpdateDefaultPlatformSources(); 525 Collection newSources = new ArrayList (Arrays.asList(sourceRoots)); 526 newSources.removeAll(Arrays.asList(urlsToRemove)); 527 URL [] sources = new URL [newSources.size()]; 528 setSourceRoots((URL []) newSources.toArray(sources)); 529 } 530 531 public void moveSourceRootUp(int indexToUp) throws IOException { 532 maybeUpdateDefaultPlatformSources(); 533 if (indexToUp <= 0) { 534 return; } 536 URL [] newSourceRoots = new URL [sourceRoots.length]; 537 System.arraycopy(sourceRoots, 0, newSourceRoots, 0, sourceRoots.length); 538 newSourceRoots[indexToUp - 1] = sourceRoots[indexToUp]; 539 newSourceRoots[indexToUp] = sourceRoots[indexToUp - 1]; 540 setSourceRoots(newSourceRoots); 541 } 542 543 public void moveSourceRootDown(int indexToDown) throws IOException { 544 maybeUpdateDefaultPlatformSources(); 545 if (indexToDown >= (sourceRoots.length - 1)) { 546 return; } 548 URL [] newSourceRoots = new URL [sourceRoots.length]; 549 System.arraycopy(sourceRoots, 0, newSourceRoots, 0, sourceRoots.length); 550 newSourceRoots[indexToDown + 1] = sourceRoots[indexToDown]; 551 newSourceRoots[indexToDown] = sourceRoots[indexToDown + 1]; 552 setSourceRoots(newSourceRoots); 553 } 554 555 public void setSourceRoots(URL [] roots) throws IOException { 556 putGlobalProperty( 557 PLATFORM_PREFIX + getID() + PLATFORM_SOURCES_SUFFIX, 558 urlsToAntPath(roots)); 559 sourceRoots = roots; 560 pcs.firePropertyChange(PROP_SOURCE_ROOTS, null, null); 561 listsForSources = null; 562 } 563 564 571 public URL [] getJavadocRoots() { 572 if (javadocRoots.length == 0 && isDefault()) { 573 return defaultPlatformJavadoc(); 574 } else { 575 return javadocRoots; 576 } 577 } 578 579 private void maybeUpdateDefaultPlatformJavadoc() { 580 if (javadocRoots.length == 0 && isDefault()) { 581 javadocRoots = defaultPlatformJavadoc(); 582 } 583 } 584 585 590 public void addJavadocRoot(URL root) throws IOException { 591 maybeUpdateDefaultPlatformJavadoc(); 592 URL [] newJavadocRoots = new URL [javadocRoots.length + 1]; 593 System.arraycopy(javadocRoots, 0, newJavadocRoots, 0, javadocRoots.length); 594 newJavadocRoots[javadocRoots.length] = root; 595 setJavadocRoots(newJavadocRoots); 596 } 597 598 603 public void removeJavadocRoots(URL [] urlsToRemove) throws IOException { 604 maybeUpdateDefaultPlatformJavadoc(); 605 Collection newJavadocs = new ArrayList (Arrays.asList(javadocRoots)); 606 newJavadocs.removeAll(Arrays.asList(urlsToRemove)); 607 URL [] javadocs = new URL [newJavadocs.size()]; 608 setJavadocRoots((URL []) newJavadocs.toArray(javadocs)); 609 } 610 611 public void moveJavadocRootUp(int indexToUp) throws IOException { 612 maybeUpdateDefaultPlatformJavadoc(); 613 if (indexToUp <= 0) { 614 return; } 616 URL [] newJavadocRoots = new URL [javadocRoots.length]; 617 System.arraycopy(javadocRoots, 0, newJavadocRoots, 0, javadocRoots.length); 618 newJavadocRoots[indexToUp - 1] = javadocRoots[indexToUp]; 619 newJavadocRoots[indexToUp] = javadocRoots[indexToUp - 1]; 620 setJavadocRoots(newJavadocRoots); 621 } 622 623 public void moveJavadocRootDown(int indexToDown) throws IOException { 624 maybeUpdateDefaultPlatformJavadoc(); 625 if (indexToDown >= (javadocRoots.length - 1)) { 626 return; } 628 URL [] newJavadocRoots = new URL [javadocRoots.length]; 629 System.arraycopy(javadocRoots, 0, newJavadocRoots, 0, javadocRoots.length); 630 newJavadocRoots[indexToDown + 1] = javadocRoots[indexToDown]; 631 newJavadocRoots[indexToDown] = javadocRoots[indexToDown + 1]; 632 setJavadocRoots(newJavadocRoots); 633 } 634 635 public void setJavadocRoots(URL [] roots) throws IOException { 636 putGlobalProperty( 637 PLATFORM_PREFIX + getID() + PLATFORM_JAVADOC_SUFFIX, 638 urlsToAntPath(roots)); 639 javadocRoots = roots; 640 } 641 642 646 public boolean isValid() { 647 return NbPlatform.isPlatformDirectory(getDestDir()); 648 } 649 650 static String urlsToAntPath(final URL [] urls) { 651 StringBuffer path = new StringBuffer (); 652 for (int i = 0; i < urls.length; i++) { 653 if (urls[i].getProtocol().equals("jar")) { path.append(urlToAntPath(FileUtil.getArchiveFile(urls[i]))); 655 } else { 656 path.append(urlToAntPath(urls[i])); 657 } 658 if (i != urls.length - 1) { 659 path.append(':'); } 661 } 662 return path.toString(); 663 } 664 665 private static String urlToAntPath(final URL url) { 666 return new File (URI.create(url.toExternalForm())).getAbsolutePath(); 667 } 668 669 private void putGlobalProperty(final String key, final String value) throws IOException { 670 try { 671 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() { 672 public Object run() throws IOException { 673 EditableProperties props = PropertyUtils.getGlobalProperties(); 674 if ("".equals(value)) { props.remove(key); 676 } else { 677 props.setProperty(key, value); 678 } 679 PropertyUtils.putGlobalProperties(props); 680 return null; 681 } 682 }); 683 } catch (MutexException e) { 684 throw (IOException ) e.getException(); 685 } 686 } 687 688 693 public File getSourceLocationOfModule(File jar) { 694 if (listsForSources == null) { 695 listsForSources = new ArrayList (); 696 URL [] sourceRoots = getSourceRoots(); 697 for (int i = 0; i < sourceRoots.length; i++) { 698 URL u = sourceRoots[i]; 699 if (!u.getProtocol().equals("file")) { continue; 701 } 702 File dir = new File (URI.create(u.toExternalForm())); 703 if (dir.isDirectory()) { 704 try { 705 if (ModuleList.isNetBeansOrg(dir)) { 706 listsForSources.add(ModuleList.findOrCreateModuleListFromNetBeansOrgSources(dir)); 707 } else { 708 listsForSources.add(ModuleList.findOrCreateModuleListFromSuiteWithoutBinaries(dir)); 709 } 710 } catch (IOException e) { 711 Util.err.notify(ErrorManager.INFORMATIONAL, e); 712 } 713 } 714 } 715 } 716 Iterator it = listsForSources.iterator(); 717 while (it.hasNext()) { 718 ModuleList l = (ModuleList) it.next(); 719 Set <ModuleEntry> entries = l.getAllEntriesSoft(); 720 Iterator it2 = entries.iterator(); 721 while (it2.hasNext()) { 722 ModuleEntry entry = (ModuleEntry) it2.next(); 723 if (!entry.getJarLocation().getName().equals(jar.getName())) { 725 continue; 726 } 727 File src = entry.getSourceLocation(); 728 if (src != null && src.isDirectory()) { 729 return src; 730 } 731 } 732 entries = l.getAllEntries(); 733 it2 = entries.iterator(); 734 while (it2.hasNext()) { 735 ModuleEntry entry = (ModuleEntry) it2.next(); 736 if (!entry.getJarLocation().getName().equals(jar.getName())) { 737 continue; 738 } 739 File src = entry.getSourceLocation(); 740 if (src != null && src.isDirectory()) { 741 return src; 742 } 743 } 744 } 745 return null; 746 } 747 748 753 public ModuleEntry[] getModules() { 754 try { 755 SortedSet <ModuleEntry> set = new TreeSet ( 756 ModuleList.findOrCreateModuleListFromBinaries(getDestDir()).getAllEntriesSoft()); 757 ModuleEntry[] entries = new ModuleEntry[set.size()]; 758 set.toArray(entries); 759 return entries; 760 } catch (IOException e) { 761 Util.err.notify(e); 762 return new ModuleEntry[0]; 763 } 764 } 765 766 private static File findCoreJar(File destdir) { 767 File [] subdirs = destdir.listFiles(); 768 if (subdirs != null) { 769 for (int i = 0; i < subdirs.length; i++) { 770 if (!subdirs[i].isDirectory()) { 771 continue; 772 } 773 if (!subdirs[i].getName().startsWith("platform")) { continue; 775 } 776 File coreJar = new File (subdirs[i], "core" + File.separatorChar + "core.jar"); if (coreJar.isFile()) { 778 return coreJar; 779 } 780 } 781 } 782 return null; 783 } 784 785 790 public static boolean isPlatformDirectory(File destdir) { 791 return findCoreJar(destdir) != null; 792 } 793 794 public static boolean isSupportedPlatform(File destdir) { 795 boolean valid = false; 796 File coreJar = findCoreJar(destdir); 797 if (coreJar != null) { 798 String platformDir = coreJar.getParentFile().getParentFile().getName(); 799 assert platformDir.startsWith("platform"); int version = Integer.parseInt(platformDir.substring(8)); valid = version >= 6; 802 } 803 return valid; 804 } 805 806 813 public static String computeDisplayName(File destdir) throws IOException { 814 File coreJar = findCoreJar(destdir); 815 if (coreJar == null) { 816 throw new IllegalArgumentException (destdir.getAbsolutePath()); 817 } 818 String currVer, implVers; 819 JarFile jf = new JarFile (coreJar); 820 try { 821 currVer = findCurrVer(jf, ""); 822 if (currVer == null) { 823 throw new IOException (coreJar.getAbsolutePath()); 824 } 825 implVers = jf.getManifest().getMainAttributes().getValue("OpenIDE-Module-Implementation-Version"); if (implVers == null) { 827 throw new IOException (coreJar.getAbsolutePath()); 828 } 829 } finally { 830 jf.close(); 831 } 832 File [] clusters = destdir.listFiles(); 835 BRANDED_CURR_VER: if (clusters != null) { 836 for (int i = 0; i < clusters.length; i++) { 837 File coreLocaleDir = new File (clusters[i], "core" + File.separatorChar + "locale"); if (!coreLocaleDir.isDirectory()) { 839 continue; 840 } 841 String [] kids = coreLocaleDir.list(); 842 if (kids != null) { 843 for (int j = 0; j < kids.length; j++) { 844 String name = kids[j]; 845 String prefix = "core"; String suffix = ".jar"; if (!name.startsWith(prefix) || !name.endsWith(suffix)) { 848 continue; 849 } 850 String infix = name.substring(prefix.length(), name.length() - suffix.length()); 851 int uscore = infix.lastIndexOf('_'); 852 if (uscore == -1) { 853 continue; 855 } 856 String lastPiece = infix.substring(uscore + 1); 857 if (Arrays.asList(Locale.getISOCountries()).contains(lastPiece) || 858 (!lastPiece.equals("nb") && Arrays.asList(Locale.getISOLanguages()).contains(lastPiece))) { continue; 863 } 864 jf = new JarFile (new File (coreLocaleDir, name)); 865 try { 866 String brandedCurrVer = findCurrVer(jf, infix); 867 if (brandedCurrVer != null) { 868 currVer = brandedCurrVer; 869 break BRANDED_CURR_VER; 870 } 871 } finally { 872 jf.close(); 873 } 874 } 875 } 876 } 877 } 878 return MessageFormat.format(currVer, new Object [] {implVers}); 879 } 880 private static String findCurrVer(JarFile jar, String infix) throws IOException { 881 ZipEntry bundle = jar.getEntry("org/netbeans/core/startup/Bundle" + infix + ".properties"); if (bundle == null) { 884 bundle = jar.getEntry("org/netbeans/core/Bundle" + infix + ".properties"); } 887 if (bundle == null) { 888 return null; 889 } 890 Properties props = new Properties (); 891 InputStream is = jar.getInputStream(bundle); 892 try { 893 props.load(is); 894 } finally { 895 is.close(); 896 } 897 return props.getProperty("currentVersion"); } 899 900 905 public static boolean isLabelValid(String supposedLabel) { 906 if (supposedLabel == null) { 907 return false; 908 } 909 for (Iterator it = NbPlatform.getPlatforms().iterator(); it.hasNext(); ) { 910 String label = ((NbPlatform) it.next()).getLabel(); 911 if (supposedLabel.equals(label)) { 912 return false; 913 } 914 } 915 return true; 916 } 917 918 public String toString() { 919 return "NbPlatform[" + getID() + ":" + getDestDir() + ";sources=" + Arrays.asList(getSourceRoots()) + ";javadoc=" + Arrays.asList(getJavadocRoots()) + "]"; } 921 922 925 public int getHarnessVersion() { 926 if (harnessVersion != -1) { 927 return harnessVersion; 928 } 929 if (!isValid()) { 930 return harnessVersion = HARNESS_VERSION_UNKNOWN; 931 } 932 File harnessJar = new File (harness, "modules" + File.separatorChar + "org-netbeans-modules-apisupport-harness.jar"); if (harnessJar.isFile()) { 934 try { 935 JarFile jf = new JarFile (harnessJar); 936 try { 937 String spec = jf.getManifest().getMainAttributes().getValue(ManifestManager.OPENIDE_MODULE_SPECIFICATION_VERSION); 938 if (spec != null) { 939 SpecificationVersion v = new SpecificationVersion(spec); 940 if (v.compareTo(new SpecificationVersion("1.10")) >= 0) { return harnessVersion = HARNESS_VERSION_60; 942 } else if (v.compareTo(new SpecificationVersion("1.9")) >= 0) { return harnessVersion = HARNESS_VERSION_55u1; 944 } else if (v.compareTo(new SpecificationVersion("1.7")) >= 0) { return harnessVersion = HARNESS_VERSION_50u1; 946 } else if (v.compareTo(new SpecificationVersion("1.6")) >= 0) { return harnessVersion = HARNESS_VERSION_50; 948 } } 950 } finally { 951 jf.close(); 952 } 953 } catch (IOException e) { 954 Util.err.notify(ErrorManager.INFORMATIONAL, e); 955 } catch (NumberFormatException e) { 956 Util.err.notify(ErrorManager.INFORMATIONAL, e); 957 } 958 } 959 return harnessVersion = HARNESS_VERSION_UNKNOWN; 960 } 961 962 965 public File getHarnessLocation() { 966 return harness; 967 } 968 969 972 public File getBundledHarnessLocation() { 973 return findHarness(nbdestdir); 974 } 975 976 979 public void setHarnessLocation(final File harness) throws IOException { 980 if (harness.equals(this.harness)) { 981 return; 982 } 983 try { 984 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() { 985 public Object run() throws IOException { 986 EditableProperties props = PropertyUtils.getGlobalProperties(); 987 storeHarnessLocation(id, nbdestdir, harness, props); 988 PropertyUtils.putGlobalProperties(props); 989 return null; 990 } 991 }); 992 } catch (MutexException e) { 993 throw (IOException ) e.getException(); 994 } 995 this.harness = harness; 996 harnessVersion = -1; 997 } 998 999 1004 public static String getHarnessVersionDisplayName(int version) { 1005 switch (version) { 1006 case HARNESS_VERSION_50: 1007 return NbBundle.getMessage(NbPlatform.class, "LBL_harness_version_5.0"); 1008 case HARNESS_VERSION_50u1: 1009 return NbBundle.getMessage(NbPlatform.class, "LBL_harness_version_5.0u1"); 1010 case HARNESS_VERSION_55u1: 1011 return NbBundle.getMessage(NbPlatform.class, "LBL_harness_version_5.5u1"); 1012 case HARNESS_VERSION_60: 1013 return NbBundle.getMessage(NbPlatform.class, "LBL_harness_version_6.0"); 1014 default: 1015 assert version == HARNESS_VERSION_UNKNOWN; 1016 return NbBundle.getMessage(NbPlatform.class, "LBL_harness_version_unknown"); 1017 } 1018 } 1019 1020 public void addPropertyChangeListener(PropertyChangeListener listener) { 1021 pcs.addPropertyChangeListener(listener); 1022 } 1023 1024 public void removePropertyChangeListener(PropertyChangeListener listener) { 1025 pcs.removePropertyChangeListener(listener); 1026 } 1027 1028} 1029 | Popular Tags |