001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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: 550523 $ $Date: 2007-06-25 11:02:09 -0400 (Mon, 25 Jun 2007) $
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 String getStateReason(AbstractName abstractName) {
541            try {
542                return ((String) invokeKernel("getStateReason", new Object[]{abstractName}, new String[]{AbstractName.class.getName()}));
543            } catch (RuntimeException e) {
544                throw e;
545            } catch (Exception e) {
546                throw new InternalKernelException(e);
547            }
548        }
549    
550        public Object invoke(AbstractName abstractName, String methodName, Object[] args, String[] types) throws GBeanNotFoundException, NoSuchOperationException, InternalKernelException, Exception {
551            return invokeKernel("invoke", new Object[]{abstractName, methodName, args, types}, new String[]{AbstractName.class.getName(), String.class.getName(), Object[].class.getName(), String[].class.getName()});
552        }
553    
554        public Object invoke(String shortName, String methodName, Object[] args, String[] types) throws GBeanNotFoundException, NoSuchOperationException, InternalKernelException, Exception {
555            return invokeKernel("invoke", new Object[]{shortName, methodName, args, types}, new String[]{String.class.getName(), String.class.getName(), Object[].class.getName(), String[].class.getName()});
556        }
557    
558        public Object invoke(Class type, String methodName, Object[] args, String[] types) throws GBeanNotFoundException, NoSuchOperationException, InternalKernelException, Exception {
559            return invokeKernel("invoke", new Object[]{type, methodName, args, types}, new String[]{Class.class.getName(), String.class.getName(), Object[].class.getName(), String[].class.getName()});
560        }
561    
562        public Object invoke(String shortName, Class type, String methodName, Object[] args, String[] types) throws GBeanNotFoundException, NoSuchOperationException, InternalKernelException, Exception {
563            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()});
564        }
565    
566        public boolean isLoaded(AbstractName name) {
567            try {
568                return ((Boolean) invokeKernel("isLoaded", new Object[]{name}, new String[]{AbstractName.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(String shortName) {
577            try {
578                return ((Boolean) invokeKernel("isLoaded", new Object[]{shortName}, new String[]{String.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(Class type) {
587            try {
588                return ((Boolean) invokeKernel("isLoaded", new Object[]{type}, new String[]{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 boolean isLoaded(String shortName, Class type) {
597            try {
598                return ((Boolean) invokeKernel("isLoaded", new Object[]{shortName, type}, new String[]{String.class.getName(), Class.class.getName()})).booleanValue();
599            } catch (RuntimeException e) {
600                throw e;
601            } catch (Exception e) {
602                throw new InternalKernelException(e);
603            }
604        }
605    
606        public GBeanInfo getGBeanInfo(ObjectName name) throws GBeanNotFoundException {
607            try {
608                return (GBeanInfo) invokeKernel("getGBeanInfo", new Object[] {name}, new String[] {ObjectName.class.getName()});
609            } catch (GBeanNotFoundException e) {
610                throw e;
611            } catch (RuntimeException e) {
612                throw e;
613            } catch (Exception e) {
614                throw new InternalKernelException(e);
615            }
616        }
617    
618        public GBeanInfo getGBeanInfo(AbstractName name) throws GBeanNotFoundException {
619            try {
620                return (GBeanInfo) invokeKernel("getGBeanInfo", new Object[] {name}, new String[] {AbstractName.class.getName()});
621            } catch (GBeanNotFoundException e) {
622                throw e;
623            } catch (RuntimeException e) {
624                throw e;
625            } catch (Exception e) {
626                throw new InternalKernelException(e);
627            }
628        }
629    
630        public GBeanInfo getGBeanInfo(String shortName) throws GBeanNotFoundException {
631            try {
632                return (GBeanInfo) invokeKernel("getGBeanInfo", new Object[] {shortName}, new String[] {String.class.getName()});
633            } catch (GBeanNotFoundException e) {
634                throw e;
635            } catch (RuntimeException e) {
636                throw e;
637            } catch (Exception e) {
638                throw new InternalKernelException(e);
639            }
640        }
641    
642        public GBeanInfo getGBeanInfo(Class type) throws GBeanNotFoundException {
643            try {
644                return (GBeanInfo) invokeKernel("getGBeanInfo", new Object[] {type}, new String[] {Class.class.getName()});
645            } catch (GBeanNotFoundException e) {
646                throw e;
647            } catch (RuntimeException e) {
648                throw e;
649            } catch (Exception e) {
650                throw new InternalKernelException(e);
651            }
652        }
653    
654        public GBeanInfo getGBeanInfo(String shortName, Class type) throws GBeanNotFoundException {
655            try {
656                return (GBeanInfo) invokeKernel("getGBeanInfo", new Object[] {shortName, type}, new String[] {String.class.getName(), Class.class.getName()});
657            } catch (GBeanNotFoundException e) {
658                throw e;
659            } catch (RuntimeException e) {
660                throw e;
661            } catch (Exception e) {
662                throw new InternalKernelException(e);
663            }
664        }
665    
666        public Set listGBeans(ObjectName pattern) {
667            try {
668                return (Set) invokeKernel("listGBeans", new Object[] {pattern}, new String[] {ObjectName.class.getName()});
669            } catch (RuntimeException e) {
670                throw e;
671            } catch (Exception e) {
672                throw new InternalKernelException(e);
673            }
674        }
675    
676        public Set listGBeans(Set patterns) {
677            try {
678                return (Set) invokeKernel("listGBeans", new Object[] {patterns}, new String[] {Set.class.getName()});
679            } catch (RuntimeException e) {
680                throw e;
681            } catch (Exception e) {
682                throw new InternalKernelException(e);
683            }
684        }
685    
686        public void registerShutdownHook(Runnable hook) {
687            try {
688                invokeKernel("registerShutdownHook", 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 unregisterShutdownHook(Runnable hook) {
697            try {
698                invokeKernel("unregisterShutdownHook", new Object[] {hook}, new String[] {Runnable.class.getName()});
699            } catch (RuntimeException e) {
700                throw e;
701            } catch (Exception e) {
702                throw new InternalKernelException(e);
703            }
704        }
705    
706        public void shutdown() {
707            try {
708                invokeKernel("shutdown", new Object[] {}, new String[] {});
709            } catch (RuntimeException e) {
710                throw e;
711            } catch (Exception e) {
712                throw new InternalKernelException(e);
713            }
714        }
715    
716        public ClassLoader getClassLoaderFor(AbstractName name) throws GBeanNotFoundException {
717            try {
718                return (ClassLoader) invokeKernel("getClassLoaderFor", new Object[] {name}, new String[] {AbstractName.class.getName()});
719            } catch (GBeanNotFoundException e) {
720                throw e;
721            } catch (RuntimeException e) {
722                throw e;
723            } catch (Exception e) {
724                throw new InternalKernelException(e);
725            }
726        }
727    
728        public ClassLoader getClassLoaderFor(String shortName) throws GBeanNotFoundException {
729            try {
730                return (ClassLoader) invokeKernel("getClassLoaderFor", new Object[] {shortName}, new String[] {String.class.getName()});
731            } catch (GBeanNotFoundException e) {
732                throw e;
733            } catch (RuntimeException e) {
734                throw e;
735            } catch (Exception e) {
736                throw new InternalKernelException(e);
737            }
738        }
739    
740        public ClassLoader getClassLoaderFor(Class type) throws GBeanNotFoundException {
741            try {
742                return (ClassLoader) invokeKernel("getClassLoaderFor", new Object[] {type}, new String[] {Class.class.getName()});
743            } catch (GBeanNotFoundException e) {
744                throw e;
745            } catch (RuntimeException e) {
746                throw e;
747            } catch (Exception e) {
748                throw new InternalKernelException(e);
749            }
750        }
751    
752        public ClassLoader getClassLoaderFor(String shortName, Class type) throws GBeanNotFoundException {
753            try {
754                return (ClassLoader) invokeKernel("getClassLoaderFor", new Object[] {shortName, type}, new String[] {String.class.getName(), Class.class.getName()});
755            } catch (GBeanNotFoundException e) {
756                throw e;
757            } catch (RuntimeException e) {
758                throw e;
759            } catch (Exception e) {
760                throw new InternalKernelException(e);
761            }
762        }
763    
764        public GBeanData getGBeanData(AbstractName name) throws GBeanNotFoundException, InternalKernelException {
765            try {
766                return (GBeanData) invokeKernel("getGBeanData", new Object[] {name}, new String[] {AbstractName.class.getName()});
767            } catch (GBeanNotFoundException e) {
768                throw e;
769            } catch (RuntimeException e) {
770                throw e;
771            } catch (Exception e) {
772                throw new InternalKernelException(e);
773            }
774        }
775    
776        public GBeanData getGBeanData(String shortName) throws GBeanNotFoundException, InternalKernelException {
777            try {
778                return (GBeanData) invokeKernel("getGBeanData", new Object[] {shortName}, new String[] {String.class.getName()});
779            } catch (GBeanNotFoundException e) {
780                throw e;
781            } catch (RuntimeException e) {
782                throw e;
783            } catch (Exception e) {
784                throw new InternalKernelException(e);
785            }
786        }
787    
788        public GBeanData getGBeanData(Class type) throws GBeanNotFoundException, InternalKernelException {
789            try {
790                return (GBeanData) invokeKernel("getGBeanData", new Object[] {type}, new String[] {Class.class.getName()});
791            } catch (GBeanNotFoundException e) {
792                throw e;
793            } catch (RuntimeException e) {
794                throw e;
795            } catch (Exception e) {
796                throw new InternalKernelException(e);
797            }
798        }
799    
800        public GBeanData getGBeanData(String shortName, Class type) throws GBeanNotFoundException, InternalKernelException {
801            try {
802                return (GBeanData) invokeKernel("getGBeanData", new Object[] {shortName, type}, new String[] {String.class.getName(), Class.class.getName()});
803            } catch (GBeanNotFoundException e) {
804                throw e;
805            } catch (RuntimeException e) {
806                throw e;
807            } catch (Exception e) {
808                throw new InternalKernelException(e);
809            }
810        }
811    
812        public AbstractName getAbstractNameFor(Object service) {
813            AbstractName name = proxyManager.getProxyTarget(service);
814            if (name != null) {
815                return name;
816            }
817            try {
818                return (AbstractName) invokeKernel("getAbstractNameFor", new Object[] {service}, new String[] {Object.class.getName()});
819            } catch (RuntimeException e) {
820                throw e;
821            } catch (Exception e) {
822                throw new InternalKernelException(e);
823            }
824        }
825    
826        public String getShortNameFor(Object service) {
827            AbstractName name = getAbstractNameFor(service);
828            return (String) name.getName().get("name");
829        }
830    
831        public boolean isRunning() {
832            return ((Boolean) getKernelAttribute("running")).booleanValue();
833        }
834    
835        public Set listGBeans(AbstractNameQuery query) {
836            try {
837                return (Set) invokeKernel("listGBeans", new Object[] {query}, new String[] {AbstractNameQuery.class.getName()});
838            } catch (RuntimeException e) {
839                throw e;
840            } catch (Exception e) {
841                throw new InternalKernelException(e);
842            }
843        }
844    
845        /**
846         * Throws UnsupportedOperationException.  The dependency manager is not accesable over a remote connection.
847         */
848        public DependencyManager getDependencyManager() {
849            throw new UnsupportedOperationException("Dependency manager is not accessable by way of a remote connection");
850        }
851    
852        /**
853         * Throws UnsupportedOperationException.  The lifecycle monitor is not accesable over a remote connection.
854         */
855        public LifecycleMonitor getLifecycleMonitor() {
856            throw new UnsupportedOperationException("Lifecycle monitor is not accessable by way of a remote connection");
857        }
858    
859        public ProxyManager getProxyManager() {
860            return proxyManager;
861        }
862    
863        /**
864         * Throws UnsupportedOperationException.  A remote kernel will alreayd be booted.
865         */
866        public void boot() throws Exception {
867            throw new UnsupportedOperationException("A remote kernel can not be booted");
868        }
869    
870        private Object getKernelAttribute(String attributeName) {
871            try {
872                return mbeanServer.getAttribute(Kernel.KERNEL, attributeName);
873            } catch (Exception e) {
874                Throwable cause = unwrapJMException(e);
875                if (cause instanceof InstanceNotFoundException) {
876                    throw new InternalKernelException("Kernel is not loaded", cause);
877                } else if (cause instanceof AttributeNotFoundException) {
878                    throw new InternalKernelException("KernelDelegate is out of synch with Kernel", cause);
879                } else {
880                    throw new InternalKernelException(cause);
881                }
882            }
883        }
884    
885        private Object invokeKernel(String methodName, Object[] args, String[] types) throws Exception {
886            if(args != null && types != null && args.length != types.length) {
887                throw new IllegalArgumentException("Call to "+methodName+" has "+args.length+" arguments but "+types.length+" argument classes!");
888            }
889            try {
890                return mbeanServer.invoke(Kernel.KERNEL, methodName, args, types);
891            } catch (Exception e) {
892                Throwable cause = unwrapJMException(e);
893                if (cause instanceof InstanceNotFoundException) {
894                    throw new InternalKernelException("Kernel is not loaded", cause);
895                } else if (cause instanceof NoSuchMethodException) {
896                    StringBuffer buf = new StringBuffer("KernelDelegate is out of synch with Kernel on ");
897                    buf.append(methodName).append("(");
898                    if(types != null) {
899                        for (int i = 0; i < types.length; i++) {
900                            String type = types[i];
901                            if(i>0) buf.append(",");
902                            buf.append(type);
903                        }
904                    }
905                    buf.append(")");
906                    throw new InternalKernelException(buf.toString());
907                } else if (cause instanceof JMException) {
908                    throw new InternalKernelException(cause);
909                } else if (cause instanceof JMRuntimeException) {
910                    throw new InternalKernelException(cause);
911                } else if (cause instanceof Error) {
912                    throw (Error) cause;
913                } else if (cause instanceof Exception) {
914                    throw (Exception) cause;
915                } else {
916                    throw new InternalKernelException("Unknown throwable", cause);
917                }
918            }
919        }
920    
921        private Throwable unwrapJMException(Throwable cause) {
922            while ((cause instanceof JMException || cause instanceof JMRuntimeException) && cause.getCause() != null) {
923                cause = cause.getCause();
924            }
925            return cause;
926        }
927    }