View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.discovery.tools;
18  
19  import java.lang.reflect.InvocationTargetException;
20  
21  import org.apache.commons.discovery.DiscoveryException;
22  
23  
24  /***
25   * Represents a Service Programming Interface (spi).
26   * - SPI's name
27   * - SPI's (provider) class
28   * - SPI's (alternate) override property name
29   * 
30   * In addition, while there are many cases where this is NOT
31   * usefull, for those in which it is:
32   * 
33   * - expected constructor argument types and parameters values.
34   * 
35   * @author Richard A. Sitze
36   */
37  public class SPInterface {
38      /***
39       * The service programming interface: intended to be
40       * an interface or abstract class, but not limited
41       * to those two.
42       */        
43      private final Class spi;
44      
45      /***
46       * The property name to be used for finding the name of
47       * the SPI implementation class.
48       */
49      private final String propertyName;
50      
51      
52      private Class  paramClasses[] = null;
53      private Object params[] = null;
54  
55  
56      /***
57       * Construct object representing Class <code>provider</code>.
58       * 
59       * @param provider The SPI class
60       */
61      public SPInterface(Class provider) {
62          this(provider, provider.getName());
63      }
64      
65      /***
66       * Construct object representing Class <code>provider</code>.
67       * 
68       * @param spi The SPI class
69       * 
70       * @param propertyName when looking for the name of a class implementing
71       *        the provider class, a discovery strategy may involve looking for
72       *        (system or other) properties having either the name of the class
73       *        (provider) or the <code>propertyName</code>.
74       */
75      public SPInterface(Class spi, String propertyName) {
76          this.spi = spi;
77          this.propertyName = propertyName;
78      }
79  
80      /***
81       * Construct object representing Class <code>provider</code>.
82       * 
83       * @param provider The SPI class
84       * 
85       * @param constructorParamClasses classes representing the
86       *        constructor argument types.
87       * 
88       * @param constructorParams objects representing the
89       *        constructor arguments.
90       */
91      public SPInterface(Class provider,
92                         Class constructorParamClasses[],
93                         Object constructorParams[])
94      {
95          this(provider,
96               provider.getName(),
97               constructorParamClasses,
98               constructorParams);
99      }
100     
101     /***
102      * Construct object representing Class <code>provider</code>.
103      * 
104      * @param spi The SPI class
105      * 
106      * @param propertyName when looking for the name of a class implementing
107      *        the provider class, a discovery strategy may involve looking for
108      *        (system or other) properties having either the name of the class
109      *        (provider) or the <code>propertyName</code>.
110      * 
111      * @param constructorParamClasses classes representing the
112      *        constructor argument types.
113      * 
114      * @param constructorParams objects representing the
115      *        constructor arguments.
116      */
117     public SPInterface(Class spi,
118                        String propertyName,
119                        Class constructorParamClasses[],
120                        Object constructorParams[])
121     {
122         this.spi = spi;
123         this.propertyName = propertyName;
124         this.paramClasses = constructorParamClasses;
125         this.params = constructorParams;
126     }
127 
128     public String getSPName() {
129         return spi.getName();
130     }
131 
132     public Class getSPClass() {
133         return spi;
134     }
135     
136     public String getPropertyName() {
137         return propertyName;
138     }
139 
140     /***
141      * Instantiate a new 
142      */    
143     public Object newInstance(Class impl)
144         throws DiscoveryException,
145                InstantiationException,
146                IllegalAccessException,
147                NoSuchMethodException,
148                InvocationTargetException
149     {
150         verifyAncestory(impl);
151         
152         return ClassUtils.newInstance(impl, paramClasses, params);
153     }
154     
155     public void verifyAncestory(Class impl) {
156         ClassUtils.verifyAncestory(spi, impl);
157     }
158 }