1. /*
  2. * ====================================================================
  3. *
  4. * The Apache Software License, Version 1.1
  5. *
  6. * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
  7. * reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. The end-user documentation included with the redistribution, if
  22. * any, must include the following acknowlegement:
  23. * "This product includes software developed by the
  24. * Apache Software Foundation (http://www.apache.org/)."
  25. * Alternately, this acknowlegement may appear in the software itself,
  26. * if and wherever such third-party acknowlegements normally appear.
  27. *
  28. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  29. * Foundation" must not be used to endorse or promote products derived
  30. * from this software without prior written permission. For written
  31. * permission, please contact apache@apache.org.
  32. *
  33. * 5. Products derived from this software may not be called "Apache"
  34. * nor may "Apache" appear in their names without prior written
  35. * permission of the Apache Group.
  36. *
  37. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  38. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  39. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  40. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  41. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  43. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  44. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  45. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  46. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  47. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  48. * SUCH DAMAGE.
  49. * ====================================================================
  50. *
  51. * This software consists of voluntary contributions made by many
  52. * individuals on behalf of the Apache Software Foundation. For more
  53. * information on the Apache Software Foundation, please see
  54. * <http://www.apache.org/>.
  55. *
  56. */
  57. package org.apache.commons.discovery.resource;
  58. import java.util.Vector;
  59. import org.apache.commons.discovery.jdk.JDKHooks;
  60. /**
  61. * There are many different contexts in which
  62. * loaders can be used. This provides a holder
  63. * for a set of class loaders, so that they
  64. * don't have to be build back up everytime...
  65. *
  66. * @author Richard A. Sitze
  67. * @author Craig R. McClanahan
  68. * @author Costin Manolache
  69. */
  70. public class ClassLoaders
  71. {
  72. protected Vector classLoaders = new Vector();
  73. /** Construct a new class loader set
  74. */
  75. public ClassLoaders() {
  76. }
  77. public int size() {
  78. return classLoaders.size();
  79. }
  80. public ClassLoader get(int idx) {
  81. return (ClassLoader)classLoaders.elementAt(idx);
  82. }
  83. /**
  84. * Specify a new class loader to be used in searching.
  85. * The order of loaders determines the order of the result.
  86. * It is recommended to add the most specific loaders first.
  87. */
  88. public void put(ClassLoader classLoader) {
  89. if (classLoader != null) {
  90. classLoaders.addElement(classLoader);
  91. }
  92. }
  93. /**
  94. * Specify a new class loader to be used in searching.
  95. * The order of loaders determines the order of the result.
  96. * It is recommended to add the most specific loaders first.
  97. *
  98. * @param prune if true, verify that the class loader is
  99. * not an Ancestor (@see isAncestor) before
  100. * adding it to our list.
  101. */
  102. public void put(ClassLoader classLoader, boolean prune) {
  103. if (classLoader != null && !(prune && isAncestor(classLoader))) {
  104. classLoaders.addElement(classLoader);
  105. }
  106. }
  107. /**
  108. * Check to see if <code>classLoader</code> is an
  109. * ancestor of any contained class loader.
  110. *
  111. * This can be used to eliminate redundant class loaders
  112. * IF all class loaders defer to parent class loaders
  113. * before resolving a class.
  114. *
  115. * It may be that this is not always true. Therefore,
  116. * this check is not done internally to eliminate
  117. * redundant class loaders, but left to the discretion
  118. * of the user.
  119. */
  120. public boolean isAncestor(final ClassLoader classLoader) {
  121. /* bootstrap classloader, at root of all trees! */
  122. if (classLoader == null)
  123. return true;
  124. for (int idx = 0; idx < size(); idx++) {
  125. for(ClassLoader walker = get(idx);
  126. walker != null;
  127. walker = walker.getParent())
  128. {
  129. if (walker == classLoader) {
  130. return true;
  131. }
  132. }
  133. }
  134. return false;
  135. }
  136. /**
  137. * Utility method. Returns a preloaded ClassLoaders instance
  138. * containing the following class loaders, in order:
  139. *
  140. * <ul>
  141. * <li>spi.getClassLoader</li>
  142. * <li>seeker.getClassLoader</li>
  143. * <li>System Class Loader</li>
  144. * </ul>
  145. *
  146. * Note that the thread context class loader is NOT present.
  147. * This is a reasonable set of loaders to try if the resource to be found
  148. * should be restricted to a libraries containing the SPI and Factory.
  149. *
  150. * @param spi WHAT is being looked for (an implementation of this class,
  151. * a default property file related to this class).
  152. * @param factory WHO is performing the lookup.
  153. * @param prune Determines if ancestors are allowed to be loaded or not.
  154. */
  155. public static ClassLoaders getLibLoaders(Class spi, Class factory, boolean prune) {
  156. ClassLoaders loaders = new ClassLoaders();
  157. if (spi != null) loaders.put(spi.getClassLoader());
  158. if (factory != null) loaders.put(factory.getClassLoader(), prune);
  159. loaders.put(JDKHooks.getJDKHooks().getSystemClassLoader(), prune);
  160. return loaders;
  161. }
  162. /**
  163. * Utility method. Returns a preloaded ClassLoaders instance
  164. * containing the following class loaders, in order:
  165. *
  166. * <ul>
  167. * <li>Thread Context Class Loader</li>
  168. * <li>spi.getClassLoader</li>
  169. * <li>seeker.getClassLoader</li>
  170. * <li>System Class Loader</li>
  171. * </ul>
  172. *
  173. * Note that the thread context class loader IS present.
  174. * This is a reasonable set of loaders to try if the resource to be found
  175. * may be provided by an application.
  176. *
  177. * @param spi WHAT is being looked for (an implementation of this class,
  178. * a default property file related to this class).
  179. * @param factory WHO is performing the lookup (factory).
  180. * @param prune Determines if ancestors are allowed to be loaded or not.
  181. */
  182. public static ClassLoaders getAppLoaders(Class spi, Class factory, boolean prune) {
  183. ClassLoaders loaders = new ClassLoaders();
  184. loaders.put(JDKHooks.getJDKHooks().getThreadContextClassLoader());
  185. if (spi != null) loaders.put(spi.getClassLoader(), prune);
  186. if (factory != null) loaders.put(factory.getClassLoader(), prune);
  187. loaders.put(JDKHooks.getJDKHooks().getSystemClassLoader(), prune);
  188. return loaders;
  189. }
  190. }