KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > jmx > KernelMBeanServer


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.system.jmx;
18
19 import java.io.ObjectInputStream JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Set JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.LinkedHashSet JavaDoc;
26 import javax.management.Attribute JavaDoc;
27 import javax.management.AttributeList JavaDoc;
28 import javax.management.AttributeNotFoundException JavaDoc;
29 import javax.management.InstanceAlreadyExistsException JavaDoc;
30 import javax.management.InstanceNotFoundException JavaDoc;
31 import javax.management.ListenerNotFoundException JavaDoc;
32 import javax.management.MBeanException JavaDoc;
33 import javax.management.MBeanInfo JavaDoc;
34 import javax.management.MBeanRegistrationException JavaDoc;
35 import javax.management.MBeanServer JavaDoc;
36 import javax.management.NotCompliantMBeanException JavaDoc;
37 import javax.management.NotificationFilter JavaDoc;
38 import javax.management.NotificationListener JavaDoc;
39 import javax.management.ObjectInstance JavaDoc;
40 import javax.management.ObjectName JavaDoc;
41 import javax.management.OperationsException JavaDoc;
42 import javax.management.QueryExp JavaDoc;
43 import javax.management.ReflectionException JavaDoc;
44 import javax.management.MalformedObjectNameException JavaDoc;
45 import javax.management.loading.ClassLoaderRepository JavaDoc;
46
47 import org.apache.geronimo.gbean.GBeanInfo;
48 import org.apache.geronimo.gbean.AbstractNameQuery;
49 import org.apache.geronimo.gbean.AbstractName;
50 import org.apache.geronimo.kernel.GBeanNotFoundException;
51 import org.apache.geronimo.kernel.InternalKernelException;
52 import org.apache.geronimo.kernel.NoSuchAttributeException;
53 import org.apache.geronimo.kernel.NoSuchOperationException;
54 import org.apache.geronimo.kernel.Kernel;
55 import org.apache.geronimo.kernel.lifecycle.LifecycleAdapter;
56
57 /**
58  * A fake MBeanServer that delegates to a Kernel.
59  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
60  */

61 public class KernelMBeanServer implements MBeanServer JavaDoc {
62     private static final AbstractNameQuery ALL = new AbstractNameQuery(null, Collections.EMPTY_MAP, Collections.EMPTY_SET);
63
64     private final HashMap JavaDoc objetNameToAbstractName = new HashMap JavaDoc();
65     private final Kernel kernel;
66
67     public KernelMBeanServer(Kernel kernel) {
68         this.kernel = kernel;
69     }
70
71     public void doStart() {
72         kernel.getLifecycleMonitor().addLifecycleListener(new GBeanRegistrationListener(), ALL);
73
74         Set JavaDoc allNames = kernel.listGBeans(ALL);
75         for (Iterator JavaDoc iterator = allNames.iterator(); iterator.hasNext();) {
76             AbstractName abstractName = (AbstractName) iterator.next();
77             register(abstractName);
78         }
79     }
80
81     public synchronized AbstractName getAbstractNameFor(ObjectName JavaDoc objectName) {
82         return (AbstractName) objetNameToAbstractName.get(objectName);
83     }
84
85     private synchronized void register(AbstractName abstractName) {
86         objetNameToAbstractName.put(abstractName.getObjectName(), abstractName);
87     }
88
89     private synchronized void unregister(AbstractName abstractName) {
90         objetNameToAbstractName.remove(abstractName.getObjectName());
91     }
92
93     public void doFail() {
94         doStop();
95     }
96
97     public synchronized void doStop() {
98         objetNameToAbstractName.clear();
99     }
100
101     private class GBeanRegistrationListener extends LifecycleAdapter {
102         public void loaded(AbstractName abstractName) {
103             register(abstractName);
104         }
105
106         public void unloaded(AbstractName abstractName) {
107             unregister(abstractName);
108         }
109     }
110
111     public AbstractName toAbstractName(ObjectName JavaDoc objectName) throws InstanceNotFoundException JavaDoc{
112         AbstractName abstractName = getAbstractNameFor(objectName);
113         if (abstractName == null) {
114             throw new InstanceNotFoundException JavaDoc(objectName.getCanonicalName());
115         }
116         return abstractName;
117     }
118
119     public Object JavaDoc getAttribute(ObjectName JavaDoc name, String JavaDoc attribute) throws MBeanException JavaDoc, AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc, ReflectionException JavaDoc {
120         AbstractName abstractName = toAbstractName(name);
121         try {
122             return kernel.getAttribute(abstractName, attribute);
123         } catch (NoSuchAttributeException e) {
124             throw new AttributeNotFoundException JavaDoc(attribute);
125         } catch (GBeanNotFoundException e) {
126             throw new InstanceNotFoundException JavaDoc(name.getCanonicalName());
127         } catch (InternalKernelException e) {
128             throw new MBeanException JavaDoc(unwrapInternalKernelException(e));
129         } catch (Exception JavaDoc e) {
130             throw new MBeanException JavaDoc(e);
131         }
132     }
133
134     public AttributeList JavaDoc getAttributes(ObjectName JavaDoc name, String JavaDoc[] attributes) throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc {
135         AbstractName abstractName = toAbstractName(name);
136         AttributeList JavaDoc attributeList = new AttributeList JavaDoc(attributes.length);
137         for (int i = 0; i < attributes.length; i++) {
138             String JavaDoc attribute = attributes[i];
139             try {
140                 Object JavaDoc value = kernel.getAttribute(abstractName, attribute);
141                 attributeList.add(i, new Attribute JavaDoc(attribute, value));
142             } catch (NoSuchAttributeException e) {
143                 // ignored - caller will simply find no value
144
} catch (GBeanNotFoundException e) {
145                 throw new InstanceNotFoundException JavaDoc(name.getCanonicalName());
146             } catch (InternalKernelException e) {
147                 throw new ReflectionException JavaDoc(unwrapInternalKernelException(e));
148             } catch (Exception JavaDoc e) {
149                 // ignored - caller will simply find no value
150
}
151         }
152         return attributeList;
153     }
154
155     public String JavaDoc getDefaultDomain() {
156         return kernel.getKernelName();
157     }
158
159     public Integer JavaDoc getMBeanCount() {
160         return new Integer JavaDoc(kernel.listGBeans((AbstractNameQuery)null).size());
161     }
162
163     public MBeanInfo JavaDoc getMBeanInfo(ObjectName JavaDoc name) throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc {
164         AbstractName abstractName = toAbstractName(name);
165         GBeanInfo gbeanInfo;
166         try {
167             gbeanInfo = kernel.getGBeanInfo(abstractName);
168         } catch (GBeanNotFoundException e) {
169             throw new InstanceNotFoundException JavaDoc(name.getCanonicalName());
170         } catch (InternalKernelException e) {
171             throw new ReflectionException JavaDoc(unwrapInternalKernelException(e));
172         }
173         return JMXUtil.toMBeanInfo(gbeanInfo);
174     }
175
176     public Object JavaDoc invoke(ObjectName JavaDoc name, String JavaDoc operationName, Object JavaDoc[] params, String JavaDoc[] signature) throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc {
177         AbstractName abstractName = toAbstractName(name);
178         try {
179             return kernel.invoke(abstractName, operationName, params, signature);
180         } catch (NoSuchOperationException e) {
181             throw new ReflectionException JavaDoc(new NoSuchMethodException JavaDoc(e.getMessage()));
182         } catch (GBeanNotFoundException e) {
183             if(name.equals(e.getGBeanName())) {
184                 throw new InstanceNotFoundException JavaDoc(name.getCanonicalName());
185             }
186             throw new MBeanException JavaDoc(e);
187         } catch (InternalKernelException e) {
188             throw new MBeanException JavaDoc(unwrapInternalKernelException(e));
189         } catch (Exception JavaDoc e) {
190             throw new MBeanException JavaDoc(e);
191         }
192     }
193
194     public boolean isRegistered(ObjectName JavaDoc name) {
195         AbstractName abstractName = getAbstractNameFor(name);
196         if (abstractName == null) {
197             return false;
198         }
199         return kernel.isLoaded(abstractName);
200     }
201
202     public Set JavaDoc queryNames(ObjectName JavaDoc pattern, QueryExp JavaDoc query) {
203         // normalize the name
204
if (pattern != null && pattern.getDomain().length() == 0) {
205             try {
206                 pattern = new ObjectName JavaDoc(kernel.getKernelName(), pattern.getKeyPropertyList());
207             } catch (MalformedObjectNameException JavaDoc e) {
208                 throw new AssertionError JavaDoc(e);
209             }
210         }
211
212         Set JavaDoc names;
213         synchronized (this) {
214             names = new LinkedHashSet JavaDoc(objetNameToAbstractName.keySet());
215         }
216
217         // fairly dumb implementation that iterates the list of all registered GBeans
218
Set JavaDoc result = new HashSet JavaDoc(names.size());
219         for (Iterator JavaDoc iterator = names.iterator(); iterator.hasNext();) {
220             ObjectName JavaDoc name = (ObjectName JavaDoc) iterator.next();
221             if (pattern == null || pattern.apply(name)) {
222                 if (query != null) {
223                     query.setMBeanServer(this);
224
225                     try {
226                         if (query.apply(name)) {
227                             result.add(name);
228                         }
229                     } catch (Exception JavaDoc e) {
230                         // reject any name that threw an exception
231
}
232                 } else {
233                     result.add(name);
234                 }
235             }
236         }
237
238         return result;
239     }
240
241     public Set JavaDoc queryMBeans(ObjectName JavaDoc pattern, QueryExp JavaDoc query) {
242         Set JavaDoc names = queryNames(pattern, query);
243         Set JavaDoc objectInstances = new HashSet JavaDoc(names.size());
244         for (Iterator JavaDoc iterator = names.iterator(); iterator.hasNext();) {
245             ObjectName JavaDoc name = (ObjectName JavaDoc) iterator.next();
246             try {
247                 objectInstances.add(getObjectInstance(name));
248             } catch (InstanceNotFoundException JavaDoc e) {
249                 // ignore
250
}
251         }
252         return objectInstances;
253     }
254
255     public void setAttribute(ObjectName JavaDoc name, Attribute JavaDoc attribute) throws InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc, MBeanException JavaDoc {
256         AbstractName abstractName = toAbstractName(name);
257         String JavaDoc attributeName = attribute.getName();
258         Object JavaDoc attributeValue = attribute.getValue();
259         try {
260             kernel.setAttribute(abstractName, attributeName, attributeValue);
261         } catch (NoSuchAttributeException e) {
262             throw new AttributeNotFoundException JavaDoc(attributeName);
263         } catch (GBeanNotFoundException e) {
264             throw new InstanceNotFoundException JavaDoc(name.getCanonicalName());
265         } catch (InternalKernelException e) {
266             throw new MBeanException JavaDoc(unwrapInternalKernelException(e));
267         } catch (Exception JavaDoc e) {
268             throw new MBeanException JavaDoc(e);
269         }
270     }
271
272     public AttributeList JavaDoc setAttributes(ObjectName JavaDoc name, AttributeList JavaDoc attributes) throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc {
273         AbstractName abstractName = toAbstractName(name);
274         AttributeList JavaDoc set = new AttributeList JavaDoc(attributes.size());
275         for (Iterator JavaDoc iterator = attributes.iterator(); iterator.hasNext();) {
276             Attribute JavaDoc attribute = (Attribute JavaDoc) iterator.next();
277             String JavaDoc attributeName = attribute.getName();
278             Object JavaDoc attributeValue = attribute.getValue();
279             try {
280                 kernel.setAttribute(abstractName, attributeName, attributeValue);
281                 set.add(attribute);
282             } catch (NoSuchAttributeException e) {
283                 // ignored - caller will see value was not set because this attribute will not be in the attribute list
284
} catch (GBeanNotFoundException e) {
285                 throw new InstanceNotFoundException JavaDoc(name.getCanonicalName());
286             } catch (InternalKernelException e) {
287                 throw new ReflectionException JavaDoc(unwrapInternalKernelException(e));
288             } catch (Exception JavaDoc e) {
289                 // ignored - caller will see value was not set because this attribute will not be in the attribute list
290
}
291         }
292         return set;
293     }
294
295     public String JavaDoc[] getDomains() {
296         Set JavaDoc domains = new HashSet JavaDoc();
297         Set JavaDoc names = kernel.listGBeans((AbstractNameQuery)null);
298         for (Iterator JavaDoc iterator = names.iterator(); iterator.hasNext();) {
299             ObjectName JavaDoc objectName = (ObjectName JavaDoc) iterator.next();
300             domains.add(objectName.getDomain());
301         }
302         return (String JavaDoc[]) domains.toArray(new String JavaDoc[domains.size()]);
303     }
304
305     public ObjectInstance JavaDoc getObjectInstance(ObjectName JavaDoc objectName) throws InstanceNotFoundException JavaDoc {
306         AbstractName abstractName = toAbstractName(objectName);
307         try {
308             GBeanInfo gbeanInfo = kernel.getGBeanInfo(abstractName);
309             return new ObjectInstance JavaDoc(objectName, gbeanInfo.getClassName());
310         } catch (GBeanNotFoundException e) {
311             throw new InstanceNotFoundException JavaDoc(objectName.getCanonicalName());
312         }
313     }
314
315     public ClassLoader JavaDoc getClassLoaderFor(ObjectName JavaDoc objectName) throws InstanceNotFoundException JavaDoc {
316         AbstractName abstractName = toAbstractName(objectName);
317         try {
318             return kernel.getClassLoaderFor(abstractName);
319         } catch (GBeanNotFoundException e) {
320             throw new InstanceNotFoundException JavaDoc(objectName.getCanonicalName());
321         }
322     }
323
324     private static Exception JavaDoc unwrapInternalKernelException(InternalKernelException e) {
325         if (e.getCause() instanceof Exception JavaDoc) {
326             return (Exception JavaDoc) e.getCause();
327         }
328         return e;
329     }
330
331     //////////////////////////////////////////////
332
//
333
// NOT ALLOWED
334
//
335
//////////////////////////////////////////////
336

337     public void addNotificationListener(ObjectName JavaDoc objectName, NotificationListener JavaDoc notificationListener, NotificationFilter JavaDoc notificationFilter, Object JavaDoc o) throws InstanceNotFoundException JavaDoc {
338         throw new SecurityException JavaDoc("Operation not allowed");
339     }
340
341     public void addNotificationListener(ObjectName JavaDoc objectName, ObjectName JavaDoc objectName1, NotificationFilter JavaDoc notificationFilter, Object JavaDoc o) throws InstanceNotFoundException JavaDoc {
342         throw new SecurityException JavaDoc("Operation not allowed");
343     }
344
345     public void removeNotificationListener(ObjectName JavaDoc objectName, ObjectName JavaDoc objectName1) throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc {
346         throw new SecurityException JavaDoc("Operation not allowed");
347     }
348
349     public void removeNotificationListener(ObjectName JavaDoc objectName, NotificationListener JavaDoc notificationListener) throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc {
350         throw new SecurityException JavaDoc("Operation not allowed");
351     }
352
353     public void removeNotificationListener(ObjectName JavaDoc objectName, ObjectName JavaDoc objectName1, NotificationFilter JavaDoc notificationFilter, Object JavaDoc o) throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc {
354         throw new SecurityException JavaDoc("Operation not allowed");
355     }
356
357     public void removeNotificationListener(ObjectName JavaDoc objectName, NotificationListener JavaDoc notificationListener, NotificationFilter JavaDoc notificationFilter, Object JavaDoc o) throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc {
358         throw new SecurityException JavaDoc("Operation not allowed");
359     }
360
361     public boolean isInstanceOf(ObjectName JavaDoc objectName, String JavaDoc s) throws InstanceNotFoundException JavaDoc {
362         throw new SecurityException JavaDoc("Operation not allowed");
363     }
364
365     public ObjectInstance JavaDoc createMBean(String JavaDoc s, ObjectName JavaDoc objectName) throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, MBeanException JavaDoc, NotCompliantMBeanException JavaDoc {
366         throw new SecurityException JavaDoc("Operation not allowed");
367     }
368
369     public ObjectInstance JavaDoc createMBean(String JavaDoc s, ObjectName JavaDoc objectName, ObjectName JavaDoc objectName1) throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, MBeanException JavaDoc, NotCompliantMBeanException JavaDoc, InstanceNotFoundException JavaDoc {
370         throw new SecurityException JavaDoc("Operation not allowed");
371     }
372
373     public ObjectInstance JavaDoc createMBean(String JavaDoc s, ObjectName JavaDoc objectName, Object JavaDoc[] objects, String JavaDoc[] strings) throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, MBeanException JavaDoc, NotCompliantMBeanException JavaDoc {
374         throw new SecurityException JavaDoc("Operation not allowed");
375     }
376
377     public ObjectInstance JavaDoc createMBean(String JavaDoc s, ObjectName JavaDoc objectName, ObjectName JavaDoc objectName1, Object JavaDoc[] objects, String JavaDoc[] strings) throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, MBeanException JavaDoc, NotCompliantMBeanException JavaDoc, InstanceNotFoundException JavaDoc {
378         throw new SecurityException JavaDoc("Operation not allowed");
379     }
380
381     public Object JavaDoc instantiate(String JavaDoc s) throws ReflectionException JavaDoc, MBeanException JavaDoc {
382         throw new SecurityException JavaDoc("Operation not allowed");
383     }
384
385     public Object JavaDoc instantiate(String JavaDoc s, ObjectName JavaDoc objectName) throws ReflectionException JavaDoc, MBeanException JavaDoc, InstanceNotFoundException JavaDoc {
386         throw new SecurityException JavaDoc("Operation not allowed");
387     }
388
389     public Object JavaDoc instantiate(String JavaDoc s, Object JavaDoc[] objects, String JavaDoc[] strings) throws ReflectionException JavaDoc, MBeanException JavaDoc {
390         throw new SecurityException JavaDoc("Operation not allowed");
391     }
392
393     public Object JavaDoc instantiate(String JavaDoc s, ObjectName JavaDoc objectName, Object JavaDoc[] objects, String JavaDoc[] strings) throws ReflectionException JavaDoc, MBeanException JavaDoc, InstanceNotFoundException JavaDoc {
394         throw new SecurityException JavaDoc("Operation not allowed");
395     }
396
397     public ObjectInstance JavaDoc registerMBean(Object JavaDoc o, ObjectName JavaDoc objectName) throws InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, NotCompliantMBeanException JavaDoc {
398         throw new SecurityException JavaDoc("Operation not allowed");
399     }
400
401     public ObjectInputStream JavaDoc deserialize(String JavaDoc s, ObjectName JavaDoc objectName, byte[] bytes) throws InstanceNotFoundException JavaDoc, OperationsException JavaDoc, ReflectionException JavaDoc {
402         throw new SecurityException JavaDoc("Operation not allowed");
403     }
404
405     public ObjectInputStream JavaDoc deserialize(String JavaDoc s, byte[] bytes) throws OperationsException JavaDoc, ReflectionException JavaDoc {
406         throw new SecurityException JavaDoc("Operation not allowed");
407     }
408
409     public ObjectInputStream JavaDoc deserialize(ObjectName JavaDoc objectName, byte[] bytes) throws InstanceNotFoundException JavaDoc, OperationsException JavaDoc {
410         throw new SecurityException JavaDoc("Operation not allowed");
411     }
412
413     public ClassLoader JavaDoc getClassLoader(ObjectName JavaDoc objectName) throws InstanceNotFoundException JavaDoc {
414         throw new SecurityException JavaDoc("Operation not allowed");
415     }
416
417     public ClassLoaderRepository JavaDoc getClassLoaderRepository() {
418         return new ClassLoaderRepository JavaDoc() {
419             public Class JavaDoc loadClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
420                 throw new ClassNotFoundException JavaDoc(className);
421             }
422
423             public Class JavaDoc loadClassWithout(ClassLoader JavaDoc loader, String JavaDoc className) throws ClassNotFoundException JavaDoc {
424                 throw new ClassNotFoundException JavaDoc(className);
425             }
426
427             public Class JavaDoc loadClassBefore(ClassLoader JavaDoc loader, String JavaDoc className) throws ClassNotFoundException JavaDoc {
428                 throw new ClassNotFoundException JavaDoc(className);
429             }
430         };
431     }
432
433     public void unregisterMBean(ObjectName JavaDoc objectName) throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc {
434         throw new SecurityException JavaDoc("Operation not allowed");
435     }
436 }
437
Popular Tags