private void setSocketOptions(final Builder builder) {
   val socketOptions = kikahaConf.config().getConfig("server.undertow.socket-options");
   for (val entry : socketOptions.entrySet()) {
     val option = getConfigOption(entry.getValue(), entry.getKey());
     if (option != null) builder.setSocketOption(option.getOption(), option.getValue());
   }
 }
 private void setWorkerThreads(final Builder builder, final Config config) {
   val workerThreads = config.getInt("worker-threads");
   if (workerThreads > 0) {
     log.info("  > worker-threads: " + workerThreads);
     builder.setWorkerThreads(workerThreads);
   }
 }
 private void setBufferSize(final Builder builder, final Config config) {
   val bufferSize = config.getInt("buffer-size");
   log.info("  > buffer-size: " + bufferSize);
   builder.setBufferSize(bufferSize);
 }
 private void setIOThreads(final Builder builder, final Config config) {
   val ioThreads = config.getInt("io-threads");
   if (ioThreads > 0) builder.setIoThreads(ioThreads);
 }
  private static void startCoreSystem() {
    if (configuration == null) {
      LOGGER.error("No configuration found. exiting..");
      stopServer(false);
      System.exit(-1);
    }

    if (!configuration.isHttpsListener()
        && !configuration.isHttpListener()
        && !configuration.isAjpListener()) {
      LOGGER.error("No listener specified. exiting..");
      stopServer(false);
      System.exit(-1);
    }

    IdentityManager identityManager = null;

    if (configuration.getIdmImpl() == null) {
      LOGGER.warn("***** No identity manager specified. authentication disabled.");
      identityManager = null;

    } else {
      try {
        Object idm =
            Class.forName(configuration.getIdmImpl())
                .getConstructor(Map.class)
                .newInstance(configuration.getIdmArgs());
        identityManager = (IdentityManager) idm;
      } catch (ClassCastException
          | NoSuchMethodException
          | SecurityException
          | ClassNotFoundException
          | IllegalArgumentException
          | InstantiationException
          | IllegalAccessException
          | InvocationTargetException ex) {
        LOGGER.error("Error configuring idm implementation {}", configuration.getIdmImpl(), ex);
        stopServer(false);
        System.exit(-3);
      }
    }

    AccessManager accessManager = null;

    if (configuration.getAmImpl() == null && configuration.getIdmImpl() != null) {
      LOGGER.warn("***** no access manager specified. authenticated users can do anything.");
      accessManager = new FullAccessManager();
    } else if (configuration.getAmImpl() == null && configuration.getIdmImpl() == null) {
      LOGGER.warn("***** No access manager specified. users can do anything.");
      accessManager = new FullAccessManager();

    } else {
      try {
        Object am =
            Class.forName(configuration.getAmImpl())
                .getConstructor(Map.class)
                .newInstance(configuration.getAmArgs());
        accessManager = (AccessManager) am;
      } catch (ClassCastException
          | NoSuchMethodException
          | SecurityException
          | ClassNotFoundException
          | IllegalArgumentException
          | InstantiationException
          | IllegalAccessException
          | InvocationTargetException ex) {
        LOGGER.error(
            "Error configuring acess manager implementation {}", configuration.getAmImpl(), ex);
        stopServer(false);
        System.exit(-3);
      }
    }

    if (configuration.isAuthTokenEnabled()) {
      LOGGER.info(
          "Token based authentication enabled with token TTL {} minutes",
          configuration.getAuthTokenTtl());
    }

    SSLContext sslContext = null;

    try {
      KeyManagerFactory kmf;
      KeyStore ks;

      if (getConfiguration().isUseEmbeddedKeystore()) {
        char[] storepass = "******".toCharArray();
        char[] keypass = "******".toCharArray();

        String storename = "rakeystore.jks";

        sslContext = SSLContext.getInstance("TLS");
        kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        ks = KeyStore.getInstance("JKS");
        ks.load(Bootstrapper.class.getClassLoader().getResourceAsStream(storename), storepass);

        kmf.init(ks, keypass);

        sslContext.init(kmf.getKeyManagers(), null, null);
      } else {
        sslContext = SSLContext.getInstance("TLS");
        kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        ks = KeyStore.getInstance("JKS");

        try (FileInputStream fis = new FileInputStream(new File(configuration.getKeystoreFile()))) {
          ks.load(fis, configuration.getKeystorePassword().toCharArray());

          kmf.init(ks, configuration.getCertPassword().toCharArray());
          sslContext.init(kmf.getKeyManagers(), null, null);
        }
      }
    } catch (KeyManagementException
        | NoSuchAlgorithmException
        | KeyStoreException
        | CertificateException
        | UnrecoverableKeyException ex) {
      LOGGER.error("Couldn't start RESTHeart, error with specified keystore. exiting..", ex);
      stopServer(false);
      System.exit(-1);
    } catch (FileNotFoundException ex) {
      LOGGER.error("Couldn't start RESTHeart, keystore file not found. exiting..", ex);
      stopServer(false);
      System.exit(-1);
    } catch (IOException ex) {
      LOGGER.error("Couldn't start RESTHeart, error reading the keystore file. exiting..", ex);
      stopServer(false);
      System.exit(-1);
    }

    Builder builder = Undertow.builder();

    if (configuration.isHttpsListener()) {
      builder.addHttpsListener(
          configuration.getHttpsPort(), configuration.getHttpHost(), sslContext);
      LOGGER.info(
          "HTTPS listener bound at {}:{}",
          configuration.getHttpsHost(),
          configuration.getHttpsPort());
    }

    if (configuration.isHttpListener()) {
      builder.addHttpListener(configuration.getHttpPort(), configuration.getHttpsHost());
      LOGGER.info(
          "HTTP listener bound at {}:{}", configuration.getHttpHost(), configuration.getHttpPort());
    }

    if (configuration.isAjpListener()) {
      builder.addAjpListener(configuration.getAjpPort(), configuration.getAjpHost());
      LOGGER.info(
          "Ajp listener bound at {}:{}", configuration.getAjpHost(), configuration.getAjpPort());
    }

    LocalCachesSingleton.init(configuration);

    if (configuration.isLocalCacheEnabled()) {
      LOGGER.info("Local cache for db and collection properties enabled");
    } else {
      LOGGER.info("Local cache for db and collection properties not enabled");
    }

    shutdownHandler = getHandlersPipe(identityManager, accessManager);

    builder
        .setIoThreads(configuration.getIoThreads())
        .setWorkerThreads(configuration.getWorkerThreads())
        .setDirectBuffers(configuration.isDirectBuffers())
        .setBufferSize(configuration.getBufferSize())
        .setBuffersPerRegion(configuration.getBuffersPerRegion())
        .setHandler(shutdownHandler);

    builder.build().start();
  }