org.apache.nifi.nar.NarClassLoaders Java Examples

The following examples show how to use org.apache.nifi.nar.NarClassLoaders. 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: ExtensionDiscovery.java    From nifi with Apache License 2.0 6 votes vote down vote up
public static ExtensionManager discover(final File narWorkingDirectory, final ClassLoader systemClassLoader) throws IOException {
    NarClassLoaders narClassLoaders = NarClassLoadersHolder.getInstance();

    final long discoveryStart = System.nanoTime();
    try {
        narClassLoaders.init(systemClassLoader, null, narWorkingDirectory);
    } catch (final ClassNotFoundException cnfe) {
        throw new IOException("Could not initialize Class Loaders", cnfe);
    }

    final Set<Bundle> narBundles = narClassLoaders.getBundles();

    final StandardExtensionDiscoveringManager extensionManager = new StandardExtensionDiscoveringManager();
    extensionManager.discoverExtensions(narBundles);
    extensionManager.logClassLoaderMapping();

    final long discoveryMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - discoveryStart);
    logger.info("Successfully discovered extensions in {} milliseconds", discoveryMillis);

    return extensionManager;
}
 
Example #2
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 #3
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 #4
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 #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: StandardFlowService.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
    final ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
    final ClassLoader cl = NarClassLoaders.getInstance().getFrameworkClassLoader();
    Thread.currentThread().setContextClassLoader(cl);
    try {
        //Hang onto the SaveHolder here rather than setting it to null because if the save fails we will try again
        final SaveHolder holder = StandardFlowService.this.saveHolder.get();
        if (holder == null) {
            return;
        }

        if (logger.isTraceEnabled()) {
            logger.trace("Save request time {} // Current time {}", holder.saveTime.getTime(), new Date());
        }

        final Calendar now = Calendar.getInstance();
        if (holder.saveTime.before(now)) {
            if (logger.isTraceEnabled()) {
                logger.trace("Waiting for write lock and then will save");
            }
            writeLock.lock();
            try {
                dao.save(controller, holder.shouldArchive);
                // Nulling it out if it is still set to our current SaveHolder.  Otherwise leave it alone because it means
                // another save is already pending.
                final boolean noSavePending = StandardFlowService.this.saveHolder.compareAndSet(holder, null);
                logger.info("Saved flow controller {} // Another save pending = {}", controller, !noSavePending);
            } finally {
                writeLock.unlock();
            }
        }
    } catch (final Throwable t) {
        logger.error("Unable to save flow controller configuration due to: " + t, t);
        if (logger.isDebugEnabled()) {
            logger.error("", t);
        }

        // record the failed save as a bulletin
        final Bulletin saveFailureBulletin = BulletinFactory.createBulletin(EVENT_CATEGORY, LogLevel.ERROR.name(), "Unable to save flow controller configuration.");
        controller.getBulletinRepository().addBulletin(saveFailureBulletin);
    } finally {
        if (currentCl != null) {
            Thread.currentThread().setContextClassLoader(currentCl);
        }
    }
}
 
Example #7
Source File: NiFi.java    From nifi with Apache License 2.0 4 votes vote down vote up
public NiFi(final NiFiProperties properties, ClassLoader rootClassLoader)
        throws ClassNotFoundException, IOException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

    // There can only be one krb5.conf for the overall Java process so set this globally during
    // start up so that processors and our Kerberos authentication code don't have to set this
    final File kerberosConfigFile = properties.getKerberosConfigurationFile();
    if (kerberosConfigFile != null) {
        final String kerberosConfigFilePath = kerberosConfigFile.getAbsolutePath();
        LOGGER.info("Setting java.security.krb5.conf to {}", new Object[]{kerberosConfigFilePath});
        System.setProperty("java.security.krb5.conf", kerberosConfigFilePath);
    }

    setDefaultUncaughtExceptionHandler();

    // register the shutdown hook
    addShutdownHook();

    final String bootstrapPort = System.getProperty(BOOTSTRAP_PORT_PROPERTY);
    if (bootstrapPort != null) {
        try {
            final int port = Integer.parseInt(bootstrapPort);

            if (port < 1 || port > 65535) {
                throw new RuntimeException("Failed to start NiFi because system property '" + BOOTSTRAP_PORT_PROPERTY + "' is not a valid integer in the range 1 - 65535");
            }

            bootstrapListener = new BootstrapListener(this, port);
            bootstrapListener.start();
        } catch (final NumberFormatException nfe) {
            throw new RuntimeException("Failed to start NiFi because system property '" + BOOTSTRAP_PORT_PROPERTY + "' is not a valid integer in the range 1 - 65535");
        }
    } else {
        LOGGER.info("NiFi started without Bootstrap Port information provided; will not listen for requests from Bootstrap");
        bootstrapListener = null;
    }

    // delete the web working dir - if the application does not start successfully
    // the web app directories might be in an invalid state. when this happens
    // jetty will not attempt to re-extract the war into the directory. by removing
    // the working directory, we can be assured that it will attempt to extract the
    // war every time the application starts.
    File webWorkingDir = properties.getWebWorkingDirectory();
    FileUtils.deleteFilesInDirectory(webWorkingDir, null, LOGGER, true, true);
    FileUtils.deleteFile(webWorkingDir, LOGGER, 3);

    detectTimingIssues();

    // redirect JUL log events
    initLogging();

    final Bundle systemBundle = SystemBundle.create(properties, rootClassLoader);

    // expand the nars
    final ExtensionMapping extensionMapping = NarUnpacker.unpackNars(properties, systemBundle);

    // load the extensions classloaders
    NarClassLoaders narClassLoaders = NarClassLoadersHolder.getInstance();

    narClassLoaders.init(rootClassLoader,
            properties.getFrameworkWorkingDirectory(), properties.getExtensionsWorkingDirectory());

    // load the framework classloader
    final ClassLoader frameworkClassLoader = narClassLoaders.getFrameworkBundle().getClassLoader();
    if (frameworkClassLoader == null) {
        throw new IllegalStateException("Unable to find the framework NAR ClassLoader.");
    }

    final Set<Bundle> narBundles = narClassLoaders.getBundles();

    // load the server from the framework classloader
    Thread.currentThread().setContextClassLoader(frameworkClassLoader);
    Class<?> jettyServer = Class.forName("org.apache.nifi.web.server.JettyServer", true, frameworkClassLoader);
    Constructor<?> jettyConstructor = jettyServer.getConstructor(NiFiProperties.class, Set.class);

    final long startTime = System.nanoTime();
    nifiServer = (NiFiServer) jettyConstructor.newInstance(properties, narBundles);
    nifiServer.setExtensionMapping(extensionMapping);
    nifiServer.setBundles(systemBundle, narBundles);

    if (shutdown) {
        LOGGER.info("NiFi has been shutdown via NiFi Bootstrap. Will not start Controller");
    } else {
        nifiServer.start();

        if (bootstrapListener != null) {
            bootstrapListener.sendStartedStatus(true);
        }

        final long duration = System.nanoTime() - startTime;
        LOGGER.info("Controller initialization took " + duration + " nanoseconds "
                + "(" + (int) TimeUnit.SECONDS.convert(duration, TimeUnit.NANOSECONDS) + " seconds).");
    }
}