Java Code Examples for org.apache.nifi.nar.NarCloseable#withComponentNarLoader()

The following examples show how to use org.apache.nifi.nar.NarCloseable#withComponentNarLoader() . 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: SearchableMatcher.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void match(final ProcessorNode component, final SearchQuery query, final List<String> matches) {
    final Processor processor = component.getProcessor();

    if (processor instanceof Searchable) {
        final Searchable searchable = (Searchable) processor;
        final String searchTerm = query.getTerm();
        final SearchContext context = new StandardSearchContext(searchTerm, component, flowController.getControllerServiceProvider(), variableRegistry);

        // search the processor using the appropriate thread context classloader
        try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(flowController.getExtensionManager(), component.getClass(), component.getIdentifier())) {
            searchable.search(context).stream().forEach(searchResult -> matches.add(searchResult.getLabel() + AttributeMatcher.SEPARATOR + searchResult.getMatch()));
        } catch (final Throwable t) {
            LOGGER.error("Error happened during searchable matching: {}", t.getMessage());
            t.printStackTrace();
        }
    }
}
 
Example 2
Source File: AuthorizerInvocationHandler.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(classLoader)) {
        if (getAccessPolicyProviderMethod.equals(method)) {
            final AccessPolicyProvider accessPolicyProvider = (AccessPolicyProvider) method.invoke(authorizer, args);
            if (accessPolicyProvider == null) {
                return accessPolicyProvider;
            } else {
                return AccessPolicyProviderFactory.withNarLoader(accessPolicyProvider, classLoader);
            }
        } else {
            return method.invoke(authorizer, args);
        }
    } catch (final InvocationTargetException e) {
        // If the proxied instance throws an Exception, it'll be wrapped in an InvocationTargetException. We want
        // to instead re-throw what the proxied instance threw, so we pull it out of the InvocationTargetException.
        throw e.getCause();
    }
}
 
Example 3
Source File: ReportingTaskWrapper.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void run() {
    scheduleState.incrementActiveThreadCount();
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(taskNode.getReportingTask().getClass(), taskNode.getIdentifier())) {
        taskNode.getReportingTask().onTrigger(taskNode.getReportingContext());
    } catch (final Throwable t) {
        final ComponentLog componentLog = new SimpleProcessLogger(taskNode.getIdentifier(), taskNode.getReportingTask());
        componentLog.error("Error running task {} due to {}", new Object[]{taskNode.getReportingTask(), t.toString()});
        if (componentLog.isDebugEnabled()) {
            componentLog.error("", t);
        }
    } finally {
        try {
            // if the reporting task is no longer scheduled to run and this is the last thread,
            // invoke the OnStopped methods
            if (!scheduleState.isScheduled() && scheduleState.getActiveThreadCount() == 1 && scheduleState.mustCallOnStoppedMethods()) {
                try (final NarCloseable x = NarCloseable.withComponentNarLoader(taskNode.getReportingTask().getClass(), taskNode.getIdentifier())) {
                    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, taskNode.getReportingTask(), taskNode.getConfigurationContext());
                }
            }
        } finally {
            scheduleState.decrementActiveThreadCount();
        }
    }
}
 
Example 4
Source File: StandardProcessorNode.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) {
    final Processor processor = processorRef.get().getProcessor();

    activateThread();
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(getExtensionManager(), processor.getClass(), processor.getIdentifier())) {
        processor.onTrigger(context, sessionFactory);
    } finally {
        deactivateThread();
    }
}
 
Example 5
Source File: ControllerServiceInitializer.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableComponent component) throws InitializationException {
    ControllerService controllerService = (ControllerService) component;
    ControllerServiceInitializationContext context = new MockControllerServiceInitializationContext();
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(extensionManager, component.getClass(), context.getIdentifier())) {
        controllerService.initialize(context);
    }
}
 
Example 6
Source File: StandardProcessorNode.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    final Processor processor = processorRef.get().getProcessor();
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(getExtensionManager(), processor.getClass(), processor.getIdentifier())) {
        return getProcessor().toString();
    }
}
 
Example 7
Source File: ControllerServiceInitializer.java    From nifi-minifi with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableComponent component) throws InitializationException {
    ControllerService controllerService = (ControllerService) component;
    ControllerServiceInitializationContext context = new MockControllerServiceInitializationContext();
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), context.getIdentifier())) {
        controllerService.initialize(context);
    }
}
 
Example 8
Source File: StandardControllerServiceNode.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void invokeDisable(ConfigurationContext configContext) {
    try (final NarCloseable nc = NarCloseable.withComponentNarLoader(getExtensionManager(), getControllerServiceImplementation().getClass(), getIdentifier())) {
        ReflectionUtils.invokeMethodsWithAnnotation(OnDisabled.class, StandardControllerServiceNode.this.getControllerServiceImplementation(), configContext);
        LOG.debug("Successfully disabled {}", this);
    } catch (Exception e) {
        final Throwable cause = e instanceof InvocationTargetException ? e.getCause() : e;
        final ComponentLog componentLog = new SimpleProcessLogger(getIdentifier(), StandardControllerServiceNode.this);
        componentLog.error("Failed to invoke @OnDisabled method due to {}", cause);
        LOG.error("Failed to invoke @OnDisabled method of {} due to {}", getControllerServiceImplementation(), cause.toString());
    }
}
 
Example 9
Source File: ControllerServiceInitializer.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void teardown(ConfigurableComponent component) {
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), component.getIdentifier())) {
        ControllerService controllerService = (ControllerService) component;

        final ComponentLog logger = new MockComponentLogger();
        final MockConfigurationContext context = new MockConfigurationContext();
        ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnShutdown.class, controllerService, logger, context);
    } finally {
        ExtensionManager.removeInstanceClassLoaderIfExists(component.getIdentifier());
    }
}
 
Example 10
Source File: AbstractComponentNode.java    From nifi with Apache License 2.0 5 votes vote down vote up
private Map<PropertyDescriptor, String> getPropertyValues(final Function<PropertyConfiguration, String> valueFunction) {
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(extensionManager, getComponent().getClass(), getIdentifier())) {
        final List<PropertyDescriptor> supported = getComponent().getPropertyDescriptors();

        final Map<PropertyDescriptor, String> props = new LinkedHashMap<>();
        for (final PropertyDescriptor descriptor : supported) {
            props.put(descriptor, null);
        }

        properties.forEach((descriptor, config) -> props.put(descriptor, valueFunction.apply(config)));
        return props;
    }
}
 
Example 11
Source File: EventDrivenSchedulingAgent.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void trigger(final Connectable worker, final LifecycleState scheduleState, final ConnectableProcessContext processContext, final ActiveProcessSessionFactory sessionFactory) {
    final int newThreadCount = scheduleState.incrementActiveThreadCount(sessionFactory);
    if (newThreadCount > worker.getMaxConcurrentTasks() && worker.getMaxConcurrentTasks() > 0) {
        // its possible that the worker queue could give us a worker node that is eligible to run based
        // on the number of threads but another thread has already incremented the thread count, result in
        // reaching the maximum number of threads. we won't know this until we atomically increment the thread count
        // on the Schedule State, so we check it here. in this case, we cannot trigger the Processor, as doing so would
        // result in using more than the maximum number of defined threads
        scheduleState.decrementActiveThreadCount(sessionFactory);
        return;
    }

    try {
        try (final AutoCloseable ncl = NarCloseable.withComponentNarLoader(extensionManager, worker.getClass(), worker.getIdentifier())) {
            worker.onTrigger(processContext, sessionFactory);
        } catch (final ProcessException pe) {
            logger.error("{} failed to process session due to {}", worker, pe.toString());
        } catch (final Throwable t) {
            logger.error("{} failed to process session due to {}", worker, t.toString());
            logger.error("", t);

            logger.warn("{} Administratively Pausing for {} due to processing failure: {}", worker, getAdministrativeYieldDuration(), t.toString());
            logger.warn("", t);
            try {
                Thread.sleep(FormatUtils.getTimeDuration(adminYieldDuration, TimeUnit.MILLISECONDS));
            } catch (final InterruptedException e) {
            }

        }
    } finally {
        if (!scheduleState.isScheduled() && scheduleState.getActiveThreadCount() == 1 && scheduleState.mustCallOnStoppedMethods()) {
            try (final NarCloseable x = NarCloseable.withComponentNarLoader(extensionManager, worker.getClass(), worker.getIdentifier())) {
                ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, worker, processContext);
            }
        }

        scheduleState.decrementActiveThreadCount(sessionFactory);
    }
}
 
Example 12
Source File: ReportingTaskingInitializer.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void teardown(ConfigurableComponent component) {
    ReportingTask reportingTask = (ReportingTask) component;
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(extensionManager, component.getClass(), component.getIdentifier())) {

        final MockConfigurationContext context = new MockConfigurationContext();
        ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnShutdown.class, reportingTask, new MockComponentLogger(), context);
    } finally {
        extensionManager.removeInstanceClassLoader(component.getIdentifier());
    }
}
 
Example 13
Source File: ReportingTaskingInitializer.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void teardown(ConfigurableComponent component) {
    ReportingTask reportingTask = (ReportingTask) component;
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), component.getIdentifier())) {

        final MockConfigurationContext context = new MockConfigurationContext();
        ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnShutdown.class, reportingTask, new MockComponentLogger(), context);
    } finally {
        ExtensionManager.removeInstanceClassLoaderIfExists(component.getIdentifier());
    }
}
 
Example 14
Source File: StandardFlowManager.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void removeReportingTask(final ReportingTaskNode reportingTaskNode) {
    final ReportingTaskNode existing = allReportingTasks.get(reportingTaskNode.getIdentifier());
    if (existing == null || existing != reportingTaskNode) {
        throw new IllegalStateException("Reporting Task " + reportingTaskNode + " does not exist in this Flow");
    }

    reportingTaskNode.verifyCanDelete();

    final Class<?> taskClass = reportingTaskNode.getReportingTask().getClass();
    try (final NarCloseable x = NarCloseable.withComponentNarLoader(flowController.getExtensionManager(), taskClass, reportingTaskNode.getReportingTask().getIdentifier())) {
        ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnRemoved.class, reportingTaskNode.getReportingTask(), reportingTaskNode.getConfigurationContext());
    }

    for (final Map.Entry<PropertyDescriptor, String> entry : reportingTaskNode.getEffectivePropertyValues().entrySet()) {
        final PropertyDescriptor descriptor = entry.getKey();
        if (descriptor.getControllerServiceDefinition() != null) {
            final String value = entry.getValue() == null ? descriptor.getDefaultValue() : entry.getValue();
            if (value != null) {
                final ControllerServiceNode serviceNode = flowController.getControllerServiceProvider().getControllerServiceNode(value);
                if (serviceNode != null) {
                    serviceNode.removeReference(reportingTaskNode, descriptor);
                }
            }
        }
    }

    allReportingTasks.remove(reportingTaskNode.getIdentifier());
    LogRepositoryFactory.removeRepository(reportingTaskNode.getIdentifier());
    processScheduler.onReportingTaskRemoved(reportingTaskNode);

    flowController.getExtensionManager().removeInstanceClassLoader(reportingTaskNode.getIdentifier());
}
 
Example 15
Source File: ContinuallyRunConnectableTask.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public Boolean call() {
    if (!scheduleState.isScheduled()) {
        return false;
    }

    // Connectable should run if the following conditions are met:
    // 1. It is not yielded.
    // 2. It has incoming connections with FlowFiles queued or doesn't expect incoming connections
    // 3. If it is a funnel, it has an outgoing connection (this is needed because funnels are "always on"; other
    //    connectable components cannot even be started if they need an outbound connection and don't have one)
    // 4. There is a connection for each relationship.
    final boolean triggerWhenEmpty = connectable.isTriggerWhenEmpty();
    boolean flowFilesQueued = true;
    boolean relationshipAvailable = true;
    final boolean shouldRun = (connectable.getYieldExpiration() < System.currentTimeMillis())
            && (triggerWhenEmpty || (flowFilesQueued = Connectables.flowFilesQueued(connectable)))
            && (connectable.getConnectableType() != ConnectableType.FUNNEL || !connectable.getConnections().isEmpty())
        && (connectable.getRelationships().isEmpty() || (relationshipAvailable = Connectables.anyRelationshipAvailable(connectable)));

    if (shouldRun) {
        scheduleState.incrementActiveThreadCount();
        try {
            try (final AutoCloseable ncl = NarCloseable.withComponentNarLoader(connectable.getClass(), connectable.getIdentifier())) {
                connectable.onTrigger(processContext, sessionFactory);
            } catch (final ProcessException pe) {
                logger.error("{} failed to process session due to {}", connectable, pe.toString());
            } catch (final Throwable t) {
                logger.error("{} failed to process session due to {}", connectable, t.toString());
                logger.error("", t);

                logger.warn("{} Administratively Pausing for 10 seconds due to processing failure: {}", connectable, t.toString(), t);
                try {
                    Thread.sleep(10000L);
                } catch (final InterruptedException e) {
                }

            }
        } finally {
            if (!scheduleState.isScheduled() && scheduleState.getActiveThreadCount() == 1 && scheduleState.mustCallOnStoppedMethods()) {
                try (final NarCloseable x = NarCloseable.withComponentNarLoader(connectable.getClass(), connectable.getIdentifier())) {
                    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, connectable, processContext);
                }
            }

            scheduleState.decrementActiveThreadCount();
        }
    } else if (!flowFilesQueued || !relationshipAvailable) {
        // Either there are no FlowFiles queued, or the relationship is not available (i.e., backpressure is applied).
        // We will yield for just a bit.
        return true;
    }

    return false; // do not yield
}
 
Example 16
Source File: StandardProcessorNode.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) {
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(processor.getClass(), processor.getIdentifier())) {
        processor.onTrigger(context, sessionFactory);
    }
}
 
Example 17
Source File: AbstractComponentNode.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public PropertyDescriptor getPropertyDescriptor(final String name) {
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(extensionManager, getComponent().getClass(), getComponent().getIdentifier())) {
        return getComponent().getPropertyDescriptor(name);
    }
}
 
Example 18
Source File: StandardProcessorNode.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Relationship> getRelationships() {
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(getProcessor().getClass(), processor.getIdentifier())) {
        return getProcessor().getRelationships();
    }
}
 
Example 19
Source File: AbstractConfiguredComponent.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onPropertyModified(final PropertyDescriptor descriptor, final String oldValue, final String newValue) {
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), component.getIdentifier())) {
        component.onPropertyModified(descriptor, oldValue, newValue);
    }
}
 
Example 20
Source File: AbstractConfiguredComponent.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), component.getIdentifier())) {
        return component.toString();
    }
}