Search in sources :

Example 1 with PluginClassLoader

use of io.cdap.cdap.internal.app.runtime.plugin.PluginClassLoader in project cdap by caskdata.

the class DefaultPluginConfigurer method addPlugin.

protected Plugin addPlugin(String pluginType, String pluginName, String pluginId, PluginProperties properties, PluginSelector selector) throws PluginNotExistsException {
    validateExistingPlugin(pluginId);
    final Class[] callerClasses = CallerClassSecurityManager.getCallerClasses();
    if (callerClasses.length < 3) {
        // This shouldn't happen as there should be someone calling this method.
        throw new IllegalStateException("Invalid call stack.");
    }
    Set<ArtifactId> parents = new LinkedHashSet<>();
    // 0 is the CallerClassSecurityManager, 1 is this class, hence 2 is the actual caller
    for (int i = 2; i < callerClasses.length; i++) {
        ClassLoader classloader = callerClasses[i].getClassLoader();
        if (classloader instanceof PluginClassLoader) {
            // if this is the first time we've seen this plugin artifact, it must be a new plugin parent.
            io.cdap.cdap.api.artifact.ArtifactId pluginCallerArtifactId = ((PluginClassLoader) classloader).getArtifactId();
            parents.add((pluginCallerArtifactId.getScope() == ArtifactScope.SYSTEM ? NamespaceId.SYSTEM : pluginNamespaceId).artifact(pluginCallerArtifactId.getName(), pluginCallerArtifactId.getVersion().getVersion()));
        }
    }
    PluginNotExistsException exception = null;
    for (ArtifactId parentId : Iterables.concat(parents, Collections.singleton(artifactId))) {
        try {
            Map.Entry<ArtifactDescriptor, PluginClass> pluginEntry = pluginFinder.findPlugin(pluginNamespaceId, parentId, pluginType, pluginName, selector);
            Plugin plugin = FindPluginHelper.getPlugin(Iterables.transform(parents, ArtifactId::toApiArtifactId), pluginEntry, properties, pluginInstantiator);
            plugins.put(pluginId, new PluginWithLocation(plugin, pluginEntry.getKey().getLocation()));
            return plugin;
        } catch (PluginNotExistsException e) {
            // ignore this in case the plugin extends something higher up in the call stack.
            // For example, suppose the app uses pluginA, which uses pluginB. However, pluginB is a plugin that
            // has the app as its parent and not pluginA as its parent. In this case, we want to keep going up the call
            // stack until we get to the app as the parent, which would be able to find plugin B.
            exception = e;
        }
    }
    throw exception == null ? new PluginNotExistsException(pluginNamespaceId, pluginType, pluginName) : exception;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactDescriptor(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor) PluginClassLoader(io.cdap.cdap.internal.app.runtime.plugin.PluginClassLoader) PluginClass(io.cdap.cdap.api.plugin.PluginClass) PluginClass(io.cdap.cdap.api.plugin.PluginClass) HashMap(java.util.HashMap) Map(java.util.Map) PluginClassLoader(io.cdap.cdap.internal.app.runtime.plugin.PluginClassLoader) Plugin(io.cdap.cdap.api.plugin.Plugin)

Example 2 with PluginClassLoader

use of io.cdap.cdap.internal.app.runtime.plugin.PluginClassLoader in project cdap by caskdata.

the class DefaultArtifactInspector method inspectPlugins.

/**
 * Inspects the plugin file and extracts plugin classes information.
 */
private void inspectPlugins(ArtifactClasses.Builder builder, File artifactFile, io.cdap.cdap.proto.id.ArtifactId artifactId, PluginInstantiator pluginInstantiator, Set<PluginClass> additionalPlugins, List<MetadataMutation> mutations) throws IOException, InvalidArtifactException {
    ArtifactId artifact = artifactId.toApiArtifactId();
    PluginClassLoader pluginClassLoader = pluginInstantiator.getArtifactClassLoader(artifact);
    inspectAdditionalPlugins(artifact, additionalPlugins, pluginClassLoader);
    // See if there are export packages. Plugins should be in those packages
    Set<String> exportPackages = getExportPackages(artifactFile);
    if (exportPackages.isEmpty()) {
        return;
    }
    try {
        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);
                String pluginName = getPluginName(cls);
                PluginId pluginId = new PluginId(artifactId.getNamespace(), artifactId.getArtifact(), artifactId.getVersion(), pluginName, pluginAnnotation.type());
                MetadataMutation mutation = getMetadataMutation(pluginId, cls);
                if (mutation != null) {
                    mutations.add(mutation);
                }
                PluginClass pluginClass = PluginClass.builder().setName(pluginName).setType(pluginAnnotation.type()).setCategory(getPluginCategory(cls)).setClassName(cls.getName()).setConfigFieldName(configField).setProperties(pluginProperties).setRequirements(getArtifactRequirements(cls)).setDescription(getPluginDescription(cls)).build();
                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);
    }
}
Also used : ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) PluginId(io.cdap.cdap.proto.id.PluginId) PluginPropertyField(io.cdap.cdap.api.plugin.PluginPropertyField) MetadataMutation(io.cdap.cdap.spi.metadata.MetadataMutation) UnsupportedTypeException(io.cdap.cdap.api.data.schema.UnsupportedTypeException) PluginClass(io.cdap.cdap.api.plugin.PluginClass) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) PluginClassLoader(io.cdap.cdap.internal.app.runtime.plugin.PluginClassLoader) Plugin(io.cdap.cdap.api.annotation.Plugin)

Aggregations

PluginClass (io.cdap.cdap.api.plugin.PluginClass)2 PluginClassLoader (io.cdap.cdap.internal.app.runtime.plugin.PluginClassLoader)2 Plugin (io.cdap.cdap.api.annotation.Plugin)1 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)1 UnsupportedTypeException (io.cdap.cdap.api.data.schema.UnsupportedTypeException)1 Plugin (io.cdap.cdap.api.plugin.Plugin)1 PluginPropertyField (io.cdap.cdap.api.plugin.PluginPropertyField)1 InvalidArtifactException (io.cdap.cdap.common.InvalidArtifactException)1 ArtifactDescriptor (io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor)1 PluginNotExistsException (io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException)1 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)1 PluginId (io.cdap.cdap.proto.id.PluginId)1 MetadataMutation (io.cdap.cdap.spi.metadata.MetadataMutation)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1