This project has retired. For details please refer to its Attic page.
Source code
001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.util;
020
021import java.io.IOException;
022import java.util.logging.Level;
023import java.util.logging.Logger;
024
025public final class OSUtils {
026  private static final Logger LOG = Logger.getLogger(OSUtils.class.getName());
027
028  private OSUtils() {
029  }
030
031  /**
032   * Determines whether the current JVM is running on the Windows OS.
033   *
034   * @return true, if the JVM is running on Windows. false, otherwise
035   */
036  public static boolean isWindows() {
037    return System.getProperty("os.name").toLowerCase().contains("windows");
038  }
039
040  /**
041   * Determines whether the current JVM is running on the Linux OS.
042   *
043   * @return true, if the JVM is running on Linux. false, otherwise
044   */
045  public static boolean isLinux() {
046    return System.getProperty("os.name").toLowerCase().contains("linux");
047  }
048
049  /**
050   * @return the process ID of the JVM, if running on Linux. This returns -1 for other OSs.
051   */
052  public static long getPID() {
053    if (isLinux()) {
054      try {
055        final Process process = new ProcessBuilder()
056            .command("bash", "-c", "echo $PPID")
057            .start();
058        final byte[] returnBytes = new byte[128];
059        process.getInputStream().read(returnBytes);
060        final Long result = Long.valueOf(new String(returnBytes).trim());
061        process.destroy();
062        return result;
063      } catch (final IOException e) {
064        LOG.log(Level.SEVERE, "Unable to determine PID", e);
065        return -1;
066      }
067
068    } else {
069      return -1;
070    }
071  }
072
073  /**
074   * Applies `kill -9` to the process.
075   *
076   * @param pid
077   * @throws IOException
078   */
079  public static void kill(final long pid) throws IOException, InterruptedException {
080    if (isLinux()) {
081      final Process process = new ProcessBuilder()
082          .command("bash", "-c", "kill", "-9", String.valueOf(pid))
083          .start();
084      final int returnCode = process.waitFor();
085      LOG.fine("Kill returned: " + returnCode);
086    } else {
087      throw new UnsupportedOperationException("Unable to execute kill on non-linux OS");
088    }
089  }
090
091  /**
092   * Formats the given variable for expansion by Windows (<code>%VARIABE%</code>) or Linux (<code>$VARIABLE</code>)
093   *
094   * @param variableName
095   * @return
096   */
097  public static String formatVariable(final String variableName) {
098    if (isWindows()) {
099      return "%" + variableName + "%";
100    } else {
101      return "$" + variableName;
102    }
103  }
104
105}