/** * Construct object representing Class {@code provider}. * * @param <T> The SPI type * @param provider The SPI class * @param propertyName when looking for the name of a class implementing * the provider class, a discovery strategy may involve looking for * (system or other) properties having either the name of the class * (provider) or the <code>propertyName</code>. * @return A new object representing Class {@code provider} * @since 0.5 */ public static <T> SPInterface<T> newSPInterface(Class<T> provider, String propertyName) { return new SPInterface<T>(provider, propertyName); }
List<String> names = new LinkedList<String>(); String spiName = spi.getSPName(); String propertyName = spi.getPropertyName();
/** * Returns the {@code Properties} instance, loaded if necessary from {@code propertiesFileName}. * * @param spi Optional SPI (may be null). * If provided, an attempt is made to load the * property file as-per Class.getResource(). * * @param loaders Used only if properties need to be loaded. * * @return The {@code Properties}, loaded if necessary. */ public Properties getProperties(SPInterface<?> spi, ClassLoaders loaders) { if (properties == null) { properties = ResourceUtils.loadProperties(spi.getSPClass(), getPropertiesFileName(), loaders); } return properties; }
ClassLoaders loaders) { if (loaders == null) { loaders = ClassLoaders.getAppLoaders(spi.getSPClass(), Service.class, true); (new DiscoverServiceNames(loaders)).findResourceNames(spi.getSPName());
/** * Create new instance of class implementing SPI. * * @param <T> The SPI type * @param loaders The class loaders holder * @param spi Service Provider Interface Class. * @param properties Used to determine name of SPI implementation, * and passed to implementation.init() method if * implementation implements Service interface. * @param defaultImpl Default implementation. * @return Instance of a class implementing the SPI. * @exception DiscoveryException Thrown if the name of a class implementing * the SPI cannot be found, if the class cannot be loaded and * instantiated, or if the resulting class does not implement * (or extend) the SPI. * @throws InstantiationException see {@link Class#newInstance()} * @throws IllegalAccessException see {@link Class#newInstance()} * @throws NoSuchMethodException see {@link Class#newInstance()} * @throws InvocationTargetException see {@link Class#newInstance()} */ public static <T> T newInstance(ClassLoaders loaders, SPInterface<T> spi, PropertiesHolder properties, DefaultClassHolder<T> defaultImpl) throws DiscoveryException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { return spi.newInstance(find(loaders, spi, properties, defaultImpl)); }
DefaultClassHolder<T> defaultImpl) throws DiscoveryException { if (loaders == null) { loaders = ClassLoaders.getLibLoaders(spi.getSPClass(), DiscoverClass.class, true); (new DiscoverServiceNames(loaders)).findResourceNames(spi.getSPName()); throw new DiscoveryException("No implementation defined for " + spi.getSPName(), error);
private S getNextClassInstance() { while (services.hasNext()) { ResourceClass<S> info = services.nextResourceClass(); try { return spi.newInstance(info.loadClass()); } catch (Exception e) { // ignore } catch (LinkageError le) { // ignore } } return null; } };
/** * Construct object representing Class {@code provider}. * * @param <T> The SPI type * @param provider The SPI class * @param propertyName when looking for the name of a class implementing * the provider class, a discovery strategy may involve looking for * (system or other) properties having either the name of the class * (provider) or the <code>propertyName</code>. * @param constructorParamClasses classes representing the * constructor argument types * @param constructorParams objects representing the * constructor arguments * @return A new object representing Class {@code provider} * @since 0.5 */ public static <T> SPInterface<T> newSPInterface(Class<T> provider, String propertyName, Class<?> constructorParamClasses[], Object constructorParams[]) { return new SPInterface<T>(provider, propertyName, constructorParamClasses, constructorParams); }
/** * as described in * sun/jdk1.3.1/docs/guide/jar/jar.html#Service Provider, * Except this uses <code>Enumeration</code> * instead of <code>Interator</code>. * * @param <T> Service Provider Interface type * @param <S> Any type extends the SPI type * @param spiClass Service Provider Interface Class * @return Enumeration of class instances ({@code S}) */ public static <T, S extends T> Enumeration<S> providers(Class<T> spiClass) { return providers(new SPInterface<T>(spiClass), null); }
/** * Find implementation of SPI. * * @param <T> Service Provider Interface type. * @param spiClass Service Provider Interface Class. * * @return Instance of a class implementing the SPI. * * @exception DiscoveryException Thrown if the name of a class implementing * the SPI cannot be found, if the class cannot be loaded and * instantiated, or if the resulting class does not implement * (or extend) the SPI. */ public static <T> T find(Class<T> spiClass) throws DiscoveryException { return find(null, new SPInterface<T>(spiClass), DiscoverClass.nullProperties, (DefaultClassHolder<T>) null); }
public static Object newInstance(Class spiClass, Class defaultClass) { return newInstance(new SPInterface(spiClass), new DefaultClassHolder(defaultClass)); }
public static Object newInstance(Class spiClass, Class defaultClass) { return newInstance(new SPInterface(spiClass), new DefaultClassHolder(defaultClass)); }
public static Object newInstance(Class spiClass, Class defaultClass) { return newInstance(new SPInterface(spiClass), new DefaultClassHolder(defaultClass)); }
/** * Find class implementing SPI. * * @param <T> The SPI type * @param <S> Any class extending T * @param spiClass Service Provider Interface Class. * @return Class implementing the SPI. * @exception DiscoveryException Thrown if the name of a class implementing * the SPI cannot be found, if the class cannot be loaded, or if * the resulting class does not implement (or extend) the SPI. */ public <T, S extends T> Class<S> find(Class<T> spiClass) throws DiscoveryException { return find(getClassLoaders(spiClass), new SPInterface<T>(spiClass), nullProperties, (DefaultClassHolder<T>) null); }
private static SPInterface getWSAG4JSPI() { // get the library classloaders for discovery ClassLoaders loaders = ClassLoaders.getLibLoaders( WSAG4JConfigurationEnvironment.class, DiscoverClass.class, true ); // load the properties file via the classloaders Properties properties = ResourceUtils.loadProperties( WSAG4JConfigurationEnvironment.class, WSAG4JEnvironment.DEFAULT_CONFIGURATION_FILE, loaders ); if ( properties == null ) { properties = new Properties(); } // find the path of the application configuration String cPath = properties.getProperty( WSAG4JEnvironment.DEFAULT_CONFIGURATION_PATH_KEY, WSAG4JEnvironment.DEFAULT_CONFIGURATION_PATH ); // instantiate new SPI with the configuration path // the configuration path is passed to the MSSConfigurationEnvironment // class on instantiation of the class by the discovery framework SPInterface mssConfigSP = new SPInterface( WSAG4JConfigurationEnvironment.class, new Class[] { String.class }, new Object[] { cPath } ); return mssConfigSP; }
/** * {@inheritDoc} */ @SuppressWarnings("unchecked") public Set<Violation> getCheckedViolations() { final Set<Violation> violations = new HashSet<Violation>(); final ClassLoaders classLoaders = new ClassLoaders(); final Class<Document>[] classes = new Class[] { Document.class }; final Document[] objects = new Document[] { new Document() }; classLoaders.put(Document.class.getClassLoader()); final Enumeration<Delegate> delegates = Service.providers(new SPInterface(Delegate.class, classes, objects), classLoaders); while (delegates.hasMoreElements()) { violations.addAll(delegates.nextElement().getCheckedViolations()); } return violations; }
/** * Find implementation of SPI. * * @param <T> Service Provider Interface type * * @param spiClass Service Provider Interface Class. * * @param defaultImpl Default implementation. * * @return Instance of a class implementing the SPI. * * @exception DiscoveryException Thrown if the name of a class implementing * the SPI cannot be found, if the class cannot be loaded and * instantiated, or if the resulting class does not implement * (or extend) the SPI. */ public static <T> T find(Class<T> spiClass, String defaultImpl) throws DiscoveryException { return find(null, new SPInterface<T>(spiClass), DiscoverClass.nullProperties, new DefaultClassHolder<T>(defaultImpl)); }
/** * Create new instance of class implementing SPI. * * @param <T> The SPI type * @param spiClass Service Provider Interface Class. * @return Instance of a class implementing the SPI. * @exception DiscoveryException Thrown if the name of a class implementing * the SPI cannot be found, if the class cannot be loaded and * instantiated, or if the resulting class does not implement * (or extend) the SPI. * @throws InstantiationException see {@link Class#newInstance()} * @throws IllegalAccessException see {@link Class#newInstance()} * @throws NoSuchMethodException see {@link Class#newInstance()} * @throws InvocationTargetException see {@link Class#newInstance()} */ public <T> T newInstance(Class<T> spiClass) throws DiscoveryException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { return newInstance(getClassLoaders(spiClass), new SPInterface<T>(spiClass), nullProperties, (DefaultClassHolder<T>) null); }
/** * Find implementation of SPI. * * @param <T> Service Provider Interface type * * @param spiClass Service Provider Interface Class. * * @param properties Used to determine name of SPI implementation, * and passed to implementation.init() method if * implementation implements Service interface. * * @return Instance of a class implementing the SPI. * * @exception DiscoveryException Thrown if the name of a class implementing * the SPI cannot be found, if the class cannot be loaded and * instantiated, or if the resulting class does not implement * (or extend) the SPI. */ public static <T> T find(Class<T> spiClass, Properties properties) throws DiscoveryException { return find(null, new SPInterface<T>(spiClass), new PropertiesHolder(properties), (DefaultClassHolder<T>) null); }