1 7 8 package com.sun.naming.internal; 9 10 import java.util.List ; 11 import javax.naming.NamingException ; 12 13 20 21 public final class FactoryEnumeration { 23 private List factories; 24 private int posn = 0; 25 private ClassLoader loader; 26 27 45 FactoryEnumeration(List factories, ClassLoader loader) { 46 this.factories = factories; 47 this.loader = loader; 48 } 49 50 public Object next() throws NamingException { 51 synchronized (factories) { 52 53 NamedWeakReference ref = (NamedWeakReference) factories.get(posn++); 54 Object answer = ref.get(); 55 if ((answer != null) && !(answer instanceof Class )) { 56 return answer; 57 } 58 59 String className = ref.getName(); 60 61 try { 62 if (answer == null) { answer = Class.forName(className, true, loader); 64 } 65 answer = ((Class ) answer).newInstance(); 67 ref = new NamedWeakReference(answer, className); 68 factories.set(posn-1, ref); return answer; 70 } catch (ClassNotFoundException e) { 71 NamingException ne = 72 new NamingException ("No longer able to load " + className); 73 ne.setRootCause(e); 74 throw ne; 75 } catch (InstantiationException e) { 76 NamingException ne = 77 new NamingException ("Cannot instantiate " + answer); 78 ne.setRootCause(e); 79 throw ne; 80 } catch (IllegalAccessException e) { 81 NamingException ne = new NamingException ("Cannot access " + answer); 82 ne.setRootCause(e); 83 throw ne; 84 } 85 } 86 } 87 88 public boolean hasMore() { 89 synchronized (factories) { 90 return posn < factories.size(); 91 } 92 } 93 } 94 | Popular Tags |