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