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 java.lang.reflect.Method;
025:
026: import javax.management.InstanceNotFoundException;
027: import javax.management.AttributeNotFoundException;
028: import javax.management.InvalidAttributeValueException;
029: import javax.management.MBeanException;
030: import javax.management.ReflectionException;
031: import javax.management.RuntimeOperationsException;
032: import javax.management.RuntimeMBeanException;
033: import javax.management.RuntimeErrorException;
034:
035: /**
036: * Default exception handler for MBean proxy.
037: *
038: * @author <a href="mailto:juha@jboss.org">Juha Lindfors</a>.
039: * @version $Revision: 57200 $
040: *
041: */
042: public class DefaultExceptionHandler implements ProxyExceptionHandler {
043:
044: // InstanceNotFound, AttributeNotFound and InvalidAttributeValue
045: // are not exceptions declared in the mgmt interface and therefore
046: // must be rethrown as runtime exceptions to avoid UndeclaredThrowable
047: // exceptions on the client
048:
049: public Object handleInstanceNotFound(ProxyContext ctx,
050: InstanceNotFoundException e, Method m, Object[] args)
051: throws Exception {
052: throw new RuntimeProxyException("Instance not found: "
053: + e.toString());
054: }
055:
056: public Object handleAttributeNotFound(ProxyContext ctx,
057: AttributeNotFoundException e, Method m, Object[] args)
058: throws Exception {
059: throw new RuntimeProxyException("Attribute not found: "
060: + e.toString());
061: }
062:
063: public Object handleInvalidAttributeValue(ProxyContext ctx,
064: InvalidAttributeValueException e, Method m, Object[] args)
065: throws Exception {
066: throw new RuntimeProxyException("Invalid attribute value: "
067: + e.toString());
068: }
069:
070: public Object handleMBeanException(ProxyContext ctx,
071: MBeanException e, Method m, Object[] args) throws Exception {
072: // assuming MBeanException only wraps mgmt interface "application"
073: // exceptions therefore we can safely rethrow the target exception
074: // as its declared in the mgmt interface
075: throw e.getTargetException();
076: }
077:
078: public Object handleReflectionException(ProxyContext ctx,
079: ReflectionException e, Method m, Object[] args)
080: throws Exception {
081: // use of reflection exception is inconsistent in the API so the
082: // safest bet is to rethrow a runtime exception
083:
084: Exception target = e.getTargetException();
085: if (target instanceof RuntimeException)
086: throw target;
087: else
088: throw new RuntimeProxyException(target.toString());
089: }
090:
091: public Object handleRuntimeOperationsException(ProxyContext ctx,
092: RuntimeOperationsException e, Method m, Object[] args)
093: throws Exception {
094: // target is always a runtime exception, so its ok to throw it from here
095: throw e.getTargetException();
096: }
097:
098: public Object handleRuntimeMBeanException(ProxyContext ctx,
099: RuntimeMBeanException e, Method m, Object[] args)
100: throws Exception {
101: // target is always a runtime exception, so its ok to throw it from here
102: throw e.getTargetException();
103: }
104:
105: public Object handleRuntimeError(ProxyContext ctx,
106: RuntimeErrorException e, Method m, Object[] args)
107: throws Exception {
108: // just unwrap and throw the actual error
109: throw e.getTargetError();
110: }
111:
112: }
|