1 17 package org.apache.geronimo.kernel; 18 19 import java.lang.ref.WeakReference ; 20 import java.lang.ref.ReferenceQueue ; 21 import java.util.Map ; 22 import java.util.HashMap ; 23 import java.util.Set ; 24 import java.util.Collections ; 25 26 29 public final class KernelRegistry { 30 33 private static final Map kernels = new HashMap (); 34 35 38 private static final ReferenceQueue queue = new ReferenceQueue (); 39 40 public static Set getKernelNames() { 41 synchronized(kernels) { 42 return Collections.unmodifiableSet(kernels.keySet()); 43 } 44 } 45 46 52 public static Kernel getKernel(String name) { 53 if (name == null) { 54 return getSingleKernel(); 55 } 56 synchronized (kernels) { 57 processQueue(); 58 KernelReference ref = (KernelReference) kernels.get(name); 59 if (ref != null) { 60 return (Kernel) ref.get(); 61 } 62 } 63 return null; 64 } 65 66 75 public static Kernel getSingleKernel() { 76 synchronized (kernels) { 77 processQueue(); 78 79 int size = kernels.size(); 80 if (size > 1) throw new IllegalStateException ("More than one kernel has been registered."); 81 if (size < 1) return null; 82 83 Kernel result = (Kernel) ((KernelReference) kernels.values().iterator().next()).get(); 84 if (result == null) { 85 kernels.clear(); 86 } 87 return result; 88 } 89 } 90 91 public static void registerKernel(Kernel kernel) { 92 synchronized (kernels) { 93 String kernelName = kernel.getKernelName(); 94 if (kernels.containsKey(kernelName)) { 95 throw new IllegalStateException ("A kernel is already running this kernel name: " + kernelName); 96 } 97 kernels.put(kernelName, new KernelReference(kernelName, kernel)); 98 } 99 } 100 101 public static void unregisterKernel(Kernel kernel) { 102 synchronized (kernels) { 103 kernels.remove(kernel.getKernelName()); 104 } 105 } 106 107 private static void processQueue() { 108 KernelReference kernelRef; 109 while ((kernelRef = (KernelReference) queue.poll()) != null) { 110 synchronized (kernels) { 111 kernels.remove(kernelRef.key); 112 } 113 } 114 } 115 116 private static class KernelReference extends WeakReference { 117 private final Object key; 118 119 public KernelReference(Object key, Object kernel) { 120 super(kernel, queue); 121 this.key = key; 122 } 123 } 124 } 125 | Popular Tags |