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

The following examples show how to use org.apache.nifi.nar.ExtensionManager#discoverExtensions() . 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: DocGeneratorTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessorLoadsNarResources() throws IOException, ClassNotFoundException {
    TemporaryFolder temporaryFolder = new TemporaryFolder();
    temporaryFolder.create();

    NiFiProperties properties = loadSpecifiedProperties("/conf/nifi.properties",
            NiFiProperties.COMPONENT_DOCS_DIRECTORY,
            temporaryFolder.getRoot().getAbsolutePath());

    NarUnpacker.unpackNars(properties);

    NarClassLoaders.getInstance().init(properties.getFrameworkWorkingDirectory(), properties.getExtensionsWorkingDirectory());

    ExtensionManager.discoverExtensions(NarClassLoaders.getInstance().getExtensionClassLoaders());

    DocGenerator.generate(properties);

    File processorDirectory = new File(temporaryFolder.getRoot(), "org.apache.nifi.processors.WriteResourceToStream");
    File indexHtml = new File(processorDirectory, "index.html");
    Assert.assertTrue(indexHtml + " should have been generated", indexHtml.exists());
    String generatedHtml = FileUtils.readFileToString(indexHtml);
    Assert.assertNotNull(generatedHtml);
    Assert.assertTrue(generatedHtml.contains("This example processor loads a resource from the nar and writes it to the FlowFile content"));
    Assert.assertTrue(generatedHtml.contains("files that were successfully processed"));
    Assert.assertTrue(generatedHtml.contains("files that were not successfully processed"));
    Assert.assertTrue(generatedHtml.contains("resources"));
}
 
Example 2
Source File: ITAccessTokenEndpoint.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setup() throws Exception {
    // configure the location of the nifi properties
    File nifiPropertiesFile = new File("src/test/resources/access-control/nifi.properties");
    System.setProperty(NiFiProperties.PROPERTIES_FILE_PATH, nifiPropertiesFile.getAbsolutePath());

    NiFiProperties props = NiFiProperties.createBasicNiFiProperties(null, null);
    flowXmlPath = props.getProperty(NiFiProperties.FLOW_CONFIGURATION_FILE);

    // delete the database directory to avoid issues with re-registration in testRequestAccessUsingToken
    FileUtils.deleteDirectory(props.getDatabaseRepositoryPath().toFile());

    // load extensions
    NarClassLoaders.getInstance().init(props.getFrameworkWorkingDirectory(), props.getExtensionsWorkingDirectory());
    ExtensionManager.discoverExtensions(NarClassLoaders.getInstance().getExtensionClassLoaders());

    // start the server
    SERVER = new NiFiTestServer("src/main/webapp", CONTEXT_PATH, props);
    SERVER.startServer();
    SERVER.loadFlow();

    // get the base url
    BASE_URL = SERVER.getBaseUrl() + CONTEXT_PATH;

    // create the user
    final Client client = WebUtils.createClient(null, createTrustContext(props));
    TOKEN_USER = new NiFiTestUser(client, null);
}
 
Example 3
Source File: AccessControlHelper.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
public AccessControlHelper(final String nifiPropertiesPath) throws Exception {
    // configure the location of the nifi properties
    File nifiPropertiesFile = new File(nifiPropertiesPath);
    System.setProperty(NiFiProperties.PROPERTIES_FILE_PATH, nifiPropertiesFile.getAbsolutePath());
    NiFiProperties props = NiFiProperties.createBasicNiFiProperties(null, null);
    flowXmlPath = props.getProperty(NiFiProperties.FLOW_CONFIGURATION_FILE);

    // load extensions
    NarClassLoaders.getInstance().init(props.getFrameworkWorkingDirectory(), props.getExtensionsWorkingDirectory());
    ExtensionManager.discoverExtensions(NarClassLoaders.getInstance().getExtensionClassLoaders());

    // start the server
    server = new NiFiTestServer("src/main/webapp", CONTEXT_PATH, props);
    server.startServer();
    server.loadFlow();

    // get the base url
    baseUrl = server.getBaseUrl() + CONTEXT_PATH;

    // create the users - user purposefully decoupled from clientId (same user different browsers tabs)
    readUser = new NiFiTestUser(server.getClient(), NiFiTestAuthorizer.READ_USER_DN);
    writeUser = new NiFiTestUser(server.getClient(), NiFiTestAuthorizer.WRITE_USER_DN);
    readWriteUser = new NiFiTestUser(server.getClient(), NiFiTestAuthorizer.READ_WRITE_USER_DN);
    noneUser = new NiFiTestUser(server.getClient(), NiFiTestAuthorizer.NONE_USER_DN);
    privilegedUser = new NiFiTestUser(server.getClient(), NiFiTestAuthorizer.PRIVILEGED_USER_DN);

    // populate the initial data flow
    NiFiWebApiTest.populateFlow(server.getClient(), baseUrl, readWriteUser, READ_WRITE_CLIENT_ID);
}
 
Example 4
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 5
Source File: StandardControllerServiceProviderTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setupSuite() throws Exception {
    System.setProperty(NiFiProperties.PROPERTIES_FILE_PATH, StandardControllerServiceProviderTest.class.getResource("/conf/nifi.properties").getFile());
    nifiProperties = NiFiProperties.createBasicNiFiProperties(null, null);
    NarClassLoaders.getInstance().init(nifiProperties.getFrameworkWorkingDirectory(), nifiProperties.getExtensionsWorkingDirectory());
    ExtensionManager.discoverExtensions(NarClassLoaders.getInstance().getExtensionClassLoaders());
    variableRegistry = new FileBasedVariableRegistry(nifiProperties.getVariableRegistryPropertiesPaths());
}
 
Example 6
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 7
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());
    }
}