Search in sources :

Example 16 with KettlePluginException

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

the class LoggingPluginType method registerXmlPlugins.

@Override
protected void registerXmlPlugins() throws KettlePluginException {
    for (PluginFolderInterface folder : pluginFolders) {
        if (folder.isPluginXmlFolder()) {
            List<FileObject> pluginXmlFiles = findPluginXmlFiles(folder.getFolder());
            for (FileObject file : pluginXmlFiles) {
                try {
                    Document document = XMLHandler.loadXMLFile(file);
                    Node pluginNode = XMLHandler.getSubNode(document, "plugin");
                    if (pluginNode != null) {
                        registerPluginFromXmlResource(pluginNode, KettleVFS.getFilename(file.getParent()), this.getClass(), false, file.getParent().getURL());
                    }
                } catch (Exception e) {
                    // We want to report this plugin.xml error, perhaps an XML typo or something like that...
                    // 
                    log.logError("Error found while reading logging plugin.xml file: " + file.getName().toString(), e);
                }
            }
        }
    }
}
Also used : PluginFolderInterface(org.pentaho.di.core.plugins.PluginFolderInterface) Node(org.w3c.dom.Node) FileObject(org.apache.commons.vfs2.FileObject) Document(org.w3c.dom.Document) KettlePluginException(org.pentaho.di.core.exception.KettlePluginException)

Example 17 with KettlePluginException

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

the class BasePluginType method registerPluginJars.

protected void registerPluginJars() throws KettlePluginException {
    List<JarFileAnnotationPlugin> jarFilePlugins = findAnnotatedClassFiles(pluginType.getName());
    for (JarFileAnnotationPlugin jarFilePlugin : jarFilePlugins) {
        URLClassLoader urlClassLoader = createUrlClassLoader(jarFilePlugin.getJarFile(), getClass().getClassLoader());
        try {
            Class<?> clazz = urlClassLoader.loadClass(jarFilePlugin.getClassName());
            if (clazz == null) {
                throw new KettlePluginException("Unable to load class: " + jarFilePlugin.getClassName());
            }
            List<String> libraries = Arrays.stream(urlClassLoader.getURLs()).map(URL::getFile).collect(Collectors.toList());
            Annotation annotation = clazz.getAnnotation(pluginType);
            handlePluginAnnotation(clazz, annotation, libraries, false, jarFilePlugin.getPluginFolder());
        } catch (Exception e) {
            // Ignore for now, don't know if it's even possible.
            LogChannel.GENERAL.logError("Unexpected error registering jar plugin file: " + jarFilePlugin.getJarFile(), e);
        } finally {
            if (urlClassLoader != null && urlClassLoader instanceof KettleURLClassLoader) {
                ((KettleURLClassLoader) urlClassLoader).closeClassLoader();
            }
        }
    }
}
Also used : KettlePluginException(org.pentaho.di.core.exception.KettlePluginException) URLClassLoader(java.net.URLClassLoader) Annotation(java.lang.annotation.Annotation) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) FileNotFoundException(java.io.FileNotFoundException) KettlePluginException(org.pentaho.di.core.exception.KettlePluginException)

Example 18 with KettlePluginException

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

the class PluginRegistry method registerType.

private void registerType(PluginTypeInterface pluginType) throws KettlePluginException {
    registerPluginType(pluginType.getClass());
    // Search plugins for this type...
    // 
    long startScan = System.currentTimeMillis();
    pluginType.searchPlugins();
    for (PluginRegistryExtension ext : extensions) {
        ext.searchForType(pluginType);
    }
    Set<String> pluginClassNames = new HashSet<>();
    // Scan for plugin classes to facilitate debugging etc.
    // 
    String pluginClasses = EnvUtil.getSystemProperty(Const.KETTLE_PLUGIN_CLASSES);
    if (!Utils.isEmpty(pluginClasses)) {
        String[] classNames = pluginClasses.split(",");
        Collections.addAll(pluginClassNames, classNames);
    }
    for (String className : pluginClassNames) {
        try {
            // What annotation does the plugin type have?
            // 
            PluginAnnotationType annotationType = pluginType.getClass().getAnnotation(PluginAnnotationType.class);
            if (annotationType != null) {
                Class<? extends Annotation> annotationClass = annotationType.value();
                Class<?> clazz = Class.forName(className);
                Annotation annotation = clazz.getAnnotation(annotationClass);
                if (annotation != null) {
                    // Register this one!
                    // 
                    pluginType.handlePluginAnnotation(clazz, annotation, new ArrayList<>(), true, null);
                    LogChannel.GENERAL.logBasic("Plugin class " + className + " registered for plugin type '" + pluginType.getName() + "'");
                } else {
                    if (KettleLogStore.isInitialized() && LogChannel.GENERAL.isDebug()) {
                        LogChannel.GENERAL.logDebug("Plugin class " + className + " doesn't contain annotation for plugin type '" + pluginType.getName() + "'");
                    }
                }
            } else {
                if (KettleLogStore.isInitialized() && LogChannel.GENERAL.isDebug()) {
                    LogChannel.GENERAL.logDebug("Plugin class " + className + " doesn't contain valid class for plugin type '" + pluginType.getName() + "'");
                }
            }
        } catch (Exception e) {
            if (KettleLogStore.isInitialized()) {
                LogChannel.GENERAL.logError("Error registring plugin class from KETTLE_PLUGIN_CLASSES: " + className + Const.CR + Const.getStackTracker(e));
            }
        }
    }
    if (LogChannel.GENERAL.isDetailed()) {
        LogChannel.GENERAL.logDetailed("Registered " + getPlugins(pluginType.getClass()).size() + " plugins of type '" + pluginType.getName() + "' in " + (System.currentTimeMillis() - startScan) + "ms.");
    }
}
Also used : ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Annotation(java.lang.annotation.Annotation) URISyntaxException(java.net.URISyntaxException) KettlePluginClassMapException(org.pentaho.di.core.exception.KettlePluginClassMapException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) KettlePluginException(org.pentaho.di.core.exception.KettlePluginException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HashSet(java.util.HashSet)

Example 19 with KettlePluginException

use of org.pentaho.di.core.exception.KettlePluginException 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)

Example 20 with KettlePluginException

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

the class PluginRegistry method getClassLoader.

/**
 * Create or retrieve the class loader for the specified plugin
 *
 * @param plugin the plugin to use
 * @return The class loader
 * @throws KettlePluginException In case there was a problem
 *                               <p/>
 *                               getClassLoader();
 */
public ClassLoader getClassLoader(PluginInterface plugin) throws KettlePluginException {
    if (plugin == null) {
        throw new KettlePluginException(BaseMessages.getString(PKG, "PluginRegistry.RuntimeError.NoValidStepOrPlugin.PLUGINREGISTRY001"));
    }
    try {
        if (plugin.isNativePlugin()) {
            return this.getClass().getClassLoader();
        } else {
            URLClassLoader ucl;
            lock.writeLock().lock();
            try {
                // This is not the default. By default we cache the class loader for each plugin ID.
                if (plugin.isSeparateClassLoaderNeeded()) {
                    // Create a new one each time
                    ucl = createClassLoader(plugin);
                } else {
                    // See if we can find a class loader to re-use.
                    Map<PluginInterface, URLClassLoader> classLoaders = classLoaderMap.computeIfAbsent(plugin.getPluginType(), k -> new HashMap<>());
                    ucl = classLoaders.get(plugin);
                    if (ucl == null) {
                        // check if plugin belongs to a group we can use
                        if (!Utils.isEmpty(plugin.getClassLoaderGroup())) {
                            ucl = classLoaderGroupsMap.get(plugin.getClassLoaderGroup());
                            if (ucl == null) {
                                ucl = createClassLoader(plugin);
                                // save for later use...
                                classLoaders.put(plugin, ucl);
                                inverseClassLoaderLookup.computeIfAbsent(ucl, k -> new HashSet<>()).add(plugin);
                                classLoaderGroupsMap.put(plugin.getClassLoaderGroup(), ucl);
                            } else {
                                // we have a classloader, but does it have this plugin?
                                try {
                                    ucl.loadClass(plugin.getClassMap().values().iterator().next());
                                } catch (ClassNotFoundException ignored) {
                                    // missed, add it to the classloader
                                    addToClassLoader(plugin, (KettleURLClassLoader) ucl);
                                }
                            }
                        } else {
                            // fallthrough folder based plugin
                            if (plugin.getPluginDirectory() != null) {
                                ucl = folderBasedClassLoaderMap.get(plugin.getPluginDirectory().toString());
                                if (ucl == null) {
                                    ucl = createClassLoader(plugin);
                                    // save for later use...
                                    classLoaders.put(plugin, ucl);
                                    inverseClassLoaderLookup.computeIfAbsent(ucl, k -> new HashSet<>()).add(plugin);
                                    folderBasedClassLoaderMap.put(plugin.getPluginDirectory().toString(), ucl);
                                } else {
                                    // we have a classloader, but does it have this plugin?
                                    try {
                                        ucl.loadClass(plugin.getClassMap().values().iterator().next());
                                    } catch (ClassNotFoundException ignored) {
                                        // missed, add it to the classloader
                                        addToClassLoader(plugin, (KettleURLClassLoader) ucl);
                                    }
                                }
                            } else {
                                ucl = classLoaders.get(plugin);
                                if (ucl == null) {
                                    if (plugin.getLibraries().size() == 0) {
                                        if (plugin instanceof ClassLoadingPluginInterface) {
                                            return ((ClassLoadingPluginInterface) plugin).getClassLoader();
                                        }
                                    }
                                    ucl = createClassLoader(plugin);
                                    // save for later use...
                                    classLoaders.put(plugin, ucl);
                                    inverseClassLoaderLookup.computeIfAbsent(ucl, k -> new HashSet<>()).add(plugin);
                                }
                            }
                        }
                    }
                }
            } finally {
                lock.writeLock().unlock();
            }
            // Load the class.
            return ucl;
        }
    } catch (MalformedURLException e) {
        throw new KettlePluginException(BaseMessages.getString(PKG, "PluginRegistry.RuntimeError.MalformedURL.PLUGINREGISTRY006"), e);
    } catch (Throwable e) {
        e.printStackTrace();
        throw new KettlePluginException(BaseMessages.getString(PKG, "PluginRegistry.RuntimeError.UnExpectedCreatingClassLoader.PLUGINREGISTRY008"), e);
    }
}
Also used : Arrays(java.util.Arrays) KettleLogStore(org.pentaho.di.core.logging.KettleLogStore) RowMeta(org.pentaho.di.core.row.RowMeta) URLDecoder(java.net.URLDecoder) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) Metrics(org.pentaho.di.core.logging.Metrics) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) URLClassLoader(java.net.URLClassLoader) Const(org.pentaho.di.core.Const) EnvUtil(org.pentaho.di.core.util.EnvUtil) Map(java.util.Map) KettlePluginClassMapException(org.pentaho.di.core.exception.KettlePluginClassMapException) RowBuffer(org.pentaho.di.core.row.RowBuffer) BaseMessages(org.pentaho.di.i18n.BaseMessages) Method(java.lang.reflect.Method) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) MalformedURLException(java.net.MalformedURLException) Set(java.util.Set) IOException(java.io.IOException) LogChannelInterface(org.pentaho.di.core.logging.LogChannelInterface) Utils(org.pentaho.di.core.util.Utils) Collectors(java.util.stream.Collectors) File(java.io.File) LogChannel(org.pentaho.di.core.logging.LogChannel) KettlePluginException(org.pentaho.di.core.exception.KettlePluginException) Objects(java.util.Objects) List(java.util.List) Annotation(java.lang.annotation.Annotation) Comparator(java.util.Comparator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) MalformedURLException(java.net.MalformedURLException) KettlePluginException(org.pentaho.di.core.exception.KettlePluginException) URLClassLoader(java.net.URLClassLoader) HashSet(java.util.HashSet)

Aggregations

KettlePluginException (org.pentaho.di.core.exception.KettlePluginException)47 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)23 FileObject (org.apache.commons.vfs2.FileObject)12 Node (org.w3c.dom.Node)11 RowMeta (org.pentaho.di.core.row.RowMeta)10 ValueMetaNone (org.pentaho.di.core.row.value.ValueMetaNone)10 Document (org.w3c.dom.Document)10 ArrayList (java.util.ArrayList)9 KettleStepException (org.pentaho.di.core.exception.KettleStepException)9 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)9 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)9 PluginInterface (org.pentaho.di.core.plugins.PluginInterface)7 ValueMetaInteger (org.pentaho.di.core.row.value.ValueMetaInteger)6 Map (java.util.Map)4 KettleException (org.pentaho.di.core.exception.KettleException)4 PluginFolderInterface (org.pentaho.di.core.plugins.PluginFolderInterface)4 IOException (java.io.IOException)3 Annotation (java.lang.annotation.Annotation)3 URLClassLoader (java.net.URLClassLoader)3 HashMap (java.util.HashMap)3