Search in sources :

Example 41 with Processor

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

the class InvokeScriptedProcessor method getRelationships.

/**
 * Returns the valid relationships for this processor as supplied by the
 * script itself.
 *
 * @return a Set of Relationships supported by this processor
 */
@Override
public Set<Relationship> getRelationships() {
    final Set<Relationship> relationships = new HashSet<>();
    final Processor instance = processor.get();
    if (instance != null) {
        try {
            final Set<Relationship> rels = instance.getRelationships();
            if (rels != null && !rels.isEmpty()) {
                relationships.addAll(rels);
            }
        } catch (final Throwable t) {
            final ComponentLog logger = getLogger();
            final String message = "Unable to get relationships from scripted Processor: " + t;
            logger.error(message);
            if (logger.isDebugEnabled()) {
                logger.error(message, t);
            }
        }
    }
    return Collections.unmodifiableSet(relationships);
}
Also used : AbstractSessionFactoryProcessor(org.apache.nifi.processor.AbstractSessionFactoryProcessor) Processor(org.apache.nifi.processor.Processor) Relationship(org.apache.nifi.processor.Relationship) ComponentLog(org.apache.nifi.logging.ComponentLog) HashSet(java.util.HashSet)

Example 42 with Processor

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

the class InvokeScriptedProcessor method onPropertyModified.

/**
 * Handles changes to this processor's properties. If changes are made to
 * script- or engine-related properties, the script will be reloaded.
 *
 * @param descriptor of the modified property
 * @param oldValue non-null property value (previous)
 * @param newValue the new property value or if null indicates the property
 */
@Override
public void onPropertyModified(final PropertyDescriptor descriptor, final String oldValue, final String newValue) {
    validationResults.set(null);
    final ComponentLog logger = getLogger();
    final Processor instance = processor.get();
    if (ScriptingComponentUtils.SCRIPT_FILE.equals(descriptor) || ScriptingComponentUtils.SCRIPT_BODY.equals(descriptor) || ScriptingComponentUtils.MODULES.equals(descriptor) || scriptingComponentHelper.SCRIPT_ENGINE.equals(descriptor)) {
        scriptNeedsReload.set(true);
        // reset engine. This happens only when a processor is stopped, so there won't be any performance impact in run-time.
        scriptEngine = null;
    } else if (instance != null) {
        // If the script provides a Processor, call its onPropertyModified() method
        try {
            instance.onPropertyModified(descriptor, oldValue, newValue);
        } catch (final Exception e) {
            final String message = "Unable to invoke onPropertyModified from script Processor: " + e;
            logger.error(message, e);
        }
    }
}
Also used : AbstractSessionFactoryProcessor(org.apache.nifi.processor.AbstractSessionFactoryProcessor) Processor(org.apache.nifi.processor.Processor) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessException(org.apache.nifi.processor.exception.ProcessException) ScriptException(javax.script.ScriptException)

Example 43 with Processor

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

the class TestPutSyslog method testIOExceptionCreatingConnection.

@Test
public void testIOExceptionCreatingConnection() throws IOException {
    final String pri = "34";
    final String version = "1";
    final String stamp = "2003-10-11T22:14:15.003Z";
    final String host = "mymachine.example.com";
    final String body = "su - ID47 - BOM'su root' failed for lonvick on /dev/pts/8";
    Processor proc = new MockCreationErrorPutSyslog(new MockErrorSender(), 1);
    runner = TestRunners.newTestRunner(proc);
    runner.setProperty(PutSyslog.HOSTNAME, "localhost");
    runner.setProperty(PutSyslog.PORT, "12345");
    runner.setProperty(PutSyslog.BATCH_SIZE, "1");
    runner.setProperty(PutSyslog.MSG_PRIORITY, pri);
    runner.setProperty(PutSyslog.MSG_VERSION, version);
    runner.setProperty(PutSyslog.MSG_TIMESTAMP, stamp);
    runner.setProperty(PutSyslog.MSG_HOSTNAME, host);
    runner.setProperty(PutSyslog.MSG_BODY, body);
    // the first run will throw IOException when calling send so the connection won't be re-qeued
    // the second run will try to create a new connection but throw an exception which should be caught and route files to failure
    runner.enqueue("incoming data".getBytes(Charset.forName("UTF-8")));
    runner.enqueue("incoming data".getBytes(Charset.forName("UTF-8")));
    runner.run(2);
    runner.assertAllFlowFilesTransferred(PutSyslog.REL_FAILURE, 2);
    Assert.assertEquals(0, sender.messages.size());
}
Also used : Processor(org.apache.nifi.processor.Processor) Test(org.junit.Test)

Example 44 with Processor

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

the class TestMockProcessSession method testRejectTransferNewlyCreatedFileToSelf.

@Test(expected = IllegalArgumentException.class)
public void testRejectTransferNewlyCreatedFileToSelf() {
    final Processor processor = new PoorlyBehavedProcessor();
    final MockProcessSession session = new MockProcessSession(new SharedSessionState(processor, new AtomicLong(0L)), processor);
    final FlowFile ff1 = session.createFlowFile("hello, world".getBytes());
    // this should throw an exception because we shouldn't allow a newly created flowfile to get routed back to self
    session.transfer(ff1);
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) AtomicLong(java.util.concurrent.atomic.AtomicLong) Processor(org.apache.nifi.processor.Processor) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) Test(org.junit.Test)

Example 45 with Processor

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

the class TestMockProcessSession method testKeepPenalizedStatusAfterPuttingAttribute.

@Test
public void testKeepPenalizedStatusAfterPuttingAttribute() {
    final Processor processor = new PoorlyBehavedProcessor();
    final MockProcessSession session = new MockProcessSession(new SharedSessionState(processor, new AtomicLong(0L)), processor);
    FlowFile ff1 = session.createFlowFile("hello, world".getBytes());
    ff1 = session.penalize(ff1);
    assertEquals(true, ff1.isPenalized());
    ff1 = session.putAttribute(ff1, "hello", "world");
    // adding attribute to flow file should not override the original penalized status
    assertEquals(true, ff1.isPenalized());
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) AtomicLong(java.util.concurrent.atomic.AtomicLong) Processor(org.apache.nifi.processor.Processor) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) 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