/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Utility methods for Alluxio {@link Process}es.
 */
public final class ProcessUtils {
  private static final Logger LOG = LoggerFactory.getLogger(ProcessUtils.class);

  /**
   * Runs the given {@link Process}. This method should only be called from {@code main()} methods.
   *
   * @param process the {@link Process} to run
'   */
  public static void run(Process process) {
    try {
      LOG.info("Starting {}.", process);
      process.start();
      LOG.info("Stopping {}.", process);
      System.exit(0);
    } catch (Exception e) {
      LOG.error("Uncaught exception while running {}, stopping it and exiting.", process, e);
      try {
        process.stop();
      } catch (Exception e2) {
        // continue to exit
        LOG.error("Uncaught exception while stopping {}, simply exiting.", process, e2);
      }
      System.exit(-1);
    }
  }

  /**
   * Adds a shutdown hook that will be invoked when a signal is sent to this process.
   *
   * The process may be utilizing some resources, and this shutdown hook will be invoked by
   * JVM when a SIGTERM is sent to the process by "kill" command. The shutdown hook calls
   * {@link Process#stop()} method to cleanly release the resources and exit.
   *
   * @param process the data structure representing the process to terminate
   */
  public static void stopProcessOnShutdown(final Process process) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        try {
          process.stop();
        } catch (Exception e) {
          LOG.error("Failed to shutdown process.", e);
          System.exit(-1);
        }
      }
    });
  }

  private ProcessUtils() {} // prevent instantiation
}

@@ -1112,35 +1112,35 @@
    %7D catch (
-Exception e
+Throwable t
 ) %7B%0A      LO
@@ -1221,17 +1221,17 @@
 rocess, 
-e
+t
 );%0A     
@@ -1276,27 +1276,27 @@
  catch (
-Exception e
+Throwable t
 2) %7B%0A   
@@ -1404,17 +1404,17 @@
 rocess, 
-e
+t
 2);%0A    
@@ -2123,19 +2123,19 @@
 ch (
-Exception e
+Throwable t
 ) %7B%0A
@@ -2185,17 +2185,17 @@
 cess.%22, 
-e
+t
 );%0A     

Handle Throwables at the process level (#6968)