at.molindo.utils.properties.SystemProperty Maven / Gradle / Ivy
Show all versions of molindo-utils Show documentation
/**
* Copyright 2010 Molindo GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package at.molindo.utils.properties;
import java.io.File;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* This enum is supposed to be a complete list of available system
* properties on different JVM and Java versions. And with complete I don't mean
* the list documented at {@link System#getProperties()}. I mean properties that
* are specific to different verndors, versions and operating stystems.
* Use {@link #get()} or {@link #toString()} to access the property values or
* {@link #set(String)} to update them - but only those that aren't read-only (
* {@link #isReadOnly()}).
If you discover some new properties in your
* environment, please run the contained {@link #main(String[])} method and send
* the output to me:
You may want to check for updates once in a while:
* SystemProperty.java and Molindo
* Techblog: The Final Take On Java System Properties
*
* List of tested JVMs
If your JVM is missing,
* please send
* me your output of the included {@link #main(String[])} method
*
- Sun JVM 1.6
- Ubuntu Hardy
- Cent OS
- Mac OS
* X
- Windows XP
- Sun JVM 1.5
- Ubuntu
* Hardy
- Open JDK 6
- Ubuntu Hardy
* - IBM JVM 1.4.2
- Windows XP
*
* @see System#getProperties()
*
* @author Stefan Fussenegger
*/
public enum SystemProperty {
/**
*
Known values:
- "MacRoman": Mac OS X
* - "Cp1252": Windows XP
- "UTF-8": CentOS, Ubuntu Hardy/JVM 1.5,
* Ubuntu Hardy/Open JDK 6
- "ISO-8859-1": Ubuntu Hardy/JVM 1.6
*
Note: This property is not part of
* {@link System#getProperties()} doc.
*/
FILE_ENCODING("file.encoding"),
/**
* This property is available on all Known values:
* - "sun.io"
Note: This property is
* not part of {@link System#getProperties()} doc.
*/
FILE_ENCODING_PKG("file.encoding.pkg"),
/**
* {@link System#getProperties()} doc: File separator ("/" on UNIX)
*
* Known values:
- "\" (Windows)
- "/"
* (Mac OS X, Linux)
- "\\" (Windows, IBM JVM)
*/
FILE_SEPARATOR("file.separator"),
/**
* Known values:
* - "sun.awt.X11GraphicsEnvironment": Ubuntu Hardy
* - "apple.awt.CGraphicsEnvironment": Mac OS X
* - "sun.awt.Win32GraphicsEnvironment": Windows XP
* Note: This property is not part of
* {@link System#getProperties()} doc.
*/
JAVA_AWT_GRAPHICSENV("java.awt.graphicsenv"), JAVA_AWT_PRINTERJOB("java.awt.printerjob"),
JAVA_CLASS_PATH("java.class.path"), JAVA_CLASS_VERSION("java.class.version"), JAVA_COMPILER("java.compiler"), JAVA_ENDORSED_DIRS(
"java.endorsed.dirs"), JAVA_EXT_DIRS("java.ext.dirs"),
JAVA_HOME("java.home"),
/**
* path to temporary directory including trailing file separator
*/
JAVA_IO_TMPDIR("java.io.tmpdir", Type.READ_WRITE) {
@Override
public String get() {
String value = super.get();
if (value == null) {
// you'll never know
return null;
}
if (!value.endsWith(File.separator)) {
// http://rationalpi.wordpress.com/2007/01/26/javaiotmpdir-inconsitency/
value += File.separator;
}
// make sure dir exists
try {
new File(value).mkdirs();
} catch (final SecurityException e) {
log.warn("not allowed to create temporary directory: " + value, e);
}
return value;
}
},
JAVA_LIBRARY_PATH("java.library.path"), JAVA_RUNTIME_NAME("java.runtime.name"), JAVA_RUNTIME_VERSION(
"java.runtime.version"),
JAVA_SPECIFICATION_NAME("java.specification.name"), JAVA_SPECIFICATION_VENDOR("java.specification.vendor"), JAVA_SPECIFICATION_VERSION(
"java.specification.version"),
JAVA_VERSION("java.version"), JAVA_VENDOR("java.vendor"), JAVA_VENDOR_URL("java.vendor.url"), JAVA_VENDOR_URL_BUG(
"java.vendor.url.bug"),
JAVA_VM_INFO("java.vm.info"),
/**
* This property may be used to distinguis between a JVM running in defaul
* (client) and server mode
*
* known values: "Java HotSpot(TM) 64-Bit Server VM",
* "Java HotSpot(TM) Client VM"
*/
JAVA_VM_NAME("java.vm.name"), JAVA_VM_SPECIFICATION_NAME("java.vm.specification.name"), JAVA_VM_SPECIFICATION_VENDOR(
"java.vm.specification.vendor"), JAVA_VM_SPECIFICATION_VERSION("java.vm.specification.version"), JAVA_VM_VERSION(
"java.vm.version"), JAVA_VM_VENDOR("java.vm.vendor"),
LINE_SEPARATOR("line.separator"),
/**
* see http://lopica.sourceforge.net/os.html for possible values
*/
OS_NAME("os.name"), OS_ARCH("os.arch"), OS_VERSION("os.version"),
PATH_SEPARATOR("path.separator"),
SUN_ARCH_DATA_MODEL("sun.arch.data.model"), SUN_BOOT_CLASS_PATH("sun.boot.class.path"), SUN_BOOT_LIBRARY_PATH(
"sun.boot.library.path"), SUN_CPU_ENDIAN("sun.cpu.endian"), SUN_CPU_ISALIST("sun.cpu.isalist"),
SUN_IO_UNICODE_ENCODING("sun.io.unicode.encoding"), SUN_JAVA_LAUNCHER("sun.java.launcher"), SUN_JNU_ENCODING(
"sun.jnu.encoding"), SUN_MANAGEMENT_COMPILER("sun.management.compiler"), SUN_OS_PATCH_LEVEL(
"sun.os.patch.level"),
USER_COUNTRY("user.country"), USER_DIR("user.dir"), USER_HOME("user.home"), USER_LANGUAGE("user.language"), USER_NAME(
"user.name"), USER_TIMEZONE("user.timezone"),
/*
* Windows ONLY
*/
/**
* Windows only: known values: "" (empty string)
*/
USER_VARIANT("user.variant"),
/*
* Linux ONLY
*/
/**
* Linux only: known values: gnome
*/
SUN_DESKTOP("sun.desktop"),
/**
* Linux only (seen on Sun an OpenJDK JVMs): known values: /usr/share/javazi
*/
USER_ZONEINFO_DIR("user.zoneinfo.dir"),
/*
* MAC ONLY
*/
/**
* Mac only: true or false
*/
AWT_NATIVE_DOUBLE_BUFFERING("awt.nativeDoubleBuffering"),
/**
* Mac only: known values: apple.awt.CToolkit
*/
AWT_TOOLKIT("awt.toolkit"),
/**
* Mac only: known values: local|*.local|169.254/16|*.169.254/16
*/
FTP_NON_PROXY_HOSTS("ftp.nonProxyHosts"),
/**
* Mac only: true or false
*/
GOPHER_PROXY_SET("gopherProxySet"),
/**
* Mac only: known values: local|*.local|169.254/16|*.169.254/16
*/
HTTP_NON_PROXY_HOSTS("http.nonProxyHosts"),
/**
* Mac only: known values: 1060.1.6.0_15-219
*/
MRJ_VERSION("mrj.version"),
/**
* Mac only: known values: local|*.local|169.254/16|*.169.254/16
*/
SOCKS_NON_PROXY_HOSTS("socksNonProxyHosts"),
/*
* GCJ only
*/
/**
* GCJ only: known values: /usr
*/
GNU_CLASSPATH_HOME("gnu.classpath.home"),
/**
* GCJ only: known values: file:///usr/lib64
*/
GNU_CLASSPATH_HOME_URL("gnu.classpath.home.url"),
/**
* GCJ only: known values: 0.98
*/
GNU_CLASSPATH_VERSION("gnu.classpath.version"),
/**
* GCJ only: known values: libgcj
*/
GNU_CLASSPATH_VM_SHORTNAME("gnu.classpath.vm.shortname"),
/**
* GCJ only: known values: little
*/
GNU_CPU_ENDIAN("gnu.cpu.endian"),
/**
* GCJ only: known values: /usr/lib64/gcj-4.4.1/classmap.db
*/
GNU_GCJ_PRECOMPILED_DB_PATH("gnu.gcj.precompiled.db.path"),
/**
* GCJ only: known values: SystemProperty
*/
GNU_GCJ_PROGNAME("gnu.gcj.progname"),
/**
* GCJ only: known values: /usr/share/java/gcj-endorsed
*/
GNU_GCJ_RUNTIME_ENDORSED_DIRS("gnu.gcj.runtime.endorsed.dirs"),
/**
* GCJ only: known values:
*/
GNU_GCJ_USER_REALNAME("gnu.gcj.user.realname"),
/**
* GCJ only: known values: /usr/share/zoneinfo
*/
GNU_JAVA_UTIL_ZONEINFO_DIR("gnu.java.util.zoneinfo.dir"),
/**
* GCJ only: known values: gnu-classpath/0.98 (libgcj/4.4.1 20090725 (Red
* Hat 4.4.1-2))
*/
HTTP_AGENT("http.agent"),
/**
* GCJ only: known values: GNU libgcj 4.4.1 20090725 (Red Hat 4.4.1-2)
*/
JAVA_FULLVERSION("java.fullversion"),
/**
* GCJ only: known values: US
*/
USER_REGION("user.region"),
/**
* AIX only
*/
/**
* AIX only: known values: big
*/
COM_IBM_CPU_ENDIAN("com.ibm.cpu.endian"),
/**
* AIX only: known values: scar
*/
COM_IBM_OTI_CONFIGURATION("com.ibm.oti.configuration"),
/**
* AIX only: known values: 20081111_1646
*/
COM_IBM_OTI_JCL_BUILD("com.ibm.oti.jcl.build"),
/**
* AIX only: known values: /prj/was/java/jre/bin
*/
COM_IBM_OTI_VM_BOOTSTRAP_LIBRARY_PATH("com.ibm.oti.vm.bootstrap.library.path"),
/**
* AIX only: known values: 23
*/
COM_IBM_OTI_VM_LIBRARY_VERSION("com.ibm.oti.vm.library.version"),
/**
* AIX only: known values:
*/
COM_IBM_UTIL_EXTRALIBS_PROPERTIES("com.ibm.util.extralibs.properties"),
/**
* AIX only: known values: 32
*/
COM_IBM_VM_BITMODE("com.ibm.vm.bitmode"),
/**
* AIX only: known values: false
*/
IBM_SIGNALHANDLING_RS("ibm.signalhandling.rs"),
/**
* AIX only: known values: true
*/
IBM_SIGNALHANDLING_SIGCHAIN("ibm.signalhandling.sigchain"),
/**
* AIX only: known values: true
*/
IBM_SIGNALHANDLING_SIGINT("ibm.signalhandling.sigint"),
/**
* AIX only: known values: ISO8859-1
*/
IBM_SYSTEM_ENCODING("ibm.system.encoding"),
/**
* AIX only: known values:
*/
INVOKEDVIAJAVA("invokedviajava"),
/**
* AIX only: known values: ON
*/
JAVA_ASSISTIVE("java.assistive"),
/**
* AIX only: known values:
*/
JAVA_AWT_FONTS("java.awt.fonts"),
/**
* AIX only: known values: 20090506
*/
JAVA_JCL_VERSION("java.jcl.version"),
/**
* AIX only: known values: java.util.prefs.FileSystemPreferencesFactory
*/
JAVA_UTIL_PREFS__PREFERENCES_FACTORY("java.util.prefs.PreferencesFactory"),
/**
* AIX only: known values: 9
*/
JXE_CURRENT_ROMIMAGE_VERSION("jxe.current.romimage.version"),
/**
* AIX only: known values: 9
*/
JXE_LOWEST_ROMIMAGE_VERSION("jxe.lowest.romimage.version"),
/**
* AIX only: known values:
*/
SUN_JAVA2D_FONTPATH("sun.java2d.fontpath"),
/*
* Groovy only
*/
/**
* Groovy only: known values: groovy
*/
PROGRAM_NAME("program.name"),
/**
* Groovy only: known values: /opt/groovy-1.6.4
*/
GROOVY_HOME("groovy.home"),
/**
* Groovy only: known values: /opt/groovy-1.6.4/conf/groovy-starter.conf
*/
GROOVY_STARTER_CONF("groovy.starter.conf"),
/*
* other common properties
*/
/**
* turns on headless mode
*/
JAVA_AWT_HEADLESS("java.awt.headless", Type.READ_WRITE) {
@Override
public String getDefault() {
return "false";
}
},
/**
* TODO doc (hint: for Sun VM on Java 6u14+)
*/
SUN_AWT_DISABLE_MIXING("sun.awt.disableMixing", Type.READ_WRITE),
/**
* TODO doc
*/
SUN_AWT_NOERASEBACKGROUND("sun.awt.noerasebackground", Type.READ_WRITE),
/**
* TODO doc
*/
SUN_AWT_XEMBEDSERVER("sun.awt.xembedserver", Type.READ_WRITE),
/*
* derived properties
*/
/**
* - windows if OS name contains the word
* "windows"
- os/2 if OS name contains the word
* "os/2"
- netware if OS name contains the word
* "netware"
- dos if OS family is not "netware",
* and its path separator is ";"
- mac if OS name
* contains the word "mac"
- tandem if OS name
* contains the word "nonstop_kernel"
- unix if OS
* family is not "openvms" and not "mac" which names does not end with "X",
* and its path separator is ":"
- win9x is OS
* family is "windows" and OS name contains "95", "98", "me", or "ce"
* - z/os if OS name contains the word "z/os" or "os/390"
*
- os/400 if OS name contains the word
* "os/400"
- openvms if OS name contains the word
* "openvms"
- unknown if none of the above matches
*
*/
OS_FAMILY("os.family") {
@Override
public String get() {
final String osName = OS_NAME.get();
if (osName != null) {
final String os = osName.toLowerCase();
if (os.contains("windows")) {
if (os.contains("95") || os.contains("98") || os.contains("me") || os.contains("ce")) {
return "win9x";
} else {
return "windows";
}
} else if (os.contains("mac")) {
return "mac";
} else if (os.contains("os/2")) {
return "os/2";
} else if (os.contains("os/400")) {
return "os/400";
} else if (os.contains("os/390") || os.contains("z/os")) {
return "z/os";
} else if (os.contains("netware")) {
return "netware";
} else if (os.contains("nonstop_kernel")) {
return "tandem";
} else if (os.contains("openvms")) {
return "openvms";
} else {
final String pathSeparator = PATH_SEPARATOR.get();
if (";".equals(pathSeparator)) {
return "dos";
}
if (":".equals(pathSeparator) && !os.endsWith("X")) {
return "unix";
}
}
}
return "unknown";
}
@Override
public boolean isDerived() {
return true;
}
},
/**
* returns true if VM is running in "Server Mode" (java -server). This
* is only true if java.vm.name starts with "Java HotSpot(TM)" and contains
* "Server". null
is returned if java.vm.name is null (which
* should never be the case).
Stackoverflow: How to make sure I'm using the "server" JVM?
*/
JAVA_VM_SERVER("java.vm.server") {
public String get() {
final String vm = JAVA_VM_NAME.get();
if (vm == null) {
return null;
}
if (vm.startsWith("Java HotSpot(TM)") && vm.contains("Server")) {
return Boolean.TRUE.toString();
} else {
return Boolean.FALSE.toString();
}
}
@Override
public boolean isDerived() {
return true;
}
};
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SystemProperty.class);
/**
* Type of property (only used in constructor for readability)
*/
private enum Type {
READ_WRITE, READ_ONLY;
}
private final String _name;
private final boolean _readOnly;
private SystemProperty(final String name) {
this(name, Type.READ_ONLY);
}
private SystemProperty(String name, final Type type) {
if (name == null) {
throw new NullPointerException("name");
}
name = name.trim();
if ("".equals(name)) {
throw new IllegalArgumentException();
}
_name = name;
_readOnly = type == Type.READ_ONLY;
if (!isReadOnly() && isDerived()) {
throw new RuntimeException("derived properties must be read-only: " + this);
}
}
/**
* @see System#getProperty(String)
* @see AccessController#doPrivileged(PrivilegedAction)
* @return the string value of the system property, or null
if
* there is no property with that key and {@link #getDefault()} is
* null
too
*/
public String get() {
final String val = AccessController.doPrivileged(new PrivilegedAction() {
public String run() {
return System.getProperty(getName());
}
});
return val != null ? val : getDefault();
}
/**
* @see System#getProperty(String)
* @see AccessController#doPrivileged(PrivilegedAction)
* @return the string value of the system property, or def
if
* there is no property with that key. ({@link #getDefault()} is
* ignored)
*/
public String get(final String def) {
final String val = AccessController.doPrivileged(new PrivilegedAction() {
public String run() {
return System.getProperty(getName());
}
});
return val != null ? val : def;
}
/**
* @return returns parsed boolean or null
if {@link #get()}
* returned null
*
* @see #get()
* @see Boolean#parseBoolean(String)
*/
public Boolean getBoolean() {
final String val = get();
return val == null ? null : Boolean.parseBoolean(val);
}
/**
* @return returns parsed boolean or null
if
* {@link #get(String)} returned null
*
* @see #get()
* @see Integer#parseInt(String)
*/
public Boolean getBoolean(final Boolean def) {
final String val = get(def == null ? null : def.toString());
return val == null ? null : Boolean.parseBoolean(val);
}
/**
* @return returns parsed integer or null
if {@link #get()}
* returned null
*
* @see #get()
* @see Integer#parseInt(String)
* @exception NumberFormatException
*/
public Integer getInteger() throws NumberFormatException {
final String val = get();
return val == null ? null : Integer.parseInt(val);
}
/**
* @return returns parsed integer or null
if
* {@link #get(String)} returned null
*
* @see #get()
* @see Integer#parseInt(String)
* @exception NumberFormatException
*/
public Integer getInteger(final Integer def) throws NumberFormatException {
final String val = get(def == null ? null : def.toString());
return val == null ? null : Integer.parseInt(val);
}
/**
* @return returns parsed Long or null
if {@link #get()}
* returned null
*
* @see #get()
* @see Long#parseLong(String)
* @exception NumberFormatException
*/
public Long getLong() throws NumberFormatException {
final String val = get();
return val == null ? null : Long.parseLong(val);
}
/**
* @return returns parsed Long or null
if {@link #get(String)}
* returned null
*
* @see #get()
* @see Long#parseLong(String)
* @exception NumberFormatException
*/
public Long getLong(final Long def) throws NumberFormatException {
final String val = get(def == null ? null : def.toString());
return val == null ? null : Long.parseLong(val);
}
/**
* @return returns parsed Float or null
if {@link #get()}
* returned null
*
* @see #get()
* @see Float#parseFloat(String)
* @exception NumberFormatException
*/
public Float getFloat() throws NumberFormatException {
final String val = get();
return val == null ? null : Float.parseFloat(val);
}
/**
* @return returns parsed Float or null
if {@link #get(String)}
* returned null
*
* @see #get()
* @see Float#parseFloat(String)
* @exception NumberFormatException
*/
public Float getFloat(final Float def) throws NumberFormatException {
final String val = get(def == null ? null : def.toString());
return val == null ? null : Float.parseFloat(val);
}
/**
* @return returns parsed Double or null
if {@link #get()}
* returned null
*
* @see #get()
* @see Double#parseDouble(String)
* @exception NumberFormatException
*/
public Double getDouble() throws NumberFormatException {
final String val = get();
return val == null ? null : Double.parseDouble(val);
}
/**
* @return returns parsed Double or null
if
* {@link #get(String)} returned null
*
* @see #get()
* @see Double#parseDouble(String)
* @exception NumberFormatException
*/
public Double getDouble(final Double def) throws NumberFormatException {
final String val = get(def == null ? null : def.toString());
return val == null ? null : Double.parseDouble(val);
}
/**
* @see #get()
* @return a file constructed with the returned value of {@link #get()} or
* null
*/
public File getFile() {
final String val = get();
return val == null ? null : new File(val);
}
/**
*
* @see #get()
* @param def
* default value
* @return a file constructed with the returned value of
* {@link #get(String)} or null
*/
public File getFile(final String def) {
final String val = get(def == null ? null : def.toString());
return val == null ? null : new File(val);
}
/**
* @param value
* the new value
* @see System#setProperty(String, String)
* @see AccessController#doPrivileged(PrivilegedAction)
* @see #isReadOnly()
* @exception UnsupportedOperationException
* if this property is read-only
* @return the previous value of this system property or null if it didn't
* have one
*/
public String set(final String value) {
if (isReadOnly()) {
throw new UnsupportedOperationException(getName() + " is a read-only property");
}
return AccessController.doPrivileged(new PrivilegedAction() {
public String run() {
return System.setProperty(getName(), value);
}
});
}
public String setBoolean(final Boolean value) throws NumberFormatException {
return set(value == null ? null : value.toString());
}
public String setInteger(final Integer value) throws NumberFormatException {
return set(value == null ? null : value.toString());
}
public String setLong(final Long value) throws NumberFormatException {
return set(value == null ? null : value.toString());
}
public String setDouble(final Double value) throws NumberFormatException {
return set(value == null ? null : value.toString());
}
public String setFloat(final Float value) throws NumberFormatException {
return set(value == null ? null : value.toString());
}
public String setFile(final File value) throws IOException {
return set(value == null ? null : value.getCanonicalPath());
}
public String unset() {
return set((String) null);
}
/**
* @return name of this property
*/
public String getName() {
return _name;
}
/**
* whether this property should not be modified (i.e. "read-only"). Note
* that it is possible to use {@link System#setProperty(String, String)}
* directly. It's use is discouraged though (as it might not have the
* desired effect)
*
* @return true
if this property should not be modified
*/
public boolean isReadOnly() {
return _readOnly;
}
/**
* @return true
if this is property doesn't exist but is
* derived from another property (e.g. OS_FAMILY which is derived by
* other properties, mainly OS_NAME)
*/
public boolean isDerived() {
return false;
}
/**
* the default return value if property is not set. for most properties this
* is null
. For example, the default for java.awt.headless is
* "false"
*
* @return the default value for this property or null
if not
* applicable
*/
public String getDefault() {
return null;
}
/**
* @return property value (same as {@link #get()}
* @see #get()
*/
@Override
public String toString() {
return get();
}
/**
* @return a string representation of this object (e.g.
* "OS_NAME: os.name=Linux (read-only)")
*/
public String toDebugString() {
final StringBuilder buf = new StringBuilder();
buf.append(name()).append(": ");
buf.append(getName()).append("=");
buf.append(get());
if (isDerived()) {
buf.append(" (derived)");
} else if (isReadOnly()) {
buf.append(" (read-only)");
}
return buf.toString();
}
/**
* a simple main method to list available system properties. additionally,
* it generates Java code for yet unknown properties. If you find some of
* them, please let us know:
* http://techblog.molindo.at/2009/11/java-system-properties.html
*
* @param args
* arguments are ignored
*/
public static void main(final String[] args) {
final TreeMap