1 61 62 package org.apache.commons.discovery.tools; 63 64 import java.lang.reflect.Constructor ; 65 import java.lang.reflect.InvocationTargetException ; 66 67 import org.apache.commons.discovery.DiscoveryException; 68 69 70 83 public class SPInterface { 84 89 private final Class spi; 90 91 95 private final String propertyName; 96 97 98 private Class paramClasses[] = null; 99 private Object params[] = null; 100 101 102 107 public SPInterface(Class provider) { 108 this(provider, provider.getName()); 109 } 110 111 121 public SPInterface(Class spi, String propertyName) { 122 this.spi = spi; 123 this.propertyName = propertyName; 124 } 125 126 137 public SPInterface(Class provider, 138 Class constructorParamClasses[], 139 Object constructorParams[]) 140 { 141 this(provider, 142 provider.getName(), 143 constructorParamClasses, 144 constructorParams); 145 } 146 147 163 public SPInterface(Class spi, 164 String propertyName, 165 Class constructorParamClasses[], 166 Object constructorParams[]) 167 { 168 this.spi = spi; 169 this.propertyName = propertyName; 170 this.paramClasses = constructorParamClasses; 171 this.params = constructorParams; 172 } 173 174 public String getSPName() { 175 return spi.getName(); 176 } 177 178 public Class getSPClass() { 179 return spi; 180 } 181 182 public String getPropertyName() { 183 return propertyName; 184 } 185 186 189 public Object newInstance(Class impl) 190 throws DiscoveryException, 191 InstantiationException , 192 IllegalAccessException , 193 NoSuchMethodException , 194 InvocationTargetException 195 { 196 if (impl == null) { 197 throw new DiscoveryException("No implementation defined for " + getSPName()); 198 } 199 200 verifyAncestory(impl); 201 202 if (paramClasses == null || params == null) { 203 return impl.newInstance(); 204 } else { 205 Constructor constructor = impl.getConstructor(paramClasses); 206 return constructor.newInstance(params); 207 } 208 } 209 210 214 public void verifyAncestory(Class impl) { 215 if (!getSPClass().isAssignableFrom(impl)) { 216 throw new DiscoveryException("Class " + impl.getName() + 217 " does not implement " + getSPName()); 218 } 219 } 220 } 221 | Popular Tags |