1 7 8 package com.sun.naming.internal; 9 10 import java.applet.Applet ; 11 import java.io.InputStream ; 12 import java.io.IOException ; 13 import java.net.URL ; 14 import java.lang.ref.WeakReference ; 15 import java.util.Enumeration ; 16 import java.util.HashMap ; 17 import java.util.Hashtable ; 18 import java.util.Map ; 19 import java.util.Properties ; 20 import java.util.StringTokenizer ; 21 import java.util.List ; 22 import java.util.ArrayList ; 23 import java.util.WeakHashMap ; 24 25 import javax.naming.*; 26 27 34 35 public final class ResourceManager { 36 37 40 private static final String PROVIDER_RESOURCE_FILE_NAME = 41 "jndiprovider.properties"; 42 43 46 private static final String APP_RESOURCE_FILE_NAME = "jndi.properties"; 47 48 51 private static final String JRELIB_PROPERTY_FILE_NAME = "jndi.properties"; 52 53 56 private static final String [] listProperties = { 57 Context.OBJECT_FACTORIES, 58 Context.URL_PKG_PREFIXES, 59 Context.STATE_FACTORIES, 60 javax.naming.ldap.LdapContext.CONTROL_FACTORIES 62 }; 63 64 private static final VersionHelper helper = 65 VersionHelper.getVersionHelper(); 66 67 74 private static final WeakHashMap propertiesCache = new WeakHashMap (11); 75 76 84 private static final WeakHashMap factoryCache = new WeakHashMap (11); 85 86 95 private static final WeakHashMap urlFactoryCache = new WeakHashMap (11); 96 private static final WeakReference NO_FACTORY = new WeakReference (null); 97 98 99 private ResourceManager() { 101 } 102 103 104 106 122 public static Hashtable getInitialEnvironment(Hashtable env) 123 throws NamingException 124 { 125 String [] props = VersionHelper.PROPS; if (env == null) { 127 env = new Hashtable (11); 128 } 129 Applet applet = (Applet )env.get(Context.APPLET); 130 131 String [] jndiSysProps = helper.getJndiProperties(); 139 for (int i = 0; i < props.length; i++) { 140 Object val = env.get(props[i]); 141 if (val == null) { 142 if (applet != null) { 143 val = applet.getParameter(props[i]); 144 } 145 if (val == null) { 146 val = (jndiSysProps != null) 148 ? jndiSysProps[i] 149 : helper.getJndiProperty(i); 150 } 151 if (val != null) { 152 env.put(props[i], val); 153 } 154 } 155 } 156 157 mergeTables(env, getApplicationResources()); 160 return env; 161 } 162 163 183 public static String getProperty(String propName, Hashtable env, 184 Context ctx, boolean concat) 185 throws NamingException { 186 187 String val1 = (env != null) ? (String )env.get(propName) : null; 188 if ((ctx == null) || 189 ((val1 != null) && !concat)) { 190 return val1; 191 } 192 String val2 = (String )getProviderResource(ctx).get(propName); 193 if (val1 == null) { 194 return val2; 195 } else if ((val2 == null) || !concat) { 196 return val1; 197 } else { 198 return (val1 + ":" + val2); 199 } 200 } 201 202 244 public static FactoryEnumeration getFactories(String propName, Hashtable env, 245 Context ctx) throws NamingException { 246 247 String facProp = getProperty(propName, env, ctx, true); 248 if (facProp == null) 249 return null; 251 ClassLoader loader = helper.getContextClassLoader(); 253 254 Map perLoaderCache = null; 255 synchronized (factoryCache) { 256 perLoaderCache = (Map ) factoryCache.get(loader); 257 if (perLoaderCache == null) { 258 perLoaderCache = new HashMap (11); 259 factoryCache.put(loader, perLoaderCache); 260 } 261 } 262 263 synchronized (perLoaderCache) { 264 List factories = (List ) perLoaderCache.get(facProp); 265 if (factories != null) { 266 return factories.size() == 0 ? null 268 : new FactoryEnumeration(factories, loader); 269 } else { 270 StringTokenizer parser = new StringTokenizer (facProp, ":"); 273 factories = new ArrayList (5); 274 while (parser.hasMoreTokens()) { 275 try { 276 String className = parser.nextToken(); 278 Class c = helper.loadClass(className, loader); 279 factories.add(new NamedWeakReference(c, className)); 280 } catch (Exception e) { 281 } 283 } 284 perLoaderCache.put(facProp, factories); 286 return new FactoryEnumeration(factories, loader); 287 } 288 } 289 } 290 291 327 public static Object getFactory(String propName, Hashtable env, Context ctx, 328 String classSuffix, String defaultPkgPrefix) throws NamingException { 329 330 String facProp = getProperty(propName, env, ctx, true); 332 if (facProp != null) 333 facProp += (":" + defaultPkgPrefix); 334 else 335 facProp = defaultPkgPrefix; 336 337 ClassLoader loader = helper.getContextClassLoader(); 340 String key = classSuffix + " " + facProp; 341 342 Map perLoaderCache = null; 343 synchronized (urlFactoryCache) { 344 perLoaderCache = (Map ) urlFactoryCache.get(loader); 345 if (perLoaderCache == null) { 346 perLoaderCache = new HashMap (11); 347 urlFactoryCache.put(loader, perLoaderCache); 348 } 349 } 350 351 synchronized (perLoaderCache) { 352 Object factory = null; 353 354 WeakReference factoryRef = (WeakReference ) perLoaderCache.get(key); 355 if (factoryRef == NO_FACTORY) { 356 return null; 357 } else if (factoryRef != null) { 358 factory = factoryRef.get(); 359 if (factory != null) { return factory; 361 } 362 } 363 364 StringTokenizer parser = new StringTokenizer (facProp, ":"); 366 String className; 367 while (factory == null && parser.hasMoreTokens()) { 368 className = parser.nextToken() + classSuffix; 369 try { 370 factory = helper.loadClass(className, loader).newInstance(); 372 } catch (InstantiationException e) { 373 NamingException ne = 374 new NamingException("Cannot instantiate " + className); 375 ne.setRootCause(e); 376 throw ne; 377 } catch (IllegalAccessException e) { 378 NamingException ne = 379 new NamingException("Cannot access " + className); 380 ne.setRootCause(e); 381 throw ne; 382 } catch (Exception e) { 383 } 386 } 387 388 perLoaderCache.put(key, (factory != null) 390 ? new WeakReference (factory) 391 : NO_FACTORY); 392 return factory; 393 } 394 } 395 396 397 399 407 private static Hashtable getProviderResource(Object obj) 408 throws NamingException 409 { 410 if (obj == null) { 411 return (new Hashtable (1)); 412 } 413 synchronized (propertiesCache) { 414 Class c = obj.getClass(); 415 416 Hashtable props = (Hashtable )propertiesCache.get(c); 417 if (props != null) { 418 return props; 419 } 420 props = new Properties (); 421 422 InputStream istream = 423 helper.getResourceAsStream(c, PROVIDER_RESOURCE_FILE_NAME); 424 425 if (istream != null) { 426 try { 427 ((Properties )props).load(istream); 428 } catch (IOException e) { 429 NamingException ne = new ConfigurationException( 430 "Error reading provider resource file for " + c); 431 ne.setRootCause(e); 432 throw ne; 433 } 434 } 435 propertiesCache.put(c, props); 436 return props; 437 } 438 } 439 440 441 457 private static Hashtable getApplicationResources() throws NamingException { 458 459 ClassLoader cl = helper.getContextClassLoader(); 460 461 synchronized (propertiesCache) { 462 Hashtable result = (Hashtable )propertiesCache.get(cl); 463 if (result != null) { 464 return result; 465 } 466 467 try { 468 NamingEnumeration resources = 469 helper.getResources(cl, APP_RESOURCE_FILE_NAME); 470 while (resources.hasMore()) { 471 Properties props = new Properties (); 472 props.load((InputStream )resources.next()); 473 474 if (result == null) { 475 result = props; 476 } else { 477 mergeTables(result, props); 478 } 479 } 480 481 InputStream istream = 483 helper.getJavaHomeLibStream(JRELIB_PROPERTY_FILE_NAME); 484 if (istream != null) { 485 Properties props = new Properties (); 486 props.load(istream); 487 488 if (result == null) { 489 result = props; 490 } else { 491 mergeTables(result, props); 492 } 493 } 494 495 } catch (IOException e) { 496 NamingException ne = new ConfigurationException( 497 "Error reading application resource file"); 498 ne.setRootCause(e); 499 throw ne; 500 } 501 if (result == null) { 502 result = new Hashtable (11); 503 } 504 propertiesCache.put(cl, result); 505 return result; 506 } 507 } 508 509 516 private static void mergeTables(Hashtable props1, Hashtable props2) { 517 Enumeration keys = props2.keys(); 518 519 while (keys.hasMoreElements()) { 520 String prop = (String )keys.nextElement(); 521 Object val1 = props1.get(prop); 522 if (val1 == null) { 523 props1.put(prop, props2.get(prop)); 524 } else if (isListProperty(prop)) { 525 String val2 = (String )props2.get(prop); 526 props1.put(prop, ((String )val1) + ":" + val2); 527 } 528 } 529 } 530 531 535 private static boolean isListProperty(String prop) { 536 prop = prop.intern(); 537 for (int i = 0; i < listProperties.length; i++) { 538 if (prop == listProperties[i]) { 539 return true; 540 } 541 } 542 return false; 543 } 544 } 545 | Popular Tags |