[Japi-cvs] SF.net SVN: japi:[1375] libs/registry/trunk
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2009-09-11 19:53:25
|
Revision: 1375 http://japi.svn.sourceforge.net/japi/?rev=1375&view=rev Author: christianhujer Date: 2009-09-11 19:53:17 +0000 (Fri, 11 Sep 2009) Log Message: ----------- Make registry compatible with 1.6 and 1.5 at the same time. Modified Paths: -------------- libs/registry/trunk/build.xml libs/registry/trunk/src/prj/net/sf/japi/registry/NamedRegistry.java Modified: libs/registry/trunk/build.xml =================================================================== --- libs/registry/trunk/build.xml 2009-07-26 10:41:50 UTC (rev 1374) +++ libs/registry/trunk/build.xml 2009-09-11 19:53:17 UTC (rev 1375) @@ -21,6 +21,8 @@ ]> <project name="japi lib registry" default="all"> + <property name="javaversion" value="1.5" /> + &commonBuild; </project> Modified: libs/registry/trunk/src/prj/net/sf/japi/registry/NamedRegistry.java =================================================================== --- libs/registry/trunk/src/prj/net/sf/japi/registry/NamedRegistry.java 2009-07-26 10:41:50 UTC (rev 1374) +++ libs/registry/trunk/src/prj/net/sf/japi/registry/NamedRegistry.java 2009-09-11 19:53:17 UTC (rev 1375) @@ -22,6 +22,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; +import java.util.ServiceLoader; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import sun.misc.Service; @@ -46,7 +47,7 @@ * @return Implementation or <code>null</code> if none found. */ @Nullable public static <T extends NamedService> T getInstance(@NotNull final Class<T> service, @NotNull final String name) { - for (final Iterator<T> it = Service.providers(service); it.hasNext();) { + for (final Iterator<T> it = instanceIterator(service); it.hasNext();) { final T t = it.next(); if (t.getName().equals(name)) { return t; @@ -65,7 +66,7 @@ */ @NotNull public static <T> Collection<T> getAllInstances(@NotNull final Class<T> service) { final List<T> instances = new ArrayList<T>(); - for (final Iterator<T> it = Service.providers(service); it.hasNext();) { + for (final Iterator<T> it = instanceIterator(service); it.hasNext();) { instances.add(it.next()); } return instances; @@ -79,7 +80,7 @@ * @return Implementation or <code>null</code> if none found. */ @Nullable public static <T extends NamedService> T getInstance(@NotNull final Class<T> service, @NotNull final String name, @NotNull final ClassLoader classLoader) { - for (final Iterator<T> it = Service.providers(service, classLoader); it.hasNext();) { + for (final Iterator<T> it = instanceIterator(service, classLoader); it.hasNext();) { final T t = it.next(); if (t.getName().equals(name)) { return t; @@ -98,11 +99,39 @@ * @return List with all implementations, empty collection if none found. */ @NotNull public static <T> Collection<T> getAllInstances(@NotNull final Class<T> service, @NotNull final ClassLoader classLoader) { - final List<T> instances = new ArrayList<T>(); - for (final Iterator<T> it = Service.providers(service, classLoader); it.hasNext();) { + final Collection<T> instances = new ArrayList<T>(); + for (final Iterator<T> it = instanceIterator(service, classLoader); it.hasNext();) { instances.add(it.next()); } return instances; } -} // class NamedRegistry + /** + * Returns a Service iterator. + * On JDK1.6 and later, the Iterator is created using {@link ServiceLoader}, on earlier platforms it is created using {@link Service}. + * @param service Class for which to return a service iterator. + * @return Iterator for {@code service}. + */ + @NotNull public static <T> Iterator<T> instanceIterator(@NotNull final Class<T> service) { + try { + return ServiceLoader.load(service).iterator(); + } catch (final NoClassDefFoundError e) { + return Service.providers(service); + } + } + + /** + * Returns a Service iterator. + * On JDK1.6 and later, the Iterator is created using {@link ServiceLoader}, on earlier platforms it is created using {@link Service}. + * @param service Class for which to return a service iterator. + * @param classLoader ClassLoader to use. + * @return Iterator for {@code service}. + */ + @NotNull public static <T> Iterator<T> instanceIterator(@NotNull final Class<T> service, @NotNull final ClassLoader classLoader) { + try { + return ServiceLoader.load(service, classLoader).iterator(); + } catch (final NoClassDefFoundError e) { + return Service.providers(service, classLoader); + } + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |