KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > KernelRegistry


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.kernel;
18
19 import java.lang.ref.WeakReference JavaDoc;
20 import java.lang.ref.ReferenceQueue JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.Collections JavaDoc;
25
26 /**
27  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
28  */

29 public final class KernelRegistry {
30     /**
31      * Index of kernel references by kernel name
32      */

33     private static final Map JavaDoc kernels = new HashMap JavaDoc();
34
35     /**
36      * ReferenceQueue that watches the weak references to our kernels
37      */

38     private static final ReferenceQueue JavaDoc queue = new ReferenceQueue JavaDoc();
39
40     public static Set JavaDoc getKernelNames() {
41         synchronized(kernels) {
42             return Collections.unmodifiableSet(kernels.keySet());
43         }
44     }
45
46     /**
47      * Get a particular kernel indexed by a name
48      *
49      * @param name the name of the kernel to be obtained
50      * @return the kernel that was registered with that name
51      */

52     public static Kernel getKernel(String JavaDoc 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     /**
67      * Obtain the single kernel that's registered.
68      * <p/>
69      * <p>This method assumes that there is only one kernel registered and will throw an
70      * <code>IllegalStateException</code> if more than one has been registered.
71      *
72      * @return the single kernel that's registered
73      * @throws IllegalStateException if more than one
74      */

75     public static Kernel getSingleKernel() {
76         synchronized (kernels) {
77             processQueue();
78
79             int size = kernels.size();
80             if (size > 1) throw new IllegalStateException JavaDoc("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 JavaDoc kernelName = kernel.getKernelName();
94             if (kernels.containsKey(kernelName)) {
95                 throw new IllegalStateException JavaDoc("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 JavaDoc {
117         private final Object JavaDoc key;
118
119         public KernelReference(Object JavaDoc key, Object JavaDoc kernel) {
120             super(kernel, queue);
121             this.key = key;
122         }
123     }
124 }
125
Popular Tags