1 18 19 package com.knowgate.jcifs; 20 21 import java.util.Properties ; 22 import java.io.*; 23 import java.net.InetAddress ; 24 import java.net.UnknownHostException ; 25 import java.util.StringTokenizer ; 26 27 import com.knowgate.debug.ErrorHandler; 28 29 46 47 public class Config { 48 49 52 53 private static Properties prp = new Properties (); 54 55 static { 56 String 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 85 86 public static void registerSmbURLHandler() { 87 String ver, pkgs; 88 89 ver = System.getProperty( "java.version" ); 90 if( ver.startsWith( "1.1." ) || ver.startsWith( "1.2." )) { 91 throw new RuntimeException ( "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 Config() {} 104 105 113 114 public static void setProperties( Properties prp ) { 115 Config.prp = new Properties ( prp ); 116 Config.prp.putAll( System.getProperties() ); 117 } 118 119 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 header ) throws IOException { 132 prp.store( out, header ); 133 } 134 135 138 139 public static void list( PrintStream out ) throws IOException { 140 prp.list( out ); 141 } 142 143 146 147 public static Object setProperty( String key, String value ) { 148 return prp.setProperty( key, value ); 149 } 150 151 154 155 public static Object get( String key ) { 156 return prp.get( key ); 157 } 158 159 163 164 public static String getProperty( String key, String def ) { 165 return prp.getProperty( key, def ); 166 } 167 168 171 172 public static String getProperty( String key ) { 173 return prp.getProperty( key ); 174 } 175 176 181 182 public static int getInt( String key, int def ) { 183 String s = prp.getProperty( key ); 184 if( s != null ) { 185 try { 186 def = Integer.parseInt( s ); 187 } catch( NumberFormatException nfe ) { 188 new ErrorHandler(nfe); 189 } 190 } 191 return def; 192 } 193 194 197 198 public static int getInt( String key ) { 199 String s = prp.getProperty( key ); 200 int result = -1; 201 if( s != null ) { 202 try { 203 result = Integer.parseInt( s ); 204 } catch( NumberFormatException nfe ) { 205 new ErrorHandler(nfe); 206 } 207 } 208 return result; 209 } 210 211 216 217 public static long getLong( String key, long def ) { 218 String s = prp.getProperty( key ); 219 if( s != null ) { 220 try { 221 def = Long.parseLong( s ); 222 } catch( NumberFormatException nfe ) { 223 new ErrorHandler(nfe); 224 } 225 } 226 return def; 227 } 228 229 234 235 public static InetAddress getInetAddress( String key, InetAddress def ) { 236 String addr = prp.getProperty( key ); 237 if( addr != null ) { 238 try { 239 def = InetAddress.getByName( addr ); 240 } catch( UnknownHostException uhe ) { 241 new ErrorHandler(uhe); 242 } 243 } 244 return def; 245 } 246 247 250 251 public static boolean getBoolean( String key, boolean def ) { 252 String b = getProperty( key ); 253 if( b != null ) { 254 def = b.toLowerCase().equals( "true" ); 255 } 256 return def; 257 } 258 259 264 265 public static InetAddress [] getInetAddressArray( String key, String delim, InetAddress [] def ) { 266 String p = getProperty( key ); 267 if( p != null ) { 268 StringTokenizer tok = new StringTokenizer ( p, delim ); 269 int len = tok.countTokens(); 270 InetAddress [] arr = new InetAddress [len]; 271 for( int i = 0; i < len; i++ ) { 272 String addr = tok.nextToken(); 273 try { 274 arr[i] = InetAddress.getByName( addr ); 275 } catch( UnknownHostException uhe ) { 276 new ErrorHandler(uhe); 277 return def; 278 } 279 } 280 return arr; 281 } 282 return def; 283 } 284 } 285 286 | Popular Tags |