net.sourceforge.groboutils.util.classes.v1
Class SPILoader

java.lang.Object
  extended bynet.sourceforge.groboutils.util.classes.v1.SPILoader

public class SPILoader
extends java.lang.Object

Loads Service Provider Interface (SPI) classes from the given classloader (if any). This will search for files in the form /META-INF/services/classname. The discovered file will be parsed using java.util.Properties parsing method, but only the keys will be recognized (note that if a line contains no ':' or '=', then the line will be recognized as a key with an empty-string value). The found keys will be used as class names, constructed, and returned, using the ClassLoadHelper class in this package.

This is intended to follow the SPI interface spec, partially described in the JDK 1.4 documentation in the package docs for java.awt.im.spi.

Note that by using a Properties instance, this class is limited to only one instance of a class type per META-INF service file. This is assumed to be an adequate restriction, since in most circumstances, there should only be one instance of a service provider loaded per need.

Since:
June 28, 2002
Version:
$Date: 2003/05/08 14:12:21 $
Author:
Matt Albrecht groboclown@users.sourceforge.net

Constructor Summary
SPILoader(java.lang.Class spiBase)
          Use the context (thread) classloader or the system class loader.
SPILoader(java.lang.Class spiBase, java.lang.ClassLoader cl)
          Create a new SPILoader, loading the service files from the given class loader classpath.
 
Method Summary
 boolean hasNext()
          Discovers if there is another provider class instance to load.
 java.lang.Object nextProvier()
          Returns a new instance of the next class name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SPILoader

public SPILoader(java.lang.Class spiBase)
          throws java.io.IOException
Use the context (thread) classloader or the system class loader.

Parameters:
spiBase - the base class that all loaded SPI classes must implement. This is also used to define the name of the files to load from the META-INF directory.
Throws:
java.io.IOException - if there is an I/O problem loading the initial set of resources.

SPILoader

public SPILoader(java.lang.Class spiBase,
                 java.lang.ClassLoader cl)
          throws java.io.IOException
Create a new SPILoader, loading the service files from the given class loader classpath. If cl is null, then the context (thread) class loader or system class loader will be used instead.

Parameters:
spiBase - the base class that all loaded SPI classes must implement. This is also used to define the name of the files to load from the META-INF directory.
cl - classloader to load files from.
Throws:
java.io.IOException - if there is an I/O problem loading the initial set of resources.
Method Detail

hasNext

public boolean hasNext()
                throws java.io.IOException
Discovers if there is another provider class instance to load.

Returns:
true if there is another provider, otherwise false.
Throws:
java.io.IOException

nextProvier

public java.lang.Object nextProvier()
                             throws java.io.IOException
Returns a new instance of the next class name. If the defined next class is invalid, then an IOException is thrown. The returned object is guaranteed to be non-null and an instance of the constructor baseClass.

Returns:
the next provider in the list of discovered SPI providers.
Throws:
java.io.IOException - if there was an I/O error, or if the referenced resource defines an invalid class to load.
java.util.NoSuchElementException - if the enumeration is already at the end of the list.


Copyright © 2001-2003 by The GroboUtils Project