Search in sources :

Example 1 with ProcessorInitializationContext

use of org.apache.nifi.processor.ProcessorInitializationContext in project nifi 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 2 with ProcessorInitializationContext

use of org.apache.nifi.processor.ProcessorInitializationContext 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 3 with ProcessorInitializationContext

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

the class TestJmsConsumer method testMap2FlowFileBytesMessage.

/**
 * Test BytesMessage to FlowFile conversion
 *
 * @throws java.lang.Exception ex
 */
@Test
public void testMap2FlowFileBytesMessage() throws Exception {
    TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class);
    BytesMessage bytesMessage = new ActiveMQBytesMessage();
    String sourceString = "Apache NiFi is an easy to use, powerful, and reliable system to process and distribute data.!";
    byte[] payload = sourceString.getBytes("UTF-8");
    bytesMessage.writeBytes(payload);
    bytesMessage.reset();
    ProcessContext context = runner.getProcessContext();
    ProcessSession session = runner.getProcessSessionFactory().createSession();
    ProcessorInitializationContext pic = new MockProcessorInitializationContext(runner.getProcessor(), (MockProcessContext) runner.getProcessContext());
    JmsProcessingSummary summary = JmsConsumer.map2FlowFile(context, session, bytesMessage, true, pic.getLogger());
    assertEquals("BytesMessage content length should equal to FlowFile content size", payload.length, summary.getLastFlowFile().getSize());
    final byte[] buffer = new byte[payload.length];
    runner.clearTransferState();
    session.read(summary.getLastFlowFile(), new InputStreamCallback() {

        @Override
        public void process(InputStream in) throws IOException {
            StreamUtils.fillBuffer(in, buffer, false);
        }
    });
    String contentString = new String(buffer, "UTF-8");
    assertEquals("", sourceString, contentString);
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) TestRunner(org.apache.nifi.util.TestRunner) InputStream(java.io.InputStream) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) ActiveMQBytesMessage(org.apache.activemq.command.ActiveMQBytesMessage) BytesMessage(javax.jms.BytesMessage) JmsProcessingSummary(org.apache.nifi.processors.standard.util.JmsProcessingSummary) IOException(java.io.IOException) ProcessContext(org.apache.nifi.processor.ProcessContext) MockProcessContext(org.apache.nifi.util.MockProcessContext) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) ActiveMQBytesMessage(org.apache.activemq.command.ActiveMQBytesMessage) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) Test(org.junit.Test)

Example 4 with ProcessorInitializationContext

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

the class TestJmsConsumer method testMap2FlowFileMapMessage.

/**
 * Test MapMessage to FlowFile conversion
 *
 * @throws java.lang.Exception ex
 */
@Test
public void testMap2FlowFileMapMessage() throws Exception {
    TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class);
    MapMessage mapMessage = createMapMessage();
    ProcessContext context = runner.getProcessContext();
    ProcessSession session = runner.getProcessSessionFactory().createSession();
    ProcessorInitializationContext pic = new MockProcessorInitializationContext(runner.getProcessor(), (MockProcessContext) runner.getProcessContext());
    JmsProcessingSummary summary = JmsConsumer.map2FlowFile(context, session, mapMessage, true, pic.getLogger());
    assertEquals("MapMessage should not create FlowFile content", 0, summary.getBytesReceived());
    Map<String, String> attributes = summary.getLastFlowFile().getAttributes();
    assertEquals("", "Arnold", attributes.get(JmsConsumer.MAP_MESSAGE_PREFIX + "name"));
    assertEquals("", "97", attributes.get(JmsConsumer.MAP_MESSAGE_PREFIX + "age"));
    assertEquals("", "89686.564", attributes.get(JmsConsumer.MAP_MESSAGE_PREFIX + "xyz"));
    assertEquals("", "true", attributes.get(JmsConsumer.MAP_MESSAGE_PREFIX + "good"));
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) TestRunner(org.apache.nifi.util.TestRunner) ActiveMQMapMessage(org.apache.activemq.command.ActiveMQMapMessage) MapMessage(javax.jms.MapMessage) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) JmsProcessingSummary(org.apache.nifi.processors.standard.util.JmsProcessingSummary) ProcessContext(org.apache.nifi.processor.ProcessContext) MockProcessContext(org.apache.nifi.util.MockProcessContext) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) Test(org.junit.Test)

Example 5 with ProcessorInitializationContext

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

the class FlowController method instantiateProcessor.

private LoggableComponent<Processor> instantiateProcessor(final String type, final String identifier, final BundleCoordinate bundleCoordinate, final Set<URL> additionalUrls) throws ProcessorInstantiationException {
    final Bundle processorBundle = ExtensionManager.getBundle(bundleCoordinate);
    if (processorBundle == null) {
        throw new ProcessorInstantiationException("Unable to find bundle for coordinate " + bundleCoordinate.getCoordinate());
    }
    final ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        final ClassLoader detectedClassLoaderForInstance = ExtensionManager.createInstanceClassLoader(type, identifier, processorBundle, additionalUrls);
        final Class<?> rawClass = Class.forName(type, true, detectedClassLoaderForInstance);
        Thread.currentThread().setContextClassLoader(detectedClassLoaderForInstance);
        final Class<? extends Processor> processorClass = rawClass.asSubclass(Processor.class);
        final Processor processor = processorClass.newInstance();
        final ComponentLog componentLogger = new SimpleProcessLogger(identifier, processor);
        final TerminationAwareLogger terminationAwareLogger = new TerminationAwareLogger(componentLogger);
        final ProcessorInitializationContext ctx = new StandardProcessorInitializationContext(identifier, terminationAwareLogger, this, this, nifiProperties);
        processor.initialize(ctx);
        LogRepositoryFactory.getRepository(identifier).setLogger(terminationAwareLogger);
        return new LoggableComponent<>(processor, bundleCoordinate, terminationAwareLogger);
    } catch (final Throwable t) {
        throw new ProcessorInstantiationException(type, t);
    } finally {
        if (ctxClassLoader != null) {
            Thread.currentThread().setContextClassLoader(ctxClassLoader);
        }
    }
}
Also used : GhostProcessor(org.apache.nifi.processor.GhostProcessor) Processor(org.apache.nifi.processor.Processor) Bundle(org.apache.nifi.bundle.Bundle) StandardProcessorInitializationContext(org.apache.nifi.processor.StandardProcessorInitializationContext) ComponentLog(org.apache.nifi.logging.ComponentLog) StandardProcessorInitializationContext(org.apache.nifi.processor.StandardProcessorInitializationContext) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) ProcessorInstantiationException(org.apache.nifi.controller.exception.ProcessorInstantiationException) NarThreadContextClassLoader(org.apache.nifi.nar.NarThreadContextClassLoader) SimpleProcessLogger(org.apache.nifi.processor.SimpleProcessLogger)

Aggregations

ProcessorInitializationContext (org.apache.nifi.processor.ProcessorInitializationContext)12 Processor (org.apache.nifi.processor.Processor)6 Test (org.junit.Test)5 ProcessContext (org.apache.nifi.processor.ProcessContext)4 ComponentLog (org.apache.nifi.logging.ComponentLog)3 ProcessSession (org.apache.nifi.processor.ProcessSession)3 StandardProcessorInitializationContext (org.apache.nifi.processor.StandardProcessorInitializationContext)3 JmsProcessingSummary (org.apache.nifi.processors.standard.util.JmsProcessingSummary)3 MockProcessContext (org.apache.nifi.util.MockProcessContext)3 MockProcessorInitializationContext (org.apache.nifi.util.MockProcessorInitializationContext)3 TestRunner (org.apache.nifi.util.TestRunner)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Bundle (org.apache.nifi.bundle.Bundle)2 MockProcessorInitializationContext (org.apache.nifi.mock.MockProcessorInitializationContext)2 NarCloseable (org.apache.nifi.nar.NarCloseable)2 AbstractProcessor (org.apache.nifi.processor.AbstractProcessor)2 InputStreamCallback (org.apache.nifi.processor.io.InputStreamCallback)2 StandardComponentVariableRegistry (org.apache.nifi.registry.variable.StandardComponentVariableRegistry)2 ModifiesClasspathNoAnnotationProcessor (org.apache.nifi.test.processors.ModifiesClasspathNoAnnotationProcessor)2