6 import java.lang.annotation.*;
9 private final Class<? extends Annotation> an;
10 private final ClassLoader cl;
11 private final Map<Class<?>, Object> instances = new HashMap<Class<?>, Object>();
13 private Loader(Class<? extends Annotation> annotation, ClassLoader loader) {
18 public Iterable<String> names() {
19 return(new Iterable<String>() {
20 public Iterator<String> iterator() {
21 return(new Iterator<String>() {
22 private Enumeration<URL> rls;
23 private Iterator<String> cur = null;
25 private Iterator<String> parse(URL url) {
27 List<String> buf = new LinkedList<String>();
28 InputStream in = url.openStream();
30 BufferedReader r = new BufferedReader(new InputStreamReader(in, "utf-8"));
32 while((ln = r.readLine()) != null) {
38 return(buf.iterator());
42 } catch(IOException e) {
43 throw(new GlobAccessException(e));
47 public boolean hasNext() {
48 if((cur == null) || !cur.hasNext()) {
51 rls = cl.getResources("META-INF/glob/" + an.getName());
52 } catch(IOException e) {
53 throw(new GlobAccessException(e));
56 if(!rls.hasMoreElements())
58 URL u = rls.nextElement();
64 public String next() {
66 throw(new NoSuchElementException());
67 String ret = cur.next();
71 public void remove() {throw(new UnsupportedOperationException());}
77 public Iterable<Class<?>> classes() {
78 return(new Iterable<Class<?>>() {
79 public Iterator<Class<?>> iterator() {
80 return(new Iterator<Class<?>>() {
81 private final Iterator<String> names = names().iterator();
82 private Class<?> n = null;
84 public boolean hasNext() {
88 String nm = names.next();
92 } catch(ClassNotFoundException e) {
95 if(c.getAnnotation(an) == null)
102 public Class<?> next() {
104 throw(new NoSuchElementException());
110 public void remove() {throw(new UnsupportedOperationException());}
116 public Iterable<?> instances() {
117 return(new Iterable<Object>() {
118 public Iterator<Object> iterator() {
119 return(new Iterator<Object>() {
120 private final Iterator<Class<?>> classes = classes().iterator();
121 private Object n = null;
123 public boolean hasNext() {
125 if(!classes.hasNext())
127 Class<?> cl = classes.next();
130 inst = cl.newInstance();
131 } catch(InstantiationException e) {
132 throw(new GlobInstantiationException(e));
133 } catch(IllegalAccessException e) {
134 throw(new GlobInstantiationException(e));
141 public Object next() {
143 throw(new NoSuchElementException());
149 public void remove() {throw(new UnsupportedOperationException());}
155 public static Loader get(Class<? extends Annotation> annotation, ClassLoader loader) {
156 return(new Loader(annotation, loader));
159 public static Loader get(Class<? extends Annotation> annotation) {
160 return(get(annotation, annotation.getClassLoader()));