Java Code Examples for org.apache.nifi.nar.ExtensionManager#removeInstanceClassLoaderIfExists()

The following examples show how to use org.apache.nifi.nar.ExtensionManager#removeInstanceClassLoaderIfExists() . 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: 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 2
Source File: ProcessorInitializer.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@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.removeInstanceClassLoaderIfExists(component.getIdentifier());
    }
}
 
Example 3
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 4
Source File: StandardControllerServiceProvider.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void removeControllerService(final ControllerServiceNode serviceNode) {
    final ProcessGroup group = requireNonNull(serviceNode).getProcessGroup();
    if (group == null) {
        flowController.removeRootControllerService(serviceNode);
        return;
    }

    group.removeControllerService(serviceNode);
    ExtensionManager.removeInstanceClassLoaderIfExists(serviceNode.getIdentifier());
}
 
Example 5
Source File: FlowController.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void removeReportingTask(final ReportingTaskNode reportingTaskNode) {
    final ReportingTaskNode existing = reportingTasks.get(reportingTaskNode.getIdentifier());
    if (existing == null || existing != reportingTaskNode) {
        throw new IllegalStateException("Reporting Task " + reportingTaskNode + " does not exist in this Flow");
    }

    reportingTaskNode.verifyCanDelete();

    try (final NarCloseable x = NarCloseable.withComponentNarLoader(reportingTaskNode.getReportingTask().getClass(), reportingTaskNode.getReportingTask().getIdentifier())) {
        ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnRemoved.class, reportingTaskNode.getReportingTask(), reportingTaskNode.getConfigurationContext());
    }

    for (final Map.Entry<PropertyDescriptor, String> entry : reportingTaskNode.getProperties().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 = controllerServiceProvider.getControllerServiceNode(value);
                if (serviceNode != null) {
                    serviceNode.removeReference(reportingTaskNode);
                }
            }
        }
    }

    reportingTasks.remove(reportingTaskNode.getIdentifier());
    ExtensionManager.removeInstanceClassLoaderIfExists(reportingTaskNode.getIdentifier());
}
 
Example 6
Source File: FlowController.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
public void removeRootControllerService(final ControllerServiceNode service) {
    final ControllerServiceNode existing = rootControllerServices.get(requireNonNull(service).getIdentifier());
    if (existing == null) {
        throw new IllegalStateException(service + " is not a member of this Process Group");
    }

    service.verifyCanDelete();

    try (final NarCloseable x = NarCloseable.withComponentNarLoader(service.getControllerServiceImplementation().getClass(), service.getIdentifier())) {
        final ConfigurationContext configurationContext = new StandardConfigurationContext(service, controllerServiceProvider, null, variableRegistry);
        ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnRemoved.class, service.getControllerServiceImplementation(), configurationContext);
    }

    for (final Map.Entry<PropertyDescriptor, String> entry : service.getProperties().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 referencedNode = getRootControllerService(value);
                if (referencedNode != null) {
                    referencedNode.removeReference(service);
                }
            }
        }
    }

    rootControllerServices.remove(service.getIdentifier());
    getStateManagerProvider().onComponentRemoved(service.getIdentifier());

    ExtensionManager.removeInstanceClassLoaderIfExists(service.getIdentifier());

    LOG.info("{} removed from Flow Controller", service, this);
}
 
Example 7
Source File: TestStandardProcessorNode.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testPropertyModifiesClasspathWhenProcessorMissingAnnotation() throws MalformedURLException {
    final ModifiesClasspathNoAnnotationProcessor processor = new ModifiesClasspathNoAnnotationProcessor();
    final StandardProcessorNode procNode = createProcessorNode(processor);

    final Set<ClassLoader> classLoaders = new HashSet<>();
    classLoaders.add(procNode.getProcessor().getClass().getClassLoader());

    // Load all of the extensions in src/test/java of this project
    ExtensionManager.discoverExtensions(classLoaders);

    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(procNode.getProcessor().getClass(), procNode.getIdentifier())){
        // Can't validate the ClassLoader here b/c the class is missing the annotation

        // Simulate setting the properties pointing to two of the resources
        final Map<String, String> properties = new HashMap<>();
        properties.put(ModifiesClasspathNoAnnotationProcessor.CLASSPATH_RESOURCE.getName(),
                "src/test/resources/TestClasspathResources/resource1.txt");
        procNode.setProperties(properties);

        // Should not have loaded any of the resources
        final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        assertTrue(classLoader instanceof URLClassLoader);

        final URL[] testResources = getTestResources();
        final URLClassLoader urlClassLoader = (URLClassLoader) classLoader;
        assertFalse(containsResource(urlClassLoader.getURLs(), testResources[0]));
        assertFalse(containsResource(urlClassLoader.getURLs(), testResources[1]));
        assertFalse(containsResource(urlClassLoader.getURLs(), testResources[2]));

        // Should pass validation
        assertTrue(procNode.isValid());

    } finally {
        ExtensionManager.removeInstanceClassLoaderIfExists(procNode.getIdentifier());
    }
}
 
Example 8
Source File: StandardProcessGroup.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void removeControllerService(final ControllerServiceNode service) {
    boolean removed = false;
    writeLock.lock();
    try {
        final ControllerServiceNode existing = controllerServices.get(requireNonNull(service).getIdentifier());
        if (existing == null) {
            throw new IllegalStateException("ControllerService " + service.getIdentifier() + " is not a member of this Process Group");
        }

        service.verifyCanDelete();

        try (final NarCloseable x = NarCloseable.withComponentNarLoader(service.getControllerServiceImplementation().getClass(), service.getIdentifier())) {
            final ConfigurationContext configurationContext = new StandardConfigurationContext(service, controllerServiceProvider, null, variableRegistry);
            ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnRemoved.class, service.getControllerServiceImplementation(), configurationContext);
        }

        for (final Map.Entry<PropertyDescriptor, String> entry : service.getProperties().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 referencedNode = getControllerService(value);
                    if (referencedNode != null) {
                        referencedNode.removeReference(service);
                    }
                }
            }
        }

        controllerServices.remove(service.getIdentifier());
        flowController.getStateManagerProvider().onComponentRemoved(service.getIdentifier());

        removed = true;
        LOG.info("{} removed from {}", service, this);

    } finally {
        if (removed) {
            try {
                ExtensionManager.removeInstanceClassLoaderIfExists(service.getIdentifier());
            } catch (Throwable t) {
            }
        }
        writeLock.unlock();
    }
}
 
Example 9
Source File: FingerprintFactory.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
private StringBuilder addFlowFileProcessorFingerprint(final StringBuilder builder, final Element processorElem, final FlowController controller) throws FingerprintException {
    // id
    appendFirstValue(builder, DomUtils.getChildNodesByTagName(processorElem, "id"));
    // class
    final NodeList childNodes = DomUtils.getChildNodesByTagName(processorElem, "class");
    final String className = childNodes.item(0).getTextContent();
    appendFirstValue(builder, childNodes);
    // annotation data
    appendFirstValue(builder, DomUtils.getChildNodesByTagName(processorElem, "annotationData"));

    // create an instance of the Processor so that we know the default property values
    Processor processor = null;
    try {
        if (controller != null) {
            processor = controller.createProcessor(className, UUID.randomUUID().toString(), false).getProcessor();
        }
    } catch (ProcessorInstantiationException e) {
        logger.warn("Unable to create Processor of type {} due to {}; its default properties will be fingerprinted instead of being ignored.", className, e.toString());
        if (logger.isDebugEnabled()) {
            logger.warn("", e);
        }
    } finally {
        // The processor instance is only for fingerprinting so we can remove the InstanceClassLoader here
        // since otherwise it will stick around in the map forever
        if (processor != null) {
            ExtensionManager.removeInstanceClassLoaderIfExists(processor.getIdentifier());
        }
    }

    // properties
    final NodeList propertyElems = DomUtils.getChildNodesByTagName(processorElem, "property");
    final List<Element> sortedPropertyElems = sortElements(propertyElems, getProcessorPropertiesComparator());
    for (final Element propertyElem : sortedPropertyElems) {
        final String propName = DomUtils.getChildElementsByTagName(propertyElem, "name").get(0).getTextContent();
        String propValue = getFirstValue(DomUtils.getChildNodesByTagName(propertyElem, "value"), null);
        addPropertyFingerprint(builder, processor, propName, propValue);
    }

    final NodeList autoTerminateElems = DomUtils.getChildNodesByTagName(processorElem, "autoTerminatedRelationship");
    final List<Element> sortedAutoTerminateElems = sortElements(autoTerminateElems, getElementTextComparator());
    for (final Element autoTerminateElem : sortedAutoTerminateElems) {
        builder.append(autoTerminateElem.getTextContent());
    }

    return builder;
}
 
Example 10
Source File: TestStandardProcessorNode.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultiplePropertiesDynamicallyModifyClasspathWithExpressionLanguage() throws MalformedURLException {
    final PropertyDescriptor classpathProp1 = new PropertyDescriptor.Builder().name("Classpath Resource 1")
            .dynamicallyModifiesClasspath(true).addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();
    final PropertyDescriptor classpathProp2 = new PropertyDescriptor.Builder().name("Classpath Resource 2")
            .dynamicallyModifiesClasspath(true).addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();

    final ModifiesClasspathProcessor processor = new ModifiesClasspathProcessor(Arrays.asList(classpathProp1, classpathProp2));
    final StandardProcessorNode procNode = createProcessorNode(processor);

    final Set<ClassLoader> classLoaders = new HashSet<>();
    classLoaders.add(procNode.getProcessor().getClass().getClassLoader());

    // Load all of the extensions in src/test/java of this project
    ExtensionManager.discoverExtensions(classLoaders);

    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(procNode.getProcessor().getClass(), procNode.getIdentifier())){
        // Should have an InstanceClassLoader here
        final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        assertTrue(contextClassLoader instanceof InstanceClassLoader);

        final InstanceClassLoader instanceClassLoader = (InstanceClassLoader) contextClassLoader;

        // Should not have any of the test resources loaded at this point
        final URL[] testResources = getTestResources();
        for (URL testResource : testResources) {
            if (containsResource(instanceClassLoader.getInstanceResources(), testResource)) {
                fail("found resource that should not have been loaded");
            }
        }

        // Simulate setting the properties pointing to two of the resources
        final Map<String, String> properties = new HashMap<>();
        properties.put(classpathProp1.getName(), "src/test/resources/TestClasspathResources/resource1.txt");
        properties.put(classpathProp2.getName(), "src/test/resources/TestClasspathResources/${myResource}");

        variableRegistry.setVariable(new VariableDescriptor("myResource"), "resource3.txt");

        procNode.setProperties(properties);

        // Should have resources 1 and 3 loaded into the InstanceClassLoader now
        assertTrue(containsResource(instanceClassLoader.getInstanceResources(), testResources[0]));
        assertTrue(containsResource(instanceClassLoader.getInstanceResources(), testResources[2]));
        assertFalse(containsResource(instanceClassLoader.getInstanceResources(), testResources[1]));

        // Should pass validation
        assertTrue(procNode.isValid());
    } finally {
        ExtensionManager.removeInstanceClassLoaderIfExists(procNode.getIdentifier());
    }
}
 
Example 11
Source File: TestStandardProcessorNode.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testSomeNonExistentPropertiesDynamicallyModifyClasspath() throws MalformedURLException {
    final PropertyDescriptor classpathProp1 = new PropertyDescriptor.Builder().name("Classpath Resource 1")
            .dynamicallyModifiesClasspath(true).addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();
    final PropertyDescriptor classpathProp2 = new PropertyDescriptor.Builder().name("Classpath Resource 2")
            .dynamicallyModifiesClasspath(true).addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();

    final ModifiesClasspathProcessor processor = new ModifiesClasspathProcessor(Arrays.asList(classpathProp1, classpathProp2));
    final StandardProcessorNode procNode = createProcessorNode(processor);

    final Set<ClassLoader> classLoaders = new HashSet<>();
    classLoaders.add(procNode.getProcessor().getClass().getClassLoader());

    // Load all of the extensions in src/test/java of this project
    ExtensionManager.discoverExtensions(classLoaders);

    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(procNode.getProcessor().getClass(), procNode.getIdentifier())){
        // Should have an InstanceClassLoader here
        final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        assertTrue(contextClassLoader instanceof InstanceClassLoader);

        final InstanceClassLoader instanceClassLoader = (InstanceClassLoader) contextClassLoader;

        // Should not have any of the test resources loaded at this point
        final URL[] testResources = getTestResources();
        for (URL testResource : testResources) {
            if (containsResource(instanceClassLoader.getInstanceResources(), testResource)) {
                fail("found resource that should not have been loaded");
            }
        }

        // Simulate setting the properties pointing to two of the resources
        final Map<String, String> properties = new HashMap<>();
        properties.put(classpathProp1.getName(), "src/test/resources/TestClasspathResources/resource1.txt");
        properties.put(classpathProp2.getName(), "src/test/resources/TestClasspathResources/DoesNotExist.txt");
        procNode.setProperties(properties);

        // Should have resources 1 and 3 loaded into the InstanceClassLoader now
        assertTrue(containsResource(instanceClassLoader.getInstanceResources(), testResources[0]));
        assertFalse(containsResource(instanceClassLoader.getInstanceResources(), testResources[1]));
        assertFalse(containsResource(instanceClassLoader.getInstanceResources(), testResources[2]));

        // Should pass validation
        assertTrue(procNode.isValid());
    } finally {
        ExtensionManager.removeInstanceClassLoaderIfExists(procNode.getIdentifier());
    }
}