KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Method JavaDoc;
25
26 import javax.management.InstanceNotFoundException JavaDoc;
27 import javax.management.AttributeNotFoundException JavaDoc;
28 import javax.management.InvalidAttributeValueException JavaDoc;
29 import javax.management.MBeanException JavaDoc;
30 import javax.management.ReflectionException JavaDoc;
31 import javax.management.RuntimeOperationsException JavaDoc;
32 import javax.management.RuntimeMBeanException JavaDoc;
33 import javax.management.RuntimeErrorException JavaDoc;
34
35 /**
36  * Default exception handler for MBean proxy.
37  *
38  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
39  * @version $Revision: 37459 $
40  *
41  */

42 public class DefaultExceptionHandler
43       implements ProxyExceptionHandler
44 {
45
46    // InstanceNotFound, AttributeNotFound and InvalidAttributeValue
47
// are not exceptions declared in the mgmt interface and therefore
48
// must be rethrown as runtime exceptions to avoid UndeclaredThrowable
49
// exceptions on the client
50

51    public Object JavaDoc handleInstanceNotFound(ProxyContext ctx,
52                                         InstanceNotFoundException JavaDoc e,
53                                         Method JavaDoc m, Object JavaDoc[] args)
54                                         throws Exception JavaDoc
55    {
56       throw new RuntimeProxyException("Instance not found: " + e.toString());
57    }
58    
59    public Object JavaDoc handleAttributeNotFound(ProxyContext ctx,
60                                          AttributeNotFoundException JavaDoc e,
61                                          Method JavaDoc m, Object JavaDoc[] args)
62                                          throws Exception JavaDoc
63    {
64       throw new RuntimeProxyException("Attribute not found: " + e.toString());
65    }
66    
67    public Object JavaDoc handleInvalidAttributeValue(ProxyContext ctx,
68                                              InvalidAttributeValueException JavaDoc e,
69                                              Method JavaDoc m, Object JavaDoc[] args)
70                                              throws Exception JavaDoc
71    {
72       throw new RuntimeProxyException("Invalid attribute value: " + e.toString());
73    }
74    
75    public Object JavaDoc handleMBeanException(ProxyContext ctx, MBeanException JavaDoc e,
76                                       Method JavaDoc m, Object JavaDoc[] args)
77                                       throws Exception JavaDoc
78    {
79       // assuming MBeanException only wraps mgmt interface "application"
80
// exceptions therefore we can safely rethrow the target exception
81
// as its declared in the mgmt interface
82
throw e.getTargetException();
83    }
84    
85    public Object JavaDoc handleReflectionException(ProxyContext ctx,
86                                            ReflectionException JavaDoc e,
87                                            Method JavaDoc m, Object JavaDoc[] args)
88                                            throws Exception JavaDoc
89    {
90       // use of reflection exception is inconsistent in the API so the
91
// safest bet is to rethrow a runtime exception
92

93       Exception JavaDoc target = e.getTargetException();
94       if (target instanceof RuntimeException JavaDoc)
95          throw target;
96       else
97          throw new RuntimeProxyException(target.toString());
98    }
99    
100    public Object JavaDoc handleRuntimeOperationsException(ProxyContext ctx,
101                                                   RuntimeOperationsException JavaDoc e,
102                                                   Method JavaDoc m, Object JavaDoc[] args)
103                                                   throws Exception JavaDoc
104    {
105       // target is always a runtime exception, so its ok to throw it from here
106
throw e.getTargetException();
107    }
108    
109    public Object JavaDoc handleRuntimeMBeanException(ProxyContext ctx,
110                                              RuntimeMBeanException JavaDoc e,
111                                              Method JavaDoc m, Object JavaDoc[] args)
112                                              throws Exception JavaDoc
113    {
114       // target is always a runtime exception, so its ok to throw it from here
115
throw e.getTargetException();
116    }
117    
118    public Object JavaDoc handleRuntimeError(ProxyContext ctx, RuntimeErrorException JavaDoc e,
119                                     Method JavaDoc m, Object JavaDoc[] args)
120                                     throws Exception JavaDoc
121    {
122       // just unwrap and throw the actual error
123
throw e.getTargetError();
124    }
125    
126 }
127       
128
129
130
131
Popular Tags