@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);
  }
Exemple #2
0
  public NiFi(final NiFiProperties properties)
      throws ClassNotFoundException, IOException, NoSuchMethodException, InstantiationException,
          IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    Thread.setDefaultUncaughtExceptionHandler(
        new UncaughtExceptionHandler() {
          @Override
          public void uncaughtException(final Thread t, final Throwable e) {
            logger.error("An Unknown Error Occurred in Thread {}: {}", t, e.toString());
            logger.error("", e);
          }
        });

    // register the shutdown hook
    Runtime.getRuntime()
        .addShutdownHook(
            new Thread(
                new Runnable() {
                  @Override
                  public void run() {
                    // shutdown the jetty server
                    shutdownHook();
                  }
                }));

    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
    SLF4JBridgeHandler.removeHandlersForRootLogger();
    SLF4JBridgeHandler.install();

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

    // load the extensions classloaders
    NarClassLoaders.getInstance()
        .init(
            properties.getFrameworkWorkingDirectory(), properties.getExtensionsWorkingDirectory());

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

    // discover the extensions
    ExtensionManager.discoverExtensions(NarClassLoaders.getInstance().getExtensionClassLoaders());
    ExtensionManager.logClassLoaderMapping();

    DocGenerator.generate(properties);

    // 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);

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

    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 endTime = System.nanoTime();
      logger.info("Controller initialization took " + (endTime - startTime) + " nanoseconds.");
    }
  }