KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > remoting > ExportedClassLoaderTable


1 package hudson.remoting;
2
3 import java.lang.ref.WeakReference JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.WeakHashMap JavaDoc;
7
8 /**
9  * Manages unique ID for classloaders.
10  *
11  * @author Kohsuke Kawaguchi
12  */

13 final class ExportedClassLoaderTable {
14     private final Map JavaDoc<Integer JavaDoc, WeakReference JavaDoc<ClassLoader JavaDoc>> table = new HashMap JavaDoc<Integer JavaDoc, WeakReference JavaDoc<ClassLoader JavaDoc>>();
15     private final WeakHashMap JavaDoc<ClassLoader JavaDoc,Integer JavaDoc> reverse = new WeakHashMap JavaDoc<ClassLoader JavaDoc,Integer JavaDoc>();
16
17     // id==0 is reserved for bootstrap classloader
18
private int iota = 1;
19
20
21     public synchronized int intern(ClassLoader JavaDoc cl) {
22         if(cl==null) return 0; // bootstrap classloader
23

24         Integer JavaDoc id = reverse.get(cl);
25         if(id==null) {
26             id = iota++;
27             table.put(id,new WeakReference JavaDoc<ClassLoader JavaDoc>(cl));
28             reverse.put(cl,id);
29         }
30
31         return id;
32     }
33
34     public synchronized ClassLoader JavaDoc get(int id) {
35         WeakReference JavaDoc<ClassLoader JavaDoc> ref = table.get(id);
36         if(ref==null) return null;
37         return ref.get();
38     }
39 }
40
Popular Tags