001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package sun.net;
026:
027: import java.io.*;
028: import java.security.AccessController;
029: import java.security.PrivilegedAction;
030: import java.util.Properties;
031:
032: /*
033: * This class allows for centralized access to Networking properties.
034: * Default values are loaded from the file jre/lib/net.properties
035: *
036: * @version 1.8, 05/05/07
037: *
038: * @author Jean-Christophe Collet
039: *
040: */
041:
042: public class NetProperties {
043: static private Properties props = new Properties();
044: static {
045: AccessController.doPrivileged(new PrivilegedAction() {
046: public Object run() {
047: loadDefaultProperties();
048: return null;
049: }
050: });
051: }
052:
053: private NetProperties() {
054: };
055:
056: /*
057: * Loads the default networking system properties
058: * the file is in jre/lib/net.properties
059: */
060: static private void loadDefaultProperties() {
061: String fname = System.getProperty("java.home");
062: if (fname == null) {
063: throw new Error("Can't find java.home ??");
064: }
065: try {
066: File f = new File(fname, "lib");
067: f = new File(f, "net.properties");
068: fname = f.getCanonicalPath();
069: InputStream in = new FileInputStream(fname);
070: BufferedInputStream bin = new BufferedInputStream(in);
071: props.load(bin);
072: bin.close();
073: } catch (Exception e) {
074: // Do nothing. We couldn't find or access the file
075: // so we won't have default properties...
076: }
077: }
078:
079: /**
080: * Get a networking system property. If no system property was defined
081: * returns the default value, if it exists, otherwise returns
082: * <code>null</code>.
083: * @param key the property name.
084: * @throws SecurityException if a security manager exists and its
085: * <code>checkPropertiesAccess</code> method doesn't allow access
086: * to the system properties.
087: * @return the <code>String</code> value for the property,
088: * or <code>null</code>
089: */
090: static public String get(String key) {
091: String def = props.getProperty(key);
092: try {
093: return System.getProperty(key, def);
094: } catch (IllegalArgumentException e) {
095: } catch (NullPointerException e) {
096: }
097: return null;
098: }
099:
100: /**
101: * Get an Integer networking system property. If no system property was
102: * defined returns the default value, if it exists, otherwise returns
103: * <code>null</code>.
104: * @param key the property name.
105: * @param defval the default value to use if the property is not found
106: * @throws SecurityException if a security manager exists and its
107: * <code>checkPropertiesAccess</code> method doesn't allow access
108: * to the system properties.
109: * @return the <code>Integer</code> value for the property,
110: * or <code>null</code>
111: */
112: static public Integer getInteger(String key, int defval) {
113: String val = null;
114:
115: try {
116: val = System.getProperty(key, props.getProperty(key));
117: } catch (IllegalArgumentException e) {
118: } catch (NullPointerException e) {
119: }
120:
121: if (val != null) {
122: try {
123: return Integer.decode(val);
124: } catch (NumberFormatException ex) {
125: }
126: }
127: return new Integer(defval);
128: }
129:
130: /**
131: * Get a Boolean networking system property. If no system property was
132: * defined returns the default value, if it exists, otherwise returns
133: * <code>null</code>.
134: * @param key the property name.
135: * @throws SecurityException if a security manager exists and its
136: * <code>checkPropertiesAccess</code> method doesn't allow access
137: * to the system properties.
138: * @return the <code>Boolean</code> value for the property,
139: * or <code>null</code>
140: */
141: static public Boolean getBoolean(String key) {
142: String val = null;
143:
144: try {
145: val = System.getProperty(key, props.getProperty(key));
146: } catch (IllegalArgumentException e) {
147: } catch (NullPointerException e) {
148: }
149:
150: if (val != null) {
151: try {
152: return Boolean.valueOf(val);
153: } catch (NumberFormatException ex) {
154: }
155: }
156: return null;
157: }
158:
159: }
|