use of co.cask.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class FindPluginHelper method getPlugin.
private static Plugin getPlugin(Map.Entry<ArtifactDescriptor, PluginClass> pluginEntry, PluginProperties properties, String pluginType, String pluginName, PluginInstantiator pluginInstantiator) {
CollectMacroEvaluator collectMacroEvaluator = new CollectMacroEvaluator();
// No type checking is done for now.
for (PluginPropertyField field : pluginEntry.getValue().getProperties().values()) {
Preconditions.checkArgument(!field.isRequired() || (properties.getProperties().containsKey(field.getName())), "Required property '%s' missing for plugin of type %s, name %s.", field.getName(), pluginType, pluginName);
if (field.isMacroSupported()) {
MacroParser parser = new MacroParser(collectMacroEvaluator, field.isMacroEscapingEnabled());
parser.parse(properties.getProperties().get(field.getName()));
}
}
ArtifactId artifact = pluginEntry.getKey().getArtifactId();
try {
pluginInstantiator.addArtifact(pluginEntry.getKey().getLocation(), artifact);
} catch (IOException e) {
Throwables.propagate(e);
}
return new Plugin(artifact, pluginEntry.getValue(), properties.setMacros(collectMacroEvaluator.getMacros()));
}
use of co.cask.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class ArtifactInspector method inspectPlugins.
/**
* Inspects the plugin file and extracts plugin classes information.
*/
private ArtifactClasses.Builder inspectPlugins(ArtifactClasses.Builder builder, File artifactFile, ArtifactId artifactId, PluginInstantiator pluginInstantiator) throws IOException, InvalidArtifactException {
// See if there are export packages. Plugins should be in those packages
Set<String> exportPackages = getExportPackages(artifactFile);
if (exportPackages.isEmpty()) {
return builder;
}
try {
ClassLoader pluginClassLoader = pluginInstantiator.getArtifactClassLoader(artifactId);
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);
Set<String> pluginEndpoints = getPluginEndpoints(cls);
PluginClass pluginClass = new PluginClass(pluginAnnotation.type(), getPluginName(cls), getPluginDescription(cls), cls.getName(), configField, pluginProperties, pluginEndpoints);
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);
}
return builder;
}
use of co.cask.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class ArtifactInspector method createPluginProperty.
/**
* Creates a {@link PluginPropertyField} based on the given field.
*/
private PluginPropertyField createPluginProperty(Field field, TypeToken<?> resolvingType) throws UnsupportedTypeException {
TypeToken<?> fieldType = resolvingType.resolveType(field.getGenericType());
Class<?> rawType = fieldType.getRawType();
Name nameAnnotation = field.getAnnotation(Name.class);
Description descAnnotation = field.getAnnotation(Description.class);
String name = nameAnnotation == null ? field.getName() : nameAnnotation.value();
String description = descAnnotation == null ? "" : descAnnotation.value();
Macro macroAnnotation = field.getAnnotation(Macro.class);
boolean macroSupported = macroAnnotation != null;
if (rawType.isPrimitive()) {
return new PluginPropertyField(name, description, rawType.getName(), true, macroSupported);
}
rawType = Primitives.unwrap(rawType);
if (!rawType.isPrimitive() && !String.class.equals(rawType)) {
throw new UnsupportedTypeException("Only primitive and String types are supported");
}
boolean required = true;
for (Annotation annotation : field.getAnnotations()) {
if (annotation.annotationType().getName().endsWith(".Nullable")) {
required = false;
break;
}
}
return new PluginPropertyField(name, description, rawType.getSimpleName().toLowerCase(), required, macroSupported);
}
use of co.cask.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 pluginType plugin type
* @param pluginName plugin name
* @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, String pluginType, String pluginName, PluginInstantiator pluginInstantiator) {
CollectMacroEvaluator collectMacroEvaluator = new CollectMacroEvaluator();
// No type checking is done for now.
for (PluginPropertyField field : pluginEntry.getValue().getProperties().values()) {
Preconditions.checkArgument(!field.isRequired() || (properties.getProperties().containsKey(field.getName())), "Required property '%s' missing for plugin of type %s, name %s.", field.getName(), pluginType, pluginName);
if (field.isMacroSupported()) {
MacroParser parser = new MacroParser(collectMacroEvaluator, field.isMacroEscapingEnabled());
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 co.cask.cdap.api.plugin.PluginPropertyField in project cdap by caskdata.
the class PluginInstantiator method getFieldsWithMacro.
private Set<String> getFieldsWithMacro(Plugin plugin) {
// TODO: cleanup after endpoint to get plugin details is merged (#6089)
Set<String> macroFields = new HashSet<>();
Map<String, PluginPropertyField> pluginPropertyFieldMap = plugin.getPluginClass().getProperties();
TrackingMacroEvaluator trackingMacroEvaluator = new TrackingMacroEvaluator();
for (Map.Entry<String, PluginPropertyField> pluginEntry : pluginPropertyFieldMap.entrySet()) {
PluginPropertyField pluginField = pluginEntry.getValue();
if (pluginEntry.getValue() != null && pluginField.isMacroSupported()) {
String macroValue = plugin.getProperties().getProperties().get(pluginEntry.getKey());
if (macroValue != null) {
MacroParser macroParser = new MacroParser(trackingMacroEvaluator, pluginField.isMacroEscapingEnabled());
macroParser.parse(macroValue);
if (trackingMacroEvaluator.hasMacro()) {
macroFields.add(pluginEntry.getKey());
trackingMacroEvaluator.reset();
}
}
}
}
return macroFields;
}
Aggregations