Esempio n. 1
0
  /**
   * This will try and find a resource of the given name using the bundle from which was originally
   * loaded the given class so as to try and detect if it is jarred. If <code>clazz</code> hasn't
   * been loaded from a bundle class loader, we'll resort to the default class loader mechanism.
   * This will only return <code>null</code> in the case where the resource at <code>resourcePath
   * </code> cannot be located at all.
   *
   * @param clazz Class which class loader will be used to try and locate the resource.
   * @param resourcePath Path of the resource we seek, relative to the class.
   * @return The URL of the resource as we could locate it.
   * @throws IOException This will be thrown if we fail to convert bundle-scheme URIs into
   *     file-scheme URIs.
   */
  public static URL getResourceURL(Class<?> clazz, String resourcePath) throws IOException {
    BundleContext context = AcceleoCommonPlugin.getDefault().getContext();
    ServiceReference packageAdminReference =
        context.getServiceReference(PackageAdmin.class.getName());
    PackageAdmin packageAdmin = null;
    if (packageAdminReference != null) {
      packageAdmin = (PackageAdmin) context.getService(packageAdminReference);
    }

    URL resourceURL = null;
    if (packageAdmin != null) {
      Bundle bundle = packageAdmin.getBundle(clazz);
      if (bundle != null) {
        final String pathSeparator = "/"; // $NON-NLS-1$
        // We found the appropriate bundle. We'll now try and determine whether the emtl is jarred
        resourceURL = getBundleResourceURL(bundle, pathSeparator, resourcePath);
      }
    }
    /*
     * We couldn't locate either the bundle which loaded the class or the resource. Resort to the class
     * loader and return null if it cannot locate the resource either.
     */
    if (resourceURL == null) {
      resourceURL = clazz.getResource(resourcePath);
    }

    if (packageAdminReference != null) {
      context.ungetService(packageAdminReference);
    }
    return resourceURL;
  }
 /**
  * Returns the bundle id of the bundle that contains the provided object, or <code>null</code> if
  * the bundle could not be determined.
  */
 public String getBundleId(Object object) {
   if (bundleTracker == null) {
     if (JobManager.DEBUG) JobMessages.message("Bundle tracker is not set"); // $NON-NLS-1$
     return null;
   }
   PackageAdmin packageAdmin = (PackageAdmin) bundleTracker.getService();
   if (object == null) return null;
   if (packageAdmin == null) return null;
   Bundle source = packageAdmin.getBundle(object.getClass());
   if (source != null && source.getSymbolicName() != null) return source.getSymbolicName();
   return null;
 }
Esempio n. 3
0
  /**
   * Return the bundle of the given class.
   *
   * @param clazz The class
   * @return The bundle of the given class
   * @since 3.1
   */
  public static Bundle getBundle(Class<?> clazz) {
    Bundle bundle = null;
    BundleContext context = AcceleoCommonPlugin.getDefault().getContext();
    ServiceReference packageAdminReference =
        context.getServiceReference(PackageAdmin.class.getName());
    PackageAdmin packageAdmin = null;
    if (packageAdminReference != null) {
      packageAdmin = (PackageAdmin) context.getService(packageAdminReference);
    }

    if (packageAdmin != null) {
      bundle = packageAdmin.getBundle(clazz);
    }
    if (packageAdminReference != null) {
      context.ungetService(packageAdminReference);
    }
    return bundle;
  }
Esempio n. 4
0
  /**
   * This can be used to check whether the given class is located in a dynamically installed bundle.
   *
   * @param clazz The class of which we need to determine the originating bundle.
   * @return <code>true</code> if the given class has been loaded from a dynamic bundle, <code>false
   *     </code> otherwise.
   */
  public boolean isInDynamicBundle(Class<?> clazz) {
    BundleContext context = AcceleoCommonPlugin.getDefault().getContext();
    ServiceReference packageAdminReference =
        context.getServiceReference(PackageAdmin.class.getName());
    PackageAdmin packageAdmin = null;
    if (packageAdminReference != null) {
      packageAdmin = (PackageAdmin) context.getService(packageAdminReference);
    }

    if (packageAdmin != null) {
      Bundle bundle = packageAdmin.getBundle(clazz);
      if (workspaceInstalledBundles.values().contains(bundle)) {
        return true;
      }
    }

    if (packageAdminReference != null) {
      context.ungetService(packageAdminReference);
    }

    return false;
  }
Esempio n. 5
0
 public Bundle getProvider(BundleContext context) {
   ServiceReference sref = context.getServiceReference(PackageAdmin.class.getName());
   PackageAdmin packageAdmin = (PackageAdmin) context.getService(sref);
   Bundle provider = packageAdmin.getBundle(getClass());
   return provider;
 }