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.server;
023:
024: // $Id: ExceptionHandler.java 57200 2006-09-26 12:10:47Z dimitris@jboss.org $
025:
026: import javax.management.*;
027:
028: /**
029: * Handles exceptions and wraps them if neccessary, arccording to the spec.
030: *
031: * @author thomas.diesler@jboss.org
032: */
033: public class ExceptionHandler {
034:
035: // hide constructor
036: private ExceptionHandler() {
037: }
038:
039: /**
040: * Handles exceptions and wraps them if neccessary, arccording to the spec.
041: *
042: * @param t the exception thrown by the invocation
043: * @return any wrapped exception
044: */
045: public static JMException handleException(Throwable t) {
046: handleRuntimeExceptionOrError(t);
047:
048: // when we get here, only exceptions are left
049: Exception e = (Exception) t;
050:
051: if (e instanceof OperationsException)
052: return (OperationsException) e;
053: if (e instanceof ReflectionException)
054: return (ReflectionException) e;
055: if (e instanceof MBeanRegistrationException)
056: return (MBeanRegistrationException) e;
057:
058: // wrap the core java exceptions
059: if (e instanceof ClassNotFoundException)
060: return new ReflectionException(e);
061: if (e instanceof IllegalAccessException)
062: return new ReflectionException(e);
063: if (e instanceof InstantiationException)
064: return new ReflectionException(e);
065: if (e instanceof NoSuchMethodException)
066: return new ReflectionException(e);
067:
068: // The MBeanException is the one that might wrap other exceptions
069: // For example, the AbstractMBeanInvoker.invoke cannot throw OperationsException
070: if (e instanceof MBeanException) {
071: Throwable cause = e.getCause();
072:
073: if (cause instanceof JMException)
074: return (JMException) cause;
075: else
076: return (MBeanException) e;
077: }
078:
079: // wrap any exception thrown by an mbean
080: return new MBeanException(e);
081: }
082:
083: /**
084: * Handles runtime exceptions and rethrows them wraped if neccessary, arccording to the spec.
085: *
086: * @param e the exception thrown by the invocation
087: */
088: private static void handleRuntimeExceptionOrError(Throwable e) {
089: // is already of throwable type
090: if (e instanceof RuntimeOperationsException)
091: throw (RuntimeOperationsException) e;
092: if (e instanceof RuntimeErrorException)
093: throw (RuntimeErrorException) e;
094: if (e instanceof RuntimeMBeanException)
095: throw (RuntimeMBeanException) e;
096:
097: // wrap java core runtime exceptions
098: if (e instanceof IllegalArgumentException)
099: throw new RuntimeOperationsException(
100: (IllegalArgumentException) e);
101: if (e instanceof IndexOutOfBoundsException)
102: throw new RuntimeOperationsException(
103: (IndexOutOfBoundsException) e);
104: if (e instanceof NullPointerException)
105: throw new RuntimeOperationsException(
106: (NullPointerException) e);
107:
108: // wrap any error
109: if (e instanceof Error)
110: throw new RuntimeErrorException((Error) e);
111:
112: // wrap any runtime exception
113: if (e instanceof RuntimeException)
114: throw new RuntimeMBeanException((RuntimeException) e);
115: }
116:
117: }
|