KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  *
3  * Copyright 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.util.Date JavaDoc;
20 import java.util.Set JavaDoc;
21 import javax.management.AttributeNotFoundException JavaDoc;
22 import javax.management.InstanceNotFoundException JavaDoc;
23 import javax.management.JMException JavaDoc;
24 import javax.management.JMRuntimeException JavaDoc;
25 import javax.management.MBeanServerConnection JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27
28 import org.apache.geronimo.gbean.GBeanData;
29 import org.apache.geronimo.gbean.GBeanInfo;
30 import org.apache.geronimo.kernel.DependencyManager;
31 import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
32 import org.apache.geronimo.kernel.GBeanNotFoundException;
33 import org.apache.geronimo.kernel.InternalKernelException;
34 import org.apache.geronimo.kernel.Kernel;
35 import org.apache.geronimo.kernel.lifecycle.LifecycleMonitor;
36 import org.apache.geronimo.kernel.proxy.ProxyManager;
37
38 /**
39  * @version $Rev$ $Date$
40  */

41 public class KernelDelegate implements Kernel {
42     private final MBeanServerConnection JavaDoc mbeanServer;
43     private final ProxyManager proxyManager;
44
45     public KernelDelegate(MBeanServerConnection JavaDoc mbeanServer) {
46         this.mbeanServer = mbeanServer;
47         proxyManager = new JMXProxyManager(this);
48     }
49
50     public Date JavaDoc getBootTime() {
51         return (Date JavaDoc) getKernelAttribute("bootTime");
52     }
53
54     public String JavaDoc getKernelName() {
55         return (String JavaDoc) getKernelAttribute("kernelName");
56     }
57
58     public void loadGBean(GBeanData gbeanData, ClassLoader JavaDoc classLoader) throws GBeanAlreadyExistsException, InternalKernelException {
59         try {
60             invokeKernel("loadGBean", new Object JavaDoc[] {gbeanData, classLoader}, new String JavaDoc[] {GBeanData.class.getName(), ClassLoader JavaDoc.class.getName()});
61         } catch (GBeanAlreadyExistsException e) {
62             throw e;
63         } catch (RuntimeException JavaDoc e) {
64             throw e;
65         } catch (Exception JavaDoc e) {
66             throw new InternalKernelException(e);
67         }
68     }
69
70     public void startGBean(ObjectName JavaDoc name) throws GBeanNotFoundException, InternalKernelException, IllegalStateException JavaDoc {
71         try {
72             invokeKernel("startGBean", new Object JavaDoc[] {name}, new String JavaDoc[] {ObjectName JavaDoc.class.getName()});
73         } catch (GBeanNotFoundException e) {
74             throw e;
75         } catch (RuntimeException JavaDoc e) {
76             throw e;
77         } catch (Exception JavaDoc e) {
78             throw new InternalKernelException(e);
79         }
80     }
81
82     public void startRecursiveGBean(ObjectName JavaDoc name) throws GBeanNotFoundException, InternalKernelException, IllegalStateException JavaDoc {
83         try {
84             invokeKernel("startRecursiveGBean", new Object JavaDoc[] {name}, new String JavaDoc[] {ObjectName JavaDoc.class.getName()});
85         } catch (GBeanNotFoundException e) {
86             throw e;
87         } catch (RuntimeException JavaDoc e) {
88             throw e;
89         } catch (Exception JavaDoc e) {
90             throw new InternalKernelException(e);
91         }
92     }
93
94     public void stopGBean(ObjectName JavaDoc name) throws GBeanNotFoundException, InternalKernelException, IllegalStateException JavaDoc {
95         try {
96             invokeKernel("stopGBean", new Object JavaDoc[] {name}, new String JavaDoc[] {ObjectName JavaDoc.class.getName()});
97         } catch (GBeanNotFoundException e) {
98             throw e;
99         } catch (RuntimeException JavaDoc e) {
100             throw e;
101         } catch (Exception JavaDoc e) {
102             throw new InternalKernelException(e);
103         }
104     }
105
106     public void unloadGBean(ObjectName JavaDoc name) throws GBeanNotFoundException {
107         try {
108             invokeKernel("unloadGBean", new Object JavaDoc[] {name}, new String JavaDoc[] {ObjectName JavaDoc.class.getName()});
109         } catch (GBeanNotFoundException e) {
110             throw e;
111         } catch (RuntimeException JavaDoc e) {
112             throw e;
113         } catch (Exception JavaDoc e) {
114             throw new InternalKernelException(e);
115         }
116     }
117
118     public int getGBeanState(ObjectName JavaDoc name) throws GBeanNotFoundException {
119         try {
120             return ((Integer JavaDoc) invokeKernel("getGBeanState", new Object JavaDoc[]{name}, new String JavaDoc[]{ObjectName JavaDoc.class.getName()})).intValue();
121         } catch (GBeanNotFoundException e) {
122             throw e;
123         } catch (RuntimeException JavaDoc e) {
124             throw e;
125         } catch (Exception JavaDoc e) {
126             throw new InternalKernelException(e);
127         }
128     }
129
130     public long getGBeanStartTime(ObjectName JavaDoc name) throws GBeanNotFoundException {
131         try {
132             return ((Long JavaDoc) invokeKernel("getGBeanStartTime", new Object JavaDoc[]{name}, new String JavaDoc[]{ObjectName JavaDoc.class.getName()})).longValue();
133         } catch (GBeanNotFoundException e) {
134             throw e;
135         } catch (RuntimeException JavaDoc e) {
136             throw e;
137         } catch (Exception JavaDoc e) {
138             throw new InternalKernelException(e);
139         }
140     }
141
142     public boolean isGBeanEnabled(ObjectName JavaDoc name) throws GBeanNotFoundException {
143         try {
144             return ((Boolean JavaDoc) invokeKernel("isGBeanEnabled", new Object JavaDoc[] {name}, new String JavaDoc[] {ObjectName JavaDoc.class.getName()})).booleanValue();
145         } catch (GBeanNotFoundException e) {
146             throw e;
147         } catch (RuntimeException JavaDoc e) {
148             throw e;
149         } catch (Exception JavaDoc e) {
150             throw new InternalKernelException(e);
151         }
152     }
153
154     public void setGBeanEnabled(ObjectName JavaDoc name, boolean enabled) throws GBeanNotFoundException {
155         try {
156             invokeKernel("setGBeanEnabled", new Object JavaDoc[] {name}, new String JavaDoc[] {ObjectName JavaDoc.class.getName()});
157         } catch (GBeanNotFoundException e) {
158             throw e;
159         } catch (RuntimeException JavaDoc e) {
160             throw e;
161         } catch (Exception JavaDoc e) {
162             throw new InternalKernelException(e);
163         }
164     }
165
166     public Object JavaDoc getAttribute(ObjectName JavaDoc objectName, String JavaDoc attributeName) throws Exception JavaDoc {
167         try {
168             return invokeKernel("getAttribute", new Object JavaDoc[]{objectName, attributeName}, new String JavaDoc[]{ObjectName JavaDoc.class.getName(), String JavaDoc.class.getName()});
169         } catch (RuntimeException JavaDoc e) {
170             throw e;
171         } catch (Exception JavaDoc e) {
172             throw new InternalKernelException(e);
173         }
174     }
175
176     public void setAttribute(ObjectName JavaDoc objectName, String JavaDoc attributeName, Object JavaDoc attributeValue) throws Exception JavaDoc {
177         try {
178             invokeKernel("setAttribute", new Object JavaDoc[]{objectName, attributeName, attributeValue}, new String JavaDoc[]{ObjectName JavaDoc.class.getName(), String JavaDoc.class.getName(), Object JavaDoc.class.getName()});
179         } catch (RuntimeException JavaDoc e) {
180             throw e;
181         } catch (Exception JavaDoc e) {
182             throw new InternalKernelException(e);
183         }
184     }
185
186     public Object JavaDoc invoke(ObjectName JavaDoc objectName, String JavaDoc methodName) throws Exception JavaDoc {
187         try {
188             return invokeKernel("invoke", new Object JavaDoc[]{objectName, methodName}, new String JavaDoc[]{ObjectName JavaDoc.class.getName(), String JavaDoc.class.getName()});
189         } catch (RuntimeException JavaDoc e) {
190             throw e;
191         } catch (Exception JavaDoc e) {
192             throw new InternalKernelException(e);
193         }
194     }
195
196     public Object JavaDoc invoke(ObjectName JavaDoc objectName, String JavaDoc methodName, Object JavaDoc[] args, String JavaDoc[] types) throws Exception JavaDoc {
197         try {
198             return invokeKernel("invoke", new Object JavaDoc[]{objectName, methodName, args, types}, new String JavaDoc[]{ObjectName JavaDoc.class.getName(), String JavaDoc.class.getName(), Object JavaDoc[].class.getName(), String JavaDoc[].class.getName()});
199         } catch (RuntimeException JavaDoc e) {
200             throw e;
201         } catch (Exception JavaDoc e) {
202             throw new InternalKernelException(e);
203         }
204     }
205
206     public boolean isLoaded(ObjectName JavaDoc name) {
207         try {
208             return ((Boolean JavaDoc) invokeKernel("isLoaded", new Object JavaDoc[]{name}, new String JavaDoc[]{ObjectName JavaDoc.class.getName()})).booleanValue();
209         } catch (RuntimeException JavaDoc e) {
210             throw e;
211         } catch (Exception JavaDoc e) {
212             throw new InternalKernelException(e);
213         }
214     }
215
216     public GBeanInfo getGBeanInfo(ObjectName JavaDoc name) throws GBeanNotFoundException {
217         try {
218             return (GBeanInfo) invokeKernel("getGBeanInfo", new Object JavaDoc[] {name}, new String JavaDoc[] {ObjectName JavaDoc.class.getName()});
219         } catch (GBeanNotFoundException e) {
220             throw e;
221         } catch (RuntimeException JavaDoc e) {
222             throw e;
223         } catch (Exception JavaDoc e) {
224             throw new InternalKernelException(e);
225         }
226     }
227
228     public Set JavaDoc listGBeans(ObjectName JavaDoc pattern) throws InternalKernelException {
229         try {
230             return (Set JavaDoc) invokeKernel("listGBeans", new Object JavaDoc[] {pattern}, new String JavaDoc[] {ObjectName JavaDoc.class.getName()});
231         } catch (RuntimeException JavaDoc e) {
232             throw e;
233         } catch (Exception JavaDoc e) {
234             throw new InternalKernelException(e);
235         }
236     }
237
238     public Set JavaDoc listGBeans(Set JavaDoc patterns) throws InternalKernelException {
239         try {
240             return (Set JavaDoc) invokeKernel("listGBeans", new Object JavaDoc[] {patterns}, new String JavaDoc[] {Set JavaDoc.class.getName()});
241         } catch (RuntimeException JavaDoc e) {
242             throw e;
243         } catch (Exception JavaDoc e) {
244             throw new InternalKernelException(e);
245         }
246     }
247
248     public void registerShutdownHook(Runnable JavaDoc hook) {
249         try {
250             invokeKernel("registerShutdownHook", new Object JavaDoc[] {hook}, new String JavaDoc[] {Runnable JavaDoc.class.getName()});
251         } catch (RuntimeException JavaDoc e) {
252             throw e;
253         } catch (Exception JavaDoc e) {
254             throw new InternalKernelException(e);
255         }
256     }
257
258     public void unregisterShutdownHook(Runnable JavaDoc hook) {
259         try {
260             invokeKernel("unregisterShutdownHook", new Object JavaDoc[] {hook}, new String JavaDoc[] {Runnable JavaDoc.class.getName()});
261         } catch (RuntimeException JavaDoc e) {
262             throw e;
263         } catch (Exception JavaDoc e) {
264             throw new InternalKernelException(e);
265         }
266     }
267
268     public void shutdown() {
269         try {
270             invokeKernel("shutdown", new Object JavaDoc[] {}, new String JavaDoc[] {});
271         } catch (RuntimeException JavaDoc e) {
272             throw e;
273         } catch (Exception JavaDoc e) {
274             throw new InternalKernelException(e);
275         }
276     }
277
278     public ClassLoader JavaDoc getClassLoaderFor(ObjectName JavaDoc name) throws GBeanNotFoundException, InternalKernelException {
279         try {
280             return (ClassLoader JavaDoc) invokeKernel("getClassLoaderFor", new Object JavaDoc[] {name}, new String JavaDoc[] {ObjectName JavaDoc.class.getName()});
281         } catch (GBeanNotFoundException e) {
282             throw e;
283         } catch (RuntimeException JavaDoc e) {
284             throw e;
285         } catch (Exception JavaDoc e) {
286             throw new InternalKernelException(e);
287         }
288     }
289
290     public GBeanData getGBeanData(ObjectName JavaDoc name) throws GBeanNotFoundException, InternalKernelException {
291         try {
292             return (GBeanData) invokeKernel("getGBeanData", new Object JavaDoc[] {name}, new String JavaDoc[] {ObjectName JavaDoc.class.getName()});
293         } catch (GBeanNotFoundException e) {
294             throw e;
295         } catch (RuntimeException JavaDoc e) {
296             throw e;
297         } catch (Exception JavaDoc e) {
298             throw new InternalKernelException(e);
299         }
300     }
301
302     private Object JavaDoc getKernelAttribute(String JavaDoc attributeName) throws InternalKernelException {
303         try {
304             return mbeanServer.getAttribute(Kernel.KERNEL, attributeName);
305         } catch (Exception JavaDoc e) {
306             Throwable JavaDoc cause = unwrapJMException(e);
307             if (cause instanceof InstanceNotFoundException JavaDoc) {
308                 throw new InternalKernelException("Kernel is not loaded");
309             } else if (cause instanceof AttributeNotFoundException JavaDoc) {
310                 throw new InternalKernelException("KernelDelegate is out of synch with Kernel");
311             } else {
312                 throw new InternalKernelException(cause);
313             }
314         }
315     }
316
317     private Object JavaDoc invokeKernel(String JavaDoc methodName, Object JavaDoc[] args, String JavaDoc[] types) throws InternalKernelException, Exception JavaDoc {
318         try {
319             return mbeanServer.invoke(Kernel.KERNEL, methodName, args, types);
320         } catch (Exception JavaDoc e) {
321             Throwable JavaDoc cause = unwrapJMException(e);
322             if (cause instanceof InstanceNotFoundException JavaDoc) {
323                 throw new InternalKernelException("Kernel is not loaded");
324             } else if (cause instanceof NoSuchMethodException JavaDoc) {
325                 throw new InternalKernelException("KernelDelegate is out of synch with Kernel");
326             } else if (cause instanceof JMException JavaDoc) {
327                 throw new InternalKernelException(cause);
328             } else if (cause instanceof JMRuntimeException JavaDoc) {
329                 throw new InternalKernelException(cause);
330             } else if (cause instanceof Error JavaDoc) {
331                 throw (Error JavaDoc) cause;
332             } else if (cause instanceof Exception JavaDoc) {
333                 throw (Exception JavaDoc) cause;
334             } else {
335                 throw new InternalKernelException("Unknown throwable", cause);
336             }
337         }
338     }
339
340     public boolean isRunning() {
341         return ((Boolean JavaDoc) getKernelAttribute("running")).booleanValue();
342     }
343
344     public DependencyManager getDependencyManager() {
345         throw new UnsupportedOperationException JavaDoc("Dependency manager is not accessable by way of a remote connection");
346     }
347
348     public LifecycleMonitor getLifecycleMonitor() {
349         throw new UnsupportedOperationException JavaDoc("Lifecycle monitor is not accessable by way of a remote connection");
350     }
351
352     public ProxyManager getProxyManager() {
353         return proxyManager;
354     }
355
356     public void boot() throws Exception JavaDoc {
357         throw new UnsupportedOperationException JavaDoc("A remote kernel can not be booted");
358     }
359
360     private Throwable JavaDoc unwrapJMException(Throwable JavaDoc cause) {
361         while ((cause instanceof JMException JavaDoc || cause instanceof JMRuntimeException JavaDoc) && cause.getCause() != null) {
362             cause = cause.getCause();
363         }
364         return cause;
365     }
366 }
367
Popular Tags