Path: blob/master/src/java.base/share/classes/sun/net/NetProperties.java
41152 views
/*1* Copyright (c) 2004, 2021, 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 jdk.internal.util.StaticProperty;2728import java.io.*;29import java.security.AccessController;30import java.security.PrivilegedAction;31import java.util.Properties;3233/*34* This class allows for centralized access to Networking properties.35* Default values are loaded from the file jre/lib/net.properties36*37*38* @author Jean-Christophe Collet39*40*/4142@SuppressWarnings("removal")43public class NetProperties {44private static Properties props = new Properties();45static {46AccessController.doPrivileged(47new PrivilegedAction<Void>() {48public Void run() {49loadDefaultProperties();50return null;51}});52}5354private NetProperties() { };555657/*58* Loads the default networking system properties59* the file is in jre/lib/net.properties60*/61private static void loadDefaultProperties() {62String fname = StaticProperty.javaHome();63if (fname == null) {64throw new Error("Can't find java.home ??");65}66try {67File f = new File(fname, "conf");68f = new File(f, "net.properties");69fname = f.getCanonicalPath();70InputStream in = new FileInputStream(fname);71BufferedInputStream bin = new BufferedInputStream(in);72props.load(bin);73bin.close();74} catch (Exception e) {75// Do nothing. We couldn't find or access the file76// so we won't have default properties...77}78}7980/**81* Get a networking system property. If no system property was defined82* returns the default value, if it exists, otherwise returns83* <code>null</code>.84* @param key the property name.85* @throws SecurityException if a security manager exists and its86* <code>checkPropertiesAccess</code> method doesn't allow access87* to the system properties.88* @return the <code>String</code> value for the property,89* or <code>null</code>90*/91public static String get(String key) {92String def = props.getProperty(key);93try {94return System.getProperty(key, def);95} catch (IllegalArgumentException e) {96} catch (NullPointerException e) {97}98return null;99}100101/**102* Get an Integer networking system property. If no system property was103* defined returns the default value, if it exists, otherwise returns104* <code>null</code>.105* @param key the property name.106* @param defval the default value to use if the property is not found107* @throws SecurityException if a security manager exists and its108* <code>checkPropertiesAccess</code> method doesn't allow access109* to the system properties.110* @return the <code>Integer</code> value for the property,111* or <code>null</code>112*/113public static Integer getInteger(String key, int defval) {114String val = null;115116try {117val = System.getProperty(key, props.getProperty(key));118} catch (IllegalArgumentException e) {119} catch (NullPointerException e) {120}121122if (val != null) {123try {124return Integer.decode(val);125} catch (NumberFormatException ex) {126}127}128return defval;129}130131/**132* Get a Boolean networking system property. If no system property was133* defined returns the default value, if it exists, otherwise returns134* <code>null</code>.135* @param key the property name.136* @throws SecurityException if a security manager exists and its137* <code>checkPropertiesAccess</code> method doesn't allow access138* to the system properties.139* @return the <code>Boolean</code> value for the property,140* or <code>null</code>141*/142public static Boolean getBoolean(String key) {143String val = null;144145try {146val = System.getProperty(key, props.getProperty(key));147} catch (IllegalArgumentException e) {148} catch (NullPointerException e) {149}150151if (val != null) {152try {153return Boolean.valueOf(val);154} catch (NumberFormatException ex) {155}156}157return null;158}159160}161162163