001    /**
002     *
003     * Copyright 2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.system.jmx;
018    
019    import java.util.Date;
020    import java.util.Set;
021    import javax.management.AttributeNotFoundException;
022    import javax.management.InstanceNotFoundException;
023    import javax.management.JMException;
024    import javax.management.JMRuntimeException;
025    import javax.management.MBeanServerConnection;
026    import javax.management.ObjectName;
027    
028    import org.apache.geronimo.gbean.GBeanData;
029    import org.apache.geronimo.gbean.GBeanInfo;
030    import org.apache.geronimo.gbean.AbstractName;
031    import org.apache.geronimo.gbean.AbstractNameQuery;
032    import org.apache.geronimo.kernel.DependencyManager;
033    import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
034    import org.apache.geronimo.kernel.GBeanNotFoundException;
035    import org.apache.geronimo.kernel.InternalKernelException;
036    import org.apache.geronimo.kernel.Kernel;
037    import org.apache.geronimo.kernel.NoSuchAttributeException;
038    import org.apache.geronimo.kernel.NoSuchOperationException;
039    import org.apache.geronimo.kernel.Naming;
040    import org.apache.geronimo.kernel.lifecycle.LifecycleMonitor;
041    import org.apache.geronimo.kernel.proxy.ProxyManager;
042    
043    /**
044     * @version $Rev: 396469 $ $Date: 2006-04-24 00:27:37 -0700 (Mon, 24 Apr 2006) $
045     */
046    public class KernelDelegate implements Kernel {
047        private final MBeanServerConnection mbeanServer;
048        private final ProxyManager proxyManager;
049    
050        public KernelDelegate(MBeanServerConnection mbeanServer) {
051            this.mbeanServer = mbeanServer;
052            proxyManager = new JMXProxyManager(this);
053        }
054    
055        public Date getBootTime() {
056            return (Date) getKernelAttribute("bootTime");
057        }
058    
059        public String getKernelName() {
060            return (String) getKernelAttribute("kernelName");
061        }
062    
063        public Naming getNaming() {
064            return (Naming) getKernelAttribute("naming");
065        }
066    
067        public Object getGBean(ObjectName name) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
068            try {
069                return invokeKernel("getGBean", new Object[] {name}, new String[] {ObjectName.class.getName()});
070            } catch (GBeanNotFoundException e) {
071                throw e;
072            } catch (RuntimeException e) {
073                throw e;
074            } catch (Exception e) {
075                throw new InternalKernelException(e);
076            }
077        }
078    
079        public Object getGBean(AbstractName name) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
080            try {
081                return invokeKernel("getGBean", new Object[] {name}, new String[] {AbstractName.class.getName()});
082            } catch (GBeanNotFoundException e) {
083                throw e;
084            } catch (RuntimeException e) {
085                throw e;
086            } catch (Exception e) {
087                throw new InternalKernelException(e);
088            }
089        }
090    
091        public Object getGBean(String shortName) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
092            try {
093                return invokeKernel("getGBean", new Object[] {shortName}, new String[] {String.class.getName()});
094            } catch (GBeanNotFoundException e) {
095                throw e;
096            } catch (RuntimeException e) {
097                throw e;
098            } catch (Exception e) {
099                throw new InternalKernelException(e);
100            }
101        }
102    
103        public Object getGBean(Class type) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
104            try {
105                return invokeKernel("getGBean", new Object[] {type}, new String[] {Class.class.getName()});
106            } catch (GBeanNotFoundException e) {
107                throw e;
108            } catch (RuntimeException e) {
109                throw e;
110            } catch (Exception e) {
111                throw new InternalKernelException(e);
112            }
113        }
114    
115        public Object getGBean(String shortName, Class type) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
116            try {
117                return invokeKernel("getGBean", new Object[] {shortName, type}, new String[] {String.class.getName(), Class.class.getName()});
118            } catch (GBeanNotFoundException e) {
119                throw e;
120            } catch (RuntimeException e) {
121                throw e;
122            } catch (Exception e) {
123                throw new InternalKernelException(e);
124            }
125        }
126    
127        public void loadGBean(GBeanData gbeanData, ClassLoader classLoader) throws GBeanAlreadyExistsException {
128            try {
129                invokeKernel("loadGBean", new Object[] {gbeanData, classLoader}, new String[] {GBeanData.class.getName(), ClassLoader.class.getName()});
130            } catch (GBeanAlreadyExistsException e) {
131                throw e;
132            } catch (RuntimeException e) {
133                throw e;
134            } catch (Exception e) {
135                throw new InternalKernelException(e);
136            }
137        }
138    
139        public void startGBean(AbstractName name) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
140            try {
141                invokeKernel("startGBean", new Object[] {name}, new String[] {AbstractName.class.getName()});
142            } catch (GBeanNotFoundException e) {
143                throw e;
144            } catch (RuntimeException e) {
145                throw e;
146            } catch (Exception e) {
147                throw new InternalKernelException(e);
148            }
149        }
150    
151        public void startGBean(String shortName) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
152            try {
153                invokeKernel("startGBean", new Object[] {shortName}, new String[] {String.class.getName()});
154            } catch (GBeanNotFoundException e) {
155                throw e;
156            } catch (RuntimeException e) {
157                throw e;
158            } catch (Exception e) {
159                throw new InternalKernelException(e);
160            }
161        }
162    
163        public void startGBean(Class type) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
164            try {
165                invokeKernel("startGBean", new Object[] {type}, new String[] {Class.class.getName()});
166            } catch (GBeanNotFoundException e) {
167                throw e;
168            } catch (RuntimeException e) {
169                throw e;
170            } catch (Exception e) {
171                throw new InternalKernelException(e);
172            }
173        }
174    
175        public void startGBean(String shortName, Class type) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
176            try {
177                invokeKernel("startGBean", new Object[] {shortName, type}, new String[] {String.class.getName(), Class.class.getName()});
178            } catch (GBeanNotFoundException e) {
179                throw e;
180            } catch (RuntimeException e) {
181                throw e;
182            } catch (Exception e) {
183                throw new InternalKernelException(e);
184            }
185        }
186    
187        public void startRecursiveGBean(AbstractName name) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
188            try {
189                invokeKernel("startRecursiveGBean", new Object[] {name}, new String[] {AbstractName.class.getName()});
190            } catch (GBeanNotFoundException e) {
191                throw e;
192            } catch (RuntimeException e) {
193                throw e;
194            } catch (Exception e) {
195                throw new InternalKernelException(e);
196            }
197        }
198    
199        public void startRecursiveGBean(String shortName) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
200            try {
201                invokeKernel("startRecursiveGBean", new Object[] {shortName}, new String[] {String.class.getName()});
202            } catch (GBeanNotFoundException e) {
203                throw e;
204            } catch (RuntimeException e) {
205                throw e;
206            } catch (Exception e) {
207                throw new InternalKernelException(e);
208            }
209        }
210    
211        public void startRecursiveGBean(Class type) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
212            try {
213                invokeKernel("startRecursiveGBean", new Object[] {type}, new String[] {Class.class.getName()});
214            } catch (GBeanNotFoundException e) {
215                throw e;
216            } catch (RuntimeException e) {
217                throw e;
218            } catch (Exception e) {
219                throw new InternalKernelException(e);
220            }
221        }
222    
223        public void startRecursiveGBean(String shortName, Class type) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
224            try {
225                invokeKernel("startRecursiveGBean", new Object[] {shortName, type}, new String[] {String.class.getName(), Class.class.getName()});
226            } catch (GBeanNotFoundException e) {
227                throw e;
228            } catch (RuntimeException e) {
229                throw e;
230            } catch (Exception e) {
231                throw new InternalKernelException(e);
232            }
233        }
234    
235        public boolean isRunning(AbstractName name) {
236            try {
237                return ((Boolean) invokeKernel("isRunning", new Object[]{name}, new String[]{AbstractName.class.getName()})).booleanValue();
238            } catch (RuntimeException e) {
239                throw e;
240            } catch (Exception e) {
241                throw new InternalKernelException(e);
242            }
243         }
244    
245        public boolean isRunning(String shortName) {
246            try {
247                return ((Boolean) invokeKernel("isRunning", new Object[]{shortName}, new String[]{String.class.getName()})).booleanValue();
248            } catch (RuntimeException e) {
249                throw e;
250            } catch (Exception e) {
251                throw new InternalKernelException(e);
252            }
253        }
254    
255        public boolean isRunning(Class type) {
256            try {
257                return ((Boolean) invokeKernel("isRunning", new Object[]{type}, new String[]{Class.class.getName()})).booleanValue();
258            } catch (RuntimeException e) {
259                throw e;
260            } catch (Exception e) {
261                throw new InternalKernelException(e);
262            }
263        }
264    
265        public boolean isRunning(String shortName, Class type) {
266            try {
267                return ((Boolean) invokeKernel("isRunning", new Object[]{shortName, type}, new String[]{String.class.getName(), Class.class.getName()})).booleanValue();
268            } catch (RuntimeException e) {
269                throw e;
270            } catch (Exception e) {
271                throw new InternalKernelException(e);
272            }
273        }
274    
275    
276        public void stopGBean(AbstractName name) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
277            try {
278                invokeKernel("stopGBean", new Object[] {name}, new String[] {AbstractName.class.getName()});
279            } catch (GBeanNotFoundException e) {
280                throw e;
281            } catch (RuntimeException e) {
282                throw e;
283            } catch (Exception e) {
284                throw new InternalKernelException(e);
285            }
286        }
287    
288        public void stopGBean(String shortName) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
289            try {
290                invokeKernel("stopGBean", new Object[] {shortName}, new String[] {String.class.getName()});
291            } catch (GBeanNotFoundException e) {
292                throw e;
293            } catch (RuntimeException e) {
294                throw e;
295            } catch (Exception e) {
296                throw new InternalKernelException(e);
297            }
298        }
299    
300        public void stopGBean(Class type) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
301            try {
302                invokeKernel("stopGBean", new Object[] {type}, new String[] {Class.class.getName()});
303            } catch (GBeanNotFoundException e) {
304                throw e;
305            } catch (RuntimeException e) {
306                throw e;
307            } catch (Exception e) {
308                throw new InternalKernelException(e);
309            }
310        }
311    
312        public void stopGBean(String shortName, Class type) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
313            try {
314                invokeKernel("stopGBean", new Object[] {shortName, type}, new String[] {String.class.getName(), Class.class.getName()});
315            } catch (GBeanNotFoundException e) {
316                throw e;
317            } catch (RuntimeException e) {
318                throw e;
319            } catch (Exception e) {
320                throw new InternalKernelException(e);
321            }
322        }
323    
324        public void unloadGBean(AbstractName name) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
325            try {
326                invokeKernel("unloadGBean", new Object[] {name}, new String[] {AbstractName.class.getName()});
327            } catch (GBeanNotFoundException e) {
328                throw e;
329            } catch (RuntimeException e) {
330                throw e;
331            } catch (Exception e) {
332                throw new InternalKernelException(e);
333            }
334        }
335    
336        public void unloadGBean(String shortName) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
337            try {
338                invokeKernel("unloadGBean", new Object[] {shortName}, new String[] {String.class.getName()});
339            } catch (GBeanNotFoundException e) {
340                throw e;
341            } catch (RuntimeException e) {
342                throw e;
343            } catch (Exception e) {
344                throw new InternalKernelException(e);
345            }
346        }
347    
348        public void unloadGBean(Class type) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
349            try {
350                invokeKernel("unloadGBean", new Object[] {type}, new String[] {Class.class.getName()});
351            } catch (GBeanNotFoundException e) {
352                throw e;
353            } catch (RuntimeException e) {
354                throw e;
355            } catch (Exception e) {
356                throw new InternalKernelException(e);
357            }
358        }
359    
360        public void unloadGBean(String shortName, Class type) throws GBeanNotFoundException, InternalKernelException, IllegalStateException {
361            try {
362                invokeKernel("unloadGBean", new Object[] {shortName, type}, new String[] {String.class.getName(), Class.class.getName()});
363            } catch (GBeanNotFoundException e) {
364                throw e;
365            } catch (RuntimeException e) {
366                throw e;
367            } catch (Exception e) {
368                throw new InternalKernelException(e);
369            }
370        }
371    
372        public int getGBeanState(ObjectName name) throws GBeanNotFoundException {
373            try {
374                return ((Integer) invokeKernel("getGBeanState", new Object[]{name}, new String[]{ObjectName.class.getName()})).intValue();
375            } catch (GBeanNotFoundException e) {
376                throw e;
377            } catch (RuntimeException e) {
378                throw e;
379            } catch (Exception e) {
380                throw new InternalKernelException(e);
381            }
382        }
383    
384        public int getGBeanState(AbstractName name) throws GBeanNotFoundException {
385            try {
386                return ((Integer) invokeKernel("getGBeanState", new Object[]{name}, new String[]{AbstractName.class.getName()})).intValue();
387            } catch (GBeanNotFoundException e) {
388                throw e;
389            } catch (RuntimeException e) {
390                throw e;
391            } catch (Exception e) {
392                throw new InternalKernelException(e);
393            }
394        }
395    
396        public int getGBeanState(String shortName) throws GBeanNotFoundException {
397            try {
398                return ((Integer) invokeKernel("getGBeanState", new Object[]{shortName}, new String[]{String.class.getName()})).intValue();
399            } catch (GBeanNotFoundException e) {
400                throw e;
401            } catch (RuntimeException e) {
402                throw e;
403            } catch (Exception e) {
404                throw new InternalKernelException(e);
405            }
406        }
407    
408        public int getGBeanState(Class type) throws GBeanNotFoundException {
409            try {
410                return ((Integer) invokeKernel("getGBeanState", new Object[]{type}, new String[]{Class.class.getName()})).intValue();
411            } catch (GBeanNotFoundException e) {
412                throw e;
413            } catch (RuntimeException e) {
414                throw e;
415            } catch (Exception e) {
416                throw new InternalKernelException(e);
417            }
418        }
419    
420        public int getGBeanState(String shortName, Class type) throws GBeanNotFoundException {
421            try {
422                return ((Integer) invokeKernel("getGBeanState", new Object[]{shortName, type}, new String[]{String.class.getName(), Class.class.getName()})).intValue();
423            } catch (GBeanNotFoundException e) {
424                throw e;
425            } catch (RuntimeException e) {
426                throw e;
427            } catch (Exception e) {
428                throw new InternalKernelException(e);
429            }
430        }
431    
432        public long getGBeanStartTime(AbstractName name) throws GBeanNotFoundException {
433            try {
434                return ((Long) invokeKernel("getGBeanStartTime", new Object[]{name}, new String[]{AbstractName.class.getName()})).longValue();
435            } catch (GBeanNotFoundException e) {
436                throw e;
437            } catch (RuntimeException e) {
438                throw e;
439            } catch (Exception e) {
440                throw new InternalKernelException(e);
441            }
442        }
443    
444        public long getGBeanStartTime(String shortName) throws GBeanNotFoundException {
445            try {
446                return ((Long) invokeKernel("getGBeanStartTime", new Object[]{shortName}, new String[]{String.class.getName()})).longValue();
447            } catch (GBeanNotFoundException e) {
448                throw e;
449            } catch (RuntimeException e) {
450                throw e;
451            } catch (Exception e) {
452                throw new InternalKernelException(e);
453            }
454        }
455    
456        public long getGBeanStartTime(Class type) throws GBeanNotFoundException {
457            try {
458                return ((Long) invokeKernel("getGBeanStartTime", new Object[]{type}, new String[]{Class.class.getName()})).longValue();
459            } catch (GBeanNotFoundException e) {
460                throw e;
461            } catch (RuntimeException e) {
462                throw e;
463            } catch (Exception e) {
464                throw new InternalKernelException(e);
465            }
466        }
467    
468        public long getGBeanStartTime(String shortName, Class type) throws GBeanNotFoundException {
469            try {
470                return ((Long) invokeKernel("getGBeanStartTime", new Object[]{shortName, type}, new String[]{String.class.getName(), Class.class.getName()})).longValue();
471            } catch (GBeanNotFoundException e) {
472                throw e;
473            } catch (RuntimeException e) {
474                throw e;
475            } catch (Exception e) {
476                throw new InternalKernelException(e);
477            }
478        }
479    
480        public Object getAttribute(ObjectName objectName, String attributeName) throws Exception {
481            return invokeKernel("getAttribute", new Object[]{objectName, attributeName}, new String[]{ObjectName.class.getName(), String.class.getName()});
482        }
483    
484        public Object getAttribute(AbstractName abstractName, String attributeName) throws GBeanNotFoundException, NoSuchAttributeException, Exception {
485            return invokeKernel("getAttribute", new Object[]{abstractName, attributeName}, new String[]{AbstractName.class.getName(), String.class.getName()});
486        }
487    
488        public Object getAttribute(String shortName, String attributeName) throws GBeanNotFoundException, NoSuchAttributeException, Exception {
489            return invokeKernel("getAttribute", new Object[]{shortName, attributeName}, new String[]{String.class.getName(), String.class.getName()});
490        }
491    
492        public Object getAttribute(Class type, String attributeName) throws GBeanNotFoundException, NoSuchAttributeException, Exception {
493            return invokeKernel("getAttribute", new Object[]{type, attributeName}, new String[]{Class.class.getName(), String.class.getName()});
494        }
495    
496        public Object getAttribute(String shortName, Class type, String attributeName) throws GBeanNotFoundException, NoSuchAttributeException, Exception {
497            return invokeKernel("getAttribute", new Object[]{shortName, type, attributeName}, new String[]{String.class.getName(), Class.class.getName(), String.class.getName()});
498        }
499    
500        public void setAttribute(AbstractName abstractName, String attributeName, Object attributeValue) throws GBeanNotFoundException, NoSuchAttributeException, Exception {
501            invokeKernel("setAttribute", new Object[]{abstractName, attributeName, attributeValue}, new String[]{AbstractName.class.getName(), String.class.getName(), Object.class.getName()});
502        }
503    
504        public void setAttribute(String shortName, String attributeName, Object attributeValue) throws GBeanNotFoundException, NoSuchAttributeException, Exception {
505            invokeKernel("setAttribute", new Object[]{shortName, attributeName, attributeValue}, new String[]{String.class.getName(), String.class.getName(), Object.class.getName()});
506        }
507    
508        public void setAttribute(Class type, String attributeName, Object attributeValue) throws GBeanNotFoundException, NoSuchAttributeException, Exception {
509            invokeKernel("setAttribute", new Object[]{type, attributeName, attributeValue}, new String[]{Class.class.getName(), String.class.getName(), Object.class.getName()});
510        }
511    
512        public void setAttribute(String shortName, Class type, String attributeName, Object attributeValue) throws GBeanNotFoundException, NoSuchAttributeException, Exception {
513            invokeKernel("setAttribute", new Object[]{shortName, type, attributeName, attributeValue}, new String[]{String.class.getName(), Class.class.getName(), String.class.getName(), Object.class.getName()});
514        }
515    
516        public Object invoke(ObjectName objectName, String methodName) throws Exception {
517            return invokeKernel("invoke", new Object[]{objectName, methodName}, new String[]{ObjectName.class.getName(), String.class.getName()});
518        }
519    
520        public Object invoke(AbstractName abstractName, String methodName) throws GBeanNotFoundException, NoSuchOperationException, InternalKernelException, Exception {
521            return invokeKernel("invoke", new Object[]{abstractName, methodName}, new String[]{AbstractName.class.getName(), String.class.getName()});
522        }
523    
524        public Object invoke(String shortName, String methodName) throws GBeanNotFoundException, NoSuchOperationException, InternalKernelException, Exception {
525            return invokeKernel("invoke", new Object[]{shortName, methodName}, new String[]{String.class.getName(), String.class.getName()});
526        }
527    
528        public Object invoke(Class type, String methodName) throws GBeanNotFoundException, NoSuchOperationException, InternalKernelException, Exception {
529            return invokeKernel("invoke", new Object[]{type, methodName}, new String[]{Class.class.getName(), String.class.getName()});
530        }
531    
532        public Object invoke(String shortName, Class type, String methodName) throws GBeanNotFoundException, NoSuchOperationException, InternalKernelException, Exception {
533            return invokeKernel("invoke", new Object[]{shortName, type, methodName}, new String[]{String.class.getName(), Class.class.getName(), String.class.getName()});
534        }
535    
536        public Object invoke(ObjectName objectName, String methodName, Object[] args, String[] types) throws Exception {
537            return invokeKernel("invoke", new Object[]{objectName, methodName, args, types}, new String[]{ObjectName.class.getName(), String.class.getName(), Object[].class.getName(), String[].class.getName()});
538        }
539    
540        public Object invoke(AbstractName abstractName, String methodName, Object[] args, String[] types) throws GBeanNotFoundException, NoSuchOperationException, InternalKernelException, Exception {
541            return invokeKernel("invoke", new Object[]{abstractName, methodName, args, types}, new String[]{AbstractName.class.getName(), String.class.getName(), Object[].class.getName(), String[].class.getName()});
542        }
543    
544        public Object invoke(String shortName, String methodName, Object[] args, String[] types) throws GBeanNotFoundException, NoSuchOperationException, InternalKernelException, Exception {
545            return invokeKernel("invoke", new Object[]{shortName, methodName, args, types}, new String[]{String.class.getName(), String.class.getName(), Object[].class.getName(), String[].class.getName()});
546        }
547    
548        public Object invoke(Class type, String methodName, Object[] args, String[] types) throws GBeanNotFoundException, NoSuchOperationException, InternalKernelException, Exception {
549            return invokeKernel("invoke", new Object[]{type, methodName, args, types}, new String[]{Class.class.getName(), String.class.getName(), Object[].class.getName(), String[].class.getName()});
550        }
551    
552        public Object invoke(String shortName, Class type, String methodName, Object[] args, String[] types) throws GBeanNotFoundException, NoSuchOperationException, InternalKernelException, Exception {
553            return invokeKernel("invoke", new Object[]{shortName, type, methodName, args, types}, new String[]{String.class.getName(), Class.class.getName(), String.class.getName(), Object[].class.getName(), String[].class.getName()});
554        }
555    
556        public boolean isLoaded(AbstractName name) {
557            try {
558                return ((Boolean) invokeKernel("isLoaded", new Object[]{name}, new String[]{AbstractName.class.getName()})).booleanValue();
559            } catch (RuntimeException e) {
560                throw e;
561            } catch (Exception e) {
562                throw new InternalKernelException(e);
563            }
564         }
565    
566        public boolean isLoaded(String shortName) {
567            try {
568                return ((Boolean) invokeKernel("isLoaded", new Object[]{shortName}, new String[]{String.class.getName()})).booleanValue();
569            } catch (RuntimeException e) {
570                throw e;
571            } catch (Exception e) {
572                throw new InternalKernelException(e);
573            }
574        }
575    
576        public boolean isLoaded(Class type) {
577            try {
578                return ((Boolean) invokeKernel("isLoaded", new Object[]{type}, new String[]{Class.class.getName()})).booleanValue();
579            } catch (RuntimeException e) {
580                throw e;
581            } catch (Exception e) {
582                throw new InternalKernelException(e);
583            }
584        }
585    
586        public boolean isLoaded(String shortName, Class type) {
587            try {
588                return ((Boolean) invokeKernel("isLoaded", new Object[]{shortName, type}, new String[]{String.class.getName(), Class.class.getName()})).booleanValue();
589            } catch (RuntimeException e) {
590                throw e;
591            } catch (Exception e) {
592                throw new InternalKernelException(e);
593            }
594        }
595    
596        public GBeanInfo getGBeanInfo(ObjectName name) throws GBeanNotFoundException {
597            try {
598                return (GBeanInfo) invokeKernel("getGBeanInfo", new Object[] {name}, new String[] {ObjectName.class.getName()});
599            } catch (GBeanNotFoundException e) {
600                throw e;
601            } catch (RuntimeException e) {
602                throw e;
603            } catch (Exception e) {
604                throw new InternalKernelException(e);
605            }
606        }
607    
608        public GBeanInfo getGBeanInfo(AbstractName name) throws GBeanNotFoundException {
609            try {
610                return (GBeanInfo) invokeKernel("getGBeanInfo", new Object[] {name}, new String[] {AbstractName.class.getName()});
611            } catch (GBeanNotFoundException e) {
612                throw e;
613            } catch (RuntimeException e) {
614                throw e;
615            } catch (Exception e) {
616                throw new InternalKernelException(e);
617            }
618        }
619    
620        public GBeanInfo getGBeanInfo(String shortName) throws GBeanNotFoundException {
621            try {
622                return (GBeanInfo) invokeKernel("getGBeanInfo", new Object[] {shortName}, new String[] {String.class.getName()});
623            } catch (GBeanNotFoundException e) {
624                throw e;
625            } catch (RuntimeException e) {
626                throw e;
627            } catch (Exception e) {
628                throw new InternalKernelException(e);
629            }
630        }
631    
632        public GBeanInfo getGBeanInfo(Class type) throws GBeanNotFoundException {
633            try {
634                return (GBeanInfo) invokeKernel("getGBeanInfo", new Object[] {type}, new String[] {Class.class.getName()});
635            } catch (GBeanNotFoundException e) {
636                throw e;
637            } catch (RuntimeException e) {
638                throw e;
639            } catch (Exception e) {
640                throw new InternalKernelException(e);
641            }
642        }
643    
644        public GBeanInfo getGBeanInfo(String shortName, Class type) throws GBeanNotFoundException {
645            try {
646                return (GBeanInfo) invokeKernel("getGBeanInfo", new Object[] {shortName, type}, new String[] {String.class.getName(), Class.class.getName()});
647            } catch (GBeanNotFoundException e) {
648                throw e;
649            } catch (RuntimeException e) {
650                throw e;
651            } catch (Exception e) {
652                throw new InternalKernelException(e);
653            }
654        }
655    
656        public Set listGBeans(ObjectName pattern) {
657            try {
658                return (Set) invokeKernel("listGBeans", new Object[] {pattern}, new String[] {ObjectName.class.getName()});
659            } catch (RuntimeException e) {
660                throw e;
661            } catch (Exception e) {
662                throw new InternalKernelException(e);
663            }
664        }
665    
666        public Set listGBeans(Set patterns) {
667            try {
668                return (Set) invokeKernel("listGBeans", new Object[] {patterns}, new String[] {Set.class.getName()});
669            } catch (RuntimeException e) {
670                throw e;
671            } catch (Exception e) {
672                throw new InternalKernelException(e);
673            }
674        }
675    
676        public void registerShutdownHook(Runnable hook) {
677            try {
678                invokeKernel("registerShutdownHook", new Object[] {hook}, new String[] {Runnable.class.getName()});
679            } catch (RuntimeException e) {
680                throw e;
681            } catch (Exception e) {
682                throw new InternalKernelException(e);
683            }
684        }
685    
686        public void unregisterShutdownHook(Runnable hook) {
687            try {
688                invokeKernel("unregisterShutdownHook", new Object[] {hook}, new String[] {Runnable.class.getName()});
689            } catch (RuntimeException e) {
690                throw e;
691            } catch (Exception e) {
692                throw new InternalKernelException(e);
693            }
694        }
695    
696        public void shutdown() {
697            try {
698                invokeKernel("shutdown", new Object[] {}, new String[] {});
699            } catch (RuntimeException e) {
700                throw e;
701            } catch (Exception e) {
702                throw new InternalKernelException(e);
703            }
704        }
705    
706        public ClassLoader getClassLoaderFor(AbstractName name) throws GBeanNotFoundException {
707            try {
708                return (ClassLoader) invokeKernel("getClassLoaderFor", new Object[] {name}, new String[] {AbstractName.class.getName()});
709            } catch (GBeanNotFoundException e) {
710                throw e;
711            } catch (RuntimeException e) {
712                throw e;
713            } catch (Exception e) {
714                throw new InternalKernelException(e);
715            }
716        }
717    
718        public ClassLoader getClassLoaderFor(String shortName) throws GBeanNotFoundException {
719            try {
720                return (ClassLoader) invokeKernel("getClassLoaderFor", new Object[] {shortName}, new String[] {String.class.getName()});
721            } catch (GBeanNotFoundException e) {
722                throw e;
723            } catch (RuntimeException e) {
724                throw e;
725            } catch (Exception e) {
726                throw new InternalKernelException(e);
727            }
728        }
729    
730        public ClassLoader getClassLoaderFor(Class type) throws GBeanNotFoundException {
731            try {
732                return (ClassLoader) invokeKernel("getClassLoaderFor", new Object[] {type}, new String[] {Class.class.getName()});
733            } catch (GBeanNotFoundException e) {
734                throw e;
735            } catch (RuntimeException e) {
736                throw e;
737            } catch (Exception e) {
738                throw new InternalKernelException(e);
739            }
740        }
741    
742        public ClassLoader getClassLoaderFor(String shortName, Class type) throws GBeanNotFoundException {
743            try {
744                return (ClassLoader) invokeKernel("getClassLoaderFor", new Object[] {shortName, type}, new String[] {String.class.getName(), Class.class.getName()});
745            } catch (GBeanNotFoundException e) {
746                throw e;
747            } catch (RuntimeException e) {
748                throw e;
749            } catch (Exception e) {
750                throw new InternalKernelException(e);
751            }
752        }
753    
754        public GBeanData getGBeanData(AbstractName name) throws GBeanNotFoundException, InternalKernelException {
755            try {
756                return (GBeanData) invokeKernel("getGBeanData", new Object[] {name}, new String[] {AbstractName.class.getName()});
757            } catch (GBeanNotFoundException e) {
758                throw e;
759            } catch (RuntimeException e) {
760                throw e;
761            } catch (Exception e) {
762                throw new InternalKernelException(e);
763            }
764        }
765    
766        public GBeanData getGBeanData(String shortName) throws GBeanNotFoundException, InternalKernelException {
767            try {
768                return (GBeanData) invokeKernel("getGBeanData", new Object[] {shortName}, new String[] {String.class.getName()});
769            } catch (GBeanNotFoundException e) {
770                throw e;
771            } catch (RuntimeException e) {
772                throw e;
773            } catch (Exception e) {
774                throw new InternalKernelException(e);
775            }
776        }
777    
778        public GBeanData getGBeanData(Class type) throws GBeanNotFoundException, InternalKernelException {
779            try {
780                return (GBeanData) invokeKernel("getGBeanData", new Object[] {type}, new String[] {Class.class.getName()});
781            } catch (GBeanNotFoundException e) {
782                throw e;
783            } catch (RuntimeException e) {
784                throw e;
785            } catch (Exception e) {
786                throw new InternalKernelException(e);
787            }
788        }
789    
790        public GBeanData getGBeanData(String shortName, Class type) throws GBeanNotFoundException, InternalKernelException {
791            try {
792                return (GBeanData) invokeKernel("getGBeanData", new Object[] {shortName, type}, new String[] {String.class.getName(), Class.class.getName()});
793            } catch (GBeanNotFoundException e) {
794                throw e;
795            } catch (RuntimeException e) {
796                throw e;
797            } catch (Exception e) {
798                throw new InternalKernelException(e);
799            }
800        }
801    
802        public AbstractName getAbstractNameFor(Object service) {
803            AbstractName name = proxyManager.getProxyTarget(service);
804            if (name != null) {
805                return name;
806            }
807            try {
808                return (AbstractName) invokeKernel("getAbstractNameFor", new Object[] {service}, new String[] {Object.class.getName()});
809            } catch (RuntimeException e) {
810                throw e;
811            } catch (Exception e) {
812                throw new InternalKernelException(e);
813            }
814        }
815    
816        public String getShortNameFor(Object service) {
817            AbstractName name = getAbstractNameFor(service);
818            return (String) name.getName().get("name");
819        }
820    
821        public boolean isRunning() {
822            return ((Boolean) getKernelAttribute("running")).booleanValue();
823        }
824    
825        public Set listGBeans(AbstractNameQuery query) {
826            try {
827                return (Set) invokeKernel("listGBeans", new Object[] {query}, new String[] {AbstractNameQuery.class.getName()});
828            } catch (RuntimeException e) {
829                throw e;
830            } catch (Exception e) {
831                throw new InternalKernelException(e);
832            }
833        }
834    
835        /**
836         * Throws UnsupportedOperationException.  The dependency manager is not accesable over a remote connection.
837         */
838        public DependencyManager getDependencyManager() {
839            throw new UnsupportedOperationException("Dependency manager is not accessable by way of a remote connection");
840        }
841    
842        /**
843         * Throws UnsupportedOperationException.  The lifecycle monitor is not accesable over a remote connection.
844         */
845        public LifecycleMonitor getLifecycleMonitor() {
846            throw new UnsupportedOperationException("Lifecycle monitor is not accessable by way of a remote connection");
847        }
848    
849        public ProxyManager getProxyManager() {
850            return proxyManager;
851        }
852    
853        /**
854         * Throws UnsupportedOperationException.  A remote kernel will alreayd be booted.
855         */
856        public void boot() throws Exception {
857            throw new UnsupportedOperationException("A remote kernel can not be booted");
858        }
859    
860        private Object getKernelAttribute(String attributeName) {
861            try {
862                return mbeanServer.getAttribute(Kernel.KERNEL, attributeName);
863            } catch (Exception e) {
864                Throwable cause = unwrapJMException(e);
865                if (cause instanceof InstanceNotFoundException) {
866                    throw new InternalKernelException("Kernel is not loaded");
867                } else if (cause instanceof AttributeNotFoundException) {
868                    throw new InternalKernelException("KernelDelegate is out of synch with Kernel");
869                } else {
870                    throw new InternalKernelException(cause);
871                }
872            }
873        }
874    
875        private Object invokeKernel(String methodName, Object[] args, String[] types) throws Exception {
876            if(args != null && types != null && args.length != types.length) {
877                throw new IllegalArgumentException("Call to "+methodName+" has "+args.length+" arguments but "+types.length+" argument classes!");
878            }
879            try {
880                return mbeanServer.invoke(Kernel.KERNEL, methodName, args, types);
881            } catch (Exception e) {
882                Throwable cause = unwrapJMException(e);
883                if (cause instanceof InstanceNotFoundException) {
884                    throw new InternalKernelException("Kernel is not loaded");
885                } else if (cause instanceof NoSuchMethodException) {
886                    StringBuffer buf = new StringBuffer("KernelDelegate is out of synch with Kernel on ");
887                    buf.append(methodName).append("(");
888                    if(types != null) {
889                        for (int i = 0; i < types.length; i++) {
890                            String type = types[i];
891                            if(i>0) buf.append(",");
892                            buf.append(type);
893                        }
894                    }
895                    buf.append(")");
896                    throw new InternalKernelException(buf.toString());
897                } else if (cause instanceof JMException) {
898                    throw new InternalKernelException(cause);
899                } else if (cause instanceof JMRuntimeException) {
900                    throw new InternalKernelException(cause);
901                } else if (cause instanceof Error) {
902                    throw (Error) cause;
903                } else if (cause instanceof Exception) {
904                    throw (Exception) cause;
905                } else {
906                    throw new InternalKernelException("Unknown throwable", cause);
907                }
908            }
909        }
910    
911        private Throwable unwrapJMException(Throwable cause) {
912            while ((cause instanceof JMException || cause instanceof JMRuntimeException) && cause.getCause() != null) {
913                cause = cause.getCause();
914            }
915            return cause;
916        }
917    }