コード例 #1
0
  /**
   * Attempt to create an instance of thea provider for the given service
   *
   * @param cls class
   * @return created instance
   */
  static <T, X extends T> T createProviderForClass(
      final PluggableService<T> service, final Class<X> cls)
      throws PluginException, ProviderCreationException {
    debug("Try loading provider " + cls.getName());

    final Plugin annotation = getPluginMetadata(cls);

    final String pluginname = annotation.name();

    if (!service.isValidProviderClass(cls)) {
      throw new PluginException(
          "Class "
              + cls.getName()
              + " was not a valid plugin class for service: "
              + service.getName()
              + ". Expected class "
              + cls.getName()
              + ", with a public constructor with no parameter");
    }
    debug("Succeeded loading plugin " + cls.getName() + " for service: " + service.getName());
    return service.createProviderInstance(cls, pluginname);
  }
コード例 #2
0
  /** Load provider instance for the service */
  @SuppressWarnings("unchecked")
  public synchronized <T> T load(final PluggableService<T> service, final String providerName)
      throws ProviderLoaderException {
    final ProviderIdent ident = new ProviderIdent(service.getName(), providerName);
    debug("loadInstance for " + ident + ": " + pluginJar);

    if (null == pluginProviderDefs.get(ident)) {
      final String[] strings = getClassnames();
      for (final String classname : strings) {
        final Class<?> cls;
        try {
          cls = loadClass(classname);
          if (matchesProviderDeclaration(ident, cls)) {
            pluginProviderDefs.put(ident, cls);
          }
        } catch (PluginException e) {
          log.error(
              "Failed to load class from "
                  + pluginJar
                  + ": classname: "
                  + classname
                  + ": "
                  + e.getMessage());
        }
      }
    }
    final Class<T> cls = pluginProviderDefs.get(ident);
    if (null != cls) {
      try {
        return createProviderForClass(service, cls);
      } catch (PluginException e) {
        throw new ProviderLoaderException(e, service.getName(), providerName);
      }
    }
    return null;
  }