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);
}
}
}
}
}
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();
}
}
}
}
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.");
}
}
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);
}
}
}
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);
}
}
Aggregations