Ejemplo n.º 1
0
 private Map<String, Plugin> loadPluginsFromClasspath(Settings settings) {
   Map<String, Plugin> plugins = newHashMap();
   Enumeration<URL> pluginUrls = null;
   try {
     pluginUrls = settings.getClassLoader().getResources("es-plugin.properties");
   } catch (IOException e) {
     logger.warn("Failed to find plugins from classpath", e);
   }
   while (pluginUrls.hasMoreElements()) {
     URL pluginUrl = pluginUrls.nextElement();
     Properties pluginProps = new Properties();
     InputStream is = null;
     try {
       is = pluginUrl.openStream();
       pluginProps.load(is);
       String sPluginClass = pluginProps.getProperty("plugin");
       Class<?> pluginClass = settings.getClassLoader().loadClass(sPluginClass);
       Plugin plugin = (Plugin) pluginClass.newInstance();
       plugins.put(plugin.name(), plugin);
     } catch (Exception e) {
       logger.warn("Failed to load plugin from [" + pluginUrl + "]", e);
     } finally {
       if (is != null) {
         try {
           is.close();
         } catch (IOException e) {
           // ignore
         }
       }
     }
   }
   return plugins;
 }
Ejemplo n.º 2
0
    private void load(Class<? extends Plugin> pluginClass, Properties properties) {
      // A plugin can be instantiated only once
      if (PLUGINS.containsKey(pluginClass)) {
        Application.LOGGER.error("Plugin for " + pluginClass + " is already loaded");
        return;
      }

      Plugin plugin;
      try {
        plugin = pluginClass.newInstance();
      } catch (InstantiationException e) {
        Application.LOGGER.error("Error while instantiating plugin from " + pluginClass, e);
        return;
      } catch (IllegalAccessException e) {
        Application.LOGGER.error(
            "Plugin class default constructor not accessible for " + pluginClass, e);
        return;
      }

      // register the plugin
      PLUGINS.put(pluginClass, plugin);

      // Load the properties if available
      if (properties != null) {
        plugin.name = properties.getProperty("plugin.name");
        plugin.version = Version.parse(properties.getProperty("plugin.version"));
        plugin.description = properties.getProperty("plugin.description");
      } else {
        // in case the application is loading the plugin directly
        plugin.name = pluginClass.getSimpleName();
        plugin.version = null;
        plugin.description = null;
      }

      // Let the plugin initialize
      Application.LOGGER.trace(
          "Initializing Plugin - " + plugin.name + " - " + pluginClass.getCanonicalName());
      plugin.onInit(app);
    }
Ejemplo n.º 3
0
  /** Get the Plugin annotation for the class */
  static Plugin getPluginMetadata(final Class<?> cls) throws PluginException {
    // try to get plugin provider name
    final String pluginname;
    if (!cls.isAnnotationPresent(Plugin.class)) {
      throw new PluginException("No Plugin annotation was found for the class: " + cls.getName());
    }

    final Plugin annotation = (Plugin) cls.getAnnotation(Plugin.class);
    pluginname = annotation.name();
    if (null == pluginname || "".equals(pluginname)) {
      throw new PluginException(
          "Plugin annotation 'name' cannot be empty for the class: " + cls.getName());
    }
    // get service name from annotation
    final String servicename = annotation.service();
    if (null == servicename || "".equals(servicename)) {
      throw new PluginException(
          "Plugin annotation 'service' cannot be empty for the class: " + cls.getName());
    }
    return annotation;
  }
Ejemplo n.º 4
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);
  }
Ejemplo n.º 5
0
 /** Return true if the ident matches the Plugin annotation for the class */
 static ProviderIdent getProviderDeclaration(final Class<?> cls) throws PluginException {
   final Plugin annotation = getPluginMetadata(cls);
   return new ProviderIdent(annotation.service(), annotation.name());
 }
Ejemplo n.º 6
0
 /** Return true if the ident matches the Plugin annotation for the class */
 static boolean matchesProviderDeclaration(final ProviderIdent ident, final Class<?> cls)
     throws PluginException {
   final Plugin annotation = getPluginMetadata(cls);
   return ident.getFirst().equals(annotation.service())
       && ident.getSecond().equals(annotation.name());
 }