Niclas Hedhman wrote:
> On Wed, Jul 9, 2008 at 2:57 PM, Georg Ragaller <[EMAIL PROTECTED]> wrote:
>
>> Why not use the Service Provider mechanism from the JRE?
>>
>> http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Service%20Provider
>>
>> Up to 1.5 the API is unfortunately not public
>> (sun.misc.Service#providers(Class)), but starting with 1.6 it is:
>>
>> http://java.sun.com/javase/6/docs/api/java/util/ServiceLoader.html
>>
>> The 1.5 implementation of the API can easily be mimiced, it's about 50
>> lines, I've already done this in a project of mine.
>
> Yeah, this sounds like a good way... Any chance you could contribute
> that code (provided you have not peeked at the Sun solution and
> triggering all kind of licensing issues), as I don't want to depend on
> sun.misc classes...
>
Never peeked at it :), here it is, it's compilable against 1.4 (without
logging statements also for 1.3). So maybe you might want to add some
generics stuff, remove the log statements and add #initCause() at the
throw statements.
The implementation was written with the information in the
docs/guide/jar/jar.html in mind. I've seen that the
java/util/ServiceLoader.html contains much more information like lazy
loading, etc., so you might want to enhance this also.
Georg
<Service.java>
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Logger;
public class Service
{
private static final Logger LOG = Logger.getAnonymousLogger();
public Iterator providers(Class clazz) throws IOException
{
return
providers(clazz,Thread.currentThread().getContextClassLoader());
}
public Iterator providers(Class clazz, ClassLoader ldr) throws
IOException
{
LOG.fine("searching providers for " + clazz);
Map result = new HashMap();
Enumeration cfg = ldr.getResources("META-INF/services/" +
clazz.getName());
while (cfg.hasMoreElements())
{
URL rc = (URL) cfg.nextElement();
InputStream in = rc.openStream();
try
{
BufferedReader rd = new BufferedReader(new
InputStreamReader(in,"UTF-8"));
try
{
LOG.fine("found provider file " + rc);
for (String line = rd.readLine() ; null != line ;
line = rd.readLine())
{
line = line.trim();
if (!line.startsWith("#"))
{
try
{
Class provider =
Class.forName(line,true,ldr);
Object instance = provider.newInstance();
if (result.containsKey(provider))
{
LOG.severe("provider exists
already: " + instance.getClass());
}
else
{
LOG.fine("found provider: " +
instance.getClass());
result.put(provider,instance);
}
}
catch (ClassNotFoundException ex)
{
throw new IOException(ex.getMessage());
}
catch (InstantiationException ex)
{
throw new IOException(ex.getMessage());
}
catch (IllegalAccessException ex)
{
throw new IOException(ex.getMessage());
}
}
}
}
finally
{
rd.close();
}
}
finally
{
in.close();
}
}
return result.values().iterator();
}
}
</Service.java>
_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev