OS.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.exec;

  18. import java.io.File;
  19. import java.util.Locale;

  20. /**
  21.  * Condition that tests the OS type.
  22.  *
  23.  * Copied and adapted from Apache Ant 1.9.6 from org.apache.tools.ant.taskdefs.condition.OS.
  24.  */
  25. public final class OS {

  26.     /**
  27.      * OS family that can be tested for. {@value}
  28.      */
  29.     public static final String FAMILY_9X = "win9x";
  30.     /**
  31.      * OS family that can be tested for. {@value}
  32.      */
  33.     public static final String FAMILY_DOS = "dos";
  34.     /**
  35.      * OS family that can be tested for. {@value}
  36.      */
  37.     public static final String FAMILY_MAC = "mac";
  38.     /**
  39.      * OS family that can be tested for. {@value}
  40.      */
  41.     public static final String FAMILY_NETWARE = "netware";
  42.     /**
  43.      * OS family that can be tested for. {@value}
  44.      */
  45.     public static final String FAMILY_NT = "winnt";
  46.     /**
  47.      * OS family that can be tested for. {@value}
  48.      */
  49.     public static final String FAMILY_OS2 = "os/2";
  50.     /**
  51.      * OS family that can be tested for. {@value}
  52.      */
  53.     public static final String FAMILY_OS400 = "os/400";
  54.     /**
  55.      * OS family that can be tested for. {@value}
  56.      */
  57.     public static final String FAMILY_TANDEM = "tandem";
  58.     /**
  59.      * OS family that can be tested for. {@value}
  60.      */
  61.     public static final String FAMILY_UNIX = "unix";
  62.     /**
  63.      * OS family that can be tested for. {@value}
  64.      */
  65.     public static final String FAMILY_VMS = "openvms";
  66.     /**
  67.      * OS family that can be tested for. {@value}
  68.      */
  69.     public static final String FAMILY_WINDOWS = "windows";
  70.     /**
  71.      * OS family that can be tested for. {@value}
  72.      */
  73.     public static final String FAMILY_ZOS = "z/os";

  74.     private static final String DARWIN = "darwin";

  75.     private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.ROOT);
  76.     private static final String OS_ARCH = System.getProperty("os.arch").toLowerCase(Locale.ROOT);
  77.     private static final String OS_VERSION = System.getProperty("os.version").toLowerCase(Locale.ROOT);
  78.     private static final String PATH_SEP = File.pathSeparator;

  79.     /**
  80.      * Tests whether the OS on which commons-exec is executing matches the given OS architecture.
  81.      *
  82.      * @param arch the OS architecture to check for.
  83.      * @return whether if the OS matches.
  84.      */
  85.     public static boolean isArch(final String arch) {
  86.         return isOs(null, null, arch, null);
  87.     }

  88.     /**
  89.      * Tests whether the OS on which commons-exec is executing matches the given OS family.
  90.      *
  91.      * @param family the family to check for.
  92.      * @return whether if the OS matches.
  93.      */
  94.     private static boolean isFamily(final String family) {
  95.         return isOs(family, null, null, null);
  96.     }

  97.     /**
  98.      * Tests whether the OS is in the DOS family.
  99.      *
  100.      * @return whether the OS is in the DOS family.
  101.      */
  102.     public static boolean isFamilyDOS() {
  103.         return isFamily(FAMILY_DOS);
  104.     }

  105.     /**
  106.      * Tests whether the OS is in the Mac family.
  107.      *
  108.      * @return whether the OS is in the Mac family.
  109.      */
  110.     public static boolean isFamilyMac() {
  111.         return isFamily(FAMILY_MAC);
  112.     }

  113.     /**
  114.      * Tests whether the OS is in the Netware family.
  115.      *
  116.      * @return whether the OS is in the Netware family.
  117.      */
  118.     public static boolean isFamilyNetware() {
  119.         return isFamily(FAMILY_NETWARE);
  120.     }

  121.     /**
  122.      * Tests whether the OS is in the OpenVMS family.
  123.      *
  124.      * @return whether the OS is in the OpenVMS family.
  125.      */
  126.     public static boolean isFamilyOpenVms() {
  127.         return isFamily(FAMILY_VMS);
  128.     }

  129.     /**
  130.      * Tests whether the OS is in the OS/2 family.
  131.      *
  132.      * @return whether the OS is in the OS/2 family.
  133.      */
  134.     public static boolean isFamilyOS2() {
  135.         return isFamily(FAMILY_OS2);
  136.     }

  137.     /**
  138.      * Tests whether the OS is in the OS/400 family.
  139.      *
  140.      * @return whether the OS is in the OS/400 family.
  141.      */
  142.     public static boolean isFamilyOS400() {
  143.         return isFamily(FAMILY_OS400);
  144.     }

  145.     /**
  146.      * Tests whether the OS is in the Tandem family.
  147.      *
  148.      * @return whether the OS is in the Tandem family.
  149.      */
  150.     public static boolean isFamilyTandem() {
  151.         return isFamily(FAMILY_TANDEM);
  152.     }

  153.     /**
  154.      * Tests whether the OS is in the Unix family.
  155.      *
  156.      * @return whether the OS is in the Unix family.
  157.      */
  158.     public static boolean isFamilyUnix() {
  159.         return isFamily(FAMILY_UNIX);
  160.     }

  161.     /**
  162.      * Tests whether the OS is in the Windows 9x family.
  163.      *
  164.      * @return whether the OS is in the Windows 9x family.
  165.      */
  166.     public static boolean isFamilyWin9x() {
  167.         return isFamily(FAMILY_9X);
  168.     }

  169.     /**
  170.      * Tests whether the OS is in the Windows family.
  171.      *
  172.      * @return whether the OS is in the Windows family.
  173.      */
  174.     public static boolean isFamilyWindows() {
  175.         return isFamily(FAMILY_WINDOWS);
  176.     }

  177.     /**
  178.      * Tests whether the OS is in the Windows NT family.
  179.      *
  180.      * @return whether the OS is in the Windows NT family.
  181.      */
  182.     public static boolean isFamilyWinNT() {
  183.         return isFamily(FAMILY_NT);
  184.     }

  185.     /**
  186.      * Tests whether the OS is in the z/OS family.
  187.      *
  188.      * @return whether the OS is in the z/OS family.
  189.      */
  190.     public static boolean isFamilyZOS() {
  191.         return isFamily(FAMILY_ZOS);
  192.     }

  193.     /**
  194.      * Tests whether if the OS on which commons-exec is executing matches the given OS name.
  195.      *
  196.      * @param name the OS name to check for.
  197.      * @return whether the OS matches.
  198.      */
  199.     public static boolean isName(final String name) {
  200.         return isOs(null, name, null, null);
  201.     }

  202.     /**
  203.      * Tests whether the OS on which commons-exec is executing matches the given OS family, name, architecture and version.
  204.      *
  205.      * @param family  The OS family.
  206.      * @param name    The OS name.
  207.      * @param arch    The OS architecture.
  208.      * @param version The OS version.
  209.      * @return whether the OS matches.
  210.      */
  211.     public static boolean isOs(final String family, final String name, final String arch, final String version) {
  212.         boolean retValue = false;
  213.         if (family != null || name != null || arch != null || version != null) {
  214.             boolean isFamily = true;
  215.             boolean isName = true;
  216.             boolean isArch = true;
  217.             boolean isVersion = true;
  218.             if (family != null) {
  219.                 // Windows probing logic relies on the word 'windows' in the OS
  220.                 final boolean isWindows = OS_NAME.contains(FAMILY_WINDOWS);
  221.                 boolean is9x = false;
  222.                 boolean isNT = false;
  223.                 if (isWindows) {
  224.                     // there are only four 9x platforms that we look for
  225.                     is9x = OS_NAME.contains("95") || OS_NAME.contains("98") || OS_NAME.contains("me")
  226.                     // Windows CE isn't really 9x, but crippled enough to
  227.                     // be a muchness. Ant doesn't run on CE, anyway.
  228.                             || OS_NAME.contains("ce");
  229.                     isNT = !is9x;
  230.                 }
  231.                 switch (family) {
  232.                 case FAMILY_WINDOWS:
  233.                     isFamily = isWindows;
  234.                     break;
  235.                 case FAMILY_9X:
  236.                     isFamily = isWindows && is9x;
  237.                     break;
  238.                 case FAMILY_NT:
  239.                     isFamily = isWindows && isNT;
  240.                     break;
  241.                 case FAMILY_OS2:
  242.                     isFamily = OS_NAME.contains(FAMILY_OS2);
  243.                     break;
  244.                 case FAMILY_NETWARE:
  245.                     isFamily = OS_NAME.contains(FAMILY_NETWARE);
  246.                     break;
  247.                 case FAMILY_DOS:
  248.                     isFamily = PATH_SEP.equals(";") && !isFamily(FAMILY_NETWARE);
  249.                     break;
  250.                 case FAMILY_MAC:
  251.                     isFamily = OS_NAME.contains(FAMILY_MAC) || OS_NAME.contains(DARWIN);
  252.                     break;
  253.                 case FAMILY_TANDEM:
  254.                     isFamily = OS_NAME.contains("nonstop_kernel");
  255.                     break;
  256.                 case FAMILY_UNIX:
  257.                     isFamily = PATH_SEP.equals(":") && !isFamily(FAMILY_VMS) && (!isFamily(FAMILY_MAC) || OS_NAME.endsWith("x") || OS_NAME.contains(DARWIN));
  258.                     break;
  259.                 case FAMILY_ZOS:
  260.                     isFamily = OS_NAME.contains(FAMILY_ZOS) || OS_NAME.contains("os/390");
  261.                     break;
  262.                 case FAMILY_OS400:
  263.                     isFamily = OS_NAME.contains(FAMILY_OS400);
  264.                     break;
  265.                 case FAMILY_VMS:
  266.                     isFamily = OS_NAME.contains(FAMILY_VMS);
  267.                     break;
  268.                 default:
  269.                     throw new IllegalArgumentException("Don\'t know how to detect OS family \"" + family + "\"");
  270.                 }
  271.             }
  272.             if (name != null) {
  273.                 isName = name.equals(OS_NAME);
  274.             }
  275.             if (arch != null) {
  276.                 isArch = arch.equals(OS_ARCH);
  277.             }
  278.             if (version != null) {
  279.                 isVersion = version.equals(OS_VERSION);
  280.             }
  281.             retValue = isFamily && isName && isArch && isVersion;
  282.         }
  283.         return retValue;
  284.     }

  285.     /**
  286.      * Tests whether the OS on which commonss-exec is executing matches the given OS version.
  287.      *
  288.      * @param version the OS version to check for.
  289.      * @return whether if the OS matches.
  290.      */
  291.     public static boolean isVersion(final String version) {
  292.         return isOs(null, null, null, version);
  293.     }

  294.     /**
  295.      * Avoids instances.
  296.      */
  297.     private OS() {
  298.     }
  299. }