Search in sources :

Example 1 with KettlePluginClassMapException

use of org.pentaho.di.core.exception.KettlePluginClassMapException in project pentaho-kettle by pentaho.

the class PluginRegistry method loadClass.

/**
 * Load and instantiate the plugin class specified
 *
 * @param plugin      the plugin to load
 * @param pluginClass the class to be loaded
 * @return The instantiated class
 * @throws KettlePluginException In case there was a class loading problem somehow
 */
@SuppressWarnings("unchecked")
public <T> T loadClass(PluginInterface plugin, Class<T> pluginClass) throws KettlePluginException {
    if (plugin == null) {
        throw new KettlePluginException(BaseMessages.getString(PKG, "PluginRegistry.RuntimeError.NoValidStepOrPlugin.PLUGINREGISTRY001"));
    }
    if (plugin instanceof ClassLoadingPluginInterface) {
        T aClass = ((ClassLoadingPluginInterface) plugin).loadClass(pluginClass);
        if (aClass == null) {
            throw new KettlePluginClassMapException(BaseMessages.getString(PKG, "PluginRegistry.RuntimeError.NoValidClassRequested.PLUGINREGISTRY002", plugin.getName(), pluginClass.getName()));
        } else {
            return aClass;
        }
    } else {
        String className = plugin.getClassMap().get(pluginClass);
        if (className == null) {
            // Look for supplemental plugin supplying extra classes
            for (String id : plugin.getIds()) {
                try {
                    T aClass = loadClass(plugin.getPluginType(), createSupplemantalKey(plugin.getPluginType().getName(), id), pluginClass);
                    if (aClass != null) {
                        return aClass;
                    }
                } catch (KettlePluginException exception) {
                // ignore. we'll fall through to the other exception if this loop doesn't produce a return
                }
            }
            throw new KettlePluginClassMapException(BaseMessages.getString(PKG, "PluginRegistry.RuntimeError.NoValidClassRequested.PLUGINREGISTRY002", plugin.getName(), pluginClass.getName()));
        }
        try {
            Class<? extends T> cl;
            if (plugin.isNativePlugin()) {
                cl = (Class<? extends T>) Class.forName(className);
            } else {
                ClassLoader ucl = getClassLoader(plugin);
                // Load the class.
                cl = (Class<? extends T>) ucl.loadClass(className);
            }
            return cl.newInstance();
        } catch (ClassNotFoundException e) {
            throw new KettlePluginException(BaseMessages.getString(PKG, "PluginRegistry.RuntimeError.ClassNotFound.PLUGINREGISTRY003"), e);
        } catch (InstantiationException e) {
            throw new KettlePluginException(BaseMessages.getString(PKG, "PluginRegistry.RuntimeError.UnableToInstantiateClass.PLUGINREGISTRY004"), e);
        } catch (IllegalAccessException e) {
            throw new KettlePluginException(BaseMessages.getString(PKG, "PluginRegistry.RuntimeError.IllegalAccessToClass.PLUGINREGISTRY005"), e);
        } catch (Throwable e) {
            e.printStackTrace();
            throw new KettlePluginException(BaseMessages.getString(PKG, "PluginRegistry.RuntimeError.UnExpectedErrorLoadingClass.PLUGINREGISTRY007"), e);
        }
    }
}
Also used : KettlePluginException(org.pentaho.di.core.exception.KettlePluginException) KettlePluginClassMapException(org.pentaho.di.core.exception.KettlePluginClassMapException) URLClassLoader(java.net.URLClassLoader) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString)

Aggregations

URLClassLoader (java.net.URLClassLoader)1 KettlePluginClassMapException (org.pentaho.di.core.exception.KettlePluginClassMapException)1 KettlePluginException (org.pentaho.di.core.exception.KettlePluginException)1 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)1