KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > server > ExceptionHandler


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.server;
23
24 // $Id: ExceptionHandler.java 37459 2005-10-30 00:04:02Z starksm $
25

26 import javax.management.*;
27
28 /**
29  * Handles exceptions and wraps them if neccessary, arccording to the spec.
30  *
31  * @author thomas.diesler@jboss.org
32  */

33 public class ExceptionHandler
34 {
35
36    // hide constructor
37
private ExceptionHandler()
38    {
39    }
40
41    /**
42     * Handles exceptions and wraps them if neccessary, arccording to the spec.
43     *
44     * @param t the exception thrown by the invocation
45     * @return any wrapped exception
46     */

47    public static JMException handleException(Throwable JavaDoc t)
48    {
49       handleRuntimeExceptionOrError(t);
50
51       // when we get here, only exceptions are left
52
Exception JavaDoc e = (Exception JavaDoc)t;
53
54       if (e instanceof OperationsException)
55          return (OperationsException)e;
56       if (e instanceof ReflectionException)
57          return (ReflectionException)e;
58       if (e instanceof MBeanRegistrationException)
59          return (MBeanRegistrationException)e;
60
61       // wrap the core java exceptions
62
if (e instanceof ClassNotFoundException JavaDoc)
63          return new ReflectionException(e);
64       if (e instanceof IllegalAccessException JavaDoc)
65          return new ReflectionException(e);
66       if (e instanceof InstantiationException JavaDoc)
67          return new ReflectionException(e);
68       if (e instanceof NoSuchMethodException JavaDoc)
69          return new ReflectionException(e);
70
71       // The MBeanException is the one that might wrap other exceptions
72
// For example, the AbstractMBeanInvoker.invoke cannot throw OperationsException
73
if (e instanceof MBeanException)
74       {
75          Throwable JavaDoc cause = e.getCause();
76
77          if (cause instanceof JMException)
78             return (JMException)cause;
79          else
80             return (MBeanException)e;
81       }
82
83       // wrap any exception thrown by an mbean
84
return new MBeanException(e);
85    }
86
87    /**
88     * Handles runtime exceptions and rethrows them wraped if neccessary, arccording to the spec.
89     *
90     * @param e the exception thrown by the invocation
91     */

92    private static void handleRuntimeExceptionOrError(Throwable JavaDoc e)
93    {
94       // is already of throwable type
95
if (e instanceof RuntimeOperationsException)
96          throw (RuntimeOperationsException)e;
97       if (e instanceof RuntimeErrorException)
98          throw (RuntimeErrorException)e;
99       if (e instanceof RuntimeMBeanException)
100          throw (RuntimeMBeanException)e;
101
102       // wrap java core runtime exceptions
103
if (e instanceof IllegalArgumentException JavaDoc)
104          throw new RuntimeOperationsException((IllegalArgumentException JavaDoc)e);
105       if (e instanceof IndexOutOfBoundsException JavaDoc)
106          throw new RuntimeOperationsException((IndexOutOfBoundsException JavaDoc)e);
107       if (e instanceof NullPointerException JavaDoc)
108          throw new RuntimeOperationsException((NullPointerException JavaDoc)e);
109
110       // wrap any error
111
if (e instanceof Error JavaDoc)
112          throw new RuntimeErrorException((Error JavaDoc)e);
113
114       // wrap any runtime exception
115
if (e instanceof RuntimeException JavaDoc)
116          throw new RuntimeMBeanException((RuntimeException JavaDoc)e);
117    }
118
119 }
120
Popular Tags