Search in sources :

Example 46 with NarCloseable

use of org.apache.nifi.nar.NarCloseable in project nifi by apache.

the class AbstractConfiguredComponent method validate.

@Override
public Collection<ValidationResult> validate(final ValidationContext context) {
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(getComponent().getClass(), getComponent().getIdentifier())) {
        final Collection<ValidationResult> validationResults = getComponent().validate(context);
        // validate selected controller services implement the API required by the processor
        final List<PropertyDescriptor> supportedDescriptors = getComponent().getPropertyDescriptors();
        if (null != supportedDescriptors) {
            for (final PropertyDescriptor descriptor : supportedDescriptors) {
                if (descriptor.getControllerServiceDefinition() == null) {
                    // skip properties that aren't for a controller service
                    continue;
                }
                final String controllerServiceId = context.getProperty(descriptor).getValue();
                if (controllerServiceId == null) {
                    // if the property value is null we should already have a validation error
                    continue;
                }
                final ControllerServiceNode controllerServiceNode = getControllerServiceProvider().getControllerServiceNode(controllerServiceId);
                if (controllerServiceNode == null) {
                    // if the node was null we should already have a validation error
                    continue;
                }
                final Class<? extends ControllerService> controllerServiceApiClass = descriptor.getControllerServiceDefinition();
                final ClassLoader controllerServiceApiClassLoader = controllerServiceApiClass.getClassLoader();
                final Consumer<String> addValidationError = explanation -> validationResults.add(new ValidationResult.Builder().input(controllerServiceId).subject(descriptor.getDisplayName()).valid(false).explanation(explanation).build());
                final Bundle controllerServiceApiBundle = ExtensionManager.getBundle(controllerServiceApiClassLoader);
                if (controllerServiceApiBundle == null) {
                    addValidationError.accept(String.format("Unable to find bundle for ControllerService API class %s.", controllerServiceApiClass.getCanonicalName()));
                    continue;
                }
                final BundleCoordinate controllerServiceApiCoordinate = controllerServiceApiBundle.getBundleDetails().getCoordinate();
                final Bundle controllerServiceBundle = ExtensionManager.getBundle(controllerServiceNode.getBundleCoordinate());
                if (controllerServiceBundle == null) {
                    addValidationError.accept(String.format("Unable to find bundle for coordinate %s.", controllerServiceNode.getBundleCoordinate()));
                    continue;
                }
                final BundleCoordinate controllerServiceCoordinate = controllerServiceBundle.getBundleDetails().getCoordinate();
                final boolean matchesApi = matchesApi(controllerServiceBundle, controllerServiceApiCoordinate);
                if (!matchesApi) {
                    final String controllerServiceType = controllerServiceNode.getComponentType();
                    final String controllerServiceApiType = controllerServiceApiClass.getSimpleName();
                    final String explanation = new StringBuilder().append(controllerServiceType).append(" - ").append(controllerServiceCoordinate.getVersion()).append(" from ").append(controllerServiceCoordinate.getGroup()).append(" - ").append(controllerServiceCoordinate.getId()).append(" is not compatible with ").append(controllerServiceApiType).append(" - ").append(controllerServiceApiCoordinate.getVersion()).append(" from ").append(controllerServiceApiCoordinate.getGroup()).append(" - ").append(controllerServiceApiCoordinate.getId()).toString();
                    addValidationError.accept(explanation);
                }
            }
        }
        return validationResults;
    }
}
Also used : Bundle(org.apache.nifi.bundle.Bundle) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate) URL(java.net.URL) ValidationContext(org.apache.nifi.components.ValidationContext) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConfigurableComponent(org.apache.nifi.components.ConfigurableComponent) AtomicReference(java.util.concurrent.atomic.AtomicReference) StringUtils(org.apache.commons.lang3.StringUtils) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ClassLoaderUtils(org.apache.nifi.util.file.classloader.ClassLoaderUtils) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ControllerServiceProvider(org.apache.nifi.controller.service.ControllerServiceProvider) NarCloseable(org.apache.nifi.nar.NarCloseable) LinkedHashSet(java.util.LinkedHashSet) ValidationResult(org.apache.nifi.components.ValidationResult) CharacterFilterUtils(org.apache.nifi.util.CharacterFilterUtils) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) Logger(org.slf4j.Logger) ReentrantLock(java.util.concurrent.locks.ReentrantLock) MalformedURLException(java.net.MalformedURLException) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Objects(java.util.Objects) Consumer(java.util.function.Consumer) List(java.util.List) Lock(java.util.concurrent.locks.Lock) ComponentVariableRegistry(org.apache.nifi.registry.ComponentVariableRegistry) ExtensionManager(org.apache.nifi.nar.ExtensionManager) Collections(java.util.Collections) NarCloseable(org.apache.nifi.nar.NarCloseable) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) Bundle(org.apache.nifi.bundle.Bundle) ValidationResult(org.apache.nifi.components.ValidationResult) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode)

Example 47 with NarCloseable

use of org.apache.nifi.nar.NarCloseable in project nifi-minifi by apache.

the class ProcessorInitializer method initialize.

@Override
public void initialize(ConfigurableComponent component) {
    Processor processor = (Processor) component;
    ProcessorInitializationContext initializationContext = new MockProcessorInitializationContext();
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), initializationContext.getIdentifier())) {
        processor.initialize(initializationContext);
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) Processor(org.apache.nifi.processor.Processor) MockProcessorInitializationContext(org.apache.nifi.mock.MockProcessorInitializationContext) MockProcessorInitializationContext(org.apache.nifi.mock.MockProcessorInitializationContext) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext)

Example 48 with NarCloseable

use of org.apache.nifi.nar.NarCloseable in project nifi-minifi by apache.

the class ControllerServiceInitializer method initialize.

@Override
public void initialize(ConfigurableComponent component) throws InitializationException {
    ControllerService controllerService = (ControllerService) component;
    ControllerServiceInitializationContext context = new MockControllerServiceInitializationContext();
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), context.getIdentifier())) {
        controllerService.initialize(context);
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) ControllerServiceInitializationContext(org.apache.nifi.controller.ControllerServiceInitializationContext) MockControllerServiceInitializationContext(org.apache.nifi.mock.MockControllerServiceInitializationContext) MockControllerServiceInitializationContext(org.apache.nifi.mock.MockControllerServiceInitializationContext) ControllerService(org.apache.nifi.controller.ControllerService)

Aggregations

NarCloseable (org.apache.nifi.nar.NarCloseable)48 ComponentLog (org.apache.nifi.logging.ComponentLog)13 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)12 Processor (org.apache.nifi.processor.Processor)12 HashMap (java.util.HashMap)9 ControllerServiceNode (org.apache.nifi.controller.service.ControllerServiceNode)9 SimpleProcessLogger (org.apache.nifi.processor.SimpleProcessLogger)9 URL (java.net.URL)8 LinkedHashMap (java.util.LinkedHashMap)8 ProcessorInstantiationException (org.apache.nifi.controller.exception.ProcessorInstantiationException)8 ReportingTask (org.apache.nifi.reporting.ReportingTask)8 Map (java.util.Map)7 ControllerService (org.apache.nifi.controller.ControllerService)6 IOException (java.io.IOException)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 ArrayList (java.util.ArrayList)5 ComponentLifeCycleException (org.apache.nifi.controller.exception.ComponentLifeCycleException)5 ControllerServiceInstantiationException (org.apache.nifi.controller.exception.ControllerServiceInstantiationException)5 Relationship (org.apache.nifi.processor.Relationship)5 ComponentVariableRegistry (org.apache.nifi.registry.ComponentVariableRegistry)5