1. /*
  2. * $Header$
  3. * $Revision$
  4. * $Date$
  5. *
  6. * ====================================================================
  7. *
  8. * The Apache Software License, Version 1.1
  9. *
  10. * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
  11. * reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in
  22. * the documentation and/or other materials provided with the
  23. * distribution.
  24. *
  25. * 3. The end-user documentation included with the redistribution, if
  26. * any, must include the following acknowlegement:
  27. * "This product includes software developed by the
  28. * Apache Software Foundation (http://www.apache.org/)."
  29. * Alternately, this acknowlegement may appear in the software itself,
  30. * if and wherever such third-party acknowlegements normally appear.
  31. *
  32. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  33. * Foundation" must not be used to endorse or promote products derived
  34. * from this software without prior written permission. For written
  35. * permission, please contact apache@apache.org.
  36. *
  37. * 5. Products derived from this software may not be called "Apache"
  38. * nor may "Apache" appear in their names without prior written
  39. * permission of the Apache Group.
  40. *
  41. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  42. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  43. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  45. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  46. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  47. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  48. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  49. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  50. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  51. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  52. * SUCH DAMAGE.
  53. * ====================================================================
  54. *
  55. * This software consists of voluntary contributions made by many
  56. * individuals on behalf of the Apache Software Foundation. For more
  57. * information on the Apache Software Foundation, please see
  58. * <http://www.apache.org/>.
  59. *
  60. */
  61. package org.apache.commons.discovery.tools;
  62. import java.lang.reflect.InvocationTargetException;
  63. import java.util.Properties;
  64. import java.util.Vector;
  65. import org.apache.commons.discovery.DiscoveryException;
  66. import org.apache.commons.discovery.ResourceClass;
  67. import org.apache.commons.discovery.ResourceClassIterator;
  68. import org.apache.commons.discovery.ResourceNameIterator;
  69. import org.apache.commons.discovery.resource.ClassLoaders;
  70. import org.apache.commons.discovery.resource.classes.DiscoverClasses;
  71. import org.apache.commons.discovery.resource.names.DiscoverServiceNames;
  72. /**
  73. * <p>Discover class that implements a given service interface,
  74. * with discovery and configuration features similar to that employed
  75. * by standard Java APIs such as JAXP.
  76. * </p>
  77. *
  78. * <p>In the context of this package, a service interface is defined by a
  79. * Service Provider Interface (SPI). The SPI is expressed as a Java interface,
  80. * abstract class, or (base) class that defines an expected programming
  81. * interface.
  82. * </p>
  83. *
  84. * <p>DiscoverClass provides the <code>find</code> methods for locating a
  85. * class that implements a service interface (SPI). Each form of
  86. * <code>find</code> varies slightly, but they all perform the same basic
  87. * function.
  88. *
  89. * The <code>DiscoverClass.find</code> methods proceed as follows:
  90. * </p>
  91. * <ul>
  92. * <p><li>
  93. * Get the name of an implementation class. The name is the first
  94. * non-null value obtained from the following resources:
  95. * <ul>
  96. * <li>
  97. * The value of the (scoped) system property whose name is the same as
  98. * the SPI's fully qualified class name (as given by SPI.class.getName()).
  99. * The <code>ScopedProperties</code> class provides a way to bind
  100. * properties by classloader, in a secure hierarchy similar in concept
  101. * to the way classloader find class and resource files.
  102. * See <code>ScopedProperties</code> for more details.
  103. * <p>If the ScopedProperties are not set by users, then behaviour
  104. * is equivalent to <code>System.getProperty()</code>.
  105. * </p>
  106. * </li>
  107. * <p><li>
  108. * The value of a <code>Properties properties</code> property, if provided
  109. * as a parameter, whose name is the same as the SPI's fully qualifed class
  110. * name (as given by SPI.class.getName()).
  111. * </li></p>
  112. * <p><li>
  113. * The value obtained using the JDK1.3+ 'Service Provider' specification
  114. * (http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html) to locate a
  115. * service named <code>SPI.class.getName()</code>. This is implemented
  116. * internally, so there is not a dependency on JDK 1.3+.
  117. * </li></p>
  118. * </ul>
  119. * </li></p>
  120. * <p><li>
  121. * If the name of the implementation class is non-null, load that class.
  122. * The class loaded is the first class loaded by the following sequence
  123. * of class loaders:
  124. * <ul>
  125. * <li>Thread Context Class Loader</li>
  126. * <li>DiscoverSingleton's Caller's Class Loader</li>
  127. * <li>SPI's Class Loader</li>
  128. * <li>DiscoverSingleton's (this class or wrapper) Class Loader</li>
  129. * <li>System Class Loader</li>
  130. * </ul>
  131. * An exception is thrown if the class cannot be loaded.
  132. * </li></p>
  133. * <p><li>
  134. * If the name of the implementation class is null, AND the default
  135. * implementation class name (<code>defaultImpl</code>) is null,
  136. * then an exception is thrown.
  137. * </li></p>
  138. * <p><li>
  139. * If the name of the implementation class is null, AND the default
  140. * implementation class (<code>defaultImpl</code>) is non-null,
  141. * then load the default implementation class. The class loaded is the
  142. * first class loaded by the following sequence of class loaders:
  143. * <ul>
  144. * <li>SPI's Class Loader</li>
  145. * <li>DiscoverSingleton's (this class or wrapper) Class Loader</li>
  146. * <li>System Class Loader</li>
  147. * </ul>
  148. * <p>
  149. * This limits the scope in which the default class loader can be found
  150. * to the SPI, DiscoverSingleton, and System class loaders. The assumption here
  151. * is that the default implementation is closely associated with the SPI
  152. * or system, and is not defined in the user's application space.
  153. * </p>
  154. * <p>
  155. * An exception is thrown if the class cannot be loaded.
  156. * </p>
  157. * </li></p>
  158. * <p><li>
  159. * Verify that the loaded class implements the SPI: an exception is thrown
  160. * if the loaded class does not implement the SPI.
  161. * </li></p>
  162. * </ul>
  163. * </p>
  164. *
  165. * <p><strong>IMPLEMENTATION NOTE</strong> - This implementation is modelled
  166. * after the SAXParserFactory and DocumentBuilderFactory implementations
  167. * (corresponding to the JAXP pluggability APIs) found in Apache Xerces.
  168. * </p>
  169. *
  170. * @author Richard A. Sitze
  171. * @author Craig R. McClanahan
  172. * @author Costin Manolache
  173. * @version $Revision$ $Date$
  174. */
  175. public class DiscoverClass {
  176. /**
  177. * Readable placeholder for a null value.
  178. */
  179. public static final DefaultClassHolder nullDefaultImpl = null;
  180. /**
  181. * Readable placeholder for a null value.
  182. */
  183. public static final PropertiesHolder nullProperties = null;
  184. private ClassLoaders classLoaders = null;
  185. /**
  186. * Create a class instance with dynamic environment
  187. * (thread context class loader is determined on each call).
  188. *
  189. * Dynamically construct class loaders on each call.
  190. */
  191. public DiscoverClass() {
  192. this(null);
  193. }
  194. /**
  195. * Create a class instance with dynamic environment
  196. * (thread context class loader is determined on each call).
  197. *
  198. * Cache static list of class loaders for each call.
  199. */
  200. public DiscoverClass(ClassLoaders classLoaders) {
  201. this.classLoaders = classLoaders;
  202. }
  203. public ClassLoaders getClassLoaders(Class spiClass) {
  204. return classLoaders;
  205. }
  206. /**
  207. * Find class implementing SPI.
  208. *
  209. * @param spiClass Service Provider Interface Class.
  210. *
  211. * @return Class implementing the SPI.
  212. *
  213. * @exception DiscoveryException Thrown if the name of a class implementing
  214. * the SPI cannot be found, if the class cannot be loaded, or if
  215. * the resulting class does not implement (or extend) the SPI.
  216. */
  217. public Class find(Class spiClass)
  218. throws DiscoveryException
  219. {
  220. return find(getClassLoaders(spiClass),
  221. new SPInterface(spiClass),
  222. nullProperties,
  223. nullDefaultImpl);
  224. }
  225. /**
  226. * Find class implementing SPI.
  227. *
  228. * @param spiClass Service Provider Interface Class.
  229. *
  230. * @param properties Used to determine name of SPI implementation.
  231. *
  232. * @return Class implementing the SPI.
  233. *
  234. * @exception DiscoveryException Thrown if the name of a class implementing
  235. * the SPI cannot be found, if the class cannot be loaded, or if
  236. * the resulting class does not implement (or extend) the SPI.
  237. */
  238. public Class find(Class spiClass, Properties properties)
  239. throws DiscoveryException
  240. {
  241. return find(getClassLoaders(spiClass),
  242. new SPInterface(spiClass),
  243. new PropertiesHolder(properties),
  244. nullDefaultImpl);
  245. }
  246. /**
  247. * Find class implementing SPI.
  248. *
  249. * @param spiClass Service Provider Interface Class.
  250. *
  251. * @param defaultImpl Default implementation name.
  252. *
  253. * @return Class implementing the SPI.
  254. *
  255. * @exception DiscoveryException Thrown if the name of a class implementing
  256. * the SPI cannot be found, if the class cannot be loaded, or if
  257. * the resulting class does not implement (or extend) the SPI.
  258. */
  259. public Class find(Class spiClass, String defaultImpl)
  260. throws DiscoveryException
  261. {
  262. return find(getClassLoaders(spiClass),
  263. new SPInterface(spiClass),
  264. nullProperties,
  265. new DefaultClassHolder(defaultImpl));
  266. }
  267. /**
  268. * Find class implementing SPI.
  269. *
  270. * @param spiClass Service Provider Interface Class.
  271. *
  272. * @param properties Used to determine name of SPI implementation,.
  273. *
  274. * @param defaultImpl Default implementation class.
  275. *
  276. * @return Class implementing the SPI.
  277. *
  278. * @exception DiscoveryException Thrown if the name of a class implementing
  279. * the SPI cannot be found, if the class cannot be loaded, or if
  280. * the resulting class does not implement (or extend) the SPI.
  281. */
  282. public Class find(Class spiClass, Properties properties, String defaultImpl)
  283. throws DiscoveryException
  284. {
  285. return find(getClassLoaders(spiClass),
  286. new SPInterface(spiClass),
  287. new PropertiesHolder(properties),
  288. new DefaultClassHolder(defaultImpl));
  289. }
  290. /**
  291. * Find class implementing SPI.
  292. *
  293. * @param spiClass Service Provider Interface Class.
  294. *
  295. * @param properties Used to determine name of SPI implementation,.
  296. *
  297. * @param defaultImpl Default implementation class.
  298. *
  299. * @return Class implementing the SPI.
  300. *
  301. * @exception DiscoveryException Thrown if the name of a class implementing
  302. * the SPI cannot be found, if the class cannot be loaded, or if
  303. * the resulting class does not implement (or extend) the SPI.
  304. */
  305. public Class find(Class spiClass, String propertiesFileName, String defaultImpl)
  306. throws DiscoveryException
  307. {
  308. return find(getClassLoaders(spiClass),
  309. new SPInterface(spiClass),
  310. new PropertiesHolder(propertiesFileName),
  311. new DefaultClassHolder(defaultImpl));
  312. }
  313. /**
  314. * Find class implementing SPI.
  315. *
  316. * @param spiClass Service Provider Interface Class.
  317. *
  318. * @param properties Used to determine name of SPI implementation,.
  319. *
  320. * @param defaultImpl Default implementation class.
  321. *
  322. * @return Class implementing the SPI.
  323. *
  324. * @exception DiscoveryException Thrown if the name of a class implementing
  325. * the SPI cannot be found, if the class cannot be loaded, or if
  326. * the resulting class does not implement (or extend) the SPI.
  327. */
  328. public static Class find(ClassLoaders loaders,
  329. SPInterface spi,
  330. PropertiesHolder properties,
  331. DefaultClassHolder defaultImpl)
  332. throws DiscoveryException
  333. {
  334. if (loaders == null) {
  335. loaders = ClassLoaders.getLibLoaders(spi.getSPClass(),
  336. DiscoverClass.class,
  337. true);
  338. }
  339. Properties props = (properties == null)
  340. ? null
  341. : properties.getProperties(spi, loaders);
  342. String[] classNames = discoverClassNames(spi, props);
  343. if (classNames.length > 0) {
  344. DiscoverClasses classDiscovery = new DiscoverClasses(loaders);
  345. ResourceClassIterator classes =
  346. classDiscovery.findResourceClasses(classNames[0]);
  347. // If it's set as a property.. it had better be there!
  348. if (classes.hasNext()) {
  349. ResourceClass info = classes.nextResourceClass();
  350. try {
  351. return info.loadClass();
  352. } catch (Exception e) {
  353. // ignore
  354. }
  355. }
  356. } else {
  357. ResourceNameIterator classIter =
  358. (new DiscoverServiceNames(loaders)).findResourceNames(spi.getSPName());
  359. ResourceClassIterator classes =
  360. (new DiscoverClasses(loaders)).findResourceClasses(classIter);
  361. if (!classes.hasNext() && defaultImpl != null) {
  362. return defaultImpl.getDefaultClass(spi, loaders);
  363. }
  364. // Services we iterate through until we find one that loads..
  365. while (classes.hasNext()) {
  366. ResourceClass info = classes.nextResourceClass();
  367. try {
  368. return info.loadClass();
  369. } catch (Exception e) {
  370. // ignore
  371. }
  372. }
  373. }
  374. throw new DiscoveryException("No implementation defined for " + spi.getSPName());
  375. // return null;
  376. }
  377. /**
  378. * Create new instance of class implementing SPI.
  379. *
  380. * @param spiClass Service Provider Interface Class.
  381. *
  382. * @return Instance of a class implementing the SPI.
  383. *
  384. * @exception DiscoveryException Thrown if the name of a class implementing
  385. * the SPI cannot be found, if the class cannot be loaded and
  386. * instantiated, or if the resulting class does not implement
  387. * (or extend) the SPI.
  388. */
  389. public Object newInstance(Class spiClass)
  390. throws DiscoveryException,
  391. InstantiationException,
  392. IllegalAccessException,
  393. NoSuchMethodException,
  394. InvocationTargetException
  395. {
  396. return newInstance(getClassLoaders(spiClass),
  397. new SPInterface(spiClass),
  398. nullProperties,
  399. nullDefaultImpl);
  400. }
  401. /**
  402. * Create new instance of class implementing SPI.
  403. *
  404. * @param spiClass Service Provider Interface Class.
  405. *
  406. * @param properties Used to determine name of SPI implementation,
  407. * and passed to implementation.init() method if
  408. * implementation implements Service interface.
  409. *
  410. * @return Instance of a class implementing the SPI.
  411. *
  412. * @exception DiscoveryException Thrown if the name of a class implementing
  413. * the SPI cannot be found, if the class cannot be loaded and
  414. * instantiated, or if the resulting class does not implement
  415. * (or extend) the SPI.
  416. */
  417. public Object newInstance(Class spiClass, Properties properties)
  418. throws DiscoveryException,
  419. InstantiationException,
  420. IllegalAccessException,
  421. NoSuchMethodException,
  422. InvocationTargetException
  423. {
  424. return newInstance(getClassLoaders(spiClass),
  425. new SPInterface(spiClass),
  426. new PropertiesHolder(properties),
  427. nullDefaultImpl);
  428. }
  429. /**
  430. * Create new instance of class implementing SPI.
  431. *
  432. * @param spiClass Service Provider Interface Class.
  433. *
  434. * @param defaultImpl Default implementation.
  435. *
  436. * @return Instance of a class implementing the SPI.
  437. *
  438. * @exception DiscoveryException Thrown if the name of a class implementing
  439. * the SPI cannot be found, if the class cannot be loaded and
  440. * instantiated, or if the resulting class does not implement
  441. * (or extend) the SPI.
  442. */
  443. public Object newInstance(Class spiClass, String defaultImpl)
  444. throws DiscoveryException,
  445. InstantiationException,
  446. IllegalAccessException,
  447. NoSuchMethodException,
  448. InvocationTargetException
  449. {
  450. return newInstance(getClassLoaders(spiClass),
  451. new SPInterface(spiClass),
  452. nullProperties,
  453. new DefaultClassHolder(defaultImpl));
  454. }
  455. /**
  456. * Create new instance of class implementing SPI.
  457. *
  458. * @param spiClass Service Provider Interface Class.
  459. *
  460. * @param properties Used to determine name of SPI implementation,
  461. * and passed to implementation.init() method if
  462. * implementation implements Service interface.
  463. *
  464. * @param defaultImpl Default implementation.
  465. *
  466. * @return Instance of a class implementing the SPI.
  467. *
  468. * @exception DiscoveryException Thrown if the name of a class implementing
  469. * the SPI cannot be found, if the class cannot be loaded and
  470. * instantiated, or if the resulting class does not implement
  471. * (or extend) the SPI.
  472. */
  473. public Object newInstance(Class spiClass, Properties properties, String defaultImpl)
  474. throws DiscoveryException,
  475. InstantiationException,
  476. IllegalAccessException,
  477. NoSuchMethodException,
  478. InvocationTargetException
  479. {
  480. return newInstance(getClassLoaders(spiClass),
  481. new SPInterface(spiClass),
  482. new PropertiesHolder(properties),
  483. new DefaultClassHolder(defaultImpl));
  484. }
  485. /**
  486. * Create new instance of class implementing SPI.
  487. *
  488. * @param spiClass Service Provider Interface Class.
  489. *
  490. * @param properties Used to determine name of SPI implementation,
  491. * and passed to implementation.init() method if
  492. * implementation implements Service interface.
  493. *
  494. * @param defaultImpl Default implementation.
  495. *
  496. * @return Instance of a class implementing the SPI.
  497. *
  498. * @exception DiscoveryException Thrown if the name of a class implementing
  499. * the SPI cannot be found, if the class cannot be loaded and
  500. * instantiated, or if the resulting class does not implement
  501. * (or extend) the SPI.
  502. */
  503. public Object newInstance(Class spiClass, String propertiesFileName, String defaultImpl)
  504. throws DiscoveryException,
  505. InstantiationException,
  506. IllegalAccessException,
  507. NoSuchMethodException,
  508. InvocationTargetException
  509. {
  510. return newInstance(getClassLoaders(spiClass),
  511. new SPInterface(spiClass),
  512. new PropertiesHolder(propertiesFileName),
  513. new DefaultClassHolder(defaultImpl));
  514. }
  515. /**
  516. * Create new instance of class implementing SPI.
  517. *
  518. * @param spiClass Service Provider Interface Class.
  519. *
  520. * @param properties Used to determine name of SPI implementation,
  521. * and passed to implementation.init() method if
  522. * implementation implements Service interface.
  523. *
  524. * @param defaultImpl Default implementation.
  525. *
  526. * @return Instance of a class implementing the SPI.
  527. *
  528. * @exception DiscoveryException Thrown if the name of a class implementing
  529. * the SPI cannot be found, if the class cannot be loaded and
  530. * instantiated, or if the resulting class does not implement
  531. * (or extend) the SPI.
  532. */
  533. public static Object newInstance(ClassLoaders loaders,
  534. SPInterface spi,
  535. PropertiesHolder properties,
  536. DefaultClassHolder defaultImpl)
  537. throws DiscoveryException,
  538. InstantiationException,
  539. IllegalAccessException,
  540. NoSuchMethodException,
  541. InvocationTargetException
  542. {
  543. return spi.newInstance(find(loaders, spi, properties, defaultImpl));
  544. }
  545. /**
  546. * <p>Discover names of SPI implementation Classes from properties.
  547. * The names are the non-null values, in order, obtained from the following
  548. * resources:
  549. * <ul>
  550. * <li>ManagedProperty.getProperty(SPI.class.getName());</li>
  551. * <li>properties.getProperty(SPI.class.getName());</li>
  552. * </ul>
  553. *
  554. * @param properties Properties that may define the implementation
  555. * class name(s).
  556. *
  557. * @return String[] Name of classes implementing the SPI.
  558. *
  559. * @exception DiscoveryException Thrown if the name of a class implementing
  560. * the SPI cannot be found.
  561. */
  562. public static String[] discoverClassNames(SPInterface spi,
  563. Properties properties)
  564. {
  565. Vector names = new Vector();
  566. String spiName = spi.getSPName();
  567. String propertyName = spi.getPropertyName();
  568. boolean includeAltProperty = !spiName.equals(propertyName);
  569. // Try the (managed) system property spiName
  570. String className = getManagedProperty(spiName);
  571. if (className != null) names.addElement(className);
  572. if (includeAltProperty) {
  573. // Try the (managed) system property propertyName
  574. className = getManagedProperty(propertyName);
  575. if (className != null) names.addElement(className);
  576. }
  577. if (properties != null) {
  578. // Try the properties parameter spiName
  579. className = properties.getProperty(spiName);
  580. if (className != null) names.addElement(className);
  581. if (includeAltProperty) {
  582. // Try the properties parameter propertyName
  583. className = properties.getProperty(propertyName);
  584. if (className != null) names.addElement(className);
  585. }
  586. }
  587. String[] results = new String[names.size()];
  588. names.copyInto(results);
  589. return results;
  590. }
  591. /**
  592. * Load the class whose name is given by the value of a (Managed)
  593. * System Property.
  594. *
  595. * @see ManagedProperties
  596. *
  597. * @param attribute the name of the system property whose value is
  598. * the name of the class to load.
  599. */
  600. public static String getManagedProperty(String propertyName) {
  601. String value;
  602. try {
  603. value = ManagedProperties.getProperty(propertyName);
  604. } catch (SecurityException e) {
  605. value = null;
  606. }
  607. return value;
  608. }
  609. }