1 /*
   2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.incubator.jpackage.internal;
  27 
  28 import java.util.regex.Pattern;
  29 
  30 /**
  31  * Platform
  32  *
  33  * Use <code>Platform</code> to detect the operating system
  34  * that is currently running.
  35  *
  36  * Example:
  37  *
  38  *  Platform platform = Platform.getPlatform();
  39  *
  40  *  switch(platform) {
  41  *    case Platform.MAC: {
  42  *      // Do something
  43  *      break;
  44  *    }
  45  *    case Platform.WINDOWS:
  46  *    case Platform.LINUX: {
  47  *      // Do something else
  48  *    }
  49  *  }
  50  *
  51  */
  52 enum Platform {UNKNOWN, WINDOWS, LINUX, MAC;
  53     private static final Platform platform;
  54     private static final int majorVersion;
  55     private static final int minorVersion;
  56 
  57     static {
  58         String os = System.getProperty("os.name").toLowerCase();
  59 
  60         if (os.indexOf("win") >= 0) {
  61             platform = Platform.WINDOWS;
  62         }
  63         else if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0) {
  64             platform = Platform.LINUX;
  65         }
  66         else if (os.indexOf("mac") >= 0) {
  67             platform = Platform.MAC;
  68         }
  69         else {
  70             platform = Platform.UNKNOWN;
  71         }
  72 
  73         String version = System.getProperty("os.version").toString();
  74         String[] parts = version.split(Pattern.quote("."));
  75 
  76         if (parts.length > 0) {
  77             majorVersion = Integer.parseInt(parts[0]);
  78 
  79             if (parts.length > 1) {
  80                 minorVersion = Integer.parseInt(parts[1]);
  81             }
  82             else {
  83                 minorVersion = -1;
  84             }
  85         }
  86         else {
  87             majorVersion = -1;
  88             minorVersion = -1;
  89         }
  90     }
  91 
  92     private Platform() {}
  93 
  94     static Platform getPlatform() {
  95         return platform;
  96     }
  97 
  98     static int getMajorVersion() {
  99         return majorVersion;
 100     }
 101 
 102     static int getMinorVersion() {
 103         return minorVersion;
 104     }
 105 
 106     static boolean isWindows() {
 107         return getPlatform() == WINDOWS;
 108     }
 109 
 110     static boolean isMac() {
 111         return getPlatform() == MAC;
 112     }
 113 
 114     static boolean isLinux() {
 115         return getPlatform() == LINUX;
 116     }
 117 
 118     static RuntimeException throwUnknownPlatformError() {
 119         throw new IllegalArgumentException("Unknown platform");
 120     }
 121 }