use of io.cdap.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class PluginInstantiator method substituteMacros.
public PluginProperties substituteMacros(Plugin plugin, @Nullable MacroEvaluator macroEvaluator, @Nullable MacroParserOptions options) {
Map<String, String> properties = new HashMap<>();
Map<String, PluginPropertyField> pluginPropertyFieldMap = plugin.getPluginClass().getProperties();
// create macro evaluator and parser based on if it is config or runtime
boolean configTime = (macroEvaluator == null);
TrackingMacroEvaluator trackingMacroEvaluator = new TrackingMacroEvaluator();
for (Map.Entry<String, String> property : plugin.getProperties().getProperties().entrySet()) {
PluginPropertyField field = pluginPropertyFieldMap.get(property.getKey());
String propertyValue = property.getValue();
if (field != null && field.isMacroSupported()) {
// TODO: cleanup after endpoint to get plugin details is merged (#6089)
if (configTime) {
// parse for syntax check and check if trackingMacroEvaluator finds macro syntax present
MacroParser macroParser = new MacroParser(trackingMacroEvaluator, MacroParserOptions.builder().setEscaping(field.isMacroEscapingEnabled()).build());
macroParser.parse(propertyValue);
propertyValue = getOriginalOrDefaultValue(propertyValue, property.getKey(), field.getType(), trackingMacroEvaluator);
} else {
MacroParserOptions parserOptions = options == null ? MacroParserOptions.builder().setEscaping(field.isMacroEscapingEnabled()).build() : options;
MacroParser macroParser = new MacroParser(macroEvaluator, parserOptions);
propertyValue = macroParser.parse(propertyValue);
}
}
properties.put(property.getKey(), propertyValue);
}
return PluginProperties.builder().addAll(properties).build();
}
use of io.cdap.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class FindPluginHelper method getPlugin.
/**
* Get the Plugin information from the specified information.
*
* @param parents plugin parents of the plugin. Each item in the iterable must be the parent of the item before it,
* with the first item as the direct parent of the plugin
* @param pluginEntry artifact and class information for the plugin
* @param properties plugin properties
* @param pluginInstantiator instantiator to add the plugin artifact to
* @return plugin information
*/
public static Plugin getPlugin(Iterable<ArtifactId> parents, Map.Entry<ArtifactDescriptor, PluginClass> pluginEntry, PluginProperties properties, PluginInstantiator pluginInstantiator) {
CollectMacroEvaluator collectMacroEvaluator = new CollectMacroEvaluator();
for (PluginPropertyField field : pluginEntry.getValue().getProperties().values()) {
if (field.isMacroSupported() && properties.getProperties().containsKey(field.getName())) {
MacroParser parser = new MacroParser(collectMacroEvaluator, MacroParserOptions.builder().setEscaping(field.isMacroEscapingEnabled()).build());
parser.parse(properties.getProperties().get(field.getName()));
}
}
ArtifactId artifact = pluginEntry.getKey().getArtifactId();
try {
pluginInstantiator.addArtifact(pluginEntry.getKey().getLocation(), artifact);
} catch (IOException e) {
throw Throwables.propagate(e);
}
return new Plugin(parents, artifact, pluginEntry.getValue(), properties.setMacros(collectMacroEvaluator.getMacros()));
}
use of io.cdap.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class ArtifactStoreTest method testExcludedPlugins.
@Test
public void testExcludedPlugins() throws Exception {
ArtifactRange parentArtifacts = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"));
// does not have any requirement
PluginClass includedPlugin1 = PluginClass.builder().setName("includedPlugin1").setType("A").setDescription("desc").setClassName("c.p2").setConfigFieldName("conf").setProperties(ImmutableMap.of("stream", new PluginPropertyField("stream", "description", "string", true, false))).build();
PluginClass excludedPlugin1 = PluginClass.builder().setName("excludedPlugin1").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE))).build();
PluginClass excludedPlugin2 = PluginClass.builder().setName("excludedPlugin2").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of(KeyValueTable.TYPE))).build();
PluginClass excludedPlugin3 = PluginClass.builder().setName("excludedPlugin3").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE, KeyValueTable.TYPE))).build();
PluginClass excludedPlugin4 = PluginClass.builder().setName("excludedPlugin4").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE, KeyValueTable.TYPE, Cube.TYPE))).build();
PluginClass excludedPlugin5 = PluginClass.builder().setName("excludedPlugin5").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE, Cube.TYPE))).build();
PluginClass includedPlugin2 = PluginClass.builder().setName("includedPlugin2").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of("noTransactionNeeded"))).build();
PluginClass includedPlugin3 = PluginClass.builder().setName("includedPlugin3").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of("noTransactionNeeded", "tpfs"))).build();
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "ArtifactWithTransactionalPlugins", "1.0.0");
ArtifactMeta artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(includedPlugin1, includedPlugin2, includedPlugin3, excludedPlugin1, excludedPlugin2, excludedPlugin3, excludedPlugin4, excludedPlugin5).build(), ImmutableSet.of(parentArtifacts));
writeArtifact(artifactId, artifactMeta, "no-content");
ArtifactDescriptor artifactInfo = artifactStore.getArtifact(artifactId).getDescriptor();
// plugins which have transaction or spark as requirement should be excluded from plugins listed for the artifact
Map<ArtifactDescriptor, Set<PluginClass>> actual = artifactStore.getPluginClasses(NamespaceId.DEFAULT, artifactId);
Set<PluginClass> expectedPlugins = ImmutableSet.of(includedPlugin1, includedPlugin2, includedPlugin3);
Assert.assertEquals(ImmutableMap.of(artifactInfo, expectedPlugins), actual);
// excluded plugins should also not be in the plugins listed for the namespace
List<ArtifactDetail> actualArtifacts = artifactStore.getArtifacts(NamespaceId.DEFAULT);
Assert.assertEquals(1, actualArtifacts.size());
Assert.assertEquals(expectedPlugins, actualArtifacts.get(0).getMeta().getClasses().getPlugins());
// excluded plugins should also not be in the plugins listed for the artifact id
ArtifactDetail actualArtifact = artifactStore.getArtifact(artifactId);
Assert.assertEquals(expectedPlugins, actualArtifact.getMeta().getClasses().getPlugins());
}
use of io.cdap.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class DefaultArtifactInspectorTest method testInspectNestedConfigPlugin.
@Test
public void testInspectNestedConfigPlugin() throws Exception {
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, NestedConfigPlugin.class.getPackage().getName());
File artifactFile = createJar(NestedConfigPlugin.class, new File(TMP_FOLDER.newFolder(), "NestedPlugin-1.0.0.jar"), manifest);
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "NestedPlugin", "1.0.0");
Location artifactLocation = Locations.toLocation(artifactFile);
List<ArtifactDescriptor> parentDescriptor = new ArrayList<>();
parentDescriptor.add(new ArtifactDescriptor(artifactId.getNamespace().getId(), artifactId.toArtifactId(), artifactLocation));
ArtifactClasses classes = artifactInspector.inspectArtifact(artifactId, artifactFile, parentDescriptor, Collections.emptySet()).getArtifactClasses();
Set<PluginClass> plugins = classes.getPlugins();
Map<String, PluginPropertyField> expectedFields = ImmutableMap.of("X", new PluginPropertyField("X", "", "int", true, false), "Nested", new PluginPropertyField("Nested", "", "nestedconfig", true, true, false, ImmutableSet.of("Nested1", "Nested2")), "Nested1", new PluginPropertyField("Nested1", "", "string", true, true), "Nested2", new PluginPropertyField("Nested2", "", "string", true, true));
PluginClass expected = PluginClass.builder().setName("nested").setType("dummy").setDescription("Nested config").setClassName(NestedConfigPlugin.class.getName()).setConfigFieldName("config").setProperties(expectedFields).build();
Assert.assertEquals(Collections.singleton(expected), plugins);
}
use of io.cdap.cdap.api.plugin.PluginPropertyField 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