[Japi-cvs] SF.net SVN: japi: [325] libs/registry/trunk/src/net/sf/japi/registry
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2007-01-30 22:19:54
|
Revision: 325 http://svn.sourceforge.net/japi/?rev=325&view=rev Author: christianhujer Date: 2007-01-30 14:19:50 -0800 (Tue, 30 Jan 2007) Log Message: ----------- Added registry implementation. Added Paths: ----------- libs/registry/trunk/src/net/sf/japi/registry/NamedRegistry.java libs/registry/trunk/src/net/sf/japi/registry/NamedService.java Added: libs/registry/trunk/src/net/sf/japi/registry/NamedRegistry.java =================================================================== --- libs/registry/trunk/src/net/sf/japi/registry/NamedRegistry.java (rev 0) +++ libs/registry/trunk/src/net/sf/japi/registry/NamedRegistry.java 2007-01-30 22:19:50 UTC (rev 325) @@ -0,0 +1,104 @@ +/* + * JAPI libs-registry is a library for providing a framework implementation lookup mechanism. + * Copyright (C) 2007 Christian Hujer. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.sf.japi.registry; + +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.NotNull; +import sun.misc.Service; +import java.util.Iterator; +import java.util.Collection; +import java.util.List; +import java.util.ArrayList; + +/** + * Lookup mechanism for {@link NamedService} implementations. + * The current implementation for Java 1.5 is based on {@link Service}. + * Future implementations (Java 1.6+) will be based on the corresponding replacement. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class NamedRegistry { + + /** + * Returns the implementation of the supplied {@link NamedService} with the specified name. + * @param service NamedService to get + * @param name Name of the implementation of the NamedService to get + * @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(); ) { + final T t = it.next(); + if (t.getName().equals(name)) { + return t; + } + } + return null; + } + + /** + * Returns all implementations of the supplied class. + * The returned list is yours, you may do with it whatever you like. + * It's not cached or so. + * Because this method is anonymous (not bound to names), it can be used for any kind of service, not just implementations of {@link NamedService}. + * @param service NamedService to get implementations for. + * @return List with all implementations, empty collection if none found. + */ + @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(); ) { + instances.add(it.next()); + } + return instances; + } + + /** + * Returns the implementation of the supplied {@link NamedService} with the specified name using the specified ClassLoader. + * @param service NamedService to get + * @param name Name of the implementation of the NamedService to get + * @param classLoader ClassLoader to use. + * @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(); ) { + final T t = it.next(); + if (t.getName().equals(name)) { + return t; + } + } + return null; + } + + /** + * Returns all implementations of the supplied class using the specified ClassLoader. + * The returned list is yours, you may do with it whatever you like. + * It's not cached or so. + * Because this method is anonymous (not bound to names), it can be used for any kind of service, not just implementations of {@link NamedService}. + * @param service Class to get implementations for. + * @param classLoader ClassLoader to use. + * @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(); ) { + instances.add(it.next()); + } + return instances; + } + +} // class NamedRegistry Property changes on: libs/registry/trunk/src/net/sf/japi/registry/NamedRegistry.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/registry/trunk/src/net/sf/japi/registry/NamedService.java =================================================================== --- libs/registry/trunk/src/net/sf/japi/registry/NamedService.java (rev 0) +++ libs/registry/trunk/src/net/sf/japi/registry/NamedService.java 2007-01-30 22:19:50 UTC (rev 325) @@ -0,0 +1,54 @@ +/* + * JAPI libs-registry is a library for providing a framework implementation lookup mechanism. + * Copyright (C) 2007 Christian Hujer. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.sf.japi.registry; + +import org.jetbrains.annotations.NotNull; +import java.util.Locale; + +/** + * A named service can be instantiated with a public non-arg (default) constructor and provides two names. + * The first name is a technical name, suitable for locale <code>C_TYPE</code>, that's a language independent name. + * If there is no language independent name, the English name is used. + * The second name is a descriptive name, suitable for + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public interface NamedService { + + /** + * Returns a technical name, suitable for locale <code>C_TYPE</code>, command line arguments etc.. + * The returned name might be the same as the name returned by {@link #getDisplayName()} for English. + * @return A technical name. + */ + @NotNull String getName(); + + /** + * Returns a display name for the default locale. + * @return A display name. + */ + @NotNull String getDisplayName(); + + /** + * Returns a display name for the specified locale. + * @param locale Locale to get display name for. + * @return A display name for the specified locale. + */ + @NotNull String getDisplayName(@NotNull Locale locale); + +} // interface NamedService Property changes on: libs/registry/trunk/src/net/sf/japi/registry/NamedService.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |