Search in sources :

Example 6 with ControllerService

use of org.apache.nifi.controller.ControllerService in project nifi by apache.

the class MockControllerServiceLookup method removeControllerService.

public void removeControllerService(final ControllerService service) {
    final ControllerService canonical = getControllerService(service.getIdentifier());
    if (canonical == null || canonical != service) {
        throw new IllegalArgumentException("Controller Service " + service + " is not known");
    }
    controllerServiceMap.remove(service.getIdentifier());
}
Also used : ControllerService(org.apache.nifi.controller.ControllerService)

Example 7 with ControllerService

use of org.apache.nifi.controller.ControllerService in project nifi by apache.

the class MockPropertyValue method asControllerService.

@Override
public <T extends ControllerService> T asControllerService(final Class<T> serviceType) throws IllegalArgumentException {
    ensureExpressionsEvaluated();
    if (rawValue == null || rawValue.equals("")) {
        return null;
    }
    final ControllerService service = serviceLookup.getControllerService(rawValue);
    if (serviceType.isAssignableFrom(service.getClass())) {
        return serviceType.cast(service);
    }
    throw new IllegalArgumentException("Controller Service with identifier " + rawValue + " is of type " + service.getClass() + " and cannot be cast to " + serviceType);
}
Also used : ControllerService(org.apache.nifi.controller.ControllerService)

Example 8 with ControllerService

use of org.apache.nifi.controller.ControllerService in project nifi by apache.

the class ExecuteGroovyScript method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession _session) throws ProcessException {
    boolean toFailureOnError = VALID_FAIL_STRATEGY[1].equals(context.getProperty(FAIL_STRATEGY).getValue());
    // create wrapped session to control list of newly created and files got from this session.
    // so transfer original input to failure will be possible
    GroovyProcessSessionWrap session = new GroovyProcessSessionWrap(_session, toFailureOnError);
    HashMap CTL = new AccessMap("CTL");
    HashMap SQL = new AccessMap("SQL");
    try {
        // compilation must be moved to validation
        Script script = getGroovyScript();
        Map bindings = script.getBinding().getVariables();
        bindings.clear();
        // Find the user-added properties and bind them for the script
        for (Map.Entry<PropertyDescriptor, String> property : context.getProperties().entrySet()) {
            if (property.getKey().isDynamic()) {
                if (property.getKey().getName().startsWith("CTL.")) {
                    // get controller service
                    ControllerService ctl = context.getProperty(property.getKey()).asControllerService(ControllerService.class);
                    CTL.put(property.getKey().getName().substring(4), ctl);
                } else if (property.getKey().getName().startsWith("SQL.")) {
                    DBCPService dbcp = context.getProperty(property.getKey()).asControllerService(DBCPService.class);
                    SQL.put(property.getKey().getName().substring(4), dbcp);
                } else {
                    // Add the dynamic property bound to its full PropertyValue to the script engine
                    if (property.getValue() != null) {
                        bindings.put(property.getKey().getName(), context.getProperty(property.getKey()));
                    }
                }
            }
        }
        onInitSQL(SQL);
        bindings.put("session", session);
        bindings.put("context", context);
        bindings.put("log", getLogger());
        bindings.put("REL_SUCCESS", REL_SUCCESS);
        bindings.put("REL_FAILURE", REL_FAILURE);
        bindings.put("CTL", CTL);
        bindings.put("SQL", SQL);
        script.run();
        bindings.clear();
        onCommitSQL(SQL);
        session.commit();
    } catch (Throwable t) {
        getLogger().error(t.toString(), t);
        onFailSQL(SQL);
        if (toFailureOnError) {
            // transfer all received to failure with two new attributes: ERROR_MESSAGE and ERROR_STACKTRACE.
            session.revertReceivedTo(REL_FAILURE, StackTraceUtils.deepSanitize(t));
        } else {
            session.rollback(true);
        }
    } finally {
        onFinitSQL(SQL);
    }
}
Also used : Script(groovy.lang.Script) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) GroovyProcessSessionWrap(org.apache.nifi.processors.groovyx.flow.GroovyProcessSessionWrap) DBCPService(org.apache.nifi.dbcp.DBCPService) HashMap(java.util.HashMap) Map(java.util.Map) ControllerService(org.apache.nifi.controller.ControllerService)

Example 9 with ControllerService

use of org.apache.nifi.controller.ControllerService in project nifi by apache.

the class NiFiRegistryFlowMapper method mapProperties.

private Map<String, String> mapProperties(final ConfiguredComponent component, final ControllerServiceProvider serviceProvider) {
    final Map<String, String> mapped = new HashMap<>();
    component.getProperties().keySet().stream().filter(property -> !property.isSensitive()).forEach(property -> {
        String value = component.getProperty(property);
        if (value == null) {
            value = property.getDefaultValue();
        }
        if (value != null && property.getControllerServiceDefinition() != null) {
            // Property references a Controller Service. Instead of storing the existing value, we want
            // to store the Versioned Component ID of the service.
            final ControllerServiceNode controllerService = serviceProvider.getControllerServiceNode(value);
            if (controllerService != null) {
                value = getId(controllerService.getVersionedComponentId(), controllerService.getIdentifier());
            }
        }
        mapped.put(property.getName(), value);
    });
    return mapped;
}
Also used : ProcessGroup(org.apache.nifi.groups.ProcessGroup) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate) ConnectableComponent(org.apache.nifi.registry.flow.ConnectableComponent) VariableDescriptor(org.apache.nifi.registry.VariableDescriptor) Port(org.apache.nifi.connectable.Port) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ClassUtils(org.apache.commons.lang3.ClassUtils) ComponentType(org.apache.nifi.registry.flow.ComponentType) ControllerServiceAPI(org.apache.nifi.registry.flow.ControllerServiceAPI) Map(java.util.Map) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) VersionedPort(org.apache.nifi.registry.flow.VersionedPort) VersionedRemoteProcessGroup(org.apache.nifi.registry.flow.VersionedRemoteProcessGroup) Connectable(org.apache.nifi.connectable.Connectable) Connection(org.apache.nifi.connectable.Connection) VersionedConnection(org.apache.nifi.registry.flow.VersionedConnection) Label(org.apache.nifi.controller.label.Label) Bundle(org.apache.nifi.registry.flow.Bundle) FlowRegistryClient(org.apache.nifi.registry.flow.FlowRegistryClient) Set(java.util.Set) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) VersionedFlowCoordinates(org.apache.nifi.registry.flow.VersionedFlowCoordinates) VersionedRemoteGroupPort(org.apache.nifi.registry.flow.VersionedRemoteGroupPort) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) PortType(org.apache.nifi.registry.flow.PortType) BatchSize(org.apache.nifi.registry.flow.BatchSize) VersionedFunnel(org.apache.nifi.registry.flow.VersionedFunnel) VersionControlInformation(org.apache.nifi.registry.flow.VersionControlInformation) ControllerService(org.apache.nifi.controller.ControllerService) RemoteProcessGroup(org.apache.nifi.groups.RemoteProcessGroup) Optional(java.util.Optional) ExtensionManager(org.apache.nifi.nar.ExtensionManager) ProcessorNode(org.apache.nifi.controller.ProcessorNode) Funnel(org.apache.nifi.connectable.Funnel) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) HashMap(java.util.HashMap) Position(org.apache.nifi.registry.flow.Position) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) FlowRegistry(org.apache.nifi.registry.flow.FlowRegistry) Relationship(org.apache.nifi.processor.Relationship) ControllerServiceProvider(org.apache.nifi.controller.service.ControllerServiceProvider) VersionedLabel(org.apache.nifi.registry.flow.VersionedLabel) LinkedHashSet(java.util.LinkedHashSet) ConnectableComponentType(org.apache.nifi.registry.flow.ConnectableComponentType) VersionedProcessor(org.apache.nifi.registry.flow.VersionedProcessor) ConfiguredComponent(org.apache.nifi.controller.ConfiguredComponent) RemoteGroupPort(org.apache.nifi.remote.RemoteGroupPort) TimeUnit(java.util.concurrent.TimeUnit) VersionedControllerService(org.apache.nifi.registry.flow.VersionedControllerService) VersionedPropertyDescriptor(org.apache.nifi.registry.flow.VersionedPropertyDescriptor) FlowFileQueue(org.apache.nifi.controller.queue.FlowFileQueue) HashMap(java.util.HashMap) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode)

Example 10 with ControllerService

use of org.apache.nifi.controller.ControllerService in project nifi by apache.

the class StandardControllerServiceProvider method createControllerService.

@Override
public ControllerServiceNode createControllerService(final String type, final String id, final BundleCoordinate bundleCoordinate, final Set<URL> additionalUrls, final boolean firstTimeAdded) {
    if (type == null || id == null || bundleCoordinate == null) {
        throw new NullPointerException();
    }
    ClassLoader cl = null;
    final ClassLoader currentContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        final Class<?> rawClass;
        try {
            final Bundle csBundle = ExtensionManager.getBundle(bundleCoordinate);
            if (csBundle == null) {
                throw new ControllerServiceInstantiationException("Unable to find bundle for coordinate " + bundleCoordinate.getCoordinate());
            }
            cl = ExtensionManager.createInstanceClassLoader(type, id, csBundle, additionalUrls);
            Thread.currentThread().setContextClassLoader(cl);
            rawClass = Class.forName(type, false, cl);
        } catch (final Exception e) {
            logger.error("Could not create Controller Service of type " + type + " for ID " + id + "; creating \"Ghost\" implementation", e);
            Thread.currentThread().setContextClassLoader(currentContextClassLoader);
            return createGhostControllerService(type, id, bundleCoordinate);
        }
        final Class<? extends ControllerService> controllerServiceClass = rawClass.asSubclass(ControllerService.class);
        final ControllerService originalService = controllerServiceClass.newInstance();
        final StandardControllerServiceInvocationHandler invocationHandler = new StandardControllerServiceInvocationHandler(originalService);
        // extract all interfaces... controllerServiceClass is non null so getAllInterfaces is non null
        final List<Class<?>> interfaceList = ClassUtils.getAllInterfaces(controllerServiceClass);
        final Class<?>[] interfaces = interfaceList.toArray(new Class<?>[interfaceList.size()]);
        final ControllerService proxiedService;
        if (cl == null) {
            proxiedService = (ControllerService) Proxy.newProxyInstance(getClass().getClassLoader(), interfaces, invocationHandler);
        } else {
            proxiedService = (ControllerService) Proxy.newProxyInstance(cl, interfaces, invocationHandler);
        }
        logger.info("Created Controller Service of type {} with identifier {}", type, id);
        final ComponentLog serviceLogger = new SimpleProcessLogger(id, originalService);
        final TerminationAwareLogger terminationAwareLogger = new TerminationAwareLogger(serviceLogger);
        originalService.initialize(new StandardControllerServiceInitializationContext(id, terminationAwareLogger, this, getStateManager(id), nifiProperties));
        final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(this, variableRegistry);
        final LoggableComponent<ControllerService> originalLoggableComponent = new LoggableComponent<>(originalService, bundleCoordinate, terminationAwareLogger);
        final LoggableComponent<ControllerService> proxiedLoggableComponent = new LoggableComponent<>(proxiedService, bundleCoordinate, terminationAwareLogger);
        final ComponentVariableRegistry componentVarRegistry = new StandardComponentVariableRegistry(this.variableRegistry);
        final ControllerServiceNode serviceNode = new StandardControllerServiceNode(originalLoggableComponent, proxiedLoggableComponent, invocationHandler, id, validationContextFactory, this, componentVarRegistry, flowController);
        serviceNode.setName(rawClass.getSimpleName());
        invocationHandler.setServiceNode(serviceNode);
        if (firstTimeAdded) {
            try (final NarCloseable x = NarCloseable.withComponentNarLoader(originalService.getClass(), originalService.getIdentifier())) {
                ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, originalService);
            } catch (final Exception e) {
                throw new ComponentLifeCycleException("Failed to invoke On-Added Lifecycle methods of " + originalService, e);
            }
        }
        serviceCache.putIfAbsent(id, serviceNode);
        return serviceNode;
    } catch (final Throwable t) {
        throw new ControllerServiceInstantiationException(t);
    } finally {
        if (currentContextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(currentContextClassLoader);
        }
    }
}
Also used : ControllerServiceInstantiationException(org.apache.nifi.controller.exception.ControllerServiceInstantiationException) ValidationContextFactory(org.apache.nifi.controller.ValidationContextFactory) StandardValidationContextFactory(org.apache.nifi.processor.StandardValidationContextFactory) ControllerService(org.apache.nifi.controller.ControllerService) StandardValidationContextFactory(org.apache.nifi.processor.StandardValidationContextFactory) SimpleProcessLogger(org.apache.nifi.processor.SimpleProcessLogger) NarCloseable(org.apache.nifi.nar.NarCloseable) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) Bundle(org.apache.nifi.bundle.Bundle) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) ComponentLog(org.apache.nifi.logging.ComponentLog) TimeoutException(java.util.concurrent.TimeoutException) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) ControllerServiceInstantiationException(org.apache.nifi.controller.exception.ControllerServiceInstantiationException) LoggableComponent(org.apache.nifi.controller.LoggableComponent) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) ComponentVariableRegistry(org.apache.nifi.registry.ComponentVariableRegistry) TerminationAwareLogger(org.apache.nifi.controller.TerminationAwareLogger)

Aggregations

ControllerService (org.apache.nifi.controller.ControllerService)25 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)6 BundleCoordinate (org.apache.nifi.bundle.BundleCoordinate)6 NarCloseable (org.apache.nifi.nar.NarCloseable)6 List (java.util.List)5 Map (java.util.Map)5 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)5 LinkedHashSet (java.util.LinkedHashSet)4 Set (java.util.Set)4 TimeUnit (java.util.concurrent.TimeUnit)4 Collectors (java.util.stream.Collectors)4 Bundle (org.apache.nifi.bundle.Bundle)4 ValidationResult (org.apache.nifi.components.ValidationResult)4 Connectable (org.apache.nifi.connectable.Connectable)4 Connection (org.apache.nifi.connectable.Connection)4 Port (org.apache.nifi.connectable.Port)4 ProcessorNode (org.apache.nifi.controller.ProcessorNode)4 Label (org.apache.nifi.controller.label.Label)4