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 org.apache.commons.discovery.DiscoveryException;
  64. /**
  65. * Represents a Service Programming Interface (spi).
  66. * - SPI's name
  67. * - SPI's (provider) class
  68. * - SPI's (alternate) override property name
  69. *
  70. * In addition, while there are many cases where this is NOT
  71. * usefull, for those in which it is:
  72. *
  73. * - expected constructor argument types and parameters values.
  74. *
  75. * @author Richard A. Sitze
  76. */
  77. public class SPInterface {
  78. /**
  79. * The service programming interface: intended to be
  80. * an interface or abstract class, but not limited
  81. * to those two.
  82. */
  83. private final Class spi;
  84. /**
  85. * The property name to be used for finding the name of
  86. * the SPI implementation class.
  87. */
  88. private final String propertyName;
  89. private Class paramClasses[] = null;
  90. private Object params[] = null;
  91. /**
  92. * Construct object representing Class <code>provider</code>.
  93. *
  94. * @param provider The SPI class
  95. */
  96. public SPInterface(Class provider) {
  97. this(provider, provider.getName());
  98. }
  99. /**
  100. * Construct object representing Class <code>provider</code>.
  101. *
  102. * @param provider The SPI class
  103. *
  104. * @param propertyName when looking for the name of a class implementing
  105. * the provider class, a discovery strategy may involve looking for
  106. * (system or other) properties having either the name of the class
  107. * (provider) or the <code>propertyName</code>.
  108. */
  109. public SPInterface(Class spi, String propertyName) {
  110. this.spi = spi;
  111. this.propertyName = propertyName;
  112. }
  113. /**
  114. * Construct object representing Class <code>provider</code>.
  115. *
  116. * @param provider The SPI class
  117. *
  118. * @param constructorParamClasses classes representing the
  119. * constructor argument types.
  120. *
  121. * @param constructorParams objects representing the
  122. * constructor arguments.
  123. */
  124. public SPInterface(Class provider,
  125. Class constructorParamClasses[],
  126. Object constructorParams[])
  127. {
  128. this(provider,
  129. provider.getName(),
  130. constructorParamClasses,
  131. constructorParams);
  132. }
  133. /**
  134. * Construct object representing Class <code>provider</code>.
  135. *
  136. * @param provider The SPI class
  137. *
  138. * @param propertyName when looking for the name of a class implementing
  139. * the provider class, a discovery strategy may involve looking for
  140. * (system or other) properties having either the name of the class
  141. * (provider) or the <code>propertyName</code>.
  142. *
  143. * @param constructorParamClasses classes representing the
  144. * constructor argument types.
  145. *
  146. * @param constructorParams objects representing the
  147. * constructor arguments.
  148. */
  149. public SPInterface(Class spi,
  150. String propertyName,
  151. Class constructorParamClasses[],
  152. Object constructorParams[])
  153. {
  154. this.spi = spi;
  155. this.propertyName = propertyName;
  156. this.paramClasses = constructorParamClasses;
  157. this.params = constructorParams;
  158. }
  159. public String getSPName() {
  160. return spi.getName();
  161. }
  162. public Class getSPClass() {
  163. return spi;
  164. }
  165. public String getPropertyName() {
  166. return propertyName;
  167. }
  168. /**
  169. * Instantiate a new
  170. */
  171. public Object newInstance(Class impl)
  172. throws DiscoveryException,
  173. InstantiationException,
  174. IllegalAccessException,
  175. NoSuchMethodException,
  176. InvocationTargetException
  177. {
  178. verifyAncestory(impl);
  179. return ClassUtils.newInstance(impl, paramClasses, params);
  180. }
  181. public void verifyAncestory(Class impl) {
  182. ClassUtils.verifyAncestory(spi, impl);
  183. }
  184. }