<commit_before>/*
 * Copyright © 2015 <NAME>, Inc.
 *
 * 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 co.cask.cdap.internal.app.runtime.batch.distributed;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * The class launches MR container (AM or Task). This class must be purely depends on standard Java library only
 * and free of any library dependencies. The {@link #launch(String, String, String, String, String[])}} method is
 * expected to be called by classes generated by {@link ContainerLauncherGenerator}.
 */
public class MapReduceContainerLauncher {

  /**
   * Calls the main method of the given main class.
   *
   * 1. Creates the MainClassLoader for dataset rewrite from the given classPath, with the context classloader
   *    parent as its own parent.
   * 2. Creates a new ClassLoader instance by instantiating the given classLoaderName from the URLClassLoader.
   * 3. Set the context ClassLoader to be the ClassLoader created in step 2.
   * 4. Calls the main method of the mainClassName loaded from the ClassLoader created in step 2.
   *
   * The ClassLoader created in step 2 is actually the MapReduceClassLoader. We cannot refer to it directly
   * in here since this class needs to have zero dependency on any library. We can hardcode the name here, but
   * that would make refactoring in future difficult. So the approach we take is to have the classLoader name
   * passed to this class. The name is "hardcode" in the generated class
   * (generated by {@link ContainerLauncherGenerator} which is invoked by MapReduceRuntimeService in
   * the client side and can uses MapReduceClassLoader.class directly.
   *
   * @see ContainerLauncherGenerator
   */
  public static void launch(String classPath, String mainClassLoaderName, String classLoaderName,
                            String mainClassName, String[] args) throws Exception {

    System.out.println("Launcher classpath: " + classPath);

    // Expands the classpath
    List<URL> urls = new ArrayList<>();
    for (String path : classPath.split("\\s*,\\s*")) {
      getClassPaths(path, urls);
    }

    // Get the current context classloader, default to system
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    if (contextClassLoader == null) {
      contextClassLoader = ClassLoader.getSystemClassLoader();
    }

    // ClassLoader mainClassLoader = new MainClassLoader(urls, contextClassLoader.getParent());
    Constructor<?> mainClassLoaderCons = contextClassLoader.loadClass(mainClassLoaderName)
      .getConstructor(URL[].class, ClassLoader.class);
    ClassLoader mainClassLoader = (ClassLoader) mainClassLoaderCons.newInstance(urls.toArray(new URL[urls.size()]),
                                                                                contextClassLoader.getParent());
    Thread.currentThread().setContextClassLoader(mainClassLoader);

    // Creates the MapReduceClassLoader.
    final ClassLoader classLoader = (ClassLoader) mainClassLoader.loadClass(classLoaderName).newInstance();
    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        if (classLoader instanceof AutoCloseable) {
          try {
            ((AutoCloseable) classLoader).close();
          } catch (Exception e) {
            System.err.println("Failed to close ClassLoader " + classLoader);
            e.printStackTrace();
          }
        }
      }
    });

    Thread.currentThread().setContextClassLoader(classLoader);

    // Invoke MapReduceClassLoader.getTaskContextProvider()
    classLoader.getClass().getDeclaredMethod("getTaskContextProvider").invoke(classLoader);
    // Invoke StandardOutErrorRedirector.redirectToLogger()
    classLoader.loadClass("co.cask.cdap.common.logging.StandardOutErrorRedirector")
      .getDeclaredMethod("redirectToLogger", String.class)
      .invoke(null, mainClassName);

    Class<?> mainClass = classLoader.loadClass(mainClassName);
    Method mainMethod = mainClass.getMethod("main", String[].class);
    mainMethod.setAccessible(true);

    System.out.println("Launch main class " + mainClass + ".main(" + Arrays.toString(args) + ")");
    mainMethod.invoke(null, new Object[]{args});
    System.out.println("Main method returned " + mainClass);
  }

  /**
   * Expands the given path into list of classpath {@link URL}s. If the path ends with "/*", it is treated as
   * classpath wildcard and all jar files under the directory represented by that path will be included.
   */
  private static void getClassPaths(String path, Collection<? super URL> collection) throws MalformedURLException {
    String classpath = expand(path);

    // Non-wildcard
    if (!classpath.endsWith(File.separator + "*")) {
      collection.add(new File(classpath).toURI().toURL());
      return;
    }

    // Wildcard, grab all .jar files
    File dir = new File(classpath.substring(0, classpath.length() - 2));
    File[] files = dir.listFiles();
    if (files == null || files.length == 0) {
      return;<commit_msg>CDAP-7392 Close classloaders in finaly block instead of closing them in shutdown hook.<commit_after>/*
 * Copyright © 2015-2016 <NAME>, Inc.
 *
 * 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 co.cask.cdap.internal.app.runtime.batch.distributed;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * The class launches MR container (AM or Task). This class must be purely depends on standard Java library only
 * and free of any library dependencies. The {@link #launch(String, String, String, String, String[])}} method is
 * expected to be called by classes generated by {@link ContainerLauncherGenerator}.
 */
public class MapReduceContainerLauncher {

  /**
   * Calls the main method of the given main class.
   *
   * 1. Creates the MainClassLoader for dataset rewrite from the given classPath, with the context classloader
   *    parent as its own parent.
   * 2. Creates a new ClassLoader instance by instantiating the given classLoaderName from the URLClassLoader.
   * 3. Set the context ClassLoader to be the ClassLoader created in step 2.
   * 4. Calls the main method of the mainClassName loaded from the ClassLoader created in step 2.
   *
   * The ClassLoader created in step 2 is actually the MapReduceClassLoader. We cannot refer to it directly
   * in here since this class needs to have zero dependency on any library. We can hardcode the name here, but
   * that would make refactoring in future difficult. So the approach we take is to have the classLoader name
   * passed to this class. The name is "hardcode" in the generated class
   * (generated by {@link ContainerLauncherGenerator} which is invoked by MapReduceRuntimeService in
   * the client side and can uses MapReduceClassLoader.class directly.
   *
   * @see ContainerLauncherGenerator
   */
  public static void launch(String classPath, String mainClassLoaderName, String classLoaderName,
                            String mainClassName, String[] args) throws Exception {

    System.out.println("Launcher classpath: " + classPath);

    // Expands the classpath
    List<URL> urls = new ArrayList<>();
    for (String path : classPath.split("\\s*,\\s*")) {
      getClassPaths(path, urls);
    }

    // Get the current context classloader, default to system
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    if (contextClassLoader == null) {
      contextClassLoader = ClassLoader.getSystemClassLoader();
    }

    // ClassLoader mainClassLoader = new MainClassLoader(urls, contextClassLoader.getParent());
    Constructor<?> mainClassLoaderCons = contextClassLoader.loadClass(mainClassLoaderName)
      .getConstructor(URL[].class, ClassLoader.class);
    ClassLoader mainClassLoader = (ClassLoader) mainClassLoaderCons.newInstance(urls.toArray(new URL[urls.size()]),
                                                                                contextClassLoader.getParent());
    Thread.currentThread().setContextClassLoader(mainClassLoader);

    // Creates the MapReduceClassLoader.
    final ClassLoader classLoader = (ClassLoader) mainClassLoader.loadClass(classLoaderName).newInstance();

    try {
      Thread.currentThread().setContextClassLoader(classLoader);

      // Invoke MapReduceClassLoader.getTaskContextProvider()
      classLoader.getClass().getDeclaredMethod("getTaskContextProvider").invoke(classLoader);
      // Invoke StandardOutErrorRedirector.redirectToLogger()
      classLoader.loadClass("co.cask.cdap.common.logging.StandardOutErrorRedirector")
        .getDeclaredMethod("redirectToLogger", String.class)
        .invoke(null, mainClassName);

      Class<?> mainClass = classLoader.loadClass(mainClassName);
      Method mainMethod = mainClass.getMethod("main", String[].class);
      mainMethod.setAccessible(true);

      System.out.println("Launch main class " + mainClass + ".main(" + Arrays.toString(args) + ")");
      mainMethod.invoke(null, new Object[]{args});
      System.out.println("Main method returned " + mainClass);
    } finally {
      if (classLoader instanceof AutoCloseable) {
        try {
          ((AutoCloseable) classLoader).close();
        } catch (Exception e) {
          System.err.println("Failed to close ClassLoader " + classLoader);
          e.printStackTrace();
        }
      }
    }
  }

  /**
   * Expands the given path into list of classpath {@link URL}s. If the path ends with "/*", it is treated as
   * classpath wildcard and all jar files under the directory represented by that path will be included.
   */
  private static void getClassPaths(String path, Collection<? super URL> collection) throws MalformedURLException {
    String classpath = expand(path);

    // Non-wildcard
    if (!classpath.endsWith(File.separator + "*")) {
      collection.add(new File(classpath).toURI().toURL());
      return;
    }

    // Wildcard, grab all .jar files
    File dir = new File(classpath.substring(0, classpath.length() - 2));
    File[] files = dir.listFiles();
    if (files == null || files.length == 0) {
      return;
{'fraction_non_alphanumeric': 0.09436083722539353, 'fraction_numerical': 0.003805569970593323, 'mean_word_length': 3.516796875, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7065505', 'n_tokens_mistral': 3029, 'n_tokens_neox': 2967, 'n_words': 1033}