KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > jcifs > Config


1 /* jcifs smb client library in Java
2  * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package com.knowgate.jcifs;
20
21 import java.util.Properties JavaDoc;
22 import java.io.*;
23 import java.net.InetAddress JavaDoc;
24 import java.net.UnknownHostException JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 import com.knowgate.debug.ErrorHandler;
28
29 /**
30  * This class uses a static {@link java.util.Properties} to act
31  * as a cental repository for all jCIFS configuration properties. It cannot be
32  * instantiated. Similar to <code>System</code> properties the namespace
33  * is global therefore property names should be unique. Before use,
34  * the <code>load</code> method should be called with the name of a
35  * <code>Properties</code> file (or <code>null</code> indicating no
36  * file) to initialize the <code>Config</code>. The <code>System</code>
37  * properties will then populate the <code>Config</code> as well potentially
38  * overwriting properties from the file. Thus properties provided on the
39  * commandline with the <code>-Dproperty.name=value</code> VM parameter
40  * will override properties from the configuration file.
41  * <p>
42  * There are several ways to set jCIFS properties. See
43  * the <a HREF="../overview-summary.html#scp">overview page of the API
44  * documentation</a> for details.
45  */

46
47 public class Config {
48
49     /**
50      * The static <code>Properties</code>.
51      */

52
53     private static Properties JavaDoc prp = new Properties JavaDoc();
54
55     static {
56         String JavaDoc filename;
57         int level;
58         FileInputStream in = null;
59
60         try {
61             filename = System.getProperty( "jcifs.properties" );
62             if( filename != null && filename.length() > 1 ) {
63                 in = new FileInputStream( filename );
64             }
65             Config.load( in );
66         } catch( IOException ioe ) {
67             new ErrorHandler(ioe);
68         }
69     }
70
71     /**
72      * This static method registers the SMB URL protocol handler which is
73      * required to use SMB URLs with the <tt>java.net.URL</tt> class. If this
74      * method is not called before attempting to create an SMB URL with the
75      * URL class the following exception will occur:
76      * <blockquote><pre>
77      * Exception MalformedURLException: unknown protocol: smb
78      * at java.net.URL.<init>(URL.java:480)
79      * at java.net.URL.<init>(URL.java:376)
80      * at java.net.URL.<init>(URL.java:330)
81      * at jcifs.smb.SmbFile.<init>(SmbFile.java:355)
82      * ...
83      * </pre><blockquote>
84      */

85
86     public static void registerSmbURLHandler() {
87         String JavaDoc ver, pkgs;
88
89         ver = System.getProperty( "java.version" );
90         if( ver.startsWith( "1.1." ) || ver.startsWith( "1.2." )) {
91              throw new RuntimeException JavaDoc( "jcifs-0.7.0b4+ requires Java 1.3 or above. You are running " + ver );
92         }
93         pkgs = System.getProperty( "java.protocol.handler.pkgs" );
94         if( pkgs == null ) {
95             pkgs = "jcifs";
96         } else {
97             pkgs += "|jcifs";
98         }
99         System.setProperty( "java.protocol.handler.pkgs", pkgs );
100     }
101
102     // supress javadoc constructor summary by removing 'protected'
103
Config() {}
104
105     /**
106      * Set the default properties of the static Properties used by <tt>Config</tt>. This permits
107      * a different Properties object/file to be used as the source of properties for
108      * use by the jCIFS library. The Properties must be set <i>before jCIFS
109      * classes are accessed</i> as most jCIFS classes load properties statically once.
110      * Using this method will also override properties loaded
111      * using the <tt>-Djcifs.properties=</tt> commandline parameter.
112      */

113
114     public static void setProperties( Properties JavaDoc prp ) {
115         Config.prp = new Properties JavaDoc( prp );
116         Config.prp.putAll( System.getProperties() );
117     }
118
119     /**
120      * Load the <code>Config</code> with properties from the stream
121      * <code>in</code> from a <code>Properties</code> file.
122      */

123
124     public static void load( InputStream in ) throws IOException {
125         if( in != null ) {
126             prp.load( in );
127         }
128         prp.putAll( System.getProperties() );
129     }
130
131     public static void store( OutputStream out, String JavaDoc header ) throws IOException {
132         prp.store( out, header );
133     }
134
135     /**
136      * List the properties in the <code>Code</code>.
137      */

138
139     public static void list( PrintStream out ) throws IOException {
140         prp.list( out );
141     }
142
143     /**
144      * Add a property.
145      */

146
147     public static Object JavaDoc setProperty( String JavaDoc key, String JavaDoc value ) {
148         return prp.setProperty( key, value );
149     }
150
151     /**
152      * Retrieve a property as an <code>Object</code>.
153      */

154
155     public static Object JavaDoc get( String JavaDoc key ) {
156         return prp.get( key );
157     }
158
159     /**
160      * Retrieve a <code>String</code>. If the key cannot be found,
161      * the provided <code>def</code> default parameter will be returned.
162      */

163
164     public static String JavaDoc getProperty( String JavaDoc key, String JavaDoc def ) {
165         return prp.getProperty( key, def );
166     }
167
168     /**
169      * Retrieve a <code>String</code>. If the property is not found, <code>null</code> is returned.
170      */

171
172     public static String JavaDoc getProperty( String JavaDoc key ) {
173         return prp.getProperty( key );
174     }
175
176     /**
177      * Retrieve an <code>int</code>. If the key does not exist or
178      * cannot be converted to an <code>int</code>, the provided default
179      * argument will be returned.
180      */

181
182     public static int getInt( String JavaDoc key, int def ) {
183         String JavaDoc s = prp.getProperty( key );
184         if( s != null ) {
185             try {
186                 def = Integer.parseInt( s );
187             } catch( NumberFormatException JavaDoc nfe ) {
188                 new ErrorHandler(nfe);
189             }
190         }
191         return def;
192     }
193
194     /**
195      * Retrieve an <code>int</code>. If the property is not found, <code>-1</code> is returned.
196      */

197
198     public static int getInt( String JavaDoc key ) {
199         String JavaDoc s = prp.getProperty( key );
200         int result = -1;
201         if( s != null ) {
202             try {
203                 result = Integer.parseInt( s );
204             } catch( NumberFormatException JavaDoc nfe ) {
205                 new ErrorHandler(nfe);
206             }
207         }
208         return result;
209     }
210
211     /**
212      * Retrieve a <code>long</code>. If the key does not exist or
213      * cannot be converted to a <code>long</code>, the provided default
214      * argument will be returned.
215      */

216
217     public static long getLong( String JavaDoc key, long def ) {
218         String JavaDoc s = prp.getProperty( key );
219         if( s != null ) {
220             try {
221                 def = Long.parseLong( s );
222             } catch( NumberFormatException JavaDoc nfe ) {
223                 new ErrorHandler(nfe);
224             }
225         }
226         return def;
227     }
228
229     /**
230      * Retrieve an <code>InetAddress</code>. If the address is not
231      * an IP address and cannot be resolved <code>null</code> will
232      * be returned.
233      */

234
235     public static InetAddress JavaDoc getInetAddress( String JavaDoc key, InetAddress JavaDoc def ) {
236         String JavaDoc addr = prp.getProperty( key );
237         if( addr != null ) {
238             try {
239                 def = InetAddress.getByName( addr );
240             } catch( UnknownHostException JavaDoc uhe ) {
241                 new ErrorHandler(uhe);
242             }
243         }
244         return def;
245     }
246
247     /**
248      * Retrieve a boolean value. If the property is not found, the value of <code>def</code> is returned.
249      */

250
251     public static boolean getBoolean( String JavaDoc key, boolean def ) {
252         String JavaDoc b = getProperty( key );
253         if( b != null ) {
254             def = b.toLowerCase().equals( "true" );
255         }
256         return def;
257     }
258
259     /**
260      * Retrieve an array of <tt>InetAddress</tt> created from a property
261      * value containting a <tt>delim</tt> separated list of hostnames and/or
262      * ipaddresses.
263      */

264
265     public static InetAddress JavaDoc[] getInetAddressArray( String JavaDoc key, String JavaDoc delim, InetAddress JavaDoc[] def ) {
266         String JavaDoc p = getProperty( key );
267         if( p != null ) {
268             StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc( p, delim );
269             int len = tok.countTokens();
270             InetAddress JavaDoc[] arr = new InetAddress JavaDoc[len];
271             for( int i = 0; i < len; i++ ) {
272                 String JavaDoc addr = tok.nextToken();
273                 try {
274                     arr[i] = InetAddress.getByName( addr );
275                 } catch( UnknownHostException JavaDoc uhe ) {
276                     new ErrorHandler(uhe);
277                     return def;
278                 }
279             }
280             return arr;
281         }
282         return def;
283     }
284 }
285
286
Popular Tags