1 18 19 package 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 import jcifs.util.LogStream; 27 28 45 46 public class Config { 47 48 51 52 private static Properties prp = new Properties (); 53 private static LogStream log; 54 public static String DEFAULT_OEM_ENCODING = "Cp850"; 55 56 static { 57 String 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 110 111 public static void registerSmbURLHandler() { 112 String ver, pkgs; 113 114 ver = System.getProperty( "java.version" ); 115 if( ver.startsWith( "1.1." ) || ver.startsWith( "1.2." )) { 116 throw new RuntimeException ( "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 Config() {} 129 130 138 139 public static void setProperties( Properties prp ) { 140 Config.prp = new Properties ( prp ); 141 try { 142 Config.prp.putAll( System.getProperties() ); 143 } catch( SecurityException se ) { 144 if( log.level > 1 ) 145 log.println( "SecurityException: jcifs will ignore System properties" ); 146 } 147 } 148 149 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 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 header ) throws IOException { 167 prp.store( out, header ); 168 } 169 170 173 174 public static void list( PrintStream out ) throws IOException { 175 prp.list( out ); 176 } 177 178 181 182 public static Object setProperty( String key, String value ) { 183 return prp.setProperty( key, value ); 184 } 185 186 189 190 public static Object get( String key ) { 191 return prp.get( key ); 192 } 193 194 198 199 public static String getProperty( String key, String def ) { 200 return prp.getProperty( key, def ); 201 } 202 203 206 207 public static String getProperty( String key ) { 208 return prp.getProperty( key ); 209 } 210 211 216 217 public static int getInt( String key, int def ) { 218 String s = prp.getProperty( key ); 219 if( s != null ) { 220 try { 221 def = Integer.parseInt( s ); 222 } catch( NumberFormatException nfe ) { 223 if( log.level > 0 ) 224 nfe.printStackTrace( log ); 225 } 226 } 227 return def; 228 } 229 230 233 234 public static int getInt( String key ) { 235 String s = prp.getProperty( key ); 236 int result = -1; 237 if( s != null ) { 238 try { 239 result = Integer.parseInt( s ); 240 } catch( NumberFormatException nfe ) { 241 if( log.level > 0 ) 242 nfe.printStackTrace( log ); 243 } 244 } 245 return result; 246 } 247 248 253 254 public static long getLong( String key, long def ) { 255 String s = prp.getProperty( key ); 256 if( s != null ) { 257 try { 258 def = Long.parseLong( s ); 259 } catch( NumberFormatException nfe ) { 260 if( log.level > 0 ) 261 nfe.printStackTrace( log ); 262 } 263 } 264 return def; 265 } 266 267 272 273 public static InetAddress getInetAddress( String key, InetAddress def ) { 274 String addr = prp.getProperty( key ); 275 if( addr != null ) { 276 try { 277 def = InetAddress.getByName( addr ); 278 } catch( UnknownHostException 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 getLocalHost() { 288 String addr = prp.getProperty( "jcifs.smb.client.laddr" ); 289 290 if (addr != null) { 291 try { 292 return InetAddress.getByName( addr ); 293 } catch( UnknownHostException 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 307 308 public static boolean getBoolean( String key, boolean def ) { 309 String b = getProperty( key ); 310 if( b != null ) { 311 def = b.toLowerCase().equals( "true" ); 312 } 313 return def; 314 } 315 316 321 322 public static InetAddress [] getInetAddressArray( String key, String delim, InetAddress [] def ) { 323 String p = getProperty( key ); 324 if( p != null ) { 325 StringTokenizer tok = new StringTokenizer ( p, delim ); 326 int len = tok.countTokens(); 327 InetAddress [] arr = new InetAddress [len]; 328 for( int i = 0; i < len; i++ ) { 329 String addr = tok.nextToken(); 330 try { 331 arr[i] = InetAddress.getByName( addr ); 332 } catch( UnknownHostException 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 |