Search in sources :

Example 26 with Processor

use of org.apache.nifi.processor.Processor 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)

Example 27 with Processor

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

the class HtmlProcessorDocumentationWriter method writeAdditionalBodyInfo.

@Override
protected void writeAdditionalBodyInfo(final ConfigurableComponent configurableComponent, final XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
    final Processor processor = (Processor) configurableComponent;
    writeRelationships(processor, xmlStreamWriter);
    writeDynamicRelationships(processor, xmlStreamWriter);
    writeAttributeInfo(processor, xmlStreamWriter);
}
Also used : Processor(org.apache.nifi.processor.Processor)

Example 28 with Processor

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

the class ProcessorInitializer method teardown.

@Override
public void teardown(ConfigurableComponent component) {
    Processor processor = (Processor) component;
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), component.getIdentifier())) {
        final ComponentLog logger = new MockComponentLogger();
        final MockProcessContext context = new MockProcessContext();
        ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnShutdown.class, processor, logger, context);
    } finally {
        ExtensionManager.removeInstanceClassLoader(component.getIdentifier());
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) Processor(org.apache.nifi.processor.Processor) MockComponentLogger(org.apache.nifi.mock.MockComponentLogger) ComponentLog(org.apache.nifi.logging.ComponentLog) MockProcessContext(org.apache.nifi.mock.MockProcessContext)

Example 29 with Processor

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

the class TestStandardProcessorNode method createProcessorNode.

private StandardProcessorNode createProcessorNode(final Processor processor, final ReloadComponent reloadComponent) {
    final String uuid = UUID.randomUUID().toString();
    final ValidationContextFactory validationContextFactory = createValidationContextFactory();
    final NiFiProperties niFiProperties = NiFiProperties.createBasicNiFiProperties("src/test/resources/conf/nifi.properties", null);
    final ProcessScheduler processScheduler = Mockito.mock(ProcessScheduler.class);
    final TerminationAwareLogger componentLog = Mockito.mock(TerminationAwareLogger.class);
    final Bundle systemBundle = SystemBundle.create(niFiProperties);
    ExtensionManager.discoverExtensions(systemBundle, Collections.emptySet());
    ExtensionManager.createInstanceClassLoader(processor.getClass().getName(), uuid, systemBundle, null);
    ProcessorInitializationContext initContext = new StandardProcessorInitializationContext(uuid, componentLog, null, null, null);
    processor.initialize(initContext);
    final LoggableComponent<Processor> loggableComponent = new LoggableComponent<>(processor, systemBundle.getBundleDetails().getCoordinate(), componentLog);
    return new StandardProcessorNode(loggableComponent, uuid, validationContextFactory, processScheduler, null, niFiProperties, new StandardComponentVariableRegistry(variableRegistry), reloadComponent);
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) ModifiesClasspathNoAnnotationProcessor(org.apache.nifi.test.processors.ModifiesClasspathNoAnnotationProcessor) Processor(org.apache.nifi.processor.Processor) ModifiesClasspathProcessor(org.apache.nifi.test.processors.ModifiesClasspathProcessor) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) Bundle(org.apache.nifi.bundle.Bundle) SystemBundle(org.apache.nifi.nar.SystemBundle) StandardProcessorInitializationContext(org.apache.nifi.processor.StandardProcessorInitializationContext) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) StandardProcessorInitializationContext(org.apache.nifi.processor.StandardProcessorInitializationContext)

Example 30 with Processor

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

the class TestStandardProcessorNode method testStart.

@Test(timeout = 10000)
public void testStart() throws InterruptedException {
    final ProcessorThatThrowsExceptionOnScheduled processor = new ProcessorThatThrowsExceptionOnScheduled();
    final String uuid = UUID.randomUUID().toString();
    ProcessorInitializationContext initContext = new StandardProcessorInitializationContext(uuid, null, null, null, null);
    processor.initialize(initContext);
    final ReloadComponent reloadComponent = Mockito.mock(ReloadComponent.class);
    final BundleCoordinate coordinate = Mockito.mock(BundleCoordinate.class);
    final LoggableComponent<Processor> loggableComponent = new LoggableComponent<>(processor, coordinate, null);
    final StandardProcessorNode procNode = new StandardProcessorNode(loggableComponent, uuid, createValidationContextFactory(), null, null, NiFiProperties.createBasicNiFiProperties(null, null), new StandardComponentVariableRegistry(VariableRegistry.EMPTY_REGISTRY), reloadComponent);
    final ScheduledExecutorService taskScheduler = new FlowEngine(1, "TestClasspathResources", true);
    final StandardProcessContext processContext = new StandardProcessContext(procNode, null, null, null, () -> false);
    final SchedulingAgentCallback schedulingAgentCallback = new SchedulingAgentCallback() {

        @Override
        public void onTaskComplete() {
        }

        @Override
        public Future<?> scheduleTask(final Callable<?> task) {
            return taskScheduler.submit(task);
        }

        @Override
        public void trigger() {
            Assert.fail("Should not have completed");
        }
    };
    procNode.start(taskScheduler, 20000L, processContext, schedulingAgentCallback, true);
    Thread.sleep(1000L);
    assertEquals(1, processor.onScheduledCount);
    assertEquals(1, processor.onUnscheduledCount);
    assertEquals(1, processor.onStoppedCount);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ModifiesClasspathNoAnnotationProcessor(org.apache.nifi.test.processors.ModifiesClasspathNoAnnotationProcessor) Processor(org.apache.nifi.processor.Processor) ModifiesClasspathProcessor(org.apache.nifi.test.processors.ModifiesClasspathProcessor) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) FlowEngine(org.apache.nifi.engine.FlowEngine) StandardProcessorInitializationContext(org.apache.nifi.processor.StandardProcessorInitializationContext) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate) ProcessorInitializationContext(org.apache.nifi.processor.ProcessorInitializationContext) StandardProcessorInitializationContext(org.apache.nifi.processor.StandardProcessorInitializationContext) Callable(java.util.concurrent.Callable) StandardProcessContext(org.apache.nifi.processor.StandardProcessContext) 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