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.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 } | 1 /* 2 * Copyright (c) 2018, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package jnlp.converter; 25 26 import java.util.regex.Pattern; 27 28 public enum Platform { 29 UNKNOWN, WINDOWS, LINUX, MAC; 30 private static final Platform platform; 31 private static final int majorVersion; 32 private static final int minorVersion; 33 34 static { 35 String os = System.getProperty("os.name").toLowerCase(); 36 37 if (os.contains("win")) { 38 platform = Platform.WINDOWS; 39 } else if (os.contains("nix") || os.contains("nux")) { 40 platform = Platform.LINUX; 41 } else if (os.contains("mac")) { 42 platform = Platform.MAC; 43 } else { 44 platform = Platform.UNKNOWN; 45 } 46 47 String version = System.getProperty("os.version"); 48 String[] parts = version.split(Pattern.quote(".")); 49 50 if (parts.length > 0) { 51 majorVersion = Integer.parseInt(parts[0]); 52 53 if (parts.length > 1) { 54 minorVersion = Integer.parseInt(parts[0]); 55 } else { 56 minorVersion = -1; 57 } 58 } else { 59 majorVersion = -1; 60 minorVersion = -1; 61 } 62 } 63 64 private Platform() { 65 } 66 67 public static Platform getPlatform() { 68 return platform; 69 } 70 71 public static boolean isWindows() { 72 return (platform == Platform.WINDOWS); 73 } 74 75 public static int getMajorVersion() { 76 return majorVersion; 77 } 78 79 public static int getMinorVersion() { 80 return minorVersion; 81 } 82 } |