[Japi-cvs] SF.net SVN: japi: [132] trunk/src/app/net/sf/japi/util/Registry.java
Status: Beta
Brought to you by:
christianhujer
|
From: <chr...@us...> - 2006-07-09 11:42:15
|
Revision: 132 Author: christianhujer Date: 2006-07-09 04:42:07 -0700 (Sun, 09 Jul 2006) ViewCVS: http://svn.sourceforge.net/japi/?rev=132&view=rev Log Message: ----------- Added Registry class. Added Paths: ----------- trunk/src/app/net/sf/japi/util/Registry.java Added: trunk/src/app/net/sf/japi/util/Registry.java =================================================================== --- trunk/src/app/net/sf/japi/util/Registry.java (rev 0) +++ trunk/src/app/net/sf/japi/util/Registry.java 2006-07-09 11:42:07 UTC (rev 132) @@ -0,0 +1,92 @@ +package net.sf.japi.util; + +import java.util.ArrayList; +import static java.util.Arrays.asList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.PropertyResourceBundle; +import java.net.URL; +import java.io.IOException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * A Registry is a class that allows registration of instances of classes that implement a certain interface. + * The Registry automatically scans classloaders, resource bundles, environment variables and system properties for looking up these classes. + * All that a Registry needs to know for that is a property key name. + * <p /> + * @note This has nothing to do with MS Windows Registry. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class Registry<T> { + + /** The registered instances of T. */ + private List<T> instances = new ArrayList<T>(); + + private String[] prefixes = { "", "META-INF/services/" }; + + /** + * Create a Registry. + * @param keyname Key name + * @param classLoadersToUse optional classloaders to use in the search + */ + public Registry(@NotNull final String keyname, @Nullable final Iterable<ClassLoader> classLoadersToUse) { + final List<String> stringsWithClassNames = new ArrayList<String>(); + stringsWithClassNames.add(System.getProperty(keyname)); + stringsWithClassNames.add(System.getenv(keyname)); + final Set<ClassLoader> classLoaders = new HashSet<ClassLoader>(); + for (final ClassLoader classLoader : classLoadersToUse) { + classLoaders.add(classLoader); + } + // Add own class loader, add context class loader + while (classLoaders.remove(null)); + for (final ClassLoader classLoader : classLoaders) { + for (final String prefix : prefixes) { + try { + for (final URL url : Collections.list(classLoader.getResources(prefix + keyname))) { + stringsWithClassNames.add(new PropertyResourceBundle(url.openStream()).getString(keyname)); + } + } catch (final IOException e) { + e.printStackTrace(); + } + } + } + while (stringsWithClassNames.remove(null)); // remove entries that eventually are null + final Set<String> classNames = new HashSet<String>(); + for (final String stringWithClassNames : stringsWithClassNames) { + classNames.addAll(asList(stringWithClassNames.split("[^.\\P{javaJavaIdentifierPart}]+"))); + } + for (final String className : classNames) { + try { + add(((Class<? extends T>) Class.forName(className)).newInstance()); + } catch (final ClassCastException e) { + e.printStackTrace(); + } catch (final InstantiationException e) { + e.printStackTrace(); + } catch (final IllegalAccessException e) { + e.printStackTrace(); + } catch (final ClassNotFoundException e) { + e.printStackTrace(); + } + } + } + + /** + * Add an instance. + * @param t Instance to add + */ + protected void add(@NotNull final T t) { + instances.add(t); + } + + /** + * Get all registered instances. + * @return all registered instances + */ + public List<T> getInstances() { + return Collections.unmodifiableList(instances); + } + +} // class Registry Property changes on: trunk/src/app/net/sf/japi/util/Registry.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. |