diff --git a/src/java/org/apache/commons/lang/SystemUtils.java b/src/java/org/apache/commons/lang/SystemUtils.java index 7974cbae0..30c240fbb 100644 --- a/src/java/org/apache/commons/lang/SystemUtils.java +++ b/src/java/org/apache/commons/lang/SystemUtils.java @@ -66,7 +66,7 @@ * @author Gary Gregory * @author Michael Becke * @since 1.0 - * @version $Id: SystemUtils.java,v 1.11 2003/05/24 12:06:37 scolebourne Exp $ + * @version $Id: SystemUtils.java,v 1.12 2003/05/24 14:34:14 scolebourne Exp $ */ public class SystemUtils { @@ -82,13 +82,23 @@ public SystemUtils() { } //----------------------------------------------------------------------- + /** + * The file.encoding System Property. + * File encoding, such as Cp1252. + * First in JDK version 1.2. + *

+ * Defaults to null if the runtime does not have + * security access to read this property or property not specified. + */ + public static final String FILE_ENCODING = getSystemProperty("file.encoding"); + /** * The file.separator System Property. * File separator ("/" on UNIX). * First in JDK version 1.1. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String FILE_SEPARATOR = getSystemProperty("file.separator"); @@ -98,7 +108,7 @@ public SystemUtils() { * First in JDK version 1.1. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_CLASS_PATH = getSystemProperty("java.class.path"); @@ -108,17 +118,17 @@ public SystemUtils() { * First in JDK version 1.1. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_CLASS_VERSION = getSystemProperty("java.class.version"); /** * The java.compiler System Property. * Name of JIT compiler to use. - * First in JDK version 1.4. + * First in JDK version 1.2. Not used in Sun JDKs after 1.2. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_COMPILER = getSystemProperty("java.compiler"); @@ -128,7 +138,7 @@ public SystemUtils() { * First in JDK version 1.3. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_EXT_DIRS = getSystemProperty("java.ext.dirs"); @@ -138,37 +148,57 @@ public SystemUtils() { * First in JDK version 1.1. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_HOME = getSystemProperty("java.home"); /** * The java.io.tmpdir System Property. * Default temp file path. - * First in JDK version 1.4. + * First in JDK version 1.2. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_IO_TMPDIR = getSystemProperty("java.io.tmpdir"); /** * The java.library.path System Property. * List of paths to search when loading libraries. - * First in JDK version 1.4. + * First in JDK version 1.2. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_LIBRARY_PATH = getSystemProperty("java.library.path"); + /** + * The java.runtime.name System Property. + * Java Runtime Environment name. + * First in JDK version 1.3. + *

+ * Defaults to null if the runtime does not have + * security access to read this property or property not specified. + */ + public static final String JAVA_RUNTIME_NAME = getSystemProperty("java.runtime.name"); + + /** + * The java.runtime.version System Property. + * Java Runtime Environment version. + * First in JDK version 1.3. + *

+ * Defaults to null if the runtime does not have + * security access to read this property or property not specified. + */ + public static final String JAVA_RUNTIME_VERSION = getSystemProperty("java.runtime.version"); + /** * The java.specification.name System Property. * Java Runtime Environment specification name. * First in JDK version 1.2. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_SPECIFICATION_NAME = getSystemProperty("java.specification.name"); @@ -178,7 +208,7 @@ public SystemUtils() { * First in JDK version 1.2. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_SPECIFICATION_VENDOR = getSystemProperty("java.specification.vendor"); @@ -188,7 +218,7 @@ public SystemUtils() { * First in JDK version 1.2. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_SPECIFICATION_VERSION = getSystemProperty("java.specification.version"); @@ -198,7 +228,7 @@ public SystemUtils() { * First in JDK version 1.1. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_VENDOR = getSystemProperty("java.vendor"); @@ -208,7 +238,7 @@ public SystemUtils() { * First in JDK version 1.1. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_VENDOR_URL = getSystemProperty("java.vendor.url"); @@ -218,17 +248,27 @@ public SystemUtils() { * First in JDK version 1.1. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_VERSION = getSystemProperty("java.version"); + /** + * The java.vm.info System Property. + * Java Virtual Machine implementation info. + * First in JDK version 1.2. + *

+ * Defaults to null if the runtime does not have + * security access to read this property or property not specified. + */ + public static final String JAVA_VM_INFO = getSystemProperty("java.vm.info"); + /** * The java.vm.name System Property. * Java Virtual Machine implementation name. * First in JDK version 1.2. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_VM_NAME = getSystemProperty("java.vm.name"); @@ -238,7 +278,7 @@ public SystemUtils() { * First in JDK version 1.2. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_VM_SPECIFICATION_NAME = getSystemProperty("java.vm.specification.name"); @@ -248,7 +288,7 @@ public SystemUtils() { * First in JDK version 1.2. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_VM_SPECIFICATION_VENDOR = getSystemProperty("java.vm.specification.vendor"); @@ -258,7 +298,7 @@ public SystemUtils() { * First in JDK version 1.2. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_VM_SPECIFICATION_VERSION = getSystemProperty("java.vm.specification.version"); @@ -268,7 +308,7 @@ public SystemUtils() { * First in JDK version 1.2. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_VM_VENDOR = getSystemProperty("java.vm.vendor"); @@ -278,7 +318,7 @@ public SystemUtils() { * First in JDK version 1.2. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String JAVA_VM_VERSION = getSystemProperty("java.vm.version"); @@ -288,7 +328,7 @@ public SystemUtils() { * First in JDK version 1.1. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String LINE_SEPARATOR = getSystemProperty("line.separator"); @@ -298,7 +338,7 @@ public SystemUtils() { * First in JDK version 1.1. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String OS_ARCH = getSystemProperty("os.arch"); @@ -308,7 +348,7 @@ public SystemUtils() { * First in JDK version 1.1. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String OS_NAME = getSystemProperty("os.name"); @@ -318,7 +358,7 @@ public SystemUtils() { * First in JDK version 1.1. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String OS_VERSION = getSystemProperty("os.version"); @@ -328,7 +368,7 @@ public SystemUtils() { * First in JDK version 1.1. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String PATH_SEPARATOR = getSystemProperty("path.separator"); @@ -338,7 +378,7 @@ public SystemUtils() { * First in JDK version 1.1. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String USER_DIR = getSystemProperty("user.dir"); @@ -348,17 +388,40 @@ public SystemUtils() { * First in JDK version 1.1. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String USER_HOME = getSystemProperty("user.home"); + /** + * The user.language System Property. + * User's language code, such as 'en'. + * First in JDK version 1.2. + *

+ * Defaults to null if the runtime does not have + * security access to read this property or property not specified. + */ + public static final String USER_LANGUAGE = getSystemProperty("user.language"); + + /** + * The user.country or user.region System Property. + * User's country code, such as 'GB'. + * First in JDK version 1.2 as user.region. + * Renamed to user.country in 1.4 + *

+ * Defaults to null if the runtime does not have + * security access to read this property or property not specified. + */ + public static final String USER_COUNTRY = + (getSystemProperty("user.country") == null ? + getSystemProperty("user.region") : getSystemProperty("user.country")); + /** * The user.name System Property. * User's account name. * First in JDK version 1.1. *

* Defaults to null if the runtime does not have - * security access to read this property. + * security access to read this property or property not specified. */ public static final String USER_NAME = getSystemProperty("user.name"); @@ -423,6 +486,128 @@ public SystemUtils() { */ public static final int JAVA_VERSION_INT = getJavaVersionAsInt(); + //----------------------------------------------------------------------- + // OS names from http://www.vamphq.com/os.html + // Selected ones included - please advise commons-dev@jakarta.apache.org + // if you want another added or a mistake corrected + + /** + * Is true if this is Windows. + *

+ * The field will return false if OS_NAME is null. + */ + public static final boolean IS_OS_WINDOWS = getOSMatches("Windows"); + + /** + * Is true if this is Windows 95. + *

+ * The field will return false if OS_NAME is null. + */ + public static final boolean IS_OS_WINDOWS_95 = getOSMatches("Windows 9", "4.0"); + // JDK 1.2 running on Windows98 returns 'Windows 95', hence the above + + /** + * Is true if this is Windows 98. + *

+ * The field will return false if OS_NAME is null. + */ + public static final boolean IS_OS_WINDOWS_98 = getOSMatches("Windows 9", "4.1"); + // JDK 1.2 running on Windows98 returns 'Windows 95', hence the above + + /** + * Is true if this is Windows ME. + *

+ * The field will return false if OS_NAME is null. + */ + public static final boolean IS_OS_WINDOWS_ME = getOSMatches("Windows", "4.9"); + // JDK 1.2 running on WindowsME may return 'Windows 95', hence the above + + /** + * Is true if this is Windows NT. + *

+ * The field will return false if OS_NAME is null. + */ + public static final boolean IS_OS_WINDOWS_NT = getOSMatches("Windows NT"); + + /** + * Is true if this is Windows 2000. + *

+ * The field will return false if OS_NAME is null. + */ + public static final boolean IS_OS_WINDOWS_2000 = getOSMatches("Windows", "5.0"); + // Windows 2000 returns 'Windows 2000' but may suffer from same JDK1.2 problem + + /** + * Is true if this is Windows XP. + *

+ * The field will return false if OS_NAME is null. + */ + public static final boolean IS_OS_WINDOWS_XP = getOSMatches("Windows", "5.1"); + // Windows XP returns 'Windows 2000' just for fun... + + /** + * Is true if this is Mac. + *

+ * The field will return false if OS_NAME is null. + */ + public static final boolean IS_OS_MAC = getOSMatches("Mac"); + + /** + * Is true if this is Mac. + *

+ * The field will return false if OS_NAME is null. + */ + public static final boolean IS_OS_MAC_OSX = getOSMatches("Mac OS X"); + + /** + * Is true if this is Linux. + *

+ * The field will return false if OS_NAME is null. + */ + public static final boolean IS_OS_LINUX = getOSMatches("Linux") || getOSMatches("LINUX"); + + /** + * Is true if this is Mac. + *

+ * The field will return false if OS_NAME is null. + */ + public static final boolean IS_OS_OS2 = getOSMatches("OS/2"); + + /** + * Is true if this is Solaris. + *

+ * The field will return false if OS_NAME is null. + */ + public static final boolean IS_OS_SOLARIS = getOSMatches("Solaris"); + + /** + * Is true if this is SunOS. + *

+ * The field will return false if OS_NAME is null. + */ + public static final boolean IS_OS_SUN_OS = getOSMatches("SunOS"); + + /** + * Is true if this is HP-UX. + *

+ * The field will return false if OS_NAME is null. + */ + public static final boolean IS_OS_HP_UX = getOSMatches("HP-UX"); + + /** + * Is true if this is AIX. + *

+ * The field will return false if OS_NAME is null. + */ + public static final boolean IS_OS_AIX = getOSMatches("AIX"); + + /** + * Is true if this is Irix. + *

+ * The field will return false if OS_NAME is null. + */ + public static final boolean IS_OS_IRIX = getOSMatches("Irix"); + //----------------------------------------------------------------------- /** * Gets a System property, defaulting to the given value if the property @@ -511,107 +696,34 @@ private static int getJavaVersionAsInt() { return Integer.parseInt(str); } - //----------------------------------------------------------------------- - // Parsing operating system may stay here, or it may be moved somewhere else entirely -// /** True iff this is running on Windows */ -// public static final boolean IS_WINDOWS; -// /** True iff this is running on Unix */ -// public static final boolean IS_UNIX; -// /** True iff this is running on Mac */ -// public static final boolean IS_MAC; -// /** True iff this is running on OS2 */ -// public static final boolean IS_OS2; -// /** True iff this is running on Linux */ -// public static final boolean IS_LINUX; -// -// /* -// * The JLS doesn't seem to specify an exact naming convention for the -// * os.name. We ensure a uniform naming here. -// */ -// static { -// // from http://www.geocities.com/vamp201/os.html -// if (OS_NAME.startsWith("Windows")) { -// IS_WINDOWS = true; -// IS_UNIX = false; -// IS_MAC = false; -// IS_OS2 = false; -// IS_LINUX = false; -// } else if (OS_NAME.startsWith("SunOS")) { -// IS_WINDOWS = false; -// IS_UNIX = true; -// IS_MAC = false; -// IS_OS2 = false; -// IS_LINUX = false; -// } else if (OS_NAME.startsWith("Solaris")) { -// IS_WINDOWS = false; -// IS_UNIX = true; -// IS_MAC = false; -// IS_OS2 = false; -// IS_LINUX = false; -// } else if (OS_NAME.startsWith("Linux")) { -// IS_WINDOWS = false; -// IS_UNIX = true; -// IS_MAC = false; -// IS_OS2 = false; -// IS_LINUX = true; -// } else if (OS_NAME.startsWith("HP-UX")) { -// IS_WINDOWS = false; -// IS_UNIX = true; -// IS_MAC = false; -// IS_OS2 = false; -// IS_LINUX = false; -// } else if (OS_NAME.startsWith("AIX")) { -// IS_WINDOWS = false; -// IS_UNIX = true; -// IS_MAC = false; -// IS_OS2 = false; -// IS_LINUX = false; -// } else if (OS_NAME.startsWith("Irix")) { -// IS_WINDOWS = false; -// IS_UNIX = true; -// IS_MAC = false; -// IS_OS2 = false; -// IS_LINUX = false; -// } else if (OS_NAME.startsWith("Digital Unix")) { -// IS_WINDOWS = false; -// IS_UNIX = true; -// IS_MAC = false; -// IS_OS2 = false; -// IS_LINUX = false; -// } else if (OS_NAME.startsWith("OS/400")) { -// IS_WINDOWS = false; -// IS_UNIX = true; -// IS_MAC = false; -// IS_OS2 = false; -// IS_LINUX = false; -// } else if (OS_NAME.startsWith("OS/2")) { -// IS_WINDOWS = false; -// IS_UNIX = false; -// IS_MAC = false; -// IS_OS2 = true; -// IS_LINUX = false; -// } else if (OS_NAME.startsWith("Mac OS X")) { -// IS_WINDOWS = false; -// IS_UNIX = true; -// IS_MAC = true; -// IS_OS2 = false; -// IS_LINUX = false; -// } else if (OS_NAME.startsWith("Mac")) { -// IS_WINDOWS = false; -// IS_UNIX = false; -// IS_MAC = true; -// IS_OS2 = false; -// IS_LINUX = false; -// } else { -// IS_WINDOWS = false; -// IS_UNIX = false; -// IS_MAC = false; -// IS_OS2 = false; -// IS_LINUX = false; -// } -// } + /** + * 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 getOSMatches(String osNamePrefix) { + if (OS_NAME == null) { + return false; + } + return OS_NAME.startsWith(osNamePrefix); + } - //----------------------------------------------------------------------- + /** + * 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); + } + + //----------------------------------------------------------------------- /** *

Get the Java version number as a float.

* diff --git a/src/test/org/apache/commons/lang/SystemUtilsTest.java b/src/test/org/apache/commons/lang/SystemUtilsTest.java index 4894976e1..52337c0d2 100644 --- a/src/test/org/apache/commons/lang/SystemUtilsTest.java +++ b/src/test/org/apache/commons/lang/SystemUtilsTest.java @@ -64,7 +64,7 @@ * Only limited testing can be performed. * * @author Stephen Colebourne - * @version $Id: SystemUtilsTest.java,v 1.1 2003/05/24 12:11:02 scolebourne Exp $ + * @version $Id: SystemUtilsTest.java,v 1.2 2003/05/24 14:34:14 scolebourne Exp $ */ public class SystemUtilsTest extends TestCase { @@ -74,6 +74,22 @@ public SystemUtilsTest(String name) { public static void main(String[] args) { TestRunner.run(suite()); +// SystemUtils s = new SystemUtils(); +// System.out.println(s.IS_OS_WINDOWS); +// System.out.println(s.IS_OS_WINDOWS_95); +// System.out.println(s.IS_OS_WINDOWS_98); +// java.util.Properties p = System.getProperties(); +// java.util.Enumeration keys = p.keys(); +// java.util.List list = new java.util.ArrayList(); +// while( keys.hasMoreElements() ) { +// list.add(keys.nextElement()); +// } +// java.util.Collections.sort(list); +// for (java.util.Iterator it = list.iterator(); it.hasNext();) { +// String key = (String) it.next(); +// System.out.println(key + " " + p.getProperty(key)); +// } + } public static Test suite() { @@ -94,6 +110,9 @@ protected void tearDown() throws Exception { // COPIED FROM SystemUtils //----------------------------------------------------------------------- private String JAVA_VERSION; + private String OS_NAME; + private String OS_VERSION; + /** * Decides if the java version matches. * @@ -160,6 +179,33 @@ private int getJavaVersionAsInt() { return Integer.parseInt(str); } + /** + * 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 boolean getOSMatches(String osNamePrefix) { + if (OS_NAME == null) { + return false; + } + return OS_NAME.startsWith(osNamePrefix); + } + + /** + * 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 boolean getOSMatches(String osNamePrefix, String osVersionPrefix) { + if (OS_NAME == null || OS_VERSION == null) { + return false; + } + return OS_NAME.startsWith(osNamePrefix) && OS_VERSION.startsWith(osVersionPrefix); + } + //----------------------------------------------------------------------- public void testJavaVersionMatches() { JAVA_VERSION = null; @@ -278,4 +324,36 @@ public void testJavaVersionAtLeastInt() { assertEquals(false, SystemUtils.isJavaVersionAtLeast(version)); } + public void testOSMatches() { + OS_NAME = null; + assertEquals(false, getOSMatches("Windows")); + OS_NAME = "Windows 95"; + assertEquals(true, getOSMatches("Windows")); + OS_NAME = "Windows NT"; + assertEquals(true, getOSMatches("Windows")); + OS_NAME = "OS/2"; + assertEquals(false, getOSMatches("Windows")); + } + + public void testOSMatches2() { + OS_NAME = null; + OS_VERSION = null; + assertEquals(false, getOSMatches("Windows 9", "4.1")); + OS_NAME = "Windows 95"; + OS_VERSION = "4.0"; + assertEquals(false, getOSMatches("Windows 9", "4.1")); + OS_NAME = "Windows 95"; + OS_VERSION = "4.1"; + assertEquals(true, getOSMatches("Windows 9", "4.1")); + OS_NAME = "Windows 98"; + OS_VERSION = "4.1"; + assertEquals(true, getOSMatches("Windows 9", "4.1")); + OS_NAME = "Windows NT"; + OS_VERSION = "4.0"; + assertEquals(false, getOSMatches("Windows 9", "4.1")); + OS_NAME = "OS/2"; + OS_VERSION = "4.0"; + assertEquals(false, getOSMatches("Windows 9", "4.1")); + } + }