1 61 62 package org.apache.commons.discovery.tools; 63 64 import java.util.HashMap ; 65 import java.util.Properties ; 66 67 import org.apache.commons.discovery.DiscoveryException; 68 import org.apache.commons.discovery.jdk.JDKHooks; 69 import org.apache.commons.discovery.resource.ClassLoaders; 70 71 72 249 public class DiscoverSingleton { 250 256 257 269 public static Object find(Class spiClass) 270 throws DiscoveryException 271 { 272 return find(null, 273 new SPInterface(spiClass), 274 DiscoverClass.nullProperties, 275 DiscoverClass.nullDefaultImpl); 276 } 277 278 294 public static Object find(Class spiClass, Properties properties) 295 throws DiscoveryException 296 { 297 return find(null, 298 new SPInterface(spiClass), 299 new PropertiesHolder(properties), 300 DiscoverClass.nullDefaultImpl); 301 } 302 303 317 public static Object find(Class spiClass, String defaultImpl) 318 throws DiscoveryException 319 { 320 return find(null, 321 new SPInterface(spiClass), 322 DiscoverClass.nullProperties, 323 new DefaultClassHolder(defaultImpl)); 324 } 325 326 344 public static Object find(Class spiClass, 345 Properties properties, 346 String defaultImpl) 347 throws DiscoveryException 348 { 349 return find(null, 350 new SPInterface(spiClass), 351 new PropertiesHolder(properties), 352 new DefaultClassHolder(defaultImpl)); 353 } 354 355 373 public static Object find(Class spiClass, 374 String propertiesFileName, 375 String defaultImpl) 376 throws DiscoveryException 377 { 378 return find(null, 379 new SPInterface(spiClass), 380 new PropertiesHolder(propertiesFileName), 381 new DefaultClassHolder(defaultImpl)); 382 } 383 384 386 387 388 406 public static Object find(ClassLoaders loaders, 407 SPInterface spi, 408 PropertiesHolder properties, 409 DefaultClassHolder defaultImpl) 410 throws DiscoveryException 411 { 412 ClassLoader contextLoader = JDKHooks.getJDKHooks().getThreadContextClassLoader(); 413 414 Object obj = get(contextLoader, spi.getSPName()); 415 416 if (obj == null) { 417 try { 418 obj = DiscoverClass.newInstance(loaders, spi, properties, defaultImpl); 419 420 if (obj != null) { 421 put(contextLoader, spi.getSPName(), obj); 422 } 423 } catch (DiscoveryException de) { 424 throw de; 425 } catch (Exception e) { 426 throw new DiscoveryException("Unable to instantiate implementation class for " + spi.getSPName(), e); 427 } 428 } 429 430 return obj; 431 } 432 433 434 435 446 public static synchronized void release() { 447 root_cache.release(); 448 } 449 450 456 public static synchronized void release(Class spiClass) { 457 HashMap spis = (HashMap )root_cache.get(JDKHooks.getJDKHooks().getThreadContextClassLoader()); 458 459 if (spis != null) { 460 spis.remove(spiClass.getName()); 461 } 462 } 463 464 465 491 492 496 private static final EnvironmentCache root_cache = new EnvironmentCache(); 497 498 501 private static synchronized Object get(ClassLoader classLoader, 502 String spiName) 503 { 504 HashMap spis = (HashMap )root_cache.get(classLoader); 505 506 return (spis != null) 507 ? spis.get(spiName) 508 : null; 509 } 510 511 514 private static synchronized void put(ClassLoader classLoader, 515 String spiName, 516 Object service) 517 { 518 if (service != null) 519 { 520 HashMap spis = (HashMap )root_cache.get(classLoader); 521 522 if (spis == null) { 523 spis = new HashMap (root_cache.smallHashSize); 524 root_cache.put(classLoader, spis); 525 } 526 527 spis.put(spiName, service); 528 } 529 } 530 } 531 | Popular Tags |