Search in sources :

Example 16 with Processor

use of org.apache.nifi.processor.Processor in project nifi by apache.

the class InvokeScriptedProcessor method reloadScript.

/**
 * Reloads the script Processor. This must be called within the lock.
 *
 * @param scriptBody An input stream associated with the script content
 * @return Whether the script was successfully reloaded
 */
private boolean reloadScript(final String scriptBody) {
    // note we are starting here with a fresh listing of validation
    // results since we are (re)loading a new/updated script. any
    // existing validation results are not relevant
    final Collection<ValidationResult> results = new HashSet<>();
    try {
        // get the engine and ensure its invocable
        if (scriptEngine instanceof Invocable) {
            final Invocable invocable = (Invocable) scriptEngine;
            // Find a custom configurator and invoke their eval() method
            ScriptEngineConfigurator configurator = scriptingComponentHelper.scriptEngineConfiguratorMap.get(scriptingComponentHelper.getScriptEngineName().toLowerCase());
            if (configurator != null) {
                configurator.eval(scriptEngine, scriptBody, scriptingComponentHelper.getModules());
            } else {
                // evaluate the script
                scriptEngine.eval(scriptBody);
            }
            // get configured processor from the script (if it exists)
            final Object obj = scriptEngine.get("processor");
            if (obj != null) {
                final ComponentLog logger = getLogger();
                try {
                    // set the logger if the processor wants it
                    invocable.invokeMethod(obj, "setLogger", logger);
                } catch (final NoSuchMethodException nsme) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Configured script Processor does not contain a setLogger method.");
                    }
                }
                // record the processor for use later
                final Processor scriptProcessor = invocable.getInterface(obj, Processor.class);
                processor.set(scriptProcessor);
                if (scriptProcessor != null) {
                    try {
                        scriptProcessor.initialize(new ProcessorInitializationContext() {

                            @Override
                            public String getIdentifier() {
                                return InvokeScriptedProcessor.this.getIdentifier();
                            }

                            @Override
                            public ComponentLog getLogger() {
                                return logger;
                            }

                            @Override
                            public ControllerServiceLookup getControllerServiceLookup() {
                                return InvokeScriptedProcessor.super.getControllerServiceLookup();
                            }

                            @Override
                            public NodeTypeProvider getNodeTypeProvider() {
                                return InvokeScriptedProcessor.super.getNodeTypeProvider();
                            }

                            @Override
                            public String getKerberosServicePrincipal() {
                                return InvokeScriptedProcessor.this.kerberosServicePrincipal;
                            }

                            @Override
                            public File getKerberosServiceKeytab() {
                                return InvokeScriptedProcessor.this.kerberosServiceKeytab;
                            }

                            @Override
                            public File getKerberosConfigurationFile() {
                                return InvokeScriptedProcessor.this.kerberosConfigFile;
                            }
                        });
                    } catch (final Exception e) {
                        logger.error("Unable to initialize scripted Processor: " + e.getLocalizedMessage(), e);
                        throw new ProcessException(e);
                    }
                }
            } else {
                throw new ScriptException("No processor was defined by the script.");
            }
        }
    } catch (final Exception ex) {
        final ComponentLog logger = getLogger();
        final String message = "Unable to load script: " + ex.getLocalizedMessage();
        logger.error(message, ex);
        results.add(new ValidationResult.Builder().subject("ScriptValidation").valid(false).explanation("Unable to load script due to " + ex.getLocalizedMessage()).input(scriptingComponentHelper.getScriptPath()).build());
    }
    // store the updated validation results
    validationResults.set(results);
    // return whether there was any issues loading the configured script
    return results.isEmpty();
}
Also used : AbstractSessionFactoryProcessor(org.apache.nifi.processor.AbstractSessionFactoryProcessor) Processor(org.apache.nifi.processor.Processor) NodeTypeProvider(org.apache.nifi.controller.NodeTypeProvider) ValidationResult(org.apache.nifi.components.ValidationResult) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) ProcessException(org.apache.nifi.processor.exception.ProcessException) ScriptException(javax.script.ScriptException) Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) ProcessException(org.apache.nifi.processor.exception.ProcessException) File(java.io.File) ControllerServiceLookup(org.apache.nifi.controller.ControllerServiceLookup) HashSet(java.util.HashSet)

Example 17 with Processor

use of org.apache.nifi.processor.Processor in project nifi by apache.

the class InvokeScriptedProcessor method customValidate.

/**
 * Invokes the validate() routine provided by the script, allowing for
 * custom validation code. This method assumes there is a valid Processor
 * defined in the script and it has been loaded by the
 * InvokeScriptedProcessor processor
 *
 * @param context The validation context to be passed into the custom
 * validate method
 * @return A collection of ValidationResults returned by the custom validate
 * method
 */
@Override
protected Collection<ValidationResult> customValidate(final ValidationContext context) {
    Collection<ValidationResult> commonValidationResults = super.customValidate(context);
    if (!commonValidationResults.isEmpty()) {
        return commonValidationResults;
    }
    // and don't print anything into log.
    if (validationResults.get() != null) {
        return validationResults.get();
    }
    scriptingComponentHelper.setScriptEngineName(context.getProperty(scriptingComponentHelper.SCRIPT_ENGINE).getValue());
    scriptingComponentHelper.setScriptPath(context.getProperty(ScriptingComponentUtils.SCRIPT_FILE).evaluateAttributeExpressions().getValue());
    scriptingComponentHelper.setScriptBody(context.getProperty(ScriptingComponentUtils.SCRIPT_BODY).getValue());
    String modulePath = context.getProperty(ScriptingComponentUtils.MODULES).evaluateAttributeExpressions().getValue();
    if (!StringUtils.isEmpty(modulePath)) {
        scriptingComponentHelper.setModules(modulePath.split(","));
    } else {
        scriptingComponentHelper.setModules(new String[0]);
    }
    setup();
    // Now that InvokeScriptedProcessor is validated, we can call validate on the scripted processor
    final Processor instance = processor.get();
    final Collection<ValidationResult> currentValidationResults = validationResults.get();
    // if there was existing validation errors and the processor loaded successfully
    if (currentValidationResults.isEmpty() && instance != null) {
        try {
            // defer to the underlying processor for validation
            final Collection<ValidationResult> instanceResults = instance.validate(context);
            if (instanceResults != null && instanceResults.size() > 0) {
                // return the validation results from the underlying instance
                return instanceResults;
            }
        } catch (final Exception e) {
            final ComponentLog logger = getLogger();
            final String message = "Unable to validate the script Processor: " + e;
            logger.error(message, e);
            // return a new validation message
            final Collection<ValidationResult> results = new HashSet<>();
            results.add(new ValidationResult.Builder().subject("Validation").valid(false).explanation("An error occurred calling validate in the configured script Processor.").input(context.getProperty(ScriptingComponentUtils.SCRIPT_FILE).getValue()).build());
            return results;
        }
    }
    return currentValidationResults;
}
Also used : AbstractSessionFactoryProcessor(org.apache.nifi.processor.AbstractSessionFactoryProcessor) Processor(org.apache.nifi.processor.Processor) Collection(java.util.Collection) ValidationResult(org.apache.nifi.components.ValidationResult) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessException(org.apache.nifi.processor.exception.ProcessException) ScriptException(javax.script.ScriptException)

Example 18 with Processor

use of org.apache.nifi.processor.Processor in project nifi by apache.

the class InvokeScriptedProcessor method onTrigger.

/**
 * Invokes the onTrigger() method of the scripted processor. If the script
 * failed to reload, the processor yields until the script can be reloaded
 * successfully. If the scripted processor's onTrigger() method throws an
 * exception, a ProcessException will be thrown. If no processor is defined
 * by the script, an error is logged with the system.
 *
 * @param context provides access to convenience methods for obtaining
 * property values, delaying the scheduling of the processor, provides
 * access to Controller Services, etc.
 * @param sessionFactory provides access to a {@link ProcessSessionFactory},
 * which can be used for accessing FlowFiles, etc.
 * @throws ProcessException if the scripted processor's onTrigger() method
 * throws an exception
 */
@Override
public void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException {
    // Initialize the rest of the processor resources if we have not already done so
    synchronized (scriptingComponentHelper.isInitialized) {
        if (!scriptingComponentHelper.isInitialized.get()) {
            scriptingComponentHelper.createResources();
        }
    }
    ComponentLog log = getLogger();
    // ensure the processor (if it exists) is loaded
    final Processor instance = processor.get();
    // ensure the processor did not fail to reload at some point
    final Collection<ValidationResult> results = validationResults.get();
    if (!results.isEmpty()) {
        log.error(String.format("Unable to run because the Processor is not valid: [%s]", StringUtils.join(results, ", ")));
        context.yield();
        return;
    }
    if (instance != null) {
        try {
            // run the processor
            instance.onTrigger(context, sessionFactory);
        } catch (final ProcessException e) {
            final String message = String.format("An error occurred executing the configured Processor [%s]: %s", context.getProperty(ScriptingComponentUtils.SCRIPT_FILE).getValue(), e);
            log.error(message);
            throw e;
        }
    } else {
        log.error("There is no processor defined by the script");
    }
}
Also used : ProcessException(org.apache.nifi.processor.exception.ProcessException) AbstractSessionFactoryProcessor(org.apache.nifi.processor.AbstractSessionFactoryProcessor) Processor(org.apache.nifi.processor.Processor) ValidationResult(org.apache.nifi.components.ValidationResult) ComponentLog(org.apache.nifi.logging.ComponentLog)

Example 19 with Processor

use of org.apache.nifi.processor.Processor in project nifi by apache.

the class TestWebSocketMessageRouter method testSendMessage.

@Test
public void testSendMessage() throws Exception {
    final WebSocketMessageRouter router = new WebSocketMessageRouter("endpoint-id");
    final Processor processor1 = mock(Processor.class);
    when(processor1.getIdentifier()).thenReturn("processor-1");
    final AbstractWebSocketSession session = mock(AbstractWebSocketSession.class);
    when(session.getSessionId()).thenReturn("session-1");
    doAnswer(invocation -> {
        assertEquals("message", invocation.getArgumentAt(0, String.class));
        return null;
    }).when(session).sendString(anyString());
    router.registerProcessor(processor1);
    router.captureSession(session);
    router.sendMessage("session-1", sender -> sender.sendString("message"));
    try {
        router.sendMessage("session-2", sender -> sender.sendString("message"));
        fail("Should fail because there's no session with id session-2.");
    } catch (IllegalStateException e) {
    }
}
Also used : Processor(org.apache.nifi.processor.Processor) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 20 with Processor

use of org.apache.nifi.processor.Processor in project nifi by apache.

the class TestJoltTransformJSON method testRelationshipsCreated.

@Test
public void testRelationshipsCreated() throws IOException {
    Processor processor = new JoltTransformJSON();
    final TestRunner runner = TestRunners.newTestRunner(processor);
    final String spec = new String(Files.readAllBytes(Paths.get("src/test/resources/TestJoltTransformJson/chainrSpec.json")));
    runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
    runner.enqueue(JSON_INPUT);
    Set<Relationship> relationships = processor.getRelationships();
    assertTrue(relationships.contains(JoltTransformJSON.REL_FAILURE));
    assertTrue(relationships.contains(JoltTransformJSON.REL_SUCCESS));
    assertTrue(relationships.size() == 2);
}
Also used : Processor(org.apache.nifi.processor.Processor) TestRunner(org.apache.nifi.util.TestRunner) Relationship(org.apache.nifi.processor.Relationship) Test(org.junit.Test)

Aggregations

Processor (org.apache.nifi.processor.Processor)50 Test (org.junit.Test)19 AbstractProcessor (org.apache.nifi.processor.AbstractProcessor)13 ComponentLog (org.apache.nifi.logging.ComponentLog)12 NarCloseable (org.apache.nifi.nar.NarCloseable)12 AtomicLong (java.util.concurrent.atomic.AtomicLong)11 Relationship (org.apache.nifi.processor.Relationship)11 ProcessorNode (org.apache.nifi.controller.ProcessorNode)9 HashSet (java.util.HashSet)8 StandardComponentVariableRegistry (org.apache.nifi.registry.variable.StandardComponentVariableRegistry)8 StandardProcessorInitializationContext (org.apache.nifi.processor.StandardProcessorInitializationContext)7 StandardProcessorNode (org.apache.nifi.controller.StandardProcessorNode)6 AbstractSessionFactoryProcessor (org.apache.nifi.processor.AbstractSessionFactoryProcessor)6 ProcessorInitializationContext (org.apache.nifi.processor.ProcessorInitializationContext)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 StandardValidationContextFactory (org.apache.nifi.processor.StandardValidationContextFactory)5 Collection (java.util.Collection)4 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)4