Java Code Examples for org.apache.nifi.processor.Processor#initialize()

The following examples show how to use org.apache.nifi.processor.Processor#initialize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StandardProcessorTestRunner.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
StandardProcessorTestRunner(final Processor processor) {
    this.processor = processor;
    this.idGenerator = new AtomicLong(0L);
    this.sharedState = new SharedSessionState(processor, idGenerator);
    this.flowFileQueue = sharedState.getFlowFileQueue();
    this.sessionFactory = new MockSessionFactory(sharedState, processor);
    this.processorStateManager = new MockStateManager(processor);
    this.variableRegistry = new MockVariableRegistry();
    this.context = new MockProcessContext(processor, processorStateManager, variableRegistry);

    final MockProcessorInitializationContext mockInitContext = new MockProcessorInitializationContext(processor, context);
    processor.initialize(mockInitContext);
    logger =  mockInitContext.getLogger();

    try {
        ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, processor);
    } catch (final Exception e) {
        Assert.fail("Could not invoke methods annotated with @OnAdded annotation due to: " + e);
    }

    triggerSerially = null != processor.getClass().getAnnotation(TriggerSerially.class);

    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnConfigurationRestored.class, processor);
}
 
Example 2
Source File: TestStandardControllerServiceProvider.java    From nifi with Apache License 2.0 6 votes vote down vote up
private ProcessorNode createProcessor(final StandardProcessScheduler scheduler, final ControllerServiceProvider serviceProvider) {
    final ReloadComponent reloadComponent = Mockito.mock(ReloadComponent.class);

    final Processor processor = new DummyProcessor();
    final MockProcessContext context = new MockProcessContext(processor, Mockito.mock(StateManager.class), variableRegistry);
    final MockProcessorInitializationContext mockInitContext = new MockProcessorInitializationContext(processor, context);
    processor.initialize(mockInitContext);

    final LoggableComponent<Processor> dummyProcessor = new LoggableComponent<>(processor, systemBundle.getBundleDetails().getCoordinate(), null);
    final ProcessorNode procNode = new StandardProcessorNode(dummyProcessor, mockInitContext.getIdentifier(),
            new StandardValidationContextFactory(serviceProvider, null), scheduler, serviceProvider,
            new StandardComponentVariableRegistry(VariableRegistry.EMPTY_REGISTRY), reloadComponent, extensionManager, new SynchronousValidationTrigger());

    final FlowManager flowManager = Mockito.mock(FlowManager.class);
    final FlowController flowController = Mockito.mock(FlowController.class );
    Mockito.when(flowController.getFlowManager()).thenReturn(flowManager);

    final ProcessGroup group = new StandardProcessGroup(UUID.randomUUID().toString(), serviceProvider, scheduler, null, null, flowController,
        new MutableVariableRegistry(variableRegistry));
    group.addProcessor(procNode);
    procNode.setProcessGroup(group);

    return procNode;
}
 
Example 3
Source File: ProcessorInitializer.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 4
Source File: FlowController.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private Processor instantiateProcessor(final String type, final String identifier) throws ProcessorInstantiationException {
    Processor processor;

    final ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        final ClassLoader detectedClassLoaderForType = ExtensionManager.getClassLoader(type, identifier);
        final Class<?> rawClass;
        if (detectedClassLoaderForType == null) {
            // try to find from the current class loader
            rawClass = Class.forName(type);
        } else {
            // try to find from the registered classloader for that type
            rawClass = Class.forName(type, true, ExtensionManager.getClassLoader(type, identifier));
        }

        Thread.currentThread().setContextClassLoader(detectedClassLoaderForType);
        final Class<? extends Processor> processorClass = rawClass.asSubclass(Processor.class);
        processor = processorClass.newInstance();
        final ComponentLog componentLogger = new SimpleProcessLogger(identifier, processor);
        final ProcessorInitializationContext ctx = new StandardProcessorInitializationContext(identifier, componentLogger, this, this, nifiProperties);
        processor.initialize(ctx);

        LogRepositoryFactory.getRepository(identifier).setLogger(componentLogger);
        return processor;
    } catch (final Throwable t) {
        throw new ProcessorInstantiationException(type, t);
    } finally {
        if (ctxClassLoader != null) {
            Thread.currentThread().setContextClassLoader(ctxClassLoader);
        }
    }
}
 
Example 5
Source File: TestStandardProcessScheduler.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testDisableControllerServiceWithProcessorTryingToStartUsingIt() throws InterruptedException {
    final String uuid = UUID.randomUUID().toString();
    final Processor proc = new ServiceReferencingProcessor();
    proc.initialize(new StandardProcessorInitializationContext(uuid, null, null, null, null));

    final StandardControllerServiceProvider serviceProvider =
            new StandardControllerServiceProvider(controller, scheduler, null, Mockito.mock(StateManagerProvider.class), variableRegistry, nifiProperties);
    final ControllerServiceNode service = serviceProvider.createControllerService(NoStartServiceImpl.class.getName(), "service", true);
    rootGroup.addControllerService(service);

    final ProcessorNode procNode = new StandardProcessorNode(proc, uuid,
            new StandardValidationContextFactory(serviceProvider, variableRegistry),
            scheduler, serviceProvider, nifiProperties, VariableRegistry.EMPTY_REGISTRY,
            Mockito.mock(ComponentLog.class));
    rootGroup.addProcessor(procNode);

    Map<String,String> procProps = new HashMap<>();
    procProps.put(ServiceReferencingProcessor.SERVICE_DESC.getName(), service.getIdentifier());
    procNode.setProperties(procProps);

    scheduler.enableControllerService(service);
    scheduler.startProcessor(procNode);

    Thread.sleep(1000L);

    scheduler.stopProcessor(procNode);
    assertTrue(service.isActive());
    assertTrue(service.getState() == ControllerServiceState.ENABLING);
    scheduler.disableControllerService(service);
    assertTrue(service.getState() == ControllerServiceState.DISABLING);
    assertFalse(service.isActive());
    Thread.sleep(2000);
    assertTrue(service.getState() == ControllerServiceState.DISABLED);
}
 
Example 6
Source File: TestStandardProcessorNode.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private StandardProcessorNode createProcessorNode(Processor processor) {
    final String uuid = UUID.randomUUID().toString();
    final ValidationContextFactory validationContextFactory = createValidationContextFactory();
    final NiFiProperties niFiProperties = NiFiProperties.createBasicNiFiProperties(null, null);
    final ProcessScheduler processScheduler = Mockito.mock(ProcessScheduler.class);
    final ComponentLog componentLog = Mockito.mock(ComponentLog.class);

    ProcessorInitializationContext initContext = new StandardProcessorInitializationContext(uuid, componentLog, null, null, null);
    processor.initialize(initContext);

    return new StandardProcessorNode(processor, uuid, validationContextFactory, processScheduler, null,
            niFiProperties, variableRegistry, componentLog);
}
 
Example 7
Source File: ProcessorInitializer.java    From nifi-minifi with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 8
Source File: StandardProcessorTestRunner.java    From nifi with Apache License 2.0 5 votes vote down vote up
StandardProcessorTestRunner(final Processor processor, String processorName, MockComponentLog logger, KerberosContext kerberosContext) {
    this.processor = processor;
    this.idGenerator = new AtomicLong(0L);
    this.sharedState = new SharedSessionState(processor, idGenerator);
    this.flowFileQueue = sharedState.getFlowFileQueue();
    this.sessionFactory = new MockSessionFactory(sharedState, processor, enforceReadStreamsClosed);
    this.processorStateManager = new MockStateManager(processor);
    this.variableRegistry = new MockVariableRegistry();

    // Ensure the test runner has the environment and build variables
    ENVIRONMENT_SYSTEM_REGISTRY.getVariableMap().forEach(this.variableRegistry::setVariable);

    this.context = new MockProcessContext(processor, processorName, processorStateManager, variableRegistry);
    this.kerberosContext = kerberosContext;

    final MockProcessorInitializationContext mockInitContext = new MockProcessorInitializationContext(processor, context, logger, kerberosContext);
    processor.initialize(mockInitContext);
    this.logger =  mockInitContext.getLogger();

    try {
        ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, processor);
    } catch (final Exception e) {
        Assert.fail("Could not invoke methods annotated with @OnAdded annotation due to: " + e);
    }

    triggerSerially = null != processor.getClass().getAnnotation(TriggerSerially.class);

    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnConfigurationRestored.class, processor);
}
 
Example 9
Source File: StatelessProcessorWrapper.java    From nifi with Apache License 2.0 5 votes vote down vote up
public StatelessProcessorWrapper(final String id, final Processor processor, final StatelessProcessorWrapper parent, final StatelessControllerServiceLookup lookup, final VariableRegistry registry,
                          final boolean materializeContent, final ClassLoader classLoader, final ParameterContext parameterContext) throws InvocationTargetException, IllegalAccessException {

    this.processor = processor;
    this.classLoader = classLoader;

    addParent(parent);

    this.lookup = lookup;
    this.materializeContent = materializeContent;

    this.provenanceEvents = new ArrayList<>();
    this.createdSessions = new CopyOnWriteArraySet<>();
    this.inputQueue = new LinkedList<>();
    this.variableRegistry = registry;
    this.context = new StatelessProcessContext(processor, lookup, processor.getIdentifier(), new StatelessStateManager(), variableRegistry, parameterContext);
    this.context.setMaxConcurrentTasks(1);

    final StatelessProcessorInitializationContext initContext = new StatelessProcessorInitializationContext(id, processor, context);
    logger = initContext.getLogger();

    try (final CloseableNarLoader c = withNarClassLoader()) {
        processor.initialize(initContext);
        ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, processor);
        ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnConfigurationRestored.class, processor);
    }
}
 
Example 10
Source File: TestStandardProcessScheduler.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testDisableControllerServiceWithProcessorTryingToStartUsingIt() throws InterruptedException, ExecutionException {
    final String uuid = UUID.randomUUID().toString();
    final Processor proc = new ServiceReferencingProcessor();
    proc.initialize(new StandardProcessorInitializationContext(uuid, null, null, null, KerberosConfig.NOT_CONFIGURED));

    final ReloadComponent reloadComponent = Mockito.mock(ReloadComponent.class);

    final ControllerServiceNode service = flowManager.createControllerService(NoStartServiceImpl.class.getName(), "service",
            systemBundle.getBundleDetails().getCoordinate(), null, true, true);

    rootGroup.addControllerService(service);

    final LoggableComponent<Processor> loggableComponent = new LoggableComponent<>(proc, systemBundle.getBundleDetails().getCoordinate(), null);
    final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(serviceProvider, variableRegistry);
    final ProcessorNode procNode = new StandardProcessorNode(loggableComponent, uuid, validationContextFactory, scheduler,
        serviceProvider, new StandardComponentVariableRegistry(VariableRegistry.EMPTY_REGISTRY), reloadComponent, extensionManager, new SynchronousValidationTrigger());

    rootGroup.addProcessor(procNode);

    Map<String, String> procProps = new HashMap<>();
    procProps.put(ServiceReferencingProcessor.SERVICE_DESC.getName(), service.getIdentifier());
    procNode.setProperties(procProps);

    service.performValidation();
    scheduler.enableControllerService(service);

    procNode.performValidation();
    scheduler.startProcessor(procNode, true);

    Thread.sleep(25L);

    scheduler.stopProcessor(procNode);
    assertTrue(service.isActive());
    assertSame(service.getState(), ControllerServiceState.ENABLING);
    scheduler.disableControllerService(service).get();
    assertFalse(service.isActive());
    assertSame(service.getState(), ControllerServiceState.DISABLED);
}
 
Example 11
Source File: TestStandardProcessorNode.java    From nifi with Apache License 2.0 5 votes vote down vote up
private StandardProcessorNode createProcessorNode(final Processor processor, final ReloadComponent reloadComponent) {
    final String uuid = UUID.randomUUID().toString();
    final ValidationContextFactory validationContextFactory = createValidationContextFactory();
    final ProcessScheduler processScheduler = mock(ProcessScheduler.class);
    final TerminationAwareLogger componentLog = mock(TerminationAwareLogger.class);

    extensionManager.createInstanceClassLoader(processor.getClass().getName(), uuid, systemBundle, null);

    ProcessorInitializationContext initContext = new StandardProcessorInitializationContext(uuid, componentLog, null, null, KerberosConfig.NOT_CONFIGURED);
    processor.initialize(initContext);

    final LoggableComponent<Processor> loggableComponent = new LoggableComponent<>(processor, systemBundle.getBundleDetails().getCoordinate(), componentLog);
    return new StandardProcessorNode(loggableComponent, uuid, validationContextFactory, processScheduler,
        null, new StandardComponentVariableRegistry(variableRegistry), reloadComponent, extensionManager, new SynchronousValidationTrigger());
}
 
Example 12
Source File: ProcessorInitializer.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableComponent component) {
    Processor processor = (Processor) component;
    ProcessorInitializationContext initializationContext = new MockProcessorInitializationContext();
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(extensionManager, component.getClass(), initializationContext.getIdentifier())) {
        processor.initialize(initializationContext);
    }
}
 
Example 13
Source File: AbstractDocumentationWriter.java    From nifi with Apache License 2.0 4 votes vote down vote up
protected void initialize(final Processor processor) {
    processor.initialize(new DocumentationProcessorInitializationContext());
}
 
Example 14
Source File: ComponentFactory.java    From nifi with Apache License 2.0 4 votes vote down vote up
public StatelessProcessorWrapper createProcessor(final VersionedProcessor versionedProcessor, final boolean materializeContent, final StatelessControllerServiceLookup controllerServiceLookup,
                                                 final VariableRegistry variableRegistry, final Set<URL> classpathUrls, final ParameterContext parameterContext)
    throws ProcessorInstantiationException {

    final String type = versionedProcessor.getType();
    final String identifier = versionedProcessor.getIdentifier();

    final Bundle bundle = getAvailableBundle(versionedProcessor.getBundle(), type);
    if (bundle == null) {
        throw new IllegalStateException("Unable to find bundle for coordinate "
            + versionedProcessor.getBundle().getGroup() + ":"
            + versionedProcessor.getBundle().getArtifact() + ":"
            + versionedProcessor.getBundle().getVersion());
    }

    final ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        final ClassLoader detectedClassLoader = extensionManager.createInstanceClassLoader(type, identifier, bundle,
            classpathUrls == null ? Collections.emptySet() : classpathUrls);

        logger.debug("Setting context class loader to {} (parent = {}) to create {}", detectedClassLoader, detectedClassLoader.getParent(), type);
        final Class<?> rawClass = Class.forName(type, true, detectedClassLoader);
        Thread.currentThread().setContextClassLoader(detectedClassLoader);

        final Object extensionInstance = rawClass.newInstance();
        final ComponentLog componentLog = new SLF4JComponentLog(extensionInstance);

        final Processor processor = (Processor) extensionInstance;
        final ProcessorInitializationContext initializationContext = new StatelessProcessorInitializationContext(versionedProcessor.getIdentifier(), processor, controllerServiceLookup);
        processor.initialize(initializationContext);

        // If no classpath urls were provided, check if we need to add additional classpath URL's based on configured properties.
        if (classpathUrls == null) {
            final Set<URL> additionalClasspathUrls = getAdditionalClasspathResources(processor.getPropertyDescriptors(), processor.getIdentifier(), versionedProcessor.getProperties(),
                parameterContext, variableRegistry,componentLog);

            if (!additionalClasspathUrls.isEmpty()) {
                return createProcessor(versionedProcessor, materializeContent, controllerServiceLookup, variableRegistry, additionalClasspathUrls, parameterContext);
            }
        }

        final StatelessProcessorWrapper processorWrapper = new StatelessProcessorWrapper(versionedProcessor.getIdentifier(), processor, null,
            controllerServiceLookup, variableRegistry, materializeContent, detectedClassLoader, parameterContext);

        // Configure the Processor
        processorWrapper.setAnnotationData(versionedProcessor.getAnnotationData());
        versionedProcessor.getProperties().forEach(processorWrapper::setProperty);
        for (String relationship : versionedProcessor.getAutoTerminatedRelationships()) {
            processorWrapper.addAutoTermination(new Relationship.Builder().name(relationship).build());
        }

        return processorWrapper;
    } catch (final Exception e) {
        throw new ProcessorInstantiationException(type, e);
    } finally {
        if (ctxClassLoader != null) {
            Thread.currentThread().setContextClassLoader(ctxClassLoader);
        }
    }
}