<commit_before>/**
 * Copyright (C) 2014 Microsoft Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.microsoft.reef.runtime.common;

import com.microsoft.reef.runtime.common.launch.LaunchClass;
import com.microsoft.reef.runtime.common.launch.REEFErrorHandler;
import com.microsoft.reef.runtime.common.launch.REEFMessageCodec;
import com.microsoft.reef.runtime.common.launch.parameters.ClockConfigurationPath;
import com.microsoft.reef.runtime.common.launch.parameters.ErrorHandlerRID;
import com.microsoft.reef.runtime.common.launch.parameters.LaunchID;
import com.microsoft.reef.util.EnvironmentUtils;
import com.microsoft.tang.Configuration;
import com.microsoft.tang.Injector;
import com.microsoft.tang.JavaConfigurationBuilder;
import com.microsoft.tang.Tang;
import com.microsoft.tang.exceptions.BindException;
import com.microsoft.tang.exceptions.InjectionException;
import com.microsoft.tang.formats.CommandLine;
import com.microsoft.wake.remote.RemoteConfiguration;

import java.io.IOException;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class Launcher {
  public final static String[] LOGGING_PROPERTIES = {
      "java.util.logging.config.file",
      "java.util.logging.config.class"
  };
  private final static Logger LOG = Logger.getLogger(Launcher.class.getName());

  private Launcher() {
  }

  /**
   * Logs the currently running threads.
   *
   * @param prefix put before the comma-separated list of threads
   * @param level  the level used for the log entry
   */

  private static void logThreads(final String prefix, final Level level) {
    final StringBuilder sb = new StringBuilder(prefix);
    for (final Thread t : Thread.getAllStackTraces().keySet()) {
      sb.append(t.getName());
      sb.append(", ");
    }
    LOG.log(level, sb.toString());
  }

  /**
   * Parses the command line options of the launcher.
   *
   * @param args
   * @return
   * @throws BindException
   * @throws IOException
   * @throws InjectionException
   */
  private static Configuration processCommandLine(final String[] args) throws BindException, IOException, InjectionException {
    final JavaConfigurationBuilder commandLineBuilder = Tang.Factory.getTang().newConfigurationBuilder();
    final CommandLine cl = new CommandLine(commandLineBuilder);
    cl.registerShortNameOfClass(ClockConfigurationPath.class);
    cl.registerShortNameOfClass(ErrorHandlerRID.class);
    cl.registerShortNameOfClass(LaunchID.class);
    cl.processCommandLine(args);
    // Bind the wake error handler
    commandLineBuilder.bindNamedParameter(RemoteConfiguration.ErrorHandler.class, REEFErrorHandler.class);
    commandLineBuilder.bindNamedParameter(RemoteConfiguration.ManagerName.class, "REEF_LAUNCHER");
    // Bind the wake codec
    commandLineBuilder.bindNamedParameter(RemoteConfiguration.MessageCodec.class, REEFMessageCodec.class);
    return commandLineBuilder.build();
  }

  private static void fail(final String msg, final Throwable t) {
    LOG.log(Level.SEVERE, msg, t);
    throw new RuntimeException(msg, t);
  }

  /**
   * Launches a REEF client process (Driver or Evaluator).
   *
   * @param args
   * @throws Exception
   */
  public static void main(final String[] args) {
    LOG.log(Level.FINEST, "Launcher started");
    if (EnvironmentUtils.areAssertionsEnabled()) {
      LOG.log(Level.INFO, "Assertions are enabled in this process.");
    } else {
      LOG.log(Level.INFO, "Assertions are NOT enabled in this process.");
    }
    Injector injector = null;

    try {
      injector = Tang.Factory.getTang().newInjector(processCommandLine(args));
    } catch (final BindException | IOException | InjectionException e) {
      fail("Error in parsing the command line", e);
    }

    LaunchClass lc = null;
    try {
      lc = injector.getInstance(LaunchClass.class);
    } catch (final InjectionException e) {
      fail("Exception in creating the launcher", e);
    }

    lc.run();

    try {
      lc.close();
    } catch (final Exception e) {
      fail("Exception in closing the launcher", e);
    }
    LOG.log(Level.FINEST, "Launcher exiting");
    logThreads("Threads running after Launcher.close(): ", Level.FINEST);
    System.exit(0);
    logThreads("Threads running after System.exit(): ", Level.FINEST);
  }

  /**
   * Pass values of the properties specified in the propNames array as <code>-D...</code>
   * command line parameters. Currently used only to pass logging configuration to child JVMs processes.
   *
   * @param vargs     List of command line parameters to append to.
   * @param propNames Array of property names.
   */
  public static void propagateProperties(final Collection<String> vargs, final String[] propNames) {
    for (final String propName : propNames) {
      final String propValue = System.getProperty(propName);
      if (!(propValue == null || propValue.isEmpty())) {
        vargs.add(String.format("-D%s=%s", propName, propValue));
      }
    }
  }

}
<commit_msg>cosmetic fixes to the code in REEF Launcher<commit_after>/**
 * Copyright (C) 2014 Microsoft Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.microsoft.reef.runtime.common;

import com.microsoft.reef.runtime.common.launch.LaunchClass;
import com.microsoft.reef.runtime.common.launch.REEFErrorHandler;
import com.microsoft.reef.runtime.common.launch.REEFMessageCodec;
import com.microsoft.reef.runtime.common.launch.parameters.ClockConfigurationPath;
import com.microsoft.reef.runtime.common.launch.parameters.ErrorHandlerRID;
import com.microsoft.reef.runtime.common.launch.parameters.LaunchID;
import com.microsoft.reef.util.EnvironmentUtils;
import com.microsoft.tang.Configuration;
import com.microsoft.tang.Injector;
import com.microsoft.tang.JavaConfigurationBuilder;
import com.microsoft.tang.Tang;
import com.microsoft.tang.exceptions.BindException;
import com.microsoft.tang.exceptions.InjectionException;
import com.microsoft.tang.formats.CommandLine;
import com.microsoft.wake.remote.RemoteConfiguration;

import java.io.IOException;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class Launcher {

  public final static String[] LOGGING_PROPERTIES = {
      "java.util.logging.config.file",
      "java.util.logging.config.class"
  };

  private final static Logger LOG = Logger.getLogger(Launcher.class.getName());

  private Launcher() {
  }

  /**
   * Parse command line options of the launcher.
   *
   * @param args Command line as passed into main().
   * @return TANG configuration object.
   */
  private static Configuration processCommandLine(
      final String[] args) throws BindException, IOException, InjectionException {

    final JavaConfigurationBuilder commandLineBuilder =
        Tang.Factory.getTang().newConfigurationBuilder();

    new CommandLine(commandLineBuilder)
      .registerShortNameOfClass(ClockConfigurationPath.class)
      .registerShortNameOfClass(ErrorHandlerRID.class)
      .registerShortNameOfClass(LaunchID.class)
      .processCommandLine(args);

    return commandLineBuilder
        // Bind the wake error handler
        .bindNamedParameter(RemoteConfiguration.ErrorHandler.class, REEFErrorHandler.class)
        .bindNamedParameter(RemoteConfiguration.ManagerName.class, "REEF_LAUNCHER")
        // Bind the wake codec
        .bindNamedParameter(RemoteConfiguration.MessageCodec.class, REEFMessageCodec.class)
        .build();
  }

  private static void fail(final String msg, final Throwable t) {
    LOG.log(Level.SEVERE, msg, t);
    throw new RuntimeException(msg, t);
  }

  /**
   * Launches a REEF client process (Driver or Evaluator).
   *
   * @param args
   * @throws Exception
   */
  public static void main(final String[] args) {

    LOG.log(Level.FINE, "Launcher started. Assertions are {0} in this process.",
        EnvironmentUtils.areAssertionsEnabled() ? "ENABLED" : "DISABLED");

    Injector injector = null;
    try {
      injector = Tang.Factory.getTang().newInjector(processCommandLine(args));
    } catch (final BindException | IOException | InjectionException e) {
      fail("Error in parsing the command line", e);
    }

    LaunchClass lc = null;
    try {
      lc = injector.getInstance(LaunchClass.class);
    } catch (final InjectionException e) {
      fail("Exception in creating the launcher", e);
    }

    lc.run();

    try {
      lc.close();
    } catch (final Exception e) {
      fail("Exception in closing the launcher", e);
    }

    LOG.log(Level.FINE, "Launcher exiting");

    LOG.log(Level.FINEST, "Threads running after Launcher.close(): {0}",
        Thread.getAllStackTraces().keySet());

    System.exit(0);

    LOG.log(Level.FINEST, "Threads running after System.exit(): {0}",
        Thread.getAllStackTraces().keySet());
  }

  /**
   * Pass values of the properties specified in the propNames array as <code>-D...</code>
   * command line parameters. Currently used only to pass logging configuration to child JVMs processes.
   *
   * @param vargs     List of command line parameters to append to.
   * @param propNames Array of property names.
   */
  public static void propagateProperties(final Collection<String> vargs, final String[] propNames) {
    for (final String propName : propNames) {
      final String propValue = System.getProperty(propName);
      if (!(propValue == null || propValue.isEmpty())) {
        vargs.add(String.format("-D%s=%s", propName, propValue));
      }
    }
  }
}

{'fraction_non_alphanumeric': 0.10361126867082625, 'fraction_numerical': 0.0019852524106636414, 'mean_word_length': 3.813011828935396, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4266163', 'n_tokens_mistral': 3043, 'n_tokens_neox': 2959, 'n_words': 817}