Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/net/NetProperties.java
38829 views
/*1* Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.net;2526import java.io.*;27import java.security.AccessController;28import java.security.PrivilegedAction;29import java.util.Properties;3031/*32* This class allows for centralized access to Networking properties.33* Default values are loaded from the file jre/lib/net.properties34*35*36* @author Jean-Christophe Collet37*38*/3940public class NetProperties {41static private Properties props = new Properties();42static {43AccessController.doPrivileged(44new PrivilegedAction<Void>() {45public Void run() {46loadDefaultProperties();47return null;48}});49}5051private NetProperties() { };525354/*55* Loads the default networking system properties56* the file is in jre/lib/net.properties57*/58static private void loadDefaultProperties() {59String fname = System.getProperty("java.home");60if (fname == null) {61throw new Error("Can't find java.home ??");62}63try {64File f = new File(fname, "lib");65f = new File(f, "net.properties");66fname = f.getCanonicalPath();67InputStream in = new FileInputStream(fname);68BufferedInputStream bin = new BufferedInputStream(in);69props.load(bin);70bin.close();71} catch (Exception e) {72// Do nothing. We couldn't find or access the file73// so we won't have default properties...74}75}7677/**78* Get a networking system property. If no system property was defined79* returns the default value, if it exists, otherwise returns80* <code>null</code>.81* @param key the property name.82* @throws SecurityException if a security manager exists and its83* <code>checkPropertiesAccess</code> method doesn't allow access84* to the system properties.85* @return the <code>String</code> value for the property,86* or <code>null</code>87*/88static public String get(String key) {89String def = props.getProperty(key);90try {91return System.getProperty(key, def);92} catch (IllegalArgumentException e) {93} catch (NullPointerException e) {94}95return null;96}9798/**99* Get an Integer networking system property. If no system property was100* defined returns the default value, if it exists, otherwise returns101* <code>null</code>.102* @param key the property name.103* @param defval the default value to use if the property is not found104* @throws SecurityException if a security manager exists and its105* <code>checkPropertiesAccess</code> method doesn't allow access106* to the system properties.107* @return the <code>Integer</code> value for the property,108* or <code>null</code>109*/110static public Integer getInteger(String key, int defval) {111String val = null;112113try {114val = System.getProperty(key, props.getProperty(key));115} catch (IllegalArgumentException e) {116} catch (NullPointerException e) {117}118119if (val != null) {120try {121return Integer.decode(val);122} catch (NumberFormatException ex) {123}124}125return new Integer(defval);126}127128/**129* Get a Boolean networking system property. If no system property was130* defined returns the default value, if it exists, otherwise returns131* <code>null</code>.132* @param key the property name.133* @throws SecurityException if a security manager exists and its134* <code>checkPropertiesAccess</code> method doesn't allow access135* to the system properties.136* @return the <code>Boolean</code> value for the property,137* or <code>null</code>138*/139static public Boolean getBoolean(String key) {140String val = null;141142try {143val = System.getProperty(key, props.getProperty(key));144} catch (IllegalArgumentException e) {145} catch (NullPointerException e) {146}147148if (val != null) {149try {150return Boolean.valueOf(val);151} catch (NumberFormatException ex) {152}153}154return null;155}156157}158159160