use of io.cdap.cdap.proto.id.ArtifactId 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.proto.id.ArtifactId 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;
}
use of io.cdap.cdap.proto.id.ArtifactId in project cdap by caskdata.
the class ListArtifactPluginsCommand method perform.
@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
String artifactName = arguments.get(ArgumentName.ARTIFACT_NAME.toString());
String artifactVersion = arguments.get(ArgumentName.ARTIFACT_VERSION.toString());
ArtifactId artifactId = cliConfig.getCurrentNamespace().artifact(artifactName, artifactVersion);
String pluginType = arguments.get(ArgumentName.PLUGIN_TYPE.toString());
final List<PluginSummary> pluginSummaries;
String scopeStr = arguments.getOptional(ArgumentName.SCOPE.toString());
if (scopeStr == null) {
pluginSummaries = artifactClient.getPluginSummaries(artifactId, pluginType);
} else {
pluginSummaries = artifactClient.getPluginSummaries(artifactId, pluginType, ArtifactScope.valueOf(scopeStr.toUpperCase()));
}
Table table = Table.builder().setHeader("type", "name", "classname", "description", "artifact").setRows(pluginSummaries, new RowMaker<PluginSummary>() {
@Override
public List<?> makeRow(PluginSummary object) {
return Lists.newArrayList(object.getType(), object.getName(), object.getClassName(), object.getDescription(), object.getArtifact().toString());
}
}).build();
cliConfig.getTableRenderer().render(cliConfig, output, table);
}
use of io.cdap.cdap.proto.id.ArtifactId in project cdap by caskdata.
the class ArtifactHttpHandler method deleteProperties.
@DELETE
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}/properties")
public void deleteProperties(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion) throws Exception {
NamespaceId namespace = NamespaceId.SYSTEM.getNamespace().equalsIgnoreCase(namespaceId) ? NamespaceId.SYSTEM : validateAndGetNamespace(namespaceId);
ArtifactId artifactId = validateAndGetArtifactId(namespace, artifactName, artifactVersion);
try {
artifactRepository.deleteArtifactProperties(Id.Artifact.fromEntityId(artifactId));
responder.sendStatus(HttpResponseStatus.OK);
} catch (IOException e) {
LOG.error("Exception deleting properties for artifact {}.", artifactId, e);
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error deleting properties for artifact.");
}
}
use of io.cdap.cdap.proto.id.ArtifactId in project cdap by caskdata.
the class ArtifactHttpHandler method getProperties.
@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}/properties")
public void getProperties(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @QueryParam("scope") @DefaultValue("user") String scope, @QueryParam("keys") @Nullable String keys) throws Exception {
NamespaceId namespace = validateAndGetScopedNamespace(Ids.namespace(namespaceId), scope);
ArtifactId artifactId = validateAndGetArtifactId(namespace, artifactName, artifactVersion);
try {
ArtifactDetail artifactDetail = artifactRepository.getArtifact(Id.Artifact.fromEntityId(artifactId));
Map<String, String> properties = artifactDetail.getMeta().getProperties();
Map<String, String> result;
if (keys != null && !keys.isEmpty()) {
result = new HashMap<>();
for (String key : Splitter.on(',').trimResults().split(keys)) {
result.put(key, properties.get(key));
}
} else {
result = properties;
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(result));
} catch (IOException e) {
LOG.error("Exception reading artifacts named {} for namespace {} from the store.", artifactName, namespaceId, e);
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error reading artifact properties from the store.");
}
}
Aggregations