예제 #1
0
  /**
   * This will refresh the workspace contributions if needed, then search through the workspace
   * loaded bundles for a class corresponding to <code>qualifiedName</code>.
   *
   * @param qualifiedName The qualified name of the class we seek to load.
   * @param honorOSGiVisibility If <code>true</code>, this will only search through exported
   *     packages for the class <code>qualifiedName</code>. Otherwise we'll search through all
   *     bundles by simply trying to load the class and catching the {@link ClassNotFoundException}
   *     if it isn't loadable.
   * @return The class <code>qualifiedName</code> if it could be found in the workspace bundles,
   *     <code>null</code> otherwise.
   */
  public synchronized Class<?> getClass(String qualifiedName, boolean honorOSGiVisibility) {
    if (changedContributions.size() > 0) {
      refreshContributions();
    }

    // Has an instance of this class already been loaded?
    Class<?> clazz = null;
    final WorkspaceClassInstance workspaceInstance = workspaceLoadedClasses.get(qualifiedName);
    if (workspaceInstance != null) {
      if (workspaceInstance.isStale()) {
        for (Map.Entry<IPluginModelBase, Bundle> entry : workspaceInstalledBundles.entrySet()) {
          final IPluginModelBase model = entry.getKey();
          if (workspaceInstance
              .getBundle()
              .equals(model.getBundleDescription().getSymbolicName())) {
            clazz = internalLoadClass(entry.getValue(), qualifiedName);
            workspaceInstance.setStale(false);
            workspaceInstance.setClass(clazz);
            break;
          }
        }
      } else {
        clazz = workspaceInstance.getInstance().getClass();
      }
    }
    if (clazz != null) {
      return clazz;
    }

    // The class hasn't been instantiated yet ; search for the class without instantiating it
    Iterator<Map.Entry<IPluginModelBase, Bundle>> iterator =
        workspaceInstalledBundles.entrySet().iterator();
    while (clazz == null && iterator.hasNext()) {
      Map.Entry<IPluginModelBase, Bundle> entry = iterator.next();
      /*
       * If we're asked to honor OSGi package visibility, we'll first check the "Export-Package" header
       * of this bundle's MANIFEST.
       */
      if (!honorOSGiVisibility || hasCorrespondingExportPackage(entry.getKey(), qualifiedName)) {
        try {
          clazz = entry.getValue().loadClass(qualifiedName);
        } catch (ClassNotFoundException e) {
          // Swallow this ; we'll log the issue later on if we cannot find the class at all
        }
      }
    }

    if (clazz == null) {
      AcceleoCommonPlugin.log(
          AcceleoCommonMessages.getString(
              "BundleClassLookupFailure", //$NON-NLS-1$
              qualifiedName),
          false);
    }

    return clazz;
  }
예제 #2
0
 /**
  * This will install or refresh the given workspace contribution if needed, then search through it
  * for a class corresponding to <code>qualifiedName</code>.
  *
  * @param project The project that is to be dynamically installed when Acceleo searches for
  *     workspace contributions.
  * @param qualifiedName The qualified name of the class we seek to load.
  * @return An instance of the class <code>qualifiedName</code> if it could be found in the
  *     workspace bundles, <code>null</code> otherwise.
  */
 public synchronized Class<?> getClass(IProject project, String qualifiedName) {
   Class<?> instance = null;
   addWorkspaceContribution(project);
   refreshContributions();
   final IPluginModelBase model = PluginRegistry.findModel(project);
   final Bundle installedBundle = workspaceInstalledBundles.get(model);
   if (installedBundle != null) {
     instance = internalLoadClass(installedBundle, qualifiedName);
   }
   return instance;
 }
예제 #3
0
  /**
   * This will try and load a ResourceBundle for the given qualified name.
   *
   * @param qualifiedName Name if the resource bundle we need to load.
   * @return The loaded resource bundle.
   */
  public synchronized ResourceBundle getResourceBundle(String qualifiedName) {
    MissingResourceException originalException = null;

    // shortcut evaluation in case this properties file can be found in the current classloader.
    try {
      ResourceBundle bundle = ResourceBundle.getBundle(qualifiedName);
      return bundle;
    } catch (MissingResourceException e) {
      originalException = e;
    }

    if (changedContributions.size() > 0) {
      refreshContributions();
    }

    /*
     * We'll iterate over the bundles that have been installed from the workspace, search for one that
     * exports a package of the name we're looking for, then try and load the properties file from this
     * bundle's class loader (bundle.getResource()). If the resource couldn't be found in that bundle, we
     * loop over to the next.
     */
    for (Map.Entry<IPluginModelBase, Bundle> entry : workspaceInstalledBundles.entrySet()) {
      final Bundle bundle = entry.getValue();
      URL propertiesResource =
          bundle.getResource(qualifiedName.replace('.', '/') + ".properties"); // $NON-NLS-1$
      if (propertiesResource != null) {
        InputStream stream = null;
        try {
          stream = propertiesResource.openStream();
          // make sure this stream is buffered
          stream = new BufferedInputStream(stream);
          return new PropertyResourceBundle(stream);
        } catch (IOException e) {
          // Swallow this, we'll throw the original MissingResourceException
        } finally {
          try {
            if (stream != null) {
              stream.close();
            }
          } catch (IOException e) {
            // Swallow this, we'll throw the original MissingResourceException
          }
        }
      }
    }
    throw originalException;
  }