Search in sources :

Example 16 with NarCloseable

use of org.apache.nifi.nar.NarCloseable in project nifi-minifi by apache.

the class ReportingTaskingInitializer method initialize.

@Override
public void initialize(ConfigurableComponent component) throws InitializationException {
    ReportingTask reportingTask = (ReportingTask) component;
    ReportingInitializationContext context = new MockReportingInitializationContext();
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), context.getIdentifier())) {
        reportingTask.initialize(context);
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) ReportingInitializationContext(org.apache.nifi.reporting.ReportingInitializationContext) MockReportingInitializationContext(org.apache.nifi.mock.MockReportingInitializationContext) MockReportingInitializationContext(org.apache.nifi.mock.MockReportingInitializationContext) ReportingTask(org.apache.nifi.reporting.ReportingTask)

Example 17 with NarCloseable

use of org.apache.nifi.nar.NarCloseable in project nifi-minifi by apache.

the class ReportingTaskingInitializer method teardown.

@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.removeInstanceClassLoader(component.getIdentifier());
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) MockConfigurationContext(org.apache.nifi.mock.MockConfigurationContext) MockComponentLogger(org.apache.nifi.mock.MockComponentLogger) ReportingTask(org.apache.nifi.reporting.ReportingTask)

Example 18 with NarCloseable

use of org.apache.nifi.nar.NarCloseable in project nifi-minifi by apache.

the class ControllerServiceInitializer method teardown.

@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.removeInstanceClassLoader(component.getIdentifier());
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) MockConfigurationContext(org.apache.nifi.mock.MockConfigurationContext) MockComponentLogger(org.apache.nifi.mock.MockComponentLogger) ControllerService(org.apache.nifi.controller.ControllerService) ComponentLog(org.apache.nifi.logging.ComponentLog)

Example 19 with NarCloseable

use of org.apache.nifi.nar.NarCloseable in project nifi by apache.

the class TestStandardProcessorNode method testMultiplePropertiesDynamicallyModifyClasspathWithExpressionLanguage.

@Test
public void testMultiplePropertiesDynamicallyModifyClasspathWithExpressionLanguage() throws MalformedURLException {
    final MockReloadComponent reloadComponent = new MockReloadComponent();
    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, reloadComponent);
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(procNode.getProcessor().getClass(), procNode.getIdentifier())) {
        // Should not have any of the test resources loaded at this point
        final URL[] testResources = getTestResources();
        for (URL testResource : testResources) {
            if (containsResource(reloadComponent.getAdditionalUrls(), 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(reloadComponent.getAdditionalUrls(), testResources[0]));
        assertTrue(containsResource(reloadComponent.getAdditionalUrls(), testResources[2]));
        assertFalse(containsResource(reloadComponent.getAdditionalUrls(), testResources[1]));
        assertEquals(ModifiesClasspathProcessor.class.getCanonicalName(), reloadComponent.getNewType());
        // Should pass validation
        assertTrue(procNode.isValid());
    } finally {
        ExtensionManager.removeInstanceClassLoader(procNode.getIdentifier());
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ModifiesClasspathProcessor(org.apache.nifi.test.processors.ModifiesClasspathProcessor) VariableDescriptor(org.apache.nifi.registry.VariableDescriptor) URL(java.net.URL) Test(org.junit.Test)

Example 20 with NarCloseable

use of org.apache.nifi.nar.NarCloseable in project nifi by apache.

the class TestStandardProcessorNode method testSinglePropertyDynamicallyModifiesClasspath.

@Test
public void testSinglePropertyDynamicallyModifiesClasspath() throws MalformedURLException {
    final MockReloadComponent reloadComponent = new MockReloadComponent();
    final PropertyDescriptor classpathProp = new PropertyDescriptor.Builder().name("Classpath Resources").dynamicallyModifiesClasspath(true).addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();
    final ModifiesClasspathProcessor processor = new ModifiesClasspathProcessor(Arrays.asList(classpathProp));
    final StandardProcessorNode procNode = createProcessorNode(processor, reloadComponent);
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(procNode.getProcessor().getClass(), procNode.getIdentifier())) {
        // Should not have any of the test resources loaded at this point
        final URL[] testResources = getTestResources();
        for (URL testResource : testResources) {
            if (containsResource(reloadComponent.getAdditionalUrls(), testResource)) {
                fail("found resource that should not have been loaded");
            }
        }
        // Simulate setting the properties of the processor to point to the test resources directory
        final Map<String, String> properties = new HashMap<>();
        properties.put(classpathProp.getName(), "src/test/resources/TestClasspathResources");
        procNode.setProperties(properties);
        // Should have all of the resources loaded into the InstanceClassLoader now
        for (URL testResource : testResources) {
            assertTrue(containsResource(reloadComponent.getAdditionalUrls(), testResource));
        }
        assertEquals(ModifiesClasspathProcessor.class.getCanonicalName(), reloadComponent.getNewType());
        // Should pass validation
        assertTrue(procNode.isValid());
    } finally {
        ExtensionManager.removeInstanceClassLoader(procNode.getIdentifier());
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ModifiesClasspathProcessor(org.apache.nifi.test.processors.ModifiesClasspathProcessor) URL(java.net.URL) Test(org.junit.Test)

Aggregations

NarCloseable (org.apache.nifi.nar.NarCloseable)48 ComponentLog (org.apache.nifi.logging.ComponentLog)13 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)12 Processor (org.apache.nifi.processor.Processor)12 HashMap (java.util.HashMap)9 ControllerServiceNode (org.apache.nifi.controller.service.ControllerServiceNode)9 SimpleProcessLogger (org.apache.nifi.processor.SimpleProcessLogger)9 URL (java.net.URL)8 LinkedHashMap (java.util.LinkedHashMap)8 ProcessorInstantiationException (org.apache.nifi.controller.exception.ProcessorInstantiationException)8 ReportingTask (org.apache.nifi.reporting.ReportingTask)8 Map (java.util.Map)7 ControllerService (org.apache.nifi.controller.ControllerService)6 IOException (java.io.IOException)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 ArrayList (java.util.ArrayList)5 ComponentLifeCycleException (org.apache.nifi.controller.exception.ComponentLifeCycleException)5 ControllerServiceInstantiationException (org.apache.nifi.controller.exception.ControllerServiceInstantiationException)5 Relationship (org.apache.nifi.processor.Relationship)5 ComponentVariableRegistry (org.apache.nifi.registry.ComponentVariableRegistry)5