Search in sources :

Example 1 with Plugin

use of io.cdap.cdap.api.plugin.Plugin in project cdap by caskdata.

the class DefaultPluginContext method getPlugin.

private Plugin getPlugin(String pluginId) {
    Plugin plugin = plugins.get(pluginId);
    Preconditions.checkArgument(plugin != null, "Plugin with id %s does not exist in program %s of application %s.", pluginId, programId.getProgram(), programId.getApplication());
    return plugin;
}
Also used : Plugin(io.cdap.cdap.api.plugin.Plugin)

Example 2 with Plugin

use of io.cdap.cdap.api.plugin.Plugin in project cdap by caskdata.

the class ConfiguratorTask method run.

@Override
public void run(RunnableTaskContext context) throws Exception {
    AppDeploymentInfo deploymentInfo = GSON.fromJson(context.getParam(), AppDeploymentInfo.class);
    Injector injector = Guice.createInjector(new ConfigModule(cConf), RemoteAuthenticatorModules.getDefaultModule(), new LocalLocationModule(), new ConfiguratorTaskModule(), new AuthenticationContextModules().getMasterWorkerModule());
    ConfigResponse result = injector.getInstance(ConfiguratorTaskRunner.class).configure(deploymentInfo);
    AppSpecInfo appSpecInfo = result.getAppSpecInfo();
    // If configuration succeeded and if only system artifacts are involved, no need to restart the task
    if (result.getExitCode() == 0 && appSpecInfo != null && NamespaceId.SYSTEM.equals(deploymentInfo.getArtifactId().getNamespaceId())) {
        boolean hasUserPlugins = appSpecInfo.getAppSpec().getPlugins().values().stream().map(Plugin::getArtifactId).map(ArtifactId::getScope).anyMatch(ArtifactScope.USER::equals);
        context.setTerminateOnComplete(hasUserPlugins);
    }
    context.writeResult(GSON.toJson(result).getBytes(StandardCharsets.UTF_8));
}
Also used : AppDeploymentInfo(io.cdap.cdap.internal.app.deploy.pipeline.AppDeploymentInfo) LocalLocationModule(io.cdap.cdap.common.guice.LocalLocationModule) AppSpecInfo(io.cdap.cdap.internal.app.deploy.pipeline.AppSpecInfo) Injector(com.google.inject.Injector) ConfigModule(io.cdap.cdap.common.guice.ConfigModule) AuthenticationContextModules(io.cdap.cdap.security.auth.context.AuthenticationContextModules) ConfigResponse(io.cdap.cdap.app.deploy.ConfigResponse) Plugin(io.cdap.cdap.api.plugin.Plugin)

Example 3 with Plugin

use of io.cdap.cdap.api.plugin.Plugin 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 4 with Plugin

use of io.cdap.cdap.api.plugin.Plugin in project cdap by caskdata.

the class DefaultServicePluginConfigurer method createClassLoader.

@Override
public ClassLoader createClassLoader() {
    Map<String, Plugin> plugins = getPlugins().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, v -> v.getValue().getPlugin()));
    ClassLoader pluginsClassLoader = PluginClassLoaders.createFilteredPluginsClassLoader(plugins, getPluginInstantiator());
    return new CombineClassLoader(null, programClassLoader, pluginsClassLoader, getClass().getClassLoader());
}
Also used : PluginSelector(io.cdap.cdap.api.plugin.PluginSelector) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PluginFinder(io.cdap.cdap.internal.app.runtime.artifact.PluginFinder) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) PluginProperties(io.cdap.cdap.api.plugin.PluginProperties) Collectors(java.util.stream.Collectors) PluginClassLoaders(io.cdap.cdap.internal.app.runtime.plugin.PluginClassLoaders) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) PluginInstantiator(io.cdap.cdap.internal.app.runtime.plugin.PluginInstantiator) Map(java.util.Map) MacroParserOptions(io.cdap.cdap.api.macro.MacroParserOptions) ServicePluginConfigurer(io.cdap.cdap.api.service.http.ServicePluginConfigurer) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) Plugin(io.cdap.cdap.api.plugin.Plugin) MacroEvaluator(io.cdap.cdap.api.macro.MacroEvaluator) CombineClassLoader(io.cdap.cdap.common.lang.CombineClassLoader) Nullable(javax.annotation.Nullable) CombineClassLoader(io.cdap.cdap.common.lang.CombineClassLoader) CombineClassLoader(io.cdap.cdap.common.lang.CombineClassLoader) Map(java.util.Map) Plugin(io.cdap.cdap.api.plugin.Plugin)

Example 5 with Plugin

use of io.cdap.cdap.api.plugin.Plugin in project cdap by caskdata.

the class RemotePluginConfigurer method addPlugin.

@Override
protected Plugin addPlugin(String pluginType, String pluginName, String pluginId, PluginProperties properties, PluginSelector selector) throws PluginNotExistsException {
    validateExistingPlugin(pluginId);
    // plugin
    if (!localPlugins.containsKey(pluginId)) {
        return super.addPlugin(pluginType, pluginName, pluginId, properties, selector);
    }
    Plugin existingPlugin = localPlugins.get(pluginId);
    File existingPluginLocation = new File(localPluginDir, Artifacts.getFileName(existingPlugin.getArtifactId()));
    // need to regenerate this plugin to ensure the plugin has updated properties with macro resolved, also
    // register it to plugin instantiator
    io.cdap.cdap.api.artifact.ArtifactId artifactId = existingPlugin.getArtifactId();
    String namespace = artifactId.getScope().equals(ArtifactScope.SYSTEM) ? NamespaceId.SYSTEM.getNamespace() : pluginNamespaceId.getNamespace();
    Location pluginLocation = Locations.toLocation(existingPluginLocation);
    SortedMap<io.cdap.cdap.api.artifact.ArtifactId, PluginClass> selectedPlugin = new TreeMap<>();
    selectedPlugin.put(artifactId, existingPlugin.getPluginClass());
    selector.select(selectedPlugin);
    Plugin plugin = FindPluginHelper.getPlugin(existingPlugin.getParents(), Maps.immutableEntry(new ArtifactDescriptor(namespace, artifactId, pluginLocation), existingPlugin.getPluginClass()), properties, pluginInstantiator);
    plugins.put(pluginId, new PluginWithLocation(plugin, pluginLocation));
    return plugin;
}
Also used : ArtifactId(io.cdap.cdap.proto.id.ArtifactId) TreeMap(java.util.TreeMap) ArtifactDescriptor(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor) PluginClass(io.cdap.cdap.api.plugin.PluginClass) File(java.io.File) Plugin(io.cdap.cdap.api.plugin.Plugin) Location(org.apache.twill.filesystem.Location)

Aggregations

Plugin (io.cdap.cdap.api.plugin.Plugin)25 Map (java.util.Map)9 HashMap (java.util.HashMap)8 PluginClass (io.cdap.cdap.api.plugin.PluginClass)7 File (java.io.File)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 JsonObject (com.google.gson.JsonObject)6 HashSet (java.util.HashSet)6 ImmutableSet (com.google.common.collect.ImmutableSet)4 Resources (io.cdap.cdap.api.Resources)4 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)4 TestPlugin (io.cdap.cdap.internal.app.plugins.test.TestPlugin)4 PluginInstantiator (io.cdap.cdap.internal.app.runtime.plugin.PluginInstantiator)4 Test (org.junit.Test)4 NestedConfigPlugin (io.cdap.cdap.internal.app.runtime.artifact.plugin.nested.NestedConfigPlugin)3 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)3 Set (java.util.Set)3 Gson (com.google.gson.Gson)2 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)2 ArtifactVersion (io.cdap.cdap.api.artifact.ArtifactVersion)2