KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > naming > internal > FactoryEnumeration


1 /*
2  * @(#)FactoryEnumeration.java 1.8 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.naming.internal;
9
10 import java.util.List JavaDoc;
11 import javax.naming.NamingException JavaDoc;
12
13 /**
14   * The FactoryEnumeration is used for returning factory instances.
15   *
16   * @author Rosanna Lee
17   * @author Scott Seligman
18   * @version 1.8 03/12/19
19  */

20
21 // no need to implement Enumeration since this is only for internal use
22
public final class FactoryEnumeration {
23     private List JavaDoc factories;
24     private int posn = 0;
25     private ClassLoader JavaDoc loader;
26
27     /**
28      * Records the input list and uses it directly to satisfy
29      * hasMore()/next() requests. An alternative would have been to use
30      * an enumeration/iterator from the list, but we want to update the
31      * list so we keep the
32      * original list. The list initially contains Class objects.
33      * As each element is used, the Class object is replaced by an
34      * instance of the Class itself; eventually, the list contains
35      * only a list of factory instances and no more updates are required.
36      *
37      * <p> Both Class objects and factories are wrapped in weak
38      * references so as not to prevent GC of the class loader. Each
39      * weak reference is tagged with the factory's class name so the
40      * class can be reloaded if the reference is cleared.
41
42      * @param factories A non-null list
43      * @param loader The class loader of the list's contents
44      */

45     FactoryEnumeration(List JavaDoc factories, ClassLoader JavaDoc loader) {
46     this.factories = factories;
47     this.loader = loader;
48     }
49  
50     public Object JavaDoc next() throws NamingException JavaDoc {
51     synchronized (factories) {
52
53         NamedWeakReference ref = (NamedWeakReference) factories.get(posn++);
54         Object JavaDoc answer = ref.get();
55         if ((answer != null) && !(answer instanceof Class JavaDoc)) {
56         return answer;
57         }
58
59         String JavaDoc className = ref.getName();
60
61         try {
62         if (answer == null) { // reload class if weak ref cleared
63
answer = Class.forName(className, true, loader);
64         }
65         // Instantiate Class to get factory
66
answer = ((Class JavaDoc) answer).newInstance();
67         ref = new NamedWeakReference(answer, className);
68         factories.set(posn-1, ref); // replace Class object or null
69
return answer;
70         } catch (ClassNotFoundException JavaDoc e) {
71         NamingException JavaDoc ne =
72             new NamingException JavaDoc("No longer able to load " + className);
73         ne.setRootCause(e);
74         throw ne;
75         } catch (InstantiationException JavaDoc e) {
76         NamingException JavaDoc ne =
77             new NamingException JavaDoc("Cannot instantiate " + answer);
78         ne.setRootCause(e);
79         throw ne;
80         } catch (IllegalAccessException JavaDoc e) {
81         NamingException JavaDoc ne = new NamingException JavaDoc("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