KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 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 import jcifs.util.LogStream;
27
28 /**
29  * This class uses a static {@link java.util.Properties} to act
30  * as a cental repository for all jCIFS configuration properties. It cannot be
31  * instantiated. Similar to <code>System</code> properties the namespace
32  * is global therefore property names should be unique. Before use,
33  * the <code>load</code> method should be called with the name of a
34  * <code>Properties</code> file (or <code>null</code> indicating no
35  * file) to initialize the <code>Config</code>. The <code>System</code>
36  * properties will then populate the <code>Config</code> as well potentially
37  * overwriting properties from the file. Thus properties provided on the
38  * commandline with the <code>-Dproperty.name=value</code> VM parameter
39  * will override properties from the configuration file.
40  * <p>
41  * There are several ways to set jCIFS properties. See
42  * the <a HREF="../overview-summary.html#scp">overview page of the API
43  * documentation</a> for details.
44  */

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

51
52     private static Properties JavaDoc prp = new Properties JavaDoc();
53     private static LogStream log;
54     public static String JavaDoc DEFAULT_OEM_ENCODING = "Cp850";
55
56     static {
57         String JavaDoc filename;
58         int level;
59         FileInputStream in = null;
60
61         log = LogStream.getInstance();
62
63         try {
64             filename = System.getProperty( "jcifs.properties" );
65             if( filename != null && filename.length() > 1 ) {
66                 in = new FileInputStream( filename );
67             }
68             Config.load( in );
69         } catch( IOException ioe ) {
70             if( log.level > 0 )
71                 ioe.printStackTrace( log );
72         }
73
74         if(( level = Config.getInt( "jcifs.util.loglevel", -1 )) != -1 ) {
75             LogStream.setLevel( level );
76         }
77
78         try {
79             "".getBytes(DEFAULT_OEM_ENCODING);
80         } catch (UnsupportedEncodingException uee) {
81             if (log.level >= 2) {
82                 log.println("WARNING: The default OEM encoding " + DEFAULT_OEM_ENCODING +
83                 " does not appear to be supported by this JRE. The default encoding will be US-ASCII.");
84             }
85             DEFAULT_OEM_ENCODING = "US-ASCII";
86         }
87
88         if (log.level >= 4) {
89             try {
90                 prp.store( log, "JCIFS PROPERTIES" );
91             } catch( IOException ioe ) {
92             }
93         }
94     }
95
96     /**
97      * This static method registers the SMB URL protocol handler which is
98      * required to use SMB URLs with the <tt>java.net.URL</tt> class. If this
99      * method is not called before attempting to create an SMB URL with the
100      * URL class the following exception will occur:
101      * <blockquote><pre>
102      * Exception MalformedURLException: unknown protocol: smb
103      * at java.net.URL.<init>(URL.java:480)
104      * at java.net.URL.<init>(URL.java:376)
105      * at java.net.URL.<init>(URL.java:330)
106      * at jcifs.smb.SmbFile.<init>(SmbFile.java:355)
107      * ...
108      * </pre><blockquote>
109      */

110
111     public static void registerSmbURLHandler() {
112         String JavaDoc ver, pkgs;
113
114         ver = System.getProperty( "java.version" );
115         if( ver.startsWith( "1.1." ) || ver.startsWith( "1.2." )) {
116              throw new RuntimeException JavaDoc( "jcifs-0.7.0b4+ requires Java 1.3 or above. You are running " + ver );
117         }
118         pkgs = System.getProperty( "java.protocol.handler.pkgs" );
119         if( pkgs == null ) {
120             System.setProperty( "java.protocol.handler.pkgs", "jcifs" );
121         } else if( pkgs.indexOf( "jcifs" ) == -1 ) {
122             pkgs += "|jcifs";
123             System.setProperty( "java.protocol.handler.pkgs", pkgs );
124         }
125     }
126
127     // supress javadoc constructor summary by removing 'protected'
128
Config() {}
129
130     /**
131      * Set the default properties of the static Properties used by <tt>Config</tt>. This permits
132      * a different Properties object/file to be used as the source of properties for
133      * use by the jCIFS library. The Properties must be set <i>before jCIFS
134      * classes are accessed</i> as most jCIFS classes load properties statically once.
135      * Using this method will also override properties loaded
136      * using the <tt>-Djcifs.properties=</tt> commandline parameter.
137      */

138
139     public static void setProperties( Properties JavaDoc prp ) {
140         Config.prp = new Properties JavaDoc( prp );
141         try {
142             Config.prp.putAll( System.getProperties() );
143         } catch( SecurityException JavaDoc se ) {
144             if( log.level > 1 )
145                 log.println( "SecurityException: jcifs will ignore System properties" );
146         }
147     }
148
149     /**
150      * Load the <code>Config</code> with properties from the stream
151      * <code>in</code> from a <code>Properties</code> file.
152      */

153
154     public static void load( InputStream in ) throws IOException {
155         if( in != null ) {
156             prp.load( in );
157         }
158         try {
159             prp.putAll( System.getProperties() );
160         } catch( SecurityException JavaDoc se ) {
161             if( log.level > 1 )
162                 log.println( "SecurityException: jcifs will ignore System properties" );
163         }
164     }
165
166     public static void store( OutputStream out, String JavaDoc header ) throws IOException {
167         prp.store( out, header );
168     }
169
170     /**
171      * List the properties in the <code>Code</code>.
172      */

173
174     public static void list( PrintStream out ) throws IOException {
175         prp.list( out );
176     }
177
178     /**
179      * Add a property.
180      */

181
182     public static Object JavaDoc setProperty( String JavaDoc key, String JavaDoc value ) {
183         return prp.setProperty( key, value );
184     }
185
186     /**
187      * Retrieve a property as an <code>Object</code>.
188      */

189
190     public static Object JavaDoc get( String JavaDoc key ) {
191         return prp.get( key );
192     }
193
194     /**
195      * Retrieve a <code>String</code>. If the key cannot be found,
196      * the provided <code>def</code> default parameter will be returned.
197      */

198
199     public static String JavaDoc getProperty( String JavaDoc key, String JavaDoc def ) {
200         return prp.getProperty( key, def );
201     }
202
203     /**
204      * Retrieve a <code>String</code>. If the property is not found, <code>null</code> is returned.
205      */

206
207     public static String JavaDoc getProperty( String JavaDoc key ) {
208         return prp.getProperty( key );
209     }
210
211     /**
212      * Retrieve an <code>int</code>. If the key does not exist or
213      * cannot be converted to an <code>int</code>, the provided default
214      * argument will be returned.
215      */

216
217     public static int getInt( String JavaDoc key, int def ) {
218         String JavaDoc s = prp.getProperty( key );
219         if( s != null ) {
220             try {
221                 def = Integer.parseInt( s );
222             } catch( NumberFormatException JavaDoc nfe ) {
223                 if( log.level > 0 )
224                     nfe.printStackTrace( log );
225             }
226         }
227         return def;
228     }
229
230     /**
231      * Retrieve an <code>int</code>. If the property is not found, <code>-1</code> is returned.
232      */

233
234     public static int getInt( String JavaDoc key ) {
235         String JavaDoc s = prp.getProperty( key );
236         int result = -1;
237         if( s != null ) {
238             try {
239                 result = Integer.parseInt( s );
240             } catch( NumberFormatException JavaDoc nfe ) {
241                 if( log.level > 0 )
242                     nfe.printStackTrace( log );
243             }
244         }
245         return result;
246     }
247
248     /**
249      * Retrieve a <code>long</code>. If the key does not exist or
250      * cannot be converted to a <code>long</code>, the provided default
251      * argument will be returned.
252      */

253
254     public static long getLong( String JavaDoc key, long def ) {
255         String JavaDoc s = prp.getProperty( key );
256         if( s != null ) {
257             try {
258                 def = Long.parseLong( s );
259             } catch( NumberFormatException JavaDoc nfe ) {
260                 if( log.level > 0 )
261                     nfe.printStackTrace( log );
262             }
263         }
264         return def;
265     }
266
267     /**
268      * Retrieve an <code>InetAddress</code>. If the address is not
269      * an IP address and cannot be resolved <code>null</code> will
270      * be returned.
271      */

272
273     public static InetAddress JavaDoc getInetAddress( String JavaDoc key, InetAddress JavaDoc def ) {
274         String JavaDoc addr = prp.getProperty( key );
275         if( addr != null ) {
276             try {
277                 def = InetAddress.getByName( addr );
278             } catch( UnknownHostException JavaDoc uhe ) {
279                 if( log.level > 0 ) {
280                     log.println( addr );
281                     uhe.printStackTrace( log );
282                 }
283             }
284         }
285         return def;
286     }
287     public static InetAddress JavaDoc getLocalHost() {
288         String JavaDoc addr = prp.getProperty( "jcifs.smb.client.laddr" );
289
290         if (addr != null) {
291             try {
292                 return InetAddress.getByName( addr );
293             } catch( UnknownHostException JavaDoc uhe ) {
294                 if( log.level > 0 ) {
295                     log.println( "Ignoring jcifs.smb.client.laddr address: " + addr );
296                     uhe.printStackTrace( log );
297                 }
298             }
299         }
300
301         return null;
302     }
303
304     /**
305      * Retrieve a boolean value. If the property is not found, the value of <code>def</code> is returned.
306      */

307
308     public static boolean getBoolean( String JavaDoc key, boolean def ) {
309         String JavaDoc b = getProperty( key );
310         if( b != null ) {
311             def = b.toLowerCase().equals( "true" );
312         }
313         return def;
314     }
315
316     /**
317      * Retrieve an array of <tt>InetAddress</tt> created from a property
318      * value containting a <tt>delim</tt> separated list of hostnames and/or
319      * ipaddresses.
320      */

321
322     public static InetAddress JavaDoc[] getInetAddressArray( String JavaDoc key, String JavaDoc delim, InetAddress JavaDoc[] def ) {
323         String JavaDoc p = getProperty( key );
324         if( p != null ) {
325             StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc( p, delim );
326             int len = tok.countTokens();
327             InetAddress JavaDoc[] arr = new InetAddress JavaDoc[len];
328             for( int i = 0; i < len; i++ ) {
329                 String JavaDoc addr = tok.nextToken();
330                 try {
331                     arr[i] = InetAddress.getByName( addr );
332                 } catch( UnknownHostException JavaDoc uhe ) {
333                     if( log.level > 0 ) {
334                         log.println( addr );
335                         uhe.printStackTrace( log );
336                     }
337                     return def;
338                 }
339             }
340             return arr;
341         }
342         return def;
343     }
344 }
345
346
Popular Tags