KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > basic > BasicRegistry


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.basic;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.IdentityHashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27 import javax.management.MalformedObjectNameException JavaDoc;
28
29 import org.apache.geronimo.gbean.AbstractName;
30 import org.apache.geronimo.gbean.AbstractNameQuery;
31 import org.apache.geronimo.gbean.runtime.GBeanInstance;
32 import org.apache.geronimo.gbean.runtime.InstanceRegistry;
33 import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
34 import org.apache.geronimo.kernel.GBeanNotFoundException;
35 import org.apache.geronimo.kernel.Kernel;
36
37 /**
38  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
39  */

40 public class BasicRegistry implements InstanceRegistry {
41     private final Map JavaDoc objectNameRegistry = new HashMap JavaDoc();
42     private final Map JavaDoc infoRegistry = new HashMap JavaDoc();
43     private final IdentityHashMap JavaDoc instanceRegistry = new IdentityHashMap JavaDoc();
44     private String JavaDoc kernelName = "";
45
46     /**
47      * Start the objectNameRegistry and associate it with a kernel.
48      *
49      * @param kernel the kernel to associate with
50      */

51     public void start(Kernel kernel) {
52         kernelName = kernel.getKernelName();
53     }
54
55     /**
56      * Shut down the objectNameRegistry and unregister any GBeans
57      */

58     public void stop() {
59         synchronized (this) {
60             objectNameRegistry.clear();
61         }
62         kernelName = "";
63     }
64
65     /**
66      * See if there is a GBean registered with a specific name.
67      *
68      * @param name the name of the GBean to check for
69      * @return true if there is a GBean registered with that name
70      */

71     public synchronized boolean isRegistered(ObjectName JavaDoc name) {
72         return objectNameRegistry.containsKey(normalizeObjectName(name));
73     }
74
75     public synchronized boolean isRegistered(AbstractName refInfo) {
76         return infoRegistry.containsKey(refInfo);
77     }
78
79     /**
80      * Register a GBean instance.
81      *
82      * @param gbeanInstance the GBean to register
83      * @throws GBeanAlreadyExistsException if there is already a GBean registered with the instance's name
84      */

85     public synchronized void register(GBeanInstance gbeanInstance) throws GBeanAlreadyExistsException {
86         ObjectName JavaDoc name = normalizeObjectName(gbeanInstance.getObjectNameObject());
87         if (objectNameRegistry.containsKey(name)) {
88             throw new GBeanAlreadyExistsException("GBean already registered: " + name);
89         }
90         objectNameRegistry.put(name, gbeanInstance);
91         infoRegistry.put(gbeanInstance.getAbstractName(), gbeanInstance);
92         gbeanInstance.setInstanceRegistry(this);
93     }
94
95     public synchronized void unregister(AbstractName abstractName) throws GBeanNotFoundException {
96         GBeanInstance gbeanInstance = (GBeanInstance) infoRegistry.remove(abstractName);
97         if (gbeanInstance == null) {
98             throw new GBeanNotFoundException(abstractName);
99         }
100         objectNameRegistry.remove(gbeanInstance.getObjectNameObject());
101     }
102
103     public synchronized void instanceCreated(Object JavaDoc instance, GBeanInstance gbeanInstance) {
104         instanceRegistry.put(instance, gbeanInstance);
105     }
106
107     public synchronized void instanceDestroyed(Object JavaDoc instance) {
108         instanceRegistry.remove(instance);
109     }
110
111     public synchronized GBeanInstance getGBeanInstanceByInstance(Object JavaDoc instance) {
112         return (GBeanInstance) instanceRegistry.get(instance);
113     }
114
115     /**
116      * Return the GBeanInstance registered with the supplied name.
117      *
118      * @param name the name of the instance to return
119      * @return the GBeanInstance
120      * @throws GBeanNotFoundException if there is no GBean registered with the supplied name
121      */

122     public synchronized GBeanInstance getGBeanInstance(ObjectName JavaDoc name) throws GBeanNotFoundException {
123         GBeanInstance instance = (GBeanInstance) objectNameRegistry.get(normalizeObjectName(name));
124         if (instance == null) {
125             throw new GBeanNotFoundException(name);
126         }
127         return instance;
128     }
129
130     public synchronized GBeanInstance getGBeanInstance(AbstractName abstractName) throws GBeanNotFoundException {
131         GBeanInstance instance = (GBeanInstance) infoRegistry.get(abstractName);
132         if (instance == null) {
133             throw new GBeanNotFoundException(abstractName);
134         }
135         return instance;
136     }
137
138
139     public synchronized GBeanInstance getGBeanInstance(String JavaDoc shortName, Class JavaDoc type) throws GBeanNotFoundException {
140         if (shortName == null && type == null) throw new IllegalArgumentException JavaDoc("shortName and type are both null");
141
142         AbstractNameQuery nameQuery;
143         if (type == null) {
144             nameQuery = new AbstractNameQuery(null, Collections.singletonMap("name", shortName));
145         } else if (shortName == null) {
146             nameQuery = new AbstractNameQuery(null, Collections.EMPTY_MAP, type.getName());
147         } else {
148             nameQuery = new AbstractNameQuery(null, Collections.singletonMap("name", shortName), type.getName());
149         }
150         Set JavaDoc instances = listGBeans(nameQuery);
151
152         if (instances.size() == 0) {
153             throw new GBeanNotFoundException("No GBeans found", Collections.singleton(nameQuery));
154         }
155
156         if (instances.size() > 1) {
157             if (type == null) {
158                 throw new GBeanNotFoundException("More then one GBean was found with shortName '" + shortName + "'", Collections.singleton(nameQuery));
159             }
160             if (shortName == null) {
161                 throw new GBeanNotFoundException("More then one GBean was found with type '" + type.getName() + "'", Collections.singleton(nameQuery));
162             }
163             throw new GBeanNotFoundException("More then one GBean was found with shortName '" + shortName + "' and type '" + type.getName() + "'", Collections.singleton(nameQuery));
164         }
165
166         GBeanInstance instance = (GBeanInstance) instances.iterator().next();
167         return instance;
168     }
169
170
171     /**
172      * Search the objectNameRegistry for GBeans matching a name pattern.
173      *
174      * @param pattern the object name pattern to search for
175      * @return an unordered Set<GBeanInstance> of GBeans that matched the pattern
176      */

177     public Set JavaDoc listGBeans(ObjectName JavaDoc pattern) {
178         pattern = normalizeObjectName(pattern);
179
180         // fairly dumb implementation that iterates the list of all registered GBeans
181
Map JavaDoc clone;
182         synchronized (this) {
183             clone = new HashMap JavaDoc(objectNameRegistry);
184         }
185         Set JavaDoc result = new HashSet JavaDoc(clone.size());
186         for (Iterator JavaDoc i = clone.entrySet().iterator(); i.hasNext();) {
187             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
188             ObjectName JavaDoc name = (ObjectName JavaDoc) entry.getKey();
189             if (pattern == null || pattern.apply(name)) {
190                 result.add(entry.getValue());
191             }
192         }
193         return result;
194     }
195
196     public Set JavaDoc listGBeans(AbstractNameQuery query) {
197         Map JavaDoc clone;
198         synchronized (this) {
199             clone = new HashMap JavaDoc(infoRegistry);
200         }
201         Set JavaDoc result = new HashSet JavaDoc(clone.size());
202         for (Iterator JavaDoc i = clone.entrySet().iterator(); i.hasNext();) {
203             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
204             AbstractName abstractName = (AbstractName) entry.getKey();
205             GBeanInstance gbeanData = (GBeanInstance) entry.getValue();
206             if (query == null || query.matches(abstractName, gbeanData.getGBeanInfo().getInterfaces())) {
207                 result.add(gbeanData);
208             }
209         }
210         return result;
211     }
212
213     private ObjectName JavaDoc normalizeObjectName(ObjectName JavaDoc objectName) {
214         if (objectName != null && objectName.getDomain().length() == 0) {
215             try {
216                 return new ObjectName JavaDoc(kernelName, objectName.getKeyPropertyList());
217             } catch (MalformedObjectNameException JavaDoc e) {
218                 throw new AssertionError JavaDoc(e);
219             }
220         }
221         return objectName;
222     }
223 }
224
Popular Tags