1 11 12 package org.eclipse.osgi.framework.adaptor.core; 13 14 import java.io.File ; 15 import java.io.IOException ; 16 import java.net.MalformedURLException ; 17 import java.net.URL ; 18 import java.util.*; 19 import org.eclipse.osgi.framework.adaptor.*; 20 import org.eclipse.osgi.framework.debug.Debug; 21 import org.eclipse.osgi.framework.internal.core.Constants; 22 import org.eclipse.osgi.framework.internal.protocol.bundleentry.Handler; 23 import org.eclipse.osgi.framework.util.Headers; 24 import org.eclipse.osgi.util.ManifestElement; 25 import org.eclipse.osgi.util.NLS; 26 import org.osgi.framework.*; 27 28 36 public abstract class AbstractBundleData implements BundleData, Cloneable { 37 38 39 protected AbstractFrameworkAdaptor adaptor; 40 41 44 protected Dictionary manifest = null; 45 46 49 protected Bundle bundle; 50 51 52 protected long id; 53 54 55 protected File bundleStoreDir; 56 57 58 protected BundleFile baseBundleFile; 59 61 62 private String location; 63 64 65 private String fileName; 66 67 68 private String [] nativePaths; 69 70 71 private int generation = 1; 72 73 74 private int startLevel = -1; 75 76 79 protected File dirData; 80 81 82 private int status = 0; 83 84 85 private boolean reference; 86 87 88 private long lastModified; 89 90 92 private String symbolicName; 94 private Version version; 95 private String activator; 96 private String classpath; 97 private String executionEnvironment; 98 private String dynamicImports; 99 private int type; 100 101 103 108 public AbstractBundleData(AbstractFrameworkAdaptor adaptor, long id) { 109 this.adaptor = adaptor; 110 this.id = id; 111 initBundleStoreDirs(String.valueOf(id)); 112 } 113 114 117 public Dictionary getManifest() throws BundleException { 118 if (manifest == null) { 119 synchronized (this) { 120 if (manifest == null) { 122 URL url = getEntry(Constants.OSGI_BUNDLE_MANIFEST); 123 if (url == null) { 124 throw new BundleException(NLS.bind(AdaptorMsg.MANIFEST_NOT_FOUND_EXCEPTION, Constants.OSGI_BUNDLE_MANIFEST, getLocation())); 125 } 126 try { 127 manifest = Headers.parseManifest(url.openStream()); 128 } catch (IOException e) { 129 throw new BundleException(NLS.bind(AdaptorMsg.MANIFEST_NOT_FOUND_EXCEPTION, Constants.OSGI_BUNDLE_MANIFEST, getLocation()), e); 130 } 131 } 132 } 133 } 134 return manifest; 135 } 136 137 140 public void setBundle(Bundle bundle) { 141 this.bundle = bundle; 142 } 143 144 148 public Bundle getBundle() { 149 return bundle; 150 } 151 152 155 public long getBundleID() { 156 return (id); 157 } 158 159 162 public URL getEntry(String path) { 163 BundleEntry entry = getBaseBundleFile().getEntry(path); 164 if (entry == null) { 165 return null; 166 } 167 if (path.length() == 0 || path.charAt(0) != '/') 168 path = path = '/' + path; 169 try { 170 return new URL (Constants.OSGI_ENTRY_URL_PROTOCOL, Long.toString(id), 0, path, new Handler(entry)); 172 } catch (MalformedURLException e) { 173 return null; 174 } 175 } 176 177 180 public Enumeration getEntryPaths(String path) { 181 return getBaseBundleFile().getEntryPaths(path); 182 } 183 184 187 public org.eclipse.osgi.framework.adaptor.BundleClassLoader createClassLoader(ClassLoaderDelegate delegate, BundleProtectionDomain domain, String [] bundleclasspath) { 188 return getAdaptor().getElementFactory().createClassLoader(delegate, domain, bundleclasspath, this); 189 } 190 191 195 public AbstractFrameworkAdaptor getAdaptor() { 196 return adaptor; 197 } 198 199 204 static String [] getClassPath(ManifestElement[] classpath) { 205 if (classpath == null) { 206 if (Debug.DEBUG && Debug.DEBUG_LOADER) 207 Debug.println(" no classpath"); 209 return new String [] {"."}; } 211 212 ArrayList result = new ArrayList(classpath.length); 213 for (int i = 0; i < classpath.length; i++) { 214 if (Debug.DEBUG && Debug.DEBUG_LOADER) 215 Debug.println(" found classpath entry " + classpath[i].getValueComponents()); String [] paths = classpath[i].getValueComponents(); 217 for (int j = 0; j < paths.length; j++) { 218 result.add(paths[j]); 219 } 220 } 221 222 return (String []) result.toArray(new String [result.size()]); 223 } 224 225 229 public String getLocation() { 230 return location; 231 } 232 236 public void setLocation(String location) { 237 this.location = location; 238 } 239 240 244 public String getFileName() { 245 return fileName; 246 } 247 248 252 public void setFileName(String fileName) { 253 this.fileName = fileName; 254 } 255 256 260 public String [] getNativePaths() { 261 return nativePaths; 262 } 263 264 268 public String getNativePathsString() { 269 if (nativePaths == null || nativePaths.length == 0) 270 return null; 271 StringBuffer sb = new StringBuffer (); 272 for (int i = 0; i < nativePaths.length; i++) { 273 sb.append(nativePaths[i]); 274 if (i < nativePaths.length - 1) 275 sb.append(','); 276 } 277 return sb.toString(); 278 } 279 280 284 public void setNativePaths(String [] nativePaths) { 285 this.nativePaths = nativePaths; 286 } 287 288 292 public void setNativePaths(String nativePaths) { 293 if (nativePaths == null) 294 return; 295 ArrayList result = new ArrayList(5); 296 StringTokenizer st = new StringTokenizer(nativePaths, ","); while (st.hasMoreTokens()) { 298 String path = st.nextToken(); 299 result.add(path); 300 } 301 setNativePaths((String []) result.toArray(new String [result.size()])); 302 } 303 304 308 public int getGeneration() { 309 return generation; 310 } 311 312 316 public void setGeneration(int generation) { 317 this.generation = generation; 318 } 319 320 323 public long getLastModified() { 324 return lastModified; 325 } 326 327 331 public void setLastModified(long lastModified) { 332 this.lastModified = lastModified; 333 } 334 335 338 public int getStartLevel() { 339 return startLevel; 340 } 341 342 345 public void setStartLevel(int startLevel) { 346 this.startLevel = startLevel; 347 } 348 349 352 public int getStatus() { 353 return status; 354 } 355 356 359 public void setStatus(int status) { 360 this.status = status; 361 } 362 363 367 public boolean isReference() { 368 return reference; 369 } 370 371 375 public void setReference(boolean reference) { 376 this.reference = reference; 377 } 378 379 381 383 386 public String getSymbolicName() { 387 return symbolicName; 388 } 389 390 394 public File getBundleStoreDir() { 395 return bundleStoreDir; 396 } 397 398 402 public void setSymbolicName(String symbolicName) { 403 this.symbolicName = symbolicName; 404 } 405 406 410 protected void loadFromManifest() throws BundleException { 411 getManifest(); 412 if (manifest == null) 413 throw new BundleException(NLS.bind(AdaptorMsg.ADAPTOR_ERROR_GETTING_MANIFEST, getLocation())); setVersion(Version.parseVersion((String ) manifest.get(Constants.BUNDLE_VERSION))); 415 ManifestElement[] bsnHeader = ManifestElement.parseHeader(Constants.BUNDLE_SYMBOLICNAME, (String ) manifest.get(Constants.BUNDLE_SYMBOLICNAME)); 416 int bundleType = 0; 417 if (bsnHeader != null) { 418 setSymbolicName(bsnHeader[0].getValue()); 419 String singleton = bsnHeader[0].getDirective(Constants.SINGLETON_DIRECTIVE); 420 if (singleton == null) 421 singleton = bsnHeader[0].getAttribute(Constants.SINGLETON_DIRECTIVE); 422 if ("true".equals(singleton)) bundleType |= TYPE_SINGLETON; 424 } 425 setClassPathString((String ) manifest.get(Constants.BUNDLE_CLASSPATH)); 426 setActivator((String ) manifest.get(Constants.BUNDLE_ACTIVATOR)); 427 String host = (String ) manifest.get(Constants.FRAGMENT_HOST); 428 if (host != null) { 429 bundleType |= TYPE_FRAGMENT; 430 ManifestElement[] hostElement = ManifestElement.parseHeader(Constants.FRAGMENT_HOST, host); 431 if (Constants.getInternalSymbolicName().equals(hostElement[0].getValue()) || Constants.OSGI_SYSTEM_BUNDLE.equals(hostElement[0].getValue())) { 432 String extensionType = hostElement[0].getDirective("extension"); if (extensionType == null || extensionType.equals("framework")) bundleType |= TYPE_FRAMEWORK_EXTENSION; 435 else 436 bundleType |= TYPE_BOOTCLASSPATH_EXTENSION; 437 } 438 } 439 setType(bundleType); 440 setExecutionEnvironment((String ) manifest.get(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT)); 441 setDynamicImports((String ) manifest.get(Constants.DYNAMICIMPORT_PACKAGE)); 442 } 443 444 447 public Version getVersion() { 448 return version; 449 } 450 451 455 public void setVersion(Version version) { 456 this.version = version; 457 } 458 459 462 public String getActivator() { 463 return activator; 464 } 465 466 470 protected File getDataDir() { 471 return dirData; 472 } 473 474 478 protected void setBundleStoreDir(File bundleStoreDir) { 479 this.bundleStoreDir = bundleStoreDir; 480 } 481 482 486 protected void initBundleStoreDirs(String bundleID) { 487 setBundleStoreDir(new File (((AbstractFrameworkAdaptor) adaptor).getBundleStoreRootDir(), bundleID)); 488 } 489 490 494 public void setActivator(String activator) { 495 this.activator = activator; 496 } 497 498 501 public String [] getClassPath() throws BundleException { 502 ManifestElement[] classpathElements = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, classpath); 503 return getClassPath(classpathElements); 504 } 505 506 510 public String getClassPathString() { 511 return classpath; 512 } 513 514 518 public void setClassPathString(String classpath) { 519 this.classpath = classpath; 520 } 521 522 525 public String getExecutionEnvironment() { 526 return executionEnvironment; 527 } 528 529 533 public void setExecutionEnvironment(String executionEnvironment) { 534 this.executionEnvironment = executionEnvironment; 535 } 536 537 540 public String getDynamicImports() { 541 return dynamicImports; 542 } 543 544 548 public void setDynamicImports(String dynamicImports) { 549 this.dynamicImports = dynamicImports; 550 } 551 552 555 public int getType() { 556 return type; 557 } 558 559 563 public void setType(int type) { 564 this.type = type; 565 } 566 567 569 572 public boolean matchDNChain(String pattern) { 573 if (System.getSecurityManager() == null) 574 return false; 575 576 if (getBaseBundleFile() instanceof SignedBundle) 577 return ((SignedBundle) getBaseBundleFile()).matchDNChain(pattern); 578 return false; 579 } 580 581 588 protected AbstractBundleData nextGeneration(String referenceFile) throws IOException { 589 int nextGeneration = getGeneration(); 590 591 while (nextGeneration < Integer.MAX_VALUE) { 592 nextGeneration++; 593 594 File nextDirGeneration = new File (getBundleStoreDir(), String.valueOf(nextGeneration)); 595 596 if (nextDirGeneration.exists()) { 597 continue; 598 } 599 600 AbstractBundleData next; 601 try { 602 next = (AbstractBundleData) clone(); 603 } catch (CloneNotSupportedException e) { 604 throw new InternalError (); 606 } 607 608 next.setGeneration(nextGeneration); 609 610 if (referenceFile != null) { 611 next.setReference(true); 612 next.setFileName(referenceFile); 613 } else { 614 if (next.isReference()) { 615 next.setReference(false); 616 next.setFileName(AbstractFrameworkAdaptor.BUNDLEFILE_NAME); 617 } 618 } 619 620 next.manifest = null; 622 return (next); 623 } 624 625 throw new IOException (AdaptorMsg.ADAPTOR_STORAGE_EXCEPTION); 626 } 627 628 633 public void initializeNewBundle() throws IOException , BundleException { 634 createBaseBundleFile(); 635 loadFromManifest(); 636 } 637 638 643 protected BundleFile createBaseBundleFile() throws IOException { 644 baseBundleFile = getAdaptor().createBaseBundleFile(getBaseFile(), this); 645 return baseBundleFile; 646 } 647 648 654 protected File getBaseFile() { 655 return isReference() ? new File (getFileName()) : new File (createGenerationDir(), getFileName()); 656 } 657 658 664 protected File [] getClasspathFiles(String [] classpaths) { 665 ArrayList results = new ArrayList(classpaths.length); 666 for (int i = 0; i < classpaths.length; i++) { 667 if (".".equals(classpaths[i])) results.add(getBaseFile()); 669 else { 670 File result = getBaseBundleFile().getFile(classpaths[i]); 671 if (result != null) 672 results.add(result); 673 } 674 } 675 return (File []) results.toArray(new File [results.size()]); 676 } 677 678 682 protected void setDataDir(File dirData) { 683 this.dirData = dirData; 684 } 685 686 689 public String findLibrary(String libname) { 690 String mappedName = System.mapLibraryName(libname); 691 String path = null; 692 693 if (Debug.DEBUG && Debug.DEBUG_LOADER) { 694 Debug.println(" mapped library name: " + mappedName); } 696 697 path = findNativePath(mappedName); 698 699 if (path == null) { 700 if (Debug.DEBUG && Debug.DEBUG_LOADER) { 701 Debug.println(" library does not exist: " + mappedName); } 703 path = findNativePath(libname); 704 } 705 706 if (Debug.DEBUG && Debug.DEBUG_LOADER) { 707 Debug.println(" returning library: " + path); } 709 return path; 710 } 711 712 715 public void open() throws IOException { 716 baseBundleFile.open(); 717 } 718 719 726 protected String findNativePath(String libname) { 727 if (!libname.startsWith("/")) { libname = '/' + libname; 729 } 730 String [] nativepaths = getNativePaths(); 731 if (nativepaths != null) { 732 for (int i = 0; i < nativepaths.length; i++) { 733 if (nativepaths[i].endsWith(libname)) { 734 File nativeFile = baseBundleFile.getFile(nativepaths[i]); 735 if (nativeFile != null) 736 return nativeFile.getAbsolutePath(); 737 } 738 } 739 } 740 return null; 741 } 742 743 750 public File createGenerationDir() { 751 File generationDir = getGenerationDir(); 752 if (!generationDir.exists() && (!adaptor.canWrite() || !generationDir.mkdirs())) { 753 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 754 Debug.println("Unable to create bundle generation directory: " + generationDir.getPath()); } 756 } 757 758 return generationDir; 759 } 760 761 766 public BundleFile getBaseBundleFile() { 767 return baseBundleFile; 768 } 769 770 773 public void close() throws IOException { 774 if (baseBundleFile != null) { 775 baseBundleFile.close(); 776 } 777 } 778 779 785 public File getDataFile(String path) { 786 if (getDataDir() == null) { 788 File dataRoot = adaptor.getDataRootDir(); 789 if (dataRoot == null) 790 throw new IllegalStateException (AdaptorMsg.ADAPTOR_DATA_AREA_NOT_SET); 791 setDataDir(new File (dataRoot, id + "/" + AbstractFrameworkAdaptor.DATA_DIR_NAME)); } 793 if (!getDataDir().exists() && (!adaptor.canWrite() || !getDataDir().mkdirs())) { 794 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 795 Debug.println("Unable to create bundle data directory: " + getDataDir().getPath()); } 797 } 798 799 return (new File (getDataDir(), path)); 800 } 801 802 805 public void installNativeCode(String [] nativepaths) throws BundleException { 806 StringBuffer sb = new StringBuffer (); 807 for (int i = 0; i < nativepaths.length; i++) { 808 File nativeFile = baseBundleFile.getFile(nativepaths[i]); 810 if (nativeFile == null) { 811 throw new BundleException(NLS.bind(AdaptorMsg.BUNDLE_NATIVECODE_EXCEPTION, nativepaths[i])); 812 } 813 sb.append(nativepaths[i]); 814 if (i < nativepaths.length - 1) { 815 sb.append(","); } 817 } 818 if (sb.length() > 0) 819 setNativePaths(sb.toString()); 820 } 821 822 827 protected File getGenerationDir() { 828 return new File (getBundleStoreDir(), String.valueOf(getGeneration())); 829 } 830 831 837 public File getParentGenerationDir() { 838 return null; 839 } 840 } 841 | Popular Tags |