69 this.loader = loader;
70 }
71
72 public Object next() throws NamingException {
73 synchronized (factories) {
74
75 NamedWeakReference<Object> ref = factories.get(posn++);
76 Object answer = ref.get();
77 if ((answer != null) && !(answer instanceof Class)) {
78 return answer;
79 }
80
81 String className = ref.getName();
82
83 try {
84 if (answer == null) { // reload class if weak ref cleared
85 Class<?> cls = Class.forName(className, true, loader);
86 answer = cls;
87 }
88 // Instantiate Class to get factory
89 answer = ((Class) answer).newInstance();
90 ref = new NamedWeakReference<>(answer, className);
91 factories.set(posn-1, ref); // replace Class object or null
92 return answer;
93 } catch (ClassNotFoundException e) {
94 NamingException ne =
95 new NamingException("No longer able to load " + className);
96 ne.setRootCause(e);
97 throw ne;
98 } catch (InstantiationException e) {
99 NamingException ne =
100 new NamingException("Cannot instantiate " + answer);
101 ne.setRootCause(e);
102 throw ne;
103 } catch (IllegalAccessException e) {
104 NamingException ne = new NamingException("Cannot access " + answer);
105 ne.setRootCause(e);
106 throw ne;
107 }
108 }
109 }
|
69 this.loader = loader;
70 }
71
72 public Object next() throws NamingException {
73 synchronized (factories) {
74
75 NamedWeakReference<Object> ref = factories.get(posn++);
76 Object answer = ref.get();
77 if ((answer != null) && !(answer instanceof Class)) {
78 return answer;
79 }
80
81 String className = ref.getName();
82
83 try {
84 if (answer == null) { // reload class if weak ref cleared
85 Class<?> cls = Class.forName(className, true, loader);
86 answer = cls;
87 }
88 // Instantiate Class to get factory
89 @SuppressWarnings("deprecation")
90 Object tmp = ((Class) answer).newInstance();
91 answer = tmp;
92 ref = new NamedWeakReference<>(answer, className);
93 factories.set(posn-1, ref); // replace Class object or null
94 return answer;
95 } catch (ClassNotFoundException e) {
96 NamingException ne =
97 new NamingException("No longer able to load " + className);
98 ne.setRootCause(e);
99 throw ne;
100 } catch (InstantiationException e) {
101 NamingException ne =
102 new NamingException("Cannot instantiate " + answer);
103 ne.setRootCause(e);
104 throw ne;
105 } catch (IllegalAccessException e) {
106 NamingException ne = new NamingException("Cannot access " + answer);
107 ne.setRootCause(e);
108 throw ne;
109 }
110 }
111 }
|