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