use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.
the class ArtifactStore method getPluginsInArtifact.
private SortedMap<ArtifactDescriptor, Set<PluginClass>> getPluginsInArtifact(Table table, Id.Artifact artifactId, Predicate<PluginClass> filter) throws ArtifactNotFoundException {
SortedMap<ArtifactDescriptor, Set<PluginClass>> result = new TreeMap<>();
// Make sure the artifact exists
ArtifactCell artifactCell = new ArtifactCell(artifactId);
byte[] artifactDataBytes = table.get(artifactCell.rowkey, artifactCell.column);
if (artifactDataBytes == null) {
throw new ArtifactNotFoundException(artifactId.toEntityId());
}
// include any plugin classes that are inside the artifact itself and is accepted by the filter
ArtifactData artifactData = GSON.fromJson(Bytes.toString(artifactDataBytes), ArtifactData.class);
Set<PluginClass> plugins = artifactData.meta.getClasses().getPlugins().stream().filter(filter).collect(Collectors.toCollection(LinkedHashSet::new));
if (!plugins.isEmpty()) {
Location location = Locations.getLocationFromAbsolutePath(locationFactory, artifactData.getLocationPath());
ArtifactDescriptor descriptor = new ArtifactDescriptor(artifactId.toArtifactId(), location);
result.put(descriptor, plugins);
}
return result;
}
use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.
the class ArtifactStore method writeMeta.
// write a new artifact snapshot and clean up the old snapshot data
private void writeMeta(Table table, Id.Artifact artifactId, ArtifactData data) throws IOException {
ArtifactCell artifactCell = new ArtifactCell(artifactId);
table.put(artifactCell.rowkey, artifactCell.column, Bytes.toBytes(GSON.toJson(data)));
// column for plugin meta and app meta. {artifact-name}:{artifact-version}
// does not need to contain namespace because namespace is in the rowkey
byte[] artifactColumn = new ArtifactColumn(artifactId).getColumn();
ArtifactClasses classes = data.meta.getClasses();
Location artifactLocation = Locations.getLocationFromAbsolutePath(locationFactory, data.getLocationPath());
// write pluginClass metadata
for (PluginClass pluginClass : classes.getPlugins()) {
// write metadata for each artifact this plugin extends
for (ArtifactRange artifactRange : data.meta.getUsableBy()) {
// p:{namespace}:{type}:{name}
PluginKey pluginKey = new PluginKey(artifactRange.getNamespace(), artifactRange.getName(), pluginClass.getType(), pluginClass.getName());
byte[] pluginDataBytes = Bytes.toBytes(GSON.toJson(new PluginData(pluginClass, artifactLocation, artifactRange)));
table.put(pluginKey.getRowKey(), artifactColumn, pluginDataBytes);
}
// by any other artifact in the same namespace.
if (data.meta.getUsableBy().isEmpty()) {
// Write a special entry for plugin that doesn't have parent, which means any artifact can use it
UniversalPluginKey pluginKey = new UniversalPluginKey(artifactId.getNamespace().getId(), pluginClass.getType(), pluginClass.getName());
byte[] pluginDataBytes = Bytes.toBytes(GSON.toJson(new PluginData(pluginClass, artifactLocation, null)));
table.put(pluginKey.getRowKey(), artifactColumn, pluginDataBytes);
}
}
// write appClass metadata
for (ApplicationClass appClass : classes.getApps()) {
// a:{namespace}:{classname}
AppClassKey appClassKey = new AppClassKey(artifactId.getNamespace().toEntityId(), appClass.getClassName());
byte[] appDataBytes = Bytes.toBytes(GSON.toJson(new AppData(appClass, artifactLocation)));
table.put(appClassKey.getRowKey(), artifactColumn, appDataBytes);
}
}
use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.
the class RemotePluginFinder method findPlugin.
@Override
public Map.Entry<ArtifactDescriptor, PluginClass> findPlugin(NamespaceId pluginNamespaceId, ArtifactId parentArtifactId, String pluginType, String pluginName, PluginSelector selector) throws PluginNotExistsException {
try {
return Retries.callWithRetries(() -> {
List<PluginInfo> infos = getPlugins(pluginNamespaceId, parentArtifactId, pluginType, pluginName);
if (infos.isEmpty()) {
throw new PluginNotExistsException(pluginNamespaceId, pluginType, pluginName);
}
SortedMap<co.cask.cdap.api.artifact.ArtifactId, PluginClass> plugins = new TreeMap<>();
for (PluginInfo info : infos) {
ArtifactSummary artifactSummary = info.getArtifact();
co.cask.cdap.api.artifact.ArtifactId pluginArtifactId = new co.cask.cdap.api.artifact.ArtifactId(artifactSummary.getName(), new ArtifactVersion(artifactSummary.getVersion()), artifactSummary.getScope());
PluginClass pluginClass = new PluginClass(info.getType(), info.getName(), info.getDescription(), info.getClassName(), info.getConfigFieldName(), info.getProperties(), info.getEndpoints());
plugins.put(pluginArtifactId, pluginClass);
}
Map.Entry<co.cask.cdap.api.artifact.ArtifactId, PluginClass> selected = selector.select(plugins);
if (selected == null) {
throw new PluginNotExistsException(pluginNamespaceId, pluginType, pluginName);
}
Location artifactLocation = getArtifactLocation(Artifacts.toArtifactId(pluginNamespaceId, selected.getKey()));
return Maps.immutableEntry(new ArtifactDescriptor(selected.getKey(), artifactLocation), selected.getValue());
}, retryStrategy);
} catch (PluginNotExistsException e) {
throw e;
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.
the class DefaultArtifactRepository method validatePluginSet.
/**
* Validates the set of plugins for an artifact. Checks that the pair of plugin type and name are unique among
* all plugins in an artifact.
*
* @param plugins the set of plugins to validate
* @throws InvalidArtifactException if there is more than one class with the same type and name
*/
@VisibleForTesting
static void validatePluginSet(Set<PluginClass> plugins) throws InvalidArtifactException {
boolean isInvalid = false;
StringBuilder errMsg = new StringBuilder("Invalid plugins field.");
Set<ImmutablePair<String, String>> existingPlugins = new HashSet<>();
Set<ImmutablePair<String, String>> dupes = new HashSet<>();
for (PluginClass plugin : plugins) {
ImmutablePair<String, String> typeAndName = ImmutablePair.of(plugin.getType(), plugin.getName());
if (!existingPlugins.add(typeAndName) && !dupes.contains(typeAndName)) {
errMsg.append(" Only one plugin with type '");
errMsg.append(typeAndName.getFirst());
errMsg.append("' and name '");
errMsg.append(typeAndName.getSecond());
errMsg.append("' can be present.");
dupes.add(typeAndName);
isInvalid = true;
}
}
// "Invalid plugins. Only one plugin with type 'source' and name 'table' can be present."
if (isInvalid) {
throw new InvalidArtifactException(errMsg.toString());
}
}
use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.
the class LookupTransform method getPluginClass.
private static PluginClass getPluginClass() {
Map<String, PluginPropertyField> properties = new HashMap<>();
properties.put("lookupKey", new PluginPropertyField("fields", "", "string", true, false));
properties.put("destinationField", new PluginPropertyField("destinationField", "", "string", true, false));
properties.put("lookupName", new PluginPropertyField("lookupName", "", "string", true, false));
return new PluginClass(Transform.PLUGIN_TYPE, "Lookup", "", LookupTransform.class.getName(), "config", properties);
}
Aggregations