Search in sources :

Example 1 with PluginPropertyField

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()));
}
Also used : ArtifactId(co.cask.cdap.api.artifact.ArtifactId) IOException(java.io.IOException) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) Plugin(co.cask.cdap.api.plugin.Plugin)

Example 2 with PluginPropertyField

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;
}
Also used : CloseableClassLoader(co.cask.cdap.api.artifact.CloseableClassLoader) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) PluginClass(co.cask.cdap.api.plugin.PluginClass) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) InvalidArtifactException(co.cask.cdap.common.InvalidArtifactException) Plugin(co.cask.cdap.api.annotation.Plugin)

Example 3 with PluginPropertyField

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);
}
Also used : Description(co.cask.cdap.api.annotation.Description) Macro(co.cask.cdap.api.annotation.Macro) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) Annotation(java.lang.annotation.Annotation) Name(co.cask.cdap.api.annotation.Name)

Example 4 with PluginPropertyField

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()));
}
Also used : ArtifactId(co.cask.cdap.api.artifact.ArtifactId) IOException(java.io.IOException) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) Plugin(co.cask.cdap.api.plugin.Plugin)

Example 5 with PluginPropertyField

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;
}
Also used : PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Aggregations

PluginPropertyField (co.cask.cdap.api.plugin.PluginPropertyField)53 PluginClass (co.cask.cdap.api.plugin.PluginClass)45 HashMap (java.util.HashMap)32 Test (org.junit.Test)14 ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)12 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)12 ArtifactId (co.cask.cdap.proto.id.ArtifactId)11 Id (co.cask.cdap.common.id.Id)10 NamespaceId (co.cask.cdap.proto.id.NamespaceId)10 ImmutableSet (com.google.common.collect.ImmutableSet)5 Set (java.util.Set)5 Manifest (java.util.jar.Manifest)5 ApplicationClass (co.cask.cdap.api.artifact.ApplicationClass)4 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)4 ReflectionSchemaGenerator (co.cask.cdap.internal.io.ReflectionSchemaGenerator)4 IOException (java.io.IOException)4 ArtifactClasses (co.cask.cdap.api.artifact.ArtifactClasses)3 ArtifactId (co.cask.cdap.api.artifact.ArtifactId)3 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)3 HashSet (java.util.HashSet)3