Refactor system property access into a new SystemProperties class
This commit is contained in:
parent
91ccf53c7d
commit
95a0e2b09b
|
@ -121,7 +121,7 @@ public class ArchUtils {
|
|||
* @return A {@link Processor} when supported, else {@code null}.
|
||||
*/
|
||||
public static Processor getProcessor() {
|
||||
return getProcessor(SystemUtils.OS_ARCH);
|
||||
return getProcessor(SystemProperties.getOsArch());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,791 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 org.apache.commons.lang3;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Accesses current system property names and values.
|
||||
*
|
||||
* @since 3.13.0
|
||||
*/
|
||||
public final class SystemProperties {
|
||||
|
||||
private static final Supplier<String> NULL_SUPPLIER = () -> null;
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String AWT_TOOLKIT = "awt.toolkit";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String FILE_ENCODING = "file.encoding";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String FILE_SEPARATOR = "file.separator";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_AWT_FONTS = "java.awt.fonts";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_AWT_GRAPHICSENV = "java.awt.graphicsenv";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_AWT_HEADLESS = "java.awt.headless";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_AWT_PRINTERJOB = "java.awt.printerjob";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_CLASS_PATH = "java.class.path";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_CLASS_VERSION = "java.class.version";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_COMPILER = "java.compiler";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_ENDORSED_DIRS = "java.endorsed.dirs";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_EXT_DIRS = "java.ext.dirs";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_HOME = "java.home";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_LIBRARY_PATH = "java.library.path";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_RUNTIME_NAME = "java.runtime.name";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_RUNTIME_VERSION = "java.runtime.version";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_SPECIFICATION_NAME = "java.specification.name";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_SPECIFICATION_VENDOR = "java.specification.vendor";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_SPECIFICATION_VERSION = "java.specification.version";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_UTIL_PREFS_PREFERENCES_FACTORY = "java.util.prefs.PreferencesFactory";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_VENDOR = "java.vendor";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_VENDOR_URL = "java.vendor.url";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_VERSION = "java.version";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_VM_INFO = "java.vm.info";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_VM_NAME = "java.vm.name";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_VM_SPECIFICATION_NAME = "java.vm.specification.name";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_VM_SPECIFICATION_VENDOR = "java.vm.specification.vendor";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_VM_SPECIFICATION_VERSION = "java.vm.specification.version";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_VM_VENDOR = "java.vm.vendor";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String JAVA_VM_VERSION = "java.vm.version";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String LINE_SEPARATOR = "line.separator";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String OS_ARCH = "os.arch";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String OS_NAME = "os.name";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String OS_VERSION = "os.version";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String PATH_SEPARATOR = "path.separator";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String USER_COUNTRY = "user.country";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String USER_DIR = "user.dir";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String USER_HOME = "user.home";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String USER_LANGUAGE = "user.language";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String USER_NAME = "user.name";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String USER_REGION = "user.region";
|
||||
|
||||
/**
|
||||
* The System property name {@value}.
|
||||
*/
|
||||
public static final String USER_TIMEZONE = "user.timezone";
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getAwtToolkit() {
|
||||
return getProperty(AWT_TOOLKIT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getFileEncoding() {
|
||||
return getProperty(FILE_ENCODING);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getFileSeparator() {
|
||||
return getProperty(FILE_SEPARATOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaAwtFonts() {
|
||||
return getProperty(JAVA_AWT_FONTS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaAwtGraphicsenv() {
|
||||
return getProperty(JAVA_AWT_GRAPHICSENV);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaAwtHeadless() {
|
||||
return getProperty(JAVA_AWT_HEADLESS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaAwtPrinterjob() {
|
||||
return getProperty(JAVA_AWT_PRINTERJOB);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaClassPath() {
|
||||
return getProperty(JAVA_CLASS_PATH);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaClassVersion() {
|
||||
return getProperty(JAVA_CLASS_VERSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaCompiler() {
|
||||
return getProperty(JAVA_COMPILER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaEndorsedDirs() {
|
||||
return getProperty(JAVA_ENDORSED_DIRS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaExtDirs() {
|
||||
return getProperty(JAVA_EXT_DIRS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaHome() {
|
||||
return getProperty(JAVA_HOME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaIoTmpdir() {
|
||||
return getProperty(JAVA_IO_TMPDIR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaLibraryPath() {
|
||||
return getProperty(JAVA_LIBRARY_PATH);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaRuntimeName() {
|
||||
return getProperty(JAVA_RUNTIME_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaRuntimeVersion() {
|
||||
return getProperty(JAVA_RUNTIME_VERSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaSpecificationName() {
|
||||
return getProperty(JAVA_SPECIFICATION_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaSpecificationVendor() {
|
||||
return getProperty(JAVA_SPECIFICATION_VENDOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaSpecificationVersion() {
|
||||
return getProperty(JAVA_SPECIFICATION_VERSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaUtilPrefsPreferencesFactory() {
|
||||
return getProperty(JAVA_UTIL_PREFS_PREFERENCES_FACTORY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaVendor() {
|
||||
return getProperty(JAVA_VENDOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaVendorUrl() {
|
||||
return getProperty(JAVA_VENDOR_URL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaVersion() {
|
||||
return getProperty(JAVA_VERSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaVmInfo() {
|
||||
return getProperty(JAVA_VM_INFO);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaVmName() {
|
||||
return getProperty(JAVA_VM_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaVmSpecificationName() {
|
||||
return getProperty(JAVA_VM_SPECIFICATION_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaVmSpecificationVendor() {
|
||||
return getProperty(JAVA_VM_SPECIFICATION_VENDOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaVmSpecificationVersion() {
|
||||
return getProperty(JAVA_VM_SPECIFICATION_VERSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaVmVendor() {
|
||||
return getProperty(JAVA_VM_VENDOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getJavaVmVersion() {
|
||||
return getProperty(JAVA_VM_VERSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getLineSeparator() {
|
||||
return getProperty(LINE_SEPARATOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getOsArch() {
|
||||
return getProperty(OS_ARCH);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getOsName() {
|
||||
return getProperty(OS_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getOsVersion() {
|
||||
return getProperty(OS_VERSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getPathSeparator() {
|
||||
return getProperty(PATH_SEPARATOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a System property, defaulting to {@code null} if the property cannot be read.
|
||||
* <p>
|
||||
* If a {@link SecurityException} is caught, the return value is {@code null}.
|
||||
* </p>
|
||||
*
|
||||
* @param property the system property name
|
||||
* @return the system property value or {@code null} if a security problem occurs
|
||||
*/
|
||||
public static String getProperty(final String property) {
|
||||
return getProperty(property, NULL_SUPPLIER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a System property, defaulting to {@code null} if the property cannot be read.
|
||||
* <p>
|
||||
* If a {@link SecurityException} is caught, the return value is {@code null}.
|
||||
* </p>
|
||||
*
|
||||
* @param property the system property name.
|
||||
* @param defaultValue get this Supplier when the property is empty or throws SecurityException.
|
||||
* @return the system property value or {@code null} if a security problem occurs
|
||||
*/
|
||||
static String getProperty(final String property, final Supplier<String> defaultValue) {
|
||||
try {
|
||||
if (StringUtils.isEmpty(property)) {
|
||||
return defaultValue.get();
|
||||
}
|
||||
final String value = System.getProperty(property);
|
||||
return StringUtils.getIfEmpty(value, defaultValue);
|
||||
} catch (final SecurityException ignore) {
|
||||
// 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 defaultValue.get();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getUserCountry() {
|
||||
return getProperty(USER_COUNTRY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getUserDir() {
|
||||
return getProperty(USER_DIR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getUserHome() {
|
||||
return getProperty(USER_HOME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getUserLanguage() {
|
||||
return getProperty(USER_LANGUAGE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getUserName() {
|
||||
return getProperty(USER_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value from the system properties map.
|
||||
* <p>
|
||||
* Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
|
||||
* </p>
|
||||
*
|
||||
* @return the current value from the system properties map.
|
||||
*/
|
||||
public static String getUserTimezone() {
|
||||
return getProperty(USER_TIMEZONE);
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -18,6 +18,8 @@ package org.apache.commons.lang3.text;
|
|||
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.lang3.SystemProperties;
|
||||
|
||||
/**
|
||||
* Lookup a String key to a String value.
|
||||
* <p>
|
||||
|
@ -179,14 +181,7 @@ public abstract class StrLookup<V> {
|
|||
*/
|
||||
@Override
|
||||
public String lookup(final String key) {
|
||||
if (!key.isEmpty()) {
|
||||
try {
|
||||
return System.getProperty(key);
|
||||
} catch (final SecurityException ignored) {
|
||||
// ignored, all lookup(String) will return null.
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return SystemProperties.getProperty(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,254 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 org.apache.commons.lang3;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SystemPropertiesTest {
|
||||
|
||||
private boolean isJava11OrGreater() {
|
||||
return SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_11);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAwtToolkit() {
|
||||
assertDoesNotThrow(SystemProperties::getAwtToolkit);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFileEncoding() {
|
||||
assertNotNull(SystemProperties.getFileEncoding());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFileSeparator() {
|
||||
assertNotNull(SystemProperties.getFileSeparator());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaAwtFonts() {
|
||||
assertNull(SystemProperties.getJavaAwtFonts());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaAwtGraphicsenv() {
|
||||
assertDoesNotThrow(SystemProperties::getJavaAwtGraphicsenv);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaAwtHeadless() {
|
||||
assertNull(SystemProperties.getJavaAwtHeadless());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaAwtPrinterjob() {
|
||||
assertDoesNotThrow(SystemProperties::getJavaAwtPrinterjob);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaClassPath() {
|
||||
assertNotNull(SystemProperties.getJavaClassPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaClassVersion() {
|
||||
assertNotNull(SystemProperties.getJavaClassVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaCompiler() {
|
||||
if (SystemUtils.IS_JAVA_14) {
|
||||
// Not in Java 11
|
||||
assertNotNull(SystemProperties.getJavaCompiler());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaEndorsedDirs() {
|
||||
if (isJava11OrGreater()) {
|
||||
// Not in Java 11
|
||||
assertNull(SystemProperties.getJavaEndorsedDirs());
|
||||
} else {
|
||||
assertNotNull(SystemProperties.getJavaExtDirs());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaExtDirs() {
|
||||
if (isJava11OrGreater()) {
|
||||
// Not in Java 11
|
||||
assertNull(SystemProperties.getJavaExtDirs());
|
||||
} else {
|
||||
assertNotNull(SystemProperties.getJavaExtDirs());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaHome() {
|
||||
assertNotNull(SystemProperties.getJavaHome());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaIoTmpdir() {
|
||||
assertNotNull(SystemProperties.getJavaIoTmpdir());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaLibraryPath() {
|
||||
assertNotNull(SystemProperties.getJavaLibraryPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaRuntimeName() {
|
||||
assertNotNull(SystemProperties.getJavaRuntimeName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaRuntimeVersion() {
|
||||
assertNotNull(SystemProperties.getJavaRuntimeVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaSpecificationName() {
|
||||
assertNotNull(SystemProperties.getJavaSpecificationName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaSpecificationVendor() {
|
||||
assertNotNull(SystemProperties.getJavaSpecificationVendor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaSpecificationVersion() {
|
||||
assertNotNull(SystemProperties.getJavaSpecificationVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaUtilPrefsPreferencesFactory() {
|
||||
assertNull(SystemProperties.getJavaUtilPrefsPreferencesFactory());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaVendor() {
|
||||
assertNotNull(SystemProperties.getJavaVendor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaVendorUrl() {
|
||||
assertNotNull(SystemProperties.getJavaVendorUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaVersion() {
|
||||
assertNotNull(SystemProperties.getJavaVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaVmInfo() {
|
||||
assertNotNull(SystemProperties.getJavaVmInfo());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaVmName() {
|
||||
assertNotNull(SystemProperties.getJavaVmName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaVmSpecificationName() {
|
||||
assertNotNull(SystemProperties.getJavaVmSpecificationName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaVmSpecificationVendor() {
|
||||
assertNotNull(SystemProperties.getJavaVmSpecificationVendor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaVmSpecificationVersion() {
|
||||
assertNotNull(SystemProperties.getJavaVmSpecificationVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaVmVendor() {
|
||||
assertNotNull(SystemProperties.getJavaVmVendor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetJavaVmVersion() {
|
||||
assertNotNull(SystemProperties.getJavaVmVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetLineSeparator() {
|
||||
assertNotNull(SystemProperties.getLineSeparator());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetOsArch() {
|
||||
assertNotNull(SystemProperties.getOsArch());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetOsName() {
|
||||
assertNotNull(SystemProperties.getOsName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetOsVersion() {
|
||||
assertNotNull(SystemProperties.getOsVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPathSeparator() {
|
||||
assertNotNull(SystemProperties.getPathSeparator());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUserCountry() {
|
||||
assertNotNull(SystemProperties.getUserCountry());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUserDir() {
|
||||
assertNotNull(SystemProperties.getUserDir());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUserHome() {
|
||||
assertNotNull(SystemProperties.getUserHome());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUserLanguage() {
|
||||
assertNotNull(SystemProperties.getUserLanguage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUserName() {
|
||||
assertNotNull(SystemProperties.getUserName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUserTimezone() {
|
||||
assertDoesNotThrow(SystemProperties::getUserTimezone);
|
||||
}
|
||||
|
||||
}
|
|
@ -19,7 +19,6 @@ package org.apache.commons.lang3.text;
|
|||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
@ -46,7 +45,7 @@ public class StrLookupTest extends AbstractLangTest {
|
|||
assertEquals(System.getProperty("os.name"), StrLookup.systemPropertiesLookup().lookup("os.name"));
|
||||
assertNull(StrLookup.systemPropertiesLookup().lookup(""));
|
||||
assertNull(StrLookup.systemPropertiesLookup().lookup("other"));
|
||||
assertThrows(NullPointerException.class, () -> StrLookup.systemPropertiesLookup().lookup(null));
|
||||
assertNull(StrLookup.systemPropertiesLookup().lookup(null));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue