Esempio n. 1
0
  final String findLocalLibrary(final String name) {
    String result = null;
    try {
      result = (String) searchHooks(name, PRE_LIBRARY);
    } catch (FileNotFoundException e) {
      return null;
    } catch (ClassNotFoundException e) {
      // will not happen
    }
    if (result != null) return result;
    result = bundle.getBundleData().findLibrary(name);
    if (result != null) return result;

    // look in fragments imports ...
    BundleFragment[] fragments = bundle.getFragments();
    if (fragments != null)
      for (int i = 0; i < fragments.length; i++) {
        result = fragments[i].getBundleData().findLibrary(name);
        if (result != null) return result;
      }
    try {
      return (String) searchHooks(name, POST_LIBRARY);
    } catch (FileNotFoundException e) {
      return null; // this is not necessary; but being consistent in case another step is added
      // below
    } catch (ClassNotFoundException e) {
      // will not happen
    }
    return null;
  }
Esempio n. 2
0
 public final synchronized BundleClassLoader createClassLoader() {
   if (classloader != null) return classloader;
   String[] classpath;
   try {
     classpath = bundle.getBundleData().getClassPath();
   } catch (BundleException e) {
     // no classpath
     classpath = new String[0];
     bundle.getFramework().publishFrameworkEvent(FrameworkEvent.ERROR, bundle, e);
   }
   if (classpath == null) {
     // no classpath
     classpath = new String[0];
     bundle
         .getFramework()
         .publishFrameworkEvent(
             FrameworkEvent.ERROR,
             bundle,
             new BundleException(Msg.BUNDLE_NO_CLASSPATH_MATCH, BundleException.MANIFEST_ERROR));
   }
   BundleClassLoader bcl = createBCLPrevileged(bundle.getProtectionDomain(), classpath);
   parent = getParentPrivileged(bcl);
   classloader = bcl;
   return classloader;
 }
Esempio n. 3
0
 private Object searchHooks(String name, int type)
     throws ClassNotFoundException, FileNotFoundException {
   ClassLoaderDelegateHook[] delegateHooks = bundle.getFramework().getDelegateHooks();
   if (delegateHooks == null) return null;
   Object result = null;
   for (int i = 0; i < delegateHooks.length && result == null; i++) {
     switch (type) {
       case PRE_CLASS:
         result = delegateHooks[i].preFindClass(name, createClassLoader(), bundle.getBundleData());
         break;
       case POST_CLASS:
         result =
             delegateHooks[i].postFindClass(name, createClassLoader(), bundle.getBundleData());
         break;
       case PRE_RESOURCE:
         result =
             delegateHooks[i].preFindResource(name, createClassLoader(), bundle.getBundleData());
         break;
       case POST_RESOURCE:
         result =
             delegateHooks[i].postFindResource(name, createClassLoader(), bundle.getBundleData());
         break;
       case PRE_RESOURCES:
         result =
             delegateHooks[i].preFindResources(name, createClassLoader(), bundle.getBundleData());
         break;
       case POST_RESOURCES:
         result =
             delegateHooks[i].postFindResources(name, createClassLoader(), bundle.getBundleData());
         break;
       case PRE_LIBRARY:
         result =
             delegateHooks[i].preFindLibrary(name, createClassLoader(), bundle.getBundleData());
         break;
       case POST_LIBRARY:
         result =
             delegateHooks[i].postFindLibrary(name, createClassLoader(), bundle.getBundleData());
         break;
     }
   }
   return result;
 }
Esempio n. 4
0
  BundleClassLoader createBCL(final BundleProtectionDomain pd, final String[] cp) {
    BundleClassLoader bcl = bundle.getBundleData().createClassLoader(BundleLoader.this, pd, cp);
    // attach existing fragments to classloader
    BundleFragment[] fragments = bundle.getFragments();
    if (fragments != null)
      for (int i = 0; i < fragments.length; i++) {
        try {
          bcl.attachFragment(
              fragments[i].getBundleData(),
              fragments[i].getProtectionDomain(),
              fragments[i].getBundleData().getClassPath());
        } catch (BundleException be) {
          bundle.getFramework().publishFrameworkEvent(FrameworkEvent.ERROR, bundle, be);
        }
      }

    // finish the initialization of the classloader.
    bcl.initialize();
    return bcl;
  }
Esempio n. 5
0
 /**
  * Return a string representation of this loader.
  *
  * @return String
  */
 public final String toString() {
   BundleData result = bundle.getBundleData();
   return result == null ? "BundleLoader.bundledata == null!" : result.toString(); // $NON-NLS-1$
 }
Esempio n. 6
0
  /**
   * BundleLoader runtime constructor. This object is created lazily when the first request for a
   * resource is made to this bundle.
   *
   * @param bundle Bundle object for this loader.
   * @param proxy the BundleLoaderProxy for this loader.
   * @exception org.osgi.framework.BundleException
   */
  protected BundleLoader(BundleHost bundle, BundleLoaderProxy proxy) throws BundleException {
    this.bundle = bundle;
    this.proxy = proxy;
    try {
      bundle.getBundleData().open(); /* make sure the BundleData is open */
    } catch (IOException e) {
      throw new BundleException(Msg.BUNDLE_READ_EXCEPTION, e);
    }
    BundleDescription description = proxy.getBundleDescription();
    // init the require bundles list.
    BundleDescription[] required = description.getResolvedRequires();
    if (required.length > 0) {
      // get a list of re-exported symbolic names
      HashSet reExportSet = new HashSet(required.length);
      BundleSpecification[] requiredSpecs = description.getRequiredBundles();
      if (requiredSpecs != null && requiredSpecs.length > 0)
        for (int i = 0; i < requiredSpecs.length; i++)
          if (requiredSpecs[i].isExported()) reExportSet.add(requiredSpecs[i].getName());

      requiredBundles = new BundleLoaderProxy[required.length];
      int[] reexported = new int[required.length];
      int reexportIndex = 0;
      for (int i = 0; i < required.length; i++) {
        requiredBundles[i] = getLoaderProxy(required[i]);
        if (reExportSet.contains(required[i].getSymbolicName())) reexported[reexportIndex++] = i;
      }
      if (reexportIndex > 0) {
        reexportTable = new int[reexportIndex];
        System.arraycopy(reexported, 0, reexportTable, 0, reexportIndex);
      } else {
        reexportTable = null;
      }
      requiredSources = new KeyedHashSet(10, false);
    } else {
      requiredBundles = null;
      reexportTable = null;
      requiredSources = null;
    }

    // init the provided packages set
    ExportPackageDescription[] exports = description.getSelectedExports();
    if (exports != null && exports.length > 0) {
      exportedPackages =
          Collections.synchronizedCollection(
              exports.length > 10
                  ? (Collection) new HashSet(exports.length)
                  : new ArrayList(exports.length));
      initializeExports(exports, exportedPackages);
    } else {
      exportedPackages = Collections.synchronizedCollection(new ArrayList(0));
    }

    ExportPackageDescription substituted[] = description.getSubstitutedExports();
    if (substituted.length > 0) {
      substitutedPackages =
          substituted.length > 10
              ? (Collection) new HashSet(substituted.length)
              : new ArrayList(substituted.length);
      for (int i = 0; i < substituted.length; i++)
        substitutedPackages.add(substituted[i].getName());
    } else {
      substitutedPackages = null;
    }

    // This is the fastest way to access to the description for fragments since the
    // hostdescription.getFragments() is slow
    BundleFragment[] fragmentObjects = bundle.getFragments();
    BundleDescription[] fragments =
        new BundleDescription[fragmentObjects == null ? 0 : fragmentObjects.length];
    for (int i = 0; i < fragments.length; i++)
      fragments[i] = fragmentObjects[i].getBundleDescription();
    // init the dynamic imports tables
    if (description.hasDynamicImports()) addDynamicImportPackage(description.getImportPackages());
    // ...and its fragments
    for (int i = 0; i < fragments.length; i++)
      if (fragments[i].isResolved() && fragments[i].hasDynamicImports())
        addDynamicImportPackage(fragments[i].getImportPackages());

    // Initialize the policy handler
    String buddyList = null;
    try {
      buddyList = (String) bundle.getBundleData().getManifest().get(Constants.BUDDY_LOADER);
    } catch (BundleException e) {
      // do nothing; buddyList == null
    }
    policy =
        buddyList != null
            ? new PolicyHandler(this, buddyList, bundle.getFramework().getPackageAdmin())
            : null;
    if (policy != null) policy.open(bundle.getFramework().getSystemBundleContext());
  }