1 61 62 package org.apache.commons.discovery.tools; 63 64 import java.lang.reflect.InvocationTargetException ; 65 import java.util.Properties ; 66 import java.util.Vector ; 67 68 import org.apache.commons.discovery.DiscoveryException; 69 import org.apache.commons.discovery.ResourceClass; 70 import org.apache.commons.discovery.listeners.FirstResourceClassListener; 71 import org.apache.commons.discovery.resource.ClassLoaders; 72 import org.apache.commons.discovery.resource.classes.DiscoverClasses; 73 import org.apache.commons.discovery.resource.names.DiscoverServiceNames; 74 75 76 179 public class DiscoverClass { 180 183 public static final DefaultClassHolder nullDefaultImpl = null; 184 185 188 public static final PropertiesHolder nullProperties = null; 189 190 191 private ClassLoaders classLoaders = null; 192 193 194 200 public DiscoverClass() { 201 this(null); 202 } 203 204 210 public DiscoverClass(ClassLoaders classLoaders) { 211 this.classLoaders = classLoaders; 212 } 213 214 215 public ClassLoaders getClassLoaders(Class spiClass) { 216 return classLoaders; 217 } 218 219 220 231 public Class find(Class spiClass) 232 throws DiscoveryException 233 { 234 return find(getClassLoaders(spiClass), 235 new SPInterface(spiClass), 236 nullProperties, 237 nullDefaultImpl); 238 } 239 240 253 public Class find(Class spiClass, Properties properties) 254 throws DiscoveryException 255 { 256 return find(getClassLoaders(spiClass), 257 new SPInterface(spiClass), 258 new PropertiesHolder(properties), 259 nullDefaultImpl); 260 } 261 262 275 public Class find(Class spiClass, String defaultImpl) 276 throws DiscoveryException 277 { 278 return find(getClassLoaders(spiClass), 279 new SPInterface(spiClass), 280 nullProperties, 281 new DefaultClassHolder(defaultImpl)); 282 } 283 284 299 public Class find(Class spiClass, Properties properties, String defaultImpl) 300 throws DiscoveryException 301 { 302 return find(getClassLoaders(spiClass), 303 new SPInterface(spiClass), 304 new PropertiesHolder(properties), 305 new DefaultClassHolder(defaultImpl)); 306 } 307 308 323 public Class find(Class spiClass, String propertiesFileName, String defaultImpl) 324 throws DiscoveryException 325 { 326 return find(getClassLoaders(spiClass), 327 new SPInterface(spiClass), 328 new PropertiesHolder(propertiesFileName), 329 new DefaultClassHolder(defaultImpl)); 330 } 331 332 347 public static Class find(ClassLoaders loaders, 348 SPInterface spi, 349 PropertiesHolder properties, 350 DefaultClassHolder defaultImpl) 351 throws DiscoveryException 352 { 353 if (loaders == null) { 354 loaders = ClassLoaders.getLibLoaders(spi.getSPClass(), 355 DiscoverClass.class, 356 true); 357 } 358 359 FirstResourceClassListener listener = 360 new FirstResourceClassListener(); 361 362 DiscoverClasses classDiscovery = new DiscoverClasses(loaders); 363 classDiscovery.setListener(listener); 364 365 String [] classNames = 366 discoverClassNames(spi, 367 (properties == null) 368 ? null 369 : properties.getProperties(spi, loaders)); 370 371 if (classNames.length > 0) { 372 classDiscovery.find(classNames[0]); 373 374 ResourceClass resource = listener.getFirst(); 376 return (resource == null) ? null : resource.loadClass(); 377 } else { 378 DiscoverServiceNames discoverServices = new DiscoverServiceNames(loaders); 379 380 383 discoverServices.setListener(classDiscovery); 384 385 discoverServices.find(spi.getSPName()); 386 ResourceClass resource = listener.getFirst(); 387 388 Class clazz = (resource == null) ? null : resource.loadClass(); 389 390 if (clazz != null) { 391 return clazz; 392 } 393 394 if (defaultImpl != null) { 395 return defaultImpl.getDefaultClass(spi, loaders); 396 } 397 } 398 399 throw new DiscoveryException("No implementation defined for " + spi.getSPName()); 400 } 401 402 414 public Object newInstance(Class spiClass) 415 throws DiscoveryException, 416 InstantiationException , 417 IllegalAccessException , 418 NoSuchMethodException , 419 InvocationTargetException 420 { 421 return newInstance(getClassLoaders(spiClass), 422 new SPInterface(spiClass), 423 nullProperties, 424 nullDefaultImpl); 425 } 426 427 443 public Object newInstance(Class spiClass, Properties properties) 444 throws DiscoveryException, 445 InstantiationException , 446 IllegalAccessException , 447 NoSuchMethodException , 448 InvocationTargetException 449 { 450 return newInstance(getClassLoaders(spiClass), 451 new SPInterface(spiClass), 452 new PropertiesHolder(properties), 453 nullDefaultImpl); 454 } 455 456 470 public Object newInstance(Class spiClass, String defaultImpl) 471 throws DiscoveryException, 472 InstantiationException , 473 IllegalAccessException , 474 NoSuchMethodException , 475 InvocationTargetException 476 { 477 return newInstance(getClassLoaders(spiClass), 478 new SPInterface(spiClass), 479 nullProperties, 480 new DefaultClassHolder(defaultImpl)); 481 } 482 483 501 public Object newInstance(Class spiClass, Properties properties, String defaultImpl) 502 throws DiscoveryException, 503 InstantiationException , 504 IllegalAccessException , 505 NoSuchMethodException , 506 InvocationTargetException 507 { 508 return newInstance(getClassLoaders(spiClass), 509 new SPInterface(spiClass), 510 new PropertiesHolder(properties), 511 new DefaultClassHolder(defaultImpl)); 512 } 513 514 532 public Object newInstance(Class spiClass, String propertiesFileName, String defaultImpl) 533 throws DiscoveryException, 534 InstantiationException , 535 IllegalAccessException , 536 NoSuchMethodException , 537 InvocationTargetException 538 { 539 return newInstance(getClassLoaders(spiClass), 540 new SPInterface(spiClass), 541 new PropertiesHolder(propertiesFileName), 542 new DefaultClassHolder(defaultImpl)); 543 } 544 545 563 public static Object newInstance(ClassLoaders loaders, 564 SPInterface spi, 565 PropertiesHolder properties, 566 DefaultClassHolder defaultImpl) 567 throws DiscoveryException, 568 InstantiationException , 569 IllegalAccessException , 570 NoSuchMethodException , 571 InvocationTargetException 572 { 573 return spi.newInstance(find(loaders, spi, properties, defaultImpl)); 574 } 575 576 593 public static String [] discoverClassNames(SPInterface spi, 594 Properties properties) 595 { 596 Vector names = new Vector (); 597 598 String spiName = spi.getSPName(); 599 String propertyName = spi.getPropertyName(); 600 601 boolean includeAltProperty = !spiName.equals(propertyName); 602 603 String className = getManagedProperty(spiName); 605 if (className != null) names.addElement(className); 606 607 if (includeAltProperty) { 608 className = getManagedProperty(propertyName); 610 if (className != null) names.addElement(className); 611 } 612 613 if (properties != null) { 614 className = properties.getProperty(spiName); 616 if (className != null) names.addElement(className); 617 618 if (includeAltProperty) { 619 className = properties.getProperty(propertyName); 621 if (className != null) names.addElement(className); 622 } 623 } 624 625 String [] results = new String [names.size()]; 626 names.copyInto(results); 627 628 return results; 629 } 630 631 632 641 public static String getManagedProperty(String propertyName) { 642 String value; 643 try { 644 value = ManagedProperties.getProperty(propertyName); 645 } catch (SecurityException e) { 646 value = null; 647 } 648 return value; 649 } 650 } 651 | Popular Tags |