KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > util > JMXExceptionDecoder


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mx.util;
23
24 import javax.management.*;
25
26 /**
27  * A simple helper to rethrow and/or decode those pesky
28  * JMX exceptions.
29  *
30  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
31  * @version $Revision: 37459 $
32  */

33 public class JMXExceptionDecoder
34 {
35    /**
36     * Attempt to decode the given Throwable. If it
37     * is a container JMX exception, then the target
38     * is returned. Otherwise the argument is returned.
39     */

40    public static Throwable JavaDoc decode(final Throwable JavaDoc t)
41    {
42       Throwable JavaDoc result = t;
43       
44       while (true)
45       {
46          if (result instanceof MBeanException)
47             result = ((MBeanException) result).getTargetException();
48          else if (result instanceof ReflectionException)
49             result = ((ReflectionException) result).getTargetException();
50          else if (result instanceof RuntimeOperationsException)
51             result = ((RuntimeOperationsException) result).getTargetException();
52          else if (result instanceof RuntimeMBeanException)
53             result = ((RuntimeMBeanException) result).getTargetException();
54          else if (result instanceof RuntimeErrorException)
55             result = ((RuntimeErrorException) result).getTargetError();
56          else
57             // can't decode
58
break;
59       }
60
61       return result;
62    }
63
64    /** Unwrap a possibly nested jmx exception down to the last
65     * JMException || JMRuntimeException.
66     *
67     * @param ex the exception to unwrap
68     * @return A JMException || JMRuntimeException if ex was of this type, or
69     * ex if it was not.
70     */

71    public static Throwable JavaDoc decodeToJMXException(final Throwable JavaDoc ex)
72    {
73       Throwable JavaDoc jmxEx = ex;
74       Throwable JavaDoc lastJmxEx = ex;
75       while( jmxEx instanceof JMException || jmxEx instanceof JMRuntimeException)
76       {
77          lastJmxEx = jmxEx;
78          jmxEx = decode(jmxEx);
79          if( jmxEx == lastJmxEx )
80             break;
81       }
82
83       return lastJmxEx;
84    }
85
86    /**
87     * Decode and rethrow the given Throwable. If it
88     * is a container JMX exception, then the target
89     * is thrown. Otherwise the argument is thrown.
90     */

91    public static void rethrow(final Exception JavaDoc e)
92       throws Exception JavaDoc
93    {
94       Throwable JavaDoc t = decode(e);
95       if (t instanceof Exception JavaDoc)
96          throw (Exception JavaDoc) t;
97       else
98          throw (Error JavaDoc) t;
99    }
100 }
101
Popular Tags