@VisibleForTesting
  protected <T> T createAndInitialize(final String pluginClassName, final Class<T> pluginClass) {
    final T instance = PluginLoader.loadSpecificPlugin(pluginClassName, pluginClass);
    Preconditions.checkState(instance != null,
        "Unable to locate %s using name '%s", pluginClass, pluginClassName);
    try {
      final Method initMeth = instance.getClass().getMethod("initialize", TSDB.class);
      initMeth.invoke(instance, tsdb);
      final Method versionMeth = instance.getClass().getMethod("version");
      String version = (String) versionMeth.invoke(instance);
      LOG.info("Successfully initialized plugin [{}] version: {}",
          instance.getClass().getCanonicalName(),
          version);
      return instance;
    } catch (Exception e) {
      throw new RuntimeException("Failed to initialize " + instance.getClass(), e);
    }
  }
@ VisibleForTesting protected < T > T createAndInitialize ( final String pluginClassName , final Class < T > pluginClass ) { final T instance = PluginLoader . loadSpecificPlugin ( pluginClassName , pluginClass ) ; Preconditions . checkState ( instance != null , "Unable to locate %s using name '%s" , pluginClass , pluginClassName ) ; try { final Method initMeth = instance . getClass ( ) . getMethod ( "initialize" , TSDB . class ) ; initMeth . invoke ( instance , tsdb ) ; final Method versionMeth = instance . getClass ( ) . getMethod ( "version" ) ; String version = ( String ) versionMeth . invoke ( instance ) ; LOG . info ( "Successfully initialized plugin [{}] version: {}" , instance . getClass ( ) . getCanonicalName ( ) , version ) ; return instance ; } catch ( Exception e ) { throw new RuntimeException ( "Failed to initialize " + instance . getClass ( ) , e ) ; } }
Helper method to load and initialize a given plugin class. This uses reflection
because plugins share no common interfaces. (They could though!)
@param pluginClassName the class name of the plugin to load
@param pluginClass class of the plugin
@return loaded an initialized instance of {@code pluginClass}