Search in sources :

Example 1 with Plugin

use of co.cask.cdap.api.annotation.Plugin in project cdap by caskdata.

the class ArtifactInspector method inspectPlugins.

/**
 * Inspects the plugin file and extracts plugin classes information.
 */
private ArtifactClasses.Builder inspectPlugins(ArtifactClasses.Builder builder, File artifactFile, ArtifactId artifactId, PluginInstantiator pluginInstantiator) throws IOException, InvalidArtifactException {
    // See if there are export packages. Plugins should be in those packages
    Set<String> exportPackages = getExportPackages(artifactFile);
    if (exportPackages.isEmpty()) {
        return builder;
    }
    try {
        ClassLoader pluginClassLoader = pluginInstantiator.getArtifactClassLoader(artifactId);
        for (Class<?> cls : getPluginClasses(exportPackages, pluginClassLoader)) {
            Plugin pluginAnnotation = cls.getAnnotation(Plugin.class);
            if (pluginAnnotation == null) {
                continue;
            }
            Map<String, PluginPropertyField> pluginProperties = Maps.newHashMap();
            try {
                String configField = getProperties(TypeToken.of(cls), pluginProperties);
                Set<String> pluginEndpoints = getPluginEndpoints(cls);
                PluginClass pluginClass = new PluginClass(pluginAnnotation.type(), getPluginName(cls), getPluginDescription(cls), cls.getName(), configField, pluginProperties, pluginEndpoints);
                builder.addPlugin(pluginClass);
            } catch (UnsupportedTypeException e) {
                LOG.warn("Plugin configuration type not supported. Plugin ignored. {}", cls, e);
            }
        }
    } catch (Throwable t) {
        throw new InvalidArtifactException(String.format("Class could not be found while inspecting artifact for plugins. " + "Please check dependencies are available, and that the correct parent artifact was specified. " + "Error class: %s, message: %s.", t.getClass(), t.getMessage()), t);
    }
    return builder;
}
Also used : CloseableClassLoader(co.cask.cdap.api.artifact.CloseableClassLoader) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) PluginClass(co.cask.cdap.api.plugin.PluginClass) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) InvalidArtifactException(co.cask.cdap.common.InvalidArtifactException) Plugin(co.cask.cdap.api.annotation.Plugin)

Example 2 with Plugin

use of co.cask.cdap.api.annotation.Plugin in project cdap by caskdata.

the class ArtifactInspector method isPlugin.

/**
 * Detects if a class is annotated with {@link Plugin} without loading the class.
 *
 * @param className name of the class
 * @param classLoader ClassLoader for loading the class file of the given class
 * @return true if the given class is annotated with {@link Plugin}
 */
private boolean isPlugin(String className, ClassLoader classLoader) {
    try (InputStream is = classLoader.getResourceAsStream(className.replace('.', '/') + ".class")) {
        if (is == null) {
            return false;
        }
        // Use ASM to inspect the class bytecode to see if it is annotated with @Plugin
        final boolean[] isPlugin = new boolean[1];
        ClassReader cr = new ClassReader(is);
        cr.accept(new ClassVisitor(Opcodes.ASM5) {

            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                if (Plugin.class.getName().equals(Type.getType(desc).getClassName()) && visible) {
                    isPlugin[0] = true;
                }
                return super.visitAnnotation(desc, visible);
            }
        }, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
        return isPlugin[0];
    } catch (IOException e) {
        // If failed to open the class file, then it cannot be a plugin
        LOG.warn("Failed to open class file for {}", className, e);
        return false;
    }
}
Also used : InputStream(java.io.InputStream) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) IOException(java.io.IOException) Plugin(co.cask.cdap.api.annotation.Plugin)

Aggregations

Plugin (co.cask.cdap.api.annotation.Plugin)2 CloseableClassLoader (co.cask.cdap.api.artifact.CloseableClassLoader)1 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)1 PluginClass (co.cask.cdap.api.plugin.PluginClass)1 PluginPropertyField (co.cask.cdap.api.plugin.PluginPropertyField)1 InvalidArtifactException (co.cask.cdap.common.InvalidArtifactException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)1 ClassReader (org.objectweb.asm.ClassReader)1 ClassVisitor (org.objectweb.asm.ClassVisitor)1