KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > jmx > MBeanServerDelegate


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.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 javax.management.Attribute JavaDoc;
24 import javax.management.AttributeList JavaDoc;
25 import javax.management.AttributeNotFoundException JavaDoc;
26 import javax.management.InstanceAlreadyExistsException JavaDoc;
27 import javax.management.InstanceNotFoundException JavaDoc;
28 import javax.management.ListenerNotFoundException JavaDoc;
29 import javax.management.MBeanException JavaDoc;
30 import javax.management.MBeanInfo JavaDoc;
31 import javax.management.MBeanRegistrationException JavaDoc;
32 import javax.management.MBeanServer JavaDoc;
33 import javax.management.NotCompliantMBeanException JavaDoc;
34 import javax.management.NotificationFilter JavaDoc;
35 import javax.management.NotificationListener JavaDoc;
36 import javax.management.ObjectInstance JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38 import javax.management.OperationsException JavaDoc;
39 import javax.management.QueryExp JavaDoc;
40 import javax.management.ReflectionException JavaDoc;
41 import javax.management.loading.ClassLoaderRepository JavaDoc;
42
43 import org.apache.geronimo.gbean.GBeanInfo;
44 import org.apache.geronimo.kernel.GBeanNotFoundException;
45 import org.apache.geronimo.kernel.InternalKernelException;
46 import org.apache.geronimo.kernel.NoSuchAttributeException;
47 import org.apache.geronimo.kernel.NoSuchOperationException;
48 import org.apache.geronimo.kernel.Kernel;
49  
50 /**
51  * A MBeanServerImplementation that delegates to a Kernel.
52  * @version $Rev: $ $Date: $
53  */

54 public class MBeanServerDelegate implements MBeanServer JavaDoc {
55     private final Kernel kernel;
56
57     public MBeanServerDelegate(Kernel kernel) {
58         this.kernel = kernel;
59     }
60
61     public Object JavaDoc getAttribute(ObjectName JavaDoc name, String JavaDoc attribute) throws MBeanException JavaDoc, AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc, ReflectionException JavaDoc {
62         try {
63             return kernel.getAttribute(name, attribute);
64         } catch (NoSuchAttributeException e) {
65             throw new AttributeNotFoundException JavaDoc(attribute);
66         } catch (GBeanNotFoundException e) {
67             throw new InstanceNotFoundException JavaDoc(name.getCanonicalName());
68         } catch (InternalKernelException e) {
69             throw new MBeanException JavaDoc(unwrapInternalKernelException(e));
70         } catch (Exception JavaDoc e) {
71             throw new MBeanException JavaDoc(e);
72         }
73     }
74
75     public AttributeList JavaDoc getAttributes(ObjectName JavaDoc name, String JavaDoc[] attributes) throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc {
76         AttributeList JavaDoc attributeList = new AttributeList JavaDoc(attributes.length);
77         for (int i = 0; i < attributes.length; i++) {
78             String JavaDoc attribute = attributes[i];
79             try {
80                 Object JavaDoc value = kernel.getAttribute(name, attribute);
81                 attributeList.add(i, new Attribute JavaDoc(attribute, value));
82             } catch (NoSuchAttributeException e) {
83                 // ignored - caller will simply find no value
84
} catch (GBeanNotFoundException e) {
85                 throw new InstanceNotFoundException JavaDoc(name.getCanonicalName());
86             } catch (InternalKernelException e) {
87                 throw new ReflectionException JavaDoc(unwrapInternalKernelException(e));
88             } catch (Exception JavaDoc e) {
89                 // ignored - caller will simply find no value
90
}
91         }
92         return attributeList;
93     }
94
95     public String JavaDoc getDefaultDomain() {
96         return kernel.getKernelName();
97     }
98
99     public Integer JavaDoc getMBeanCount() {
100         return new Integer JavaDoc(kernel.listGBeans((ObjectName JavaDoc)null).size());
101     }
102
103     public MBeanInfo JavaDoc getMBeanInfo(ObjectName JavaDoc name) throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc {
104         GBeanInfo gbeanInfo;
105         try {
106             gbeanInfo = kernel.getGBeanInfo(name);
107         } catch (GBeanNotFoundException e) {
108             throw new InstanceNotFoundException JavaDoc(name.toString());
109         } catch (InternalKernelException e) {
110             throw new ReflectionException JavaDoc(unwrapInternalKernelException(e));
111         }
112         return JMXUtil.toMBeanInfo(gbeanInfo);
113     }
114
115     public Object JavaDoc invoke(ObjectName JavaDoc name, String JavaDoc operationName, Object JavaDoc[] params, String JavaDoc[] signature) throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc {
116         try {
117             return kernel.invoke(name, operationName, params, signature);
118         } catch (NoSuchOperationException e) {
119             throw new ReflectionException JavaDoc(new NoSuchMethodException JavaDoc(e.getMessage()));
120         } catch (GBeanNotFoundException e) {
121             throw new InstanceNotFoundException JavaDoc(name.getCanonicalName());
122         } catch (InternalKernelException e) {
123             throw new MBeanException JavaDoc(unwrapInternalKernelException(e));
124         } catch (Exception JavaDoc e) {
125             throw new MBeanException JavaDoc(e);
126         }
127     }
128
129     public boolean isRegistered(ObjectName JavaDoc name) {
130         return kernel.isLoaded(name);
131     }
132
133     public Set JavaDoc queryNames(ObjectName JavaDoc pattern, QueryExp JavaDoc query) {
134         Set JavaDoc names = kernel.listGBeans(pattern);
135         if (query == null) {
136             return names;
137         }
138
139         Set JavaDoc filteredNames = new HashSet JavaDoc(names.size());
140         for (Iterator JavaDoc iterator = names.iterator(); iterator.hasNext();) {
141             // this must be done for each objectName applied
142
query.setMBeanServer(this);
143
144             ObjectName JavaDoc name = (ObjectName JavaDoc) iterator.next();
145             try {
146                 if (query.apply(name)) {
147                     filteredNames.add(name);
148                 }
149             } catch (Exception JavaDoc e) {
150                 // reject any name that threw an exception
151
}
152         }
153         return filteredNames;
154     }
155
156     public Set JavaDoc queryMBeans(ObjectName JavaDoc pattern, QueryExp JavaDoc query) {
157         Set JavaDoc names = queryNames(pattern, query);
158         Set JavaDoc objectInstances = new HashSet JavaDoc(names.size());
159         for (Iterator JavaDoc iterator = names.iterator(); iterator.hasNext();) {
160             ObjectName JavaDoc name = (ObjectName JavaDoc) iterator.next();
161             try {
162                 objectInstances.add(getObjectInstance(name));
163             } catch (InstanceNotFoundException JavaDoc e) {
164                 // ignore
165
}
166         }
167         return objectInstances;
168     }
169
170     public void setAttribute(ObjectName JavaDoc name, Attribute JavaDoc attribute) throws InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc, MBeanException JavaDoc {
171         String JavaDoc attributeName = attribute.getName();
172         Object JavaDoc attributeValue = attribute.getValue();
173         try {
174             kernel.setAttribute(name, attributeName, attributeValue);
175         } catch (NoSuchAttributeException e) {
176             throw new AttributeNotFoundException JavaDoc(attributeName);
177         } catch (GBeanNotFoundException e) {
178             throw new InstanceNotFoundException JavaDoc(name.getCanonicalName());
179         } catch (InternalKernelException e) {
180             throw new MBeanException JavaDoc(unwrapInternalKernelException(e));
181         } catch (Exception JavaDoc e) {
182             throw new MBeanException JavaDoc(e);
183         }
184     }
185
186     public AttributeList JavaDoc setAttributes(ObjectName JavaDoc name, AttributeList JavaDoc attributes) throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc {
187         AttributeList JavaDoc set = new AttributeList JavaDoc(attributes.size());
188         for (Iterator JavaDoc iterator = attributes.iterator(); iterator.hasNext();) {
189             Attribute JavaDoc attribute = (Attribute JavaDoc) iterator.next();
190             String JavaDoc attributeName = attribute.getName();
191             Object JavaDoc attributeValue = attribute.getValue();
192             try {
193                 kernel.setAttribute(name, attributeName, attributeValue);
194                 set.add(attribute);
195             } catch (NoSuchAttributeException e) {
196                 // ignored - caller will see value was not set because this attribute will not be in the attribute list
197
} catch (GBeanNotFoundException e) {
198                 throw new InstanceNotFoundException JavaDoc(name.getCanonicalName());
199             } catch (InternalKernelException e) {
200                 throw new ReflectionException JavaDoc(unwrapInternalKernelException(e));
201             } catch (Exception JavaDoc e) {
202                 // ignored - caller will see value was not set because this attribute will not be in the attribute list
203
}
204         }
205         return set;
206     }
207
208     public String JavaDoc[] getDomains() {
209         Set JavaDoc domains = new HashSet JavaDoc();
210         Set JavaDoc names = kernel.listGBeans((ObjectName JavaDoc)null);
211         for (Iterator JavaDoc iterator = names.iterator(); iterator.hasNext();) {
212             ObjectName JavaDoc objectName = (ObjectName JavaDoc) iterator.next();
213             domains.add(objectName.getDomain());
214         }
215         return (String JavaDoc[]) domains.toArray(new String JavaDoc[domains.size()]);
216     }
217
218     public ObjectInstance JavaDoc getObjectInstance(ObjectName JavaDoc objectName) throws InstanceNotFoundException JavaDoc {
219         try {
220             GBeanInfo gbeanInfo = kernel.getGBeanInfo(objectName);
221             return new ObjectInstance JavaDoc(objectName, gbeanInfo.getClassName());
222         } catch (GBeanNotFoundException e) {
223             throw new InstanceNotFoundException JavaDoc(objectName.getCanonicalName());
224         }
225     }
226
227     public ClassLoader JavaDoc getClassLoaderFor(ObjectName JavaDoc objectName) throws InstanceNotFoundException JavaDoc {
228         try {
229             return kernel.getClassLoaderFor(objectName);
230         } catch (GBeanNotFoundException e) {
231             throw new InstanceNotFoundException JavaDoc(objectName.getCanonicalName());
232         }
233     }
234
235     private static Exception JavaDoc unwrapInternalKernelException(InternalKernelException e) {
236         if (e.getCause() instanceof Exception JavaDoc) {
237             return (Exception JavaDoc) e.getCause();
238         }
239         return e;
240     }
241
242     //////////////////////////////////////////////
243
//
244
// NOT ALLOWED
245
//
246
//////////////////////////////////////////////
247

248     public void addNotificationListener(ObjectName JavaDoc objectName, NotificationListener JavaDoc notificationListener, NotificationFilter JavaDoc notificationFilter, Object JavaDoc o) throws InstanceNotFoundException JavaDoc {
249         throw new SecurityException JavaDoc("Operation not allowed");
250     }
251
252     public void addNotificationListener(ObjectName JavaDoc objectName, ObjectName JavaDoc objectName1, NotificationFilter JavaDoc notificationFilter, Object JavaDoc o) throws InstanceNotFoundException JavaDoc {
253         throw new SecurityException JavaDoc("Operation not allowed");
254     }
255
256     public void removeNotificationListener(ObjectName JavaDoc objectName, ObjectName JavaDoc objectName1) throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc {
257         throw new SecurityException JavaDoc("Operation not allowed");
258     }
259
260     public void removeNotificationListener(ObjectName JavaDoc objectName, NotificationListener JavaDoc notificationListener) throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc {
261         throw new SecurityException JavaDoc("Operation not allowed");
262     }
263
264     public void removeNotificationListener(ObjectName JavaDoc objectName, ObjectName JavaDoc objectName1, NotificationFilter JavaDoc notificationFilter, Object JavaDoc o) throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc {
265         throw new SecurityException JavaDoc("Operation not allowed");
266     }
267
268     public void removeNotificationListener(ObjectName JavaDoc objectName, NotificationListener JavaDoc notificationListener, NotificationFilter JavaDoc notificationFilter, Object JavaDoc o) throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc {
269         throw new SecurityException JavaDoc("Operation not allowed");
270     }
271
272     public boolean isInstanceOf(ObjectName JavaDoc objectName, String JavaDoc s) throws InstanceNotFoundException JavaDoc {
273         throw new SecurityException JavaDoc("Operation not allowed");
274     }
275
276     public ObjectInstance JavaDoc createMBean(String JavaDoc s, ObjectName JavaDoc objectName) throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, MBeanException JavaDoc, NotCompliantMBeanException JavaDoc {
277         throw new SecurityException JavaDoc("Operation not allowed");
278     }
279
280     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 {
281         throw new SecurityException JavaDoc("Operation not allowed");
282     }
283
284     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 {
285         throw new SecurityException JavaDoc("Operation not allowed");
286     }
287
288     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 {
289         throw new SecurityException JavaDoc("Operation not allowed");
290     }
291
292     public Object JavaDoc instantiate(String JavaDoc s) throws ReflectionException JavaDoc, MBeanException JavaDoc {
293         throw new SecurityException JavaDoc("Operation not allowed");
294     }
295
296     public Object JavaDoc instantiate(String JavaDoc s, ObjectName JavaDoc objectName) throws ReflectionException JavaDoc, MBeanException JavaDoc, InstanceNotFoundException JavaDoc {
297         throw new SecurityException JavaDoc("Operation not allowed");
298     }
299
300     public Object JavaDoc instantiate(String JavaDoc s, Object JavaDoc[] objects, String JavaDoc[] strings) throws ReflectionException JavaDoc, MBeanException JavaDoc {
301         throw new SecurityException JavaDoc("Operation not allowed");
302     }
303
304     public Object JavaDoc instantiate(String JavaDoc s, ObjectName JavaDoc objectName, Object JavaDoc[] objects, String JavaDoc[] strings) throws ReflectionException JavaDoc, MBeanException JavaDoc, InstanceNotFoundException JavaDoc {
305         throw new SecurityException JavaDoc("Operation not allowed");
306     }
307
308     public ObjectInstance JavaDoc registerMBean(Object JavaDoc o, ObjectName JavaDoc objectName) throws InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc, NotCompliantMBeanException JavaDoc {
309         throw new SecurityException JavaDoc("Operation not allowed");
310     }
311
312     public ObjectInputStream JavaDoc deserialize(String JavaDoc s, ObjectName JavaDoc objectName, byte[] bytes) throws InstanceNotFoundException JavaDoc, OperationsException JavaDoc, ReflectionException JavaDoc {
313         throw new SecurityException JavaDoc("Operation not allowed");
314     }
315
316     public ObjectInputStream JavaDoc deserialize(String JavaDoc s, byte[] bytes) throws OperationsException JavaDoc, ReflectionException JavaDoc {
317         throw new SecurityException JavaDoc("Operation not allowed");
318     }
319
320     public ObjectInputStream JavaDoc deserialize(ObjectName JavaDoc objectName, byte[] bytes) throws InstanceNotFoundException JavaDoc, OperationsException JavaDoc {
321         throw new SecurityException JavaDoc("Operation not allowed");
322     }
323
324     public ClassLoader JavaDoc getClassLoader(ObjectName JavaDoc objectName) throws InstanceNotFoundException JavaDoc {
325         throw new SecurityException JavaDoc("Operation not allowed");
326     }
327
328     public ClassLoaderRepository JavaDoc getClassLoaderRepository() {
329         return new ClassLoaderRepository JavaDoc() {
330             public Class JavaDoc loadClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
331                 throw new ClassNotFoundException JavaDoc(className);
332             }
333
334             public Class JavaDoc loadClassWithout(ClassLoader JavaDoc loader, String JavaDoc className) throws ClassNotFoundException JavaDoc {
335                 throw new ClassNotFoundException JavaDoc(className);
336             }
337
338             public Class JavaDoc loadClassBefore(ClassLoader JavaDoc loader, String JavaDoc className) throws ClassNotFoundException JavaDoc {
339                 throw new ClassNotFoundException JavaDoc(className);
340             }
341         };
342     }
343
344     public void unregisterMBean(ObjectName JavaDoc objectName) throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc {
345         throw new SecurityException JavaDoc("Operation not allowed");
346     }
347 }
348
Popular Tags