/**
* Xtreme Media Player a cross-platform media player.
* Copyright (C) 2005-2011 Besmir Beqiri
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package xtrememp.util;
/**
*
* @author Besmir Beqiri
*/
public class OSUtils {
/**
* The prefix String for all Windows OS.
*/
private static final String OS_NAME_WINDOWS_PREFIX = "Windows";
/**
* <p>
* The {@code os.arch} System Property. Operating system architecture.
* </p>
* <p>
* Defaults to {@code null} if the runtime does not have security access to read this property or the property does
* not exist.
* </p>
* <p>
* This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
* sync with that System property.
* </p>
*/
public static final String OS_ARCH = getSystemProperty("os.arch");
/**
* <p>
* The {@code os.name} System Property. Operating system name.
* </p>
* <p>
* Defaults to {@code null} if the runtime does not have security access to read this property or the property does
* not exist.
* </p>
* <p>
* This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
* sync with that System property.
* </p>
*/
public static final String OS_NAME = getSystemProperty("os.name");
/**
* <p>
* The {@code os.version} System Property. Operating system version.
* </p>
* <p>
* Defaults to {@code null} if the runtime does not have security access to read this property or the property does
* not exist.
* </p>
* <p>
* This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
* sync with that System property.
* </p>
*/
public static final String OS_VERSION = getSystemProperty("os.version");
/**
* <p>
* The {@code user.dir} System Property. User's current working directory.
* </p>
* <p>
* Defaults to {@code null} if the runtime does not have security access to read this property or the property does
* not exist.
* </p>
* <p>
* This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
* {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
* sync with that System property.
* </p>
*/
public static final String USER_DIR = getSystemProperty("user.dir");
/**
* </p
* Is {@code true} if operating system architecture is 32-bit.
* <p>
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
* </p>
*/
public static final boolean IS_ARCH_X86 = getOSMatchesArch("86");
/**
* <p>
* Is {@code true} if operating system architecture is 64-bit.
* </p>
* <p>
* The field will return {@code false} if {@code OS_ARCH} is {@code null}.
* </p>
*/
public static final boolean IS_ARCH_X64 = getOSMatchesArch("64");
/**
* <p>
* Is {@code true} if this is Linux.
* </p>
* <p>
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
* </p>
*/
public static final boolean IS_OS_LINUX = getOSMatchesName("Linux") || getOSMatchesName("LINUX");
/**
* <p>
* Is {@code true} if this is Windows.
* </p>
* <p>
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
* </p>
*/
public static final boolean IS_OS_WINDOWS = getOSMatchesName(OS_NAME_WINDOWS_PREFIX);
/**
* <p>
* Is {@code true} if this is Windows XP.
* </p>
* <p>
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
* </p>
*/
public static final boolean IS_OS_WINDOWS_XP = getOSMatches(OS_NAME_WINDOWS_PREFIX, "5.1");
/**
* <p>
* Is {@code true} if this is Windows Vista.
* </p>
* <p>
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
* </p>
*/
public static final boolean IS_OS_WINDOWS_VISTA = getOSMatches(OS_NAME_WINDOWS_PREFIX, "6.0");
/**
* <p>
* Is {@code true} if this is Windows 7.
* </p>
* <p>
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
* </p>
*/
public static final boolean IS_OS_WINDOWS_7 = getOSMatches(OS_NAME_WINDOWS_PREFIX, "6.1");
/**
* <p>
* Is {@code true} if this is Windows 8.
* </p>
* <p>
* The field will return {@code false} if {@code OS_NAME} is {@code null}.
* </p>
*/
public static final boolean IS_OS_WINDOWS_8 = getOSMatches(OS_NAME_WINDOWS_PREFIX, "6.2");
/**
* Decides if the operating system matches.
*
* @param osNamePrefix the prefix for the os name
* @param osVersionPrefix the prefix for the version
* @return true if matches, or false if not or can't determine
*/
private static boolean getOSMatches(String osNamePrefix, String osVersionPrefix) {
if (OS_NAME == null || OS_VERSION == null) {
return false;
}
return OS_NAME.startsWith(osNamePrefix) && OS_VERSION.startsWith(osVersionPrefix);
}
/**
* Decides if the operating system matches.
*
* @param osNamePrefix the prefix for the os name
* @return true if matches, or false if not or can't determine
*/
private static boolean getOSMatchesName(String osNamePrefix) {
if (OS_NAME == null) {
return false;
}
return OS_NAME.startsWith(osNamePrefix);
}
/**
* Verify if the current operating system is running on 64-bit.
*
* @return <code>true</code> if the current OS has x64 architecture,
* else <code>false</code>.
*/
private static boolean getOSMatchesArch(String osArchPrefix) {
if (OS_ARCH == null) {
return false;
}
return OS_ARCH.contains(osArchPrefix);
}
/**
* <p>
* Gets a System property, defaulting to {@code null} if the property cannot be read.
* </p>
* <p>
* If a {@code SecurityException} is caught, the return value is {@code null} and a message is written to
* {@code System.err}.
* </p>
*
* @param property the system property name
* @return the system property value or {@code null} if a security problem occurs
*/
private static String getSystemProperty(String property) {
try {
return System.getProperty(property);
} catch (SecurityException ex) {
// we are not allowed to look at this property
System.err.println("Caught a SecurityException reading the system property '" + property
+ "'; the SystemUtils property value will default to null.");
return null;
}
}
}