001: /* jcifs smb client library in Java
002: * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package jcifs;
020:
021: import java.util.Properties;
022: import java.io.*;
023: import java.net.InetAddress;
024: import java.net.UnknownHostException;
025: import java.util.StringTokenizer;
026: import jcifs.util.LogStream;
027:
028: /**
029: * This class uses a static {@link java.util.Properties} to act
030: * as a cental repository for all jCIFS configuration properties. It cannot be
031: * instantiated. Similar to <code>System</code> properties the namespace
032: * is global therefore property names should be unique. Before use,
033: * the <code>load</code> method should be called with the name of a
034: * <code>Properties</code> file (or <code>null</code> indicating no
035: * file) to initialize the <code>Config</code>. The <code>System</code>
036: * properties will then populate the <code>Config</code> as well potentially
037: * overwriting properties from the file. Thus properties provided on the
038: * commandline with the <code>-Dproperty.name=value</code> VM parameter
039: * will override properties from the configuration file.
040: * <p>
041: * There are several ways to set jCIFS properties. See
042: * the <a href="../overview-summary.html#scp">overview page of the API
043: * documentation</a> for details.
044: */
045:
046: public class Config {
047:
048: /**
049: * The static <code>Properties</code>.
050: */
051:
052: private static Properties prp = new Properties();
053: private static LogStream log;
054:
055: static {
056: String filename;
057: int level;
058: FileInputStream in = null;
059:
060: log = LogStream.getInstance();
061:
062: try {
063: filename = System.getProperty("jcifs.properties");
064: if (filename != null && filename.length() > 1) {
065: in = new FileInputStream(filename);
066: }
067: Config.load(in);
068: } catch (IOException ioe) {
069: if (log.level > 0)
070: ioe.printStackTrace(log);
071: }
072:
073: if ((level = Config.getInt("jcifs.util.loglevel", -1)) != -1) {
074: LogStream.setLevel(level);
075: }
076:
077: if (log.level > 2) {
078: try {
079: prp.store(log, "JCIFS PROPERTIES");
080: } catch (IOException ioe) {
081: }
082: }
083: }
084:
085: /**
086: * This static method registers the SMB URL protocol handler which is
087: * required to use SMB URLs with the <tt>java.net.URL</tt> class. If this
088: * method is not called before attempting to create an SMB URL with the
089: * URL class the following exception will occur:
090: * <blockquote><pre>
091: * Exception MalformedURLException: unknown protocol: smb
092: * at java.net.URL.<init>(URL.java:480)
093: * at java.net.URL.<init>(URL.java:376)
094: * at java.net.URL.<init>(URL.java:330)
095: * at jcifs.smb.SmbFile.<init>(SmbFile.java:355)
096: * ...
097: * </pre><blockquote>
098: */
099:
100: public static void registerSmbURLHandler() {
101: String ver, pkgs;
102:
103: ver = System.getProperty("java.version");
104: if (ver.startsWith("1.1.") || ver.startsWith("1.2.")) {
105: throw new RuntimeException(
106: "jcifs-0.7.0b4+ requires Java 1.3 or above. You are running "
107: + ver);
108: }
109: pkgs = System.getProperty("java.protocol.handler.pkgs");
110: if (pkgs == null) {
111: System.setProperty("java.protocol.handler.pkgs", "jcifs");
112: } else if (pkgs.indexOf("jcifs") == -1) {
113: pkgs += "|jcifs";
114: System.setProperty("java.protocol.handler.pkgs", pkgs);
115: }
116: }
117:
118: // supress javadoc constructor summary by removing 'protected'
119: Config() {
120: }
121:
122: /**
123: * Set the default properties of the static Properties used by <tt>Config</tt>. This permits
124: * a different Properties object/file to be used as the source of properties for
125: * use by the jCIFS library. The Properties must be set <i>before jCIFS
126: * classes are accessed</i> as most jCIFS classes load properties statically once.
127: * Using this method will also override properties loaded
128: * using the <tt>-Djcifs.properties=</tt> commandline parameter.
129: */
130:
131: public static void setProperties(Properties prp) {
132: Config.prp = new Properties(prp);
133: try {
134: Config.prp.putAll(System.getProperties());
135: } catch (SecurityException se) {
136: if (log.level > 1)
137: log
138: .println("SecurityException: jcifs will ignore System properties");
139: }
140: }
141:
142: /**
143: * Load the <code>Config</code> with properties from the stream
144: * <code>in</code> from a <code>Properties</code> file.
145: */
146:
147: public static void load(InputStream in) throws IOException {
148: if (in != null) {
149: prp.load(in);
150: }
151: try {
152: prp.putAll(System.getProperties());
153: } catch (SecurityException se) {
154: if (log.level > 1)
155: log
156: .println("SecurityException: jcifs will ignore System properties");
157: }
158: }
159:
160: public static void store(OutputStream out, String header)
161: throws IOException {
162: prp.store(out, header);
163: }
164:
165: /**
166: * List the properties in the <code>Code</code>.
167: */
168:
169: public static void list(PrintStream out) throws IOException {
170: prp.list(out);
171: }
172:
173: /**
174: * Add a property.
175: */
176:
177: public static Object setProperty(String key, String value) {
178: return prp.setProperty(key, value);
179: }
180:
181: /**
182: * Retrieve a property as an <code>Object</code>.
183: */
184:
185: public static Object get(String key) {
186: return prp.get(key);
187: }
188:
189: /**
190: * Retrieve a <code>String</code>. If the key cannot be found,
191: * the provided <code>def</code> default parameter will be returned.
192: */
193:
194: public static String getProperty(String key, String def) {
195: return prp.getProperty(key, def);
196: }
197:
198: /**
199: * Retrieve a <code>String</code>. If the property is not found, <code>null</code> is returned.
200: */
201:
202: public static String getProperty(String key) {
203: return prp.getProperty(key);
204: }
205:
206: /**
207: * Retrieve an <code>int</code>. If the key does not exist or
208: * cannot be converted to an <code>int</code>, the provided default
209: * argument will be returned.
210: */
211:
212: public static int getInt(String key, int def) {
213: String s = prp.getProperty(key);
214: if (s != null) {
215: try {
216: def = Integer.parseInt(s);
217: } catch (NumberFormatException nfe) {
218: if (log.level > 0)
219: nfe.printStackTrace(log);
220: }
221: }
222: return def;
223: }
224:
225: /**
226: * Retrieve an <code>int</code>. If the property is not found, <code>-1</code> is returned.
227: */
228:
229: public static int getInt(String key) {
230: String s = prp.getProperty(key);
231: int result = -1;
232: if (s != null) {
233: try {
234: result = Integer.parseInt(s);
235: } catch (NumberFormatException nfe) {
236: if (log.level > 0)
237: nfe.printStackTrace(log);
238: }
239: }
240: return result;
241: }
242:
243: /**
244: * Retrieve a <code>long</code>. If the key does not exist or
245: * cannot be converted to a <code>long</code>, the provided default
246: * argument will be returned.
247: */
248:
249: public static long getLong(String key, long def) {
250: String s = prp.getProperty(key);
251: if (s != null) {
252: try {
253: def = Long.parseLong(s);
254: } catch (NumberFormatException nfe) {
255: if (log.level > 0)
256: nfe.printStackTrace(log);
257: }
258: }
259: return def;
260: }
261:
262: /**
263: * Retrieve an <code>InetAddress</code>. If the address is not
264: * an IP address and cannot be resolved <code>null</code> will
265: * be returned.
266: */
267:
268: public static InetAddress getInetAddress(String key, InetAddress def) {
269: String addr = prp.getProperty(key);
270: if (addr != null) {
271: try {
272: def = InetAddress.getByName(addr);
273: } catch (UnknownHostException uhe) {
274: if (log.level > 0) {
275: log.println(addr);
276: uhe.printStackTrace(log);
277: }
278: }
279: }
280: return def;
281: }
282:
283: /**
284: * Retrieve a boolean value. If the property is not found, the value of <code>def</code> is returned.
285: */
286:
287: public static boolean getBoolean(String key, boolean def) {
288: String b = getProperty(key);
289: if (b != null) {
290: def = b.toLowerCase().equals("true");
291: }
292: return def;
293: }
294:
295: /**
296: * Retrieve an array of <tt>InetAddress</tt> created from a property
297: * value containting a <tt>delim</tt> separated list of hostnames and/or
298: * ipaddresses.
299: */
300:
301: public static InetAddress[] getInetAddressArray(String key,
302: String delim, InetAddress[] def) {
303: String p = getProperty(key);
304: if (p != null) {
305: StringTokenizer tok = new StringTokenizer(p, delim);
306: int len = tok.countTokens();
307: InetAddress[] arr = new InetAddress[len];
308: for (int i = 0; i < len; i++) {
309: String addr = tok.nextToken();
310: try {
311: arr[i] = InetAddress.getByName(addr);
312: } catch (UnknownHostException uhe) {
313: if (log.level > 0) {
314: log.println(addr);
315: uhe.printStackTrace(log);
316: }
317: return def;
318: }
319: }
320: return arr;
321: }
322: return def;
323: }
324: }
|