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 com.knowgate.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:
027: import com.knowgate.debug.ErrorHandler;
028:
029: /**
030: * This class uses a static {@link java.util.Properties} to act
031: * as a cental repository for all jCIFS configuration properties. It cannot be
032: * instantiated. Similar to <code>System</code> properties the namespace
033: * is global therefore property names should be unique. Before use,
034: * the <code>load</code> method should be called with the name of a
035: * <code>Properties</code> file (or <code>null</code> indicating no
036: * file) to initialize the <code>Config</code>. The <code>System</code>
037: * properties will then populate the <code>Config</code> as well potentially
038: * overwriting properties from the file. Thus properties provided on the
039: * commandline with the <code>-Dproperty.name=value</code> VM parameter
040: * will override properties from the configuration file.
041: * <p>
042: * There are several ways to set jCIFS properties. See
043: * the <a href="../overview-summary.html#scp">overview page of the API
044: * documentation</a> for details.
045: */
046:
047: public class Config {
048:
049: /**
050: * The static <code>Properties</code>.
051: */
052:
053: private static Properties prp = new Properties();
054:
055: static {
056: String filename;
057: int level;
058: FileInputStream in = null;
059:
060: try {
061: filename = System.getProperty("jcifs.properties");
062: if (filename != null && filename.length() > 1) {
063: in = new FileInputStream(filename);
064: }
065: Config.load(in);
066: } catch (IOException ioe) {
067: new ErrorHandler(ioe);
068: }
069: }
070:
071: /**
072: * This static method registers the SMB URL protocol handler which is
073: * required to use SMB URLs with the <tt>java.net.URL</tt> class. If this
074: * method is not called before attempting to create an SMB URL with the
075: * URL class the following exception will occur:
076: * <blockquote><pre>
077: * Exception MalformedURLException: unknown protocol: smb
078: * at java.net.URL.<init>(URL.java:480)
079: * at java.net.URL.<init>(URL.java:376)
080: * at java.net.URL.<init>(URL.java:330)
081: * at jcifs.smb.SmbFile.<init>(SmbFile.java:355)
082: * ...
083: * </pre><blockquote>
084: */
085:
086: public static void registerSmbURLHandler() {
087: String ver, pkgs;
088:
089: ver = System.getProperty("java.version");
090: if (ver.startsWith("1.1.") || ver.startsWith("1.2.")) {
091: throw new RuntimeException(
092: "jcifs-0.7.0b4+ requires Java 1.3 or above. You are running "
093: + ver);
094: }
095: pkgs = System.getProperty("java.protocol.handler.pkgs");
096: if (pkgs == null) {
097: pkgs = "jcifs";
098: } else {
099: pkgs += "|jcifs";
100: }
101: System.setProperty("java.protocol.handler.pkgs", pkgs);
102: }
103:
104: // supress javadoc constructor summary by removing 'protected'
105: Config() {
106: }
107:
108: /**
109: * Set the default properties of the static Properties used by <tt>Config</tt>. This permits
110: * a different Properties object/file to be used as the source of properties for
111: * use by the jCIFS library. The Properties must be set <i>before jCIFS
112: * classes are accessed</i> as most jCIFS classes load properties statically once.
113: * Using this method will also override properties loaded
114: * using the <tt>-Djcifs.properties=</tt> commandline parameter.
115: */
116:
117: public static void setProperties(Properties prp) {
118: Config.prp = new Properties(prp);
119: Config.prp.putAll(System.getProperties());
120: }
121:
122: /**
123: * Load the <code>Config</code> with properties from the stream
124: * <code>in</code> from a <code>Properties</code> file.
125: */
126:
127: public static void load(InputStream in) throws IOException {
128: if (in != null) {
129: prp.load(in);
130: }
131: prp.putAll(System.getProperties());
132: }
133:
134: public static void store(OutputStream out, String header)
135: throws IOException {
136: prp.store(out, header);
137: }
138:
139: /**
140: * List the properties in the <code>Code</code>.
141: */
142:
143: public static void list(PrintStream out) throws IOException {
144: prp.list(out);
145: }
146:
147: /**
148: * Add a property.
149: */
150:
151: public static Object setProperty(String key, String value) {
152: return prp.setProperty(key, value);
153: }
154:
155: /**
156: * Retrieve a property as an <code>Object</code>.
157: */
158:
159: public static Object get(String key) {
160: return prp.get(key);
161: }
162:
163: /**
164: * Retrieve a <code>String</code>. If the key cannot be found,
165: * the provided <code>def</code> default parameter will be returned.
166: */
167:
168: public static String getProperty(String key, String def) {
169: return prp.getProperty(key, def);
170: }
171:
172: /**
173: * Retrieve a <code>String</code>. If the property is not found, <code>null</code> is returned.
174: */
175:
176: public static String getProperty(String key) {
177: return prp.getProperty(key);
178: }
179:
180: /**
181: * Retrieve an <code>int</code>. If the key does not exist or
182: * cannot be converted to an <code>int</code>, the provided default
183: * argument will be returned.
184: */
185:
186: public static int getInt(String key, int def) {
187: String s = prp.getProperty(key);
188: if (s != null) {
189: try {
190: def = Integer.parseInt(s);
191: } catch (NumberFormatException nfe) {
192: new ErrorHandler(nfe);
193: }
194: }
195: return def;
196: }
197:
198: /**
199: * Retrieve an <code>int</code>. If the property is not found, <code>-1</code> is returned.
200: */
201:
202: public static int getInt(String key) {
203: String s = prp.getProperty(key);
204: int result = -1;
205: if (s != null) {
206: try {
207: result = Integer.parseInt(s);
208: } catch (NumberFormatException nfe) {
209: new ErrorHandler(nfe);
210: }
211: }
212: return result;
213: }
214:
215: /**
216: * Retrieve a <code>long</code>. If the key does not exist or
217: * cannot be converted to a <code>long</code>, the provided default
218: * argument will be returned.
219: */
220:
221: public static long getLong(String key, long def) {
222: String s = prp.getProperty(key);
223: if (s != null) {
224: try {
225: def = Long.parseLong(s);
226: } catch (NumberFormatException nfe) {
227: new ErrorHandler(nfe);
228: }
229: }
230: return def;
231: }
232:
233: /**
234: * Retrieve an <code>InetAddress</code>. If the address is not
235: * an IP address and cannot be resolved <code>null</code> will
236: * be returned.
237: */
238:
239: public static InetAddress getInetAddress(String key, InetAddress def) {
240: String addr = prp.getProperty(key);
241: if (addr != null) {
242: try {
243: def = InetAddress.getByName(addr);
244: } catch (UnknownHostException uhe) {
245: new ErrorHandler(uhe);
246: }
247: }
248: return def;
249: }
250:
251: /**
252: * Retrieve a boolean value. If the property is not found, the value of <code>def</code> is returned.
253: */
254:
255: public static boolean getBoolean(String key, boolean def) {
256: String b = getProperty(key);
257: if (b != null) {
258: def = b.toLowerCase().equals("true");
259: }
260: return def;
261: }
262:
263: /**
264: * Retrieve an array of <tt>InetAddress</tt> created from a property
265: * value containting a <tt>delim</tt> separated list of hostnames and/or
266: * ipaddresses.
267: */
268:
269: public static InetAddress[] getInetAddressArray(String key,
270: String delim, InetAddress[] def) {
271: String p = getProperty(key);
272: if (p != null) {
273: StringTokenizer tok = new StringTokenizer(p, delim);
274: int len = tok.countTokens();
275: InetAddress[] arr = new InetAddress[len];
276: for (int i = 0; i < len; i++) {
277: String addr = tok.nextToken();
278: try {
279: arr[i] = InetAddress.getByName(addr);
280: } catch (UnknownHostException uhe) {
281: new ErrorHandler(uhe);
282: return def;
283: }
284: }
285: return arr;
286: }
287: return def;
288: }
289: }
|