001package jmri.util; 002 003import org.slf4j.Logger; 004import org.slf4j.LoggerFactory; 005 006/** 007 * Common utility methods for determining which type of operating system is in 008 * use. 009 * 010 * @author Bob Jacobsen Copyright 2006 011 * @author Daniel Boudreau Copyright 2012 (add Unix) 012 * @author Randall Wood Copyright 2013 013 */ 014public class SystemType { 015 016 static final public int MACCLASSIC = 1; // no longer supported - latest JVM is 1.1.8 017 static final public int MACOSX = 2; 018 static final public int WINDOWS = 4; 019 static final public int LINUX = 5; 020 static final public int OS2 = 6; 021 static final public int UNIX = 7; 022 023 static int type = 0; 024 static boolean isSet = false; 025 026 static String osName; 027 028 /** 029 * Get the integer constant for the OS. Useful in switch statements. 030 * 031 * @return Type as an integer 032 */ 033 public static int getType() { 034 setType(); 035 return type; 036 } 037 038 /** 039 * The os.name property 040 * 041 * @return OS name 042 */ 043 public static String getOSName() { 044 setType(); 045 return osName; 046 } 047 048 /** 049 * Convenience method to determine if OS is Mac OS X. Useful if an exception 050 * needs to be made for Mac OS X. 051 * 052 * @return true if on Mac OS X. 053 */ 054 public static boolean isMacOSX() { 055 setType(); 056 return (type == MACOSX); 057 } 058 059 /** 060 * Convenience method to determine if OS is Linux. Useful if an exception 061 * needs to be made for Linux. 062 * 063 * @return true if on Linux 064 */ 065 public static boolean isLinux() { 066 setType(); 067 return (type == LINUX); 068 } 069 070 /** 071 * Convenience method to determine if OS is Microsoft Windows. Useful if an 072 * exception needs to be made for Microsoft Windows. 073 * 074 * @return true if on Microsoft Windows 075 */ 076 public static boolean isWindows() { 077 setType(); 078 return (type == WINDOWS); 079 } 080 081 /** 082 * Convenience method to determine if OS is OS/2. Useful if an exception 083 * needs to be made for OS/2. 084 * 085 * @return true if on OS/2 086 */ 087 public static boolean isOS2() { 088 setType(); 089 return (type == OS2); 090 } 091 092 /** 093 * Convenience method to determine if OS is Unix. Useful if an exception 094 * needs to be made for Unix. 095 * 096 * @return true if on Unix 097 */ 098 public static boolean isUnix() { 099 setType(); 100 return (type == UNIX); 101 } 102 103 static void setType() { 104 if (isSet) { 105 return; 106 } 107 isSet = true; 108 109 osName = System.getProperty("os.name"); 110 String lowerCaseName = osName.toLowerCase(); 111 112 if (lowerCaseName.contains("os x")) { // Prefered test per http://developer.apple.com/library/mac/#technotes/tn2002/tn2110.html 113 // Mac OS X 114 type = MACOSX; 115 } else if (lowerCaseName.contains("linux")) { 116 // Linux 117 type = LINUX; 118 } else if (lowerCaseName.contains("os/2")) { 119 // OS/2 120 type = OS2; 121 } else if (lowerCaseName.contains("windows")) { // usually a suffix indicates flavor 122 // Windows 123 type = WINDOWS; 124 } else if (lowerCaseName.contains("nix") || lowerCaseName.contains("nux") || lowerCaseName.contains("aix") || lowerCaseName.contains("solaris")) { 125 // Unix 126 type = UNIX; 127 } else { 128 // No match 129 type = 0; 130 log.error("Could not determine system type from os.name=/{}/", osName); 131 } 132 } 133 134 // initialize logging 135 private final static Logger log = LoggerFactory.getLogger(SystemType.class); 136}