001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mx.util;
023:
024: import javax.management.MBeanException;
025: import javax.management.ReflectionException;
026: import javax.management.RuntimeErrorException;
027: import javax.management.RuntimeMBeanException;
028: import javax.management.RuntimeOperationsException;
029: import javax.management.JMRuntimeException;
030: import javax.management.JMException;
031:
032: /**
033: * A simple helper to rethrow and/or decode those pesky
034: * JMX exceptions.
035: *
036: * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
037: * @author Scott.Stark@jboss.org
038: * @version $Revision: 57200 $
039: */
040: public class JMXExceptionDecoder {
041: /**
042: * Attempt to decode the given Throwable. If it
043: * is a container JMX exception, then the target
044: * is returned. Otherwise the argument is returned.
045: */
046: public static Throwable decode(final Throwable t) {
047: Throwable result = t;
048:
049: while (true) {
050: if (result instanceof MBeanException)
051: result = ((MBeanException) result).getTargetException();
052: else if (result instanceof ReflectionException)
053: result = ((ReflectionException) result)
054: .getTargetException();
055: else if (result instanceof RuntimeOperationsException)
056: result = ((RuntimeOperationsException) result)
057: .getTargetException();
058: else if (result instanceof RuntimeMBeanException)
059: result = ((RuntimeMBeanException) result)
060: .getTargetException();
061: else if (result instanceof RuntimeErrorException)
062: result = ((RuntimeErrorException) result)
063: .getTargetError();
064: else
065: // can't decode
066: break;
067: }
068:
069: return result;
070: }
071:
072: /** Unwrap a possibly nested jmx exception down to the last
073: * JMException || JMRuntimeException.
074: *
075: * @param ex the exception to unwrap
076: * @return A JMException || JMRuntimeException if ex was of this type, or
077: * ex if it was not.
078: */
079: public static Throwable decodeToJMXException(final Throwable ex) {
080: Throwable jmxEx = ex;
081: Throwable lastJmxEx = ex;
082: while (jmxEx instanceof JMException
083: || jmxEx instanceof JMRuntimeException) {
084: lastJmxEx = jmxEx;
085: jmxEx = decode(jmxEx);
086: if (jmxEx == lastJmxEx)
087: break;
088: }
089:
090: return lastJmxEx;
091: }
092:
093: /**
094: * Decode and rethrow the given Throwable. If it
095: * is a container JMX exception, then the target
096: * is thrown. Otherwise the argument is thrown.
097: */
098: public static void rethrow(final Exception e) throws Exception {
099: Throwable t = decode(e);
100: if (t instanceof Exception)
101: throw (Exception) t;
102: else
103: throw (Error) t;
104: }
105: }
|