1 /*
2  * Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package sun.net;
26 
27 import java.io.*;
28 import java.security.AccessController;
29 import java.security.PrivilegedAction;
30 import java.util.Properties;
31 
32 /*
33  * This class allows for centralized access to Networking properties.
34  * Default values are loaded from the file jre/lib/net.properties
35  *
36  *
37  * @author Jean-Christophe Collet
38  *
39  */
40 
41 public class NetProperties {
42     static private Properties props = new Properties();
43     static {
AccessController.doPrivileged( new PrivilegedAction<Void>() { public Void run() { loadDefaultProperties(); return null; }})44         AccessController.doPrivileged(
45             new PrivilegedAction<Void>() {
46                 public Void run() {
47                     loadDefaultProperties();
48                     return null;
49                 }});
50     }
51 
NetProperties()52     private NetProperties() { };
53 
54 
55     /*
56      * Loads the default networking system properties
57      * the file is in jre/lib/net.properties
58      */
loadDefaultProperties()59     static private void loadDefaultProperties() {
60         String fname = System.getProperty("java.home");
61         if (fname == null) {
62             throw new Error("Can't find java.home ??");
63         }
64         try {
65             File f = new File(fname, "lib");
66             f = new File(f, "net.properties");
67             fname = f.getCanonicalPath();
68             InputStream in = new FileInputStream(fname);
69             BufferedInputStream bin = new BufferedInputStream(in);
70             props.load(bin);
71             bin.close();
72         } catch (Exception e) {
73             // Do nothing. We couldn't find or access the file
74             // so we won't have default properties...
75         }
76     }
77 
78     /**
79      * Get a networking system property. If no system property was defined
80      * returns the default value, if it exists, otherwise returns
81      * <code>null</code>.
82      * @param      key  the property name.
83      * @throws  SecurityException  if a security manager exists and its
84      *          <code>checkPropertiesAccess</code> method doesn't allow access
85      *          to the system properties.
86      * @return the <code>String</code> value for the property,
87      *         or <code>null</code>
88      */
get(String key)89     static public String get(String key) {
90         String def = props.getProperty(key);
91         try {
92             return System.getProperty(key, def);
93         } catch (IllegalArgumentException e) {
94         } catch (NullPointerException e) {
95         }
96         return null;
97     }
98 
99     /**
100      * Get an Integer networking system property. If no system property was
101      * defined returns the default value, if it exists, otherwise returns
102      * <code>null</code>.
103      * @param   key     the property name.
104      * @param   defval  the default value to use if the property is not found
105      * @throws  SecurityException  if a security manager exists and its
106      *          <code>checkPropertiesAccess</code> method doesn't allow access
107      *          to the system properties.
108      * @return the <code>Integer</code> value for the property,
109      *         or <code>null</code>
110      */
getInteger(String key, int defval)111     static public Integer getInteger(String key, int defval) {
112         String val = null;
113 
114         try {
115             val = System.getProperty(key, props.getProperty(key));
116         } catch (IllegalArgumentException e) {
117         } catch (NullPointerException e) {
118         }
119 
120         if (val != null) {
121             try {
122                 return Integer.decode(val);
123             } catch (NumberFormatException ex) {
124             }
125         }
126         return new Integer(defval);
127     }
128 
129     /**
130      * Get a Boolean networking system property. If no system property was
131      * defined returns the default value, if it exists, otherwise returns
132      * <code>null</code>.
133      * @param   key     the property name.
134      * @throws  SecurityException  if a security manager exists and its
135      *          <code>checkPropertiesAccess</code> method doesn't allow access
136      *          to the system properties.
137      * @return the <code>Boolean</code> value for the property,
138      *         or <code>null</code>
139      */
getBoolean(String key)140     static public Boolean getBoolean(String key) {
141         String val = null;
142 
143         try {
144             val = System.getProperty(key, props.getProperty(key));
145         } catch (IllegalArgumentException e) {
146         } catch (NullPointerException e) {
147         }
148 
149         if (val != null) {
150             try {
151                 return Boolean.valueOf(val);
152             } catch (NumberFormatException ex) {
153             }
154         }
155         return null;
156     }
157 
158 }
159