User: juhalindfors
  Date: 02/01/24 14:13:33

  Modified:    src/main/org/jboss/mx/interceptor
                        StandardMBeanInterceptor.java
                        InvocationException.java
  Log:
  fixed exception handling
  
  Revision  Changes    Path
  1.5       +73 -17    
jmx/src/main/org/jboss/mx/interceptor/StandardMBeanInterceptor.java
  
  Index: StandardMBeanInterceptor.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/jmx/src/main/org/jboss/mx/interceptor/StandardMBeanInterceptor.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- StandardMBeanInterceptor.java     2001/12/13 00:04:15     1.4
  +++ StandardMBeanInterceptor.java     2002/01/24 22:13:33     1.5
  @@ -18,7 +18,9 @@
   import javax.management.MBeanOperationInfo;
   import javax.management.ReflectionException;
   import javax.management.RuntimeErrorException;
  +import javax.management.RuntimeMBeanException;
   import javax.management.MBeanException;
  +import javax.management.AttributeNotFoundException;
   import javax.management.loading.DefaultLoaderRepository;
   
   import org.jboss.mx.server.StandardMBeanInvoker;
  @@ -32,7 +34,7 @@
    * @see org.jboss.mx.server.StandardMBean
    *
    * @author  <a href="mailto:[EMAIL PROTECTED]";>Juha Lindfors</a>.
  - * @version $Revision: 1.4 $
  + * @version $Revision: 1.5 $
    *   
    */
   public class StandardMBeanInterceptor 
  @@ -40,19 +42,20 @@
   {
   
      // Attributes ----------------------------------------------------   
  -   private Map methodMap = new HashMap();
  -   private Class invocationInterface;
  -   private MBeanInfo info;
  -   private Object resource;
  +   private Map methodMap               = new HashMap();
  +   private Class invocationInterface   = null;
  +   private MBeanInfo info              = null;
  +   private Object resource             = null;
      
      // Constructors --------------------------------------------------
      public StandardMBeanInterceptor(Object resource, MBeanInfo info) throws 
ReflectionException
      {
         super("Standard MBean Interceptor");
         
  +      this.info = info;
         this.resource = resource;
         this.invocationInterface = StandardMBeanInvoker.getMBeanInterface(resource);
  -
  +      
         MBeanOperationInfo[] operations = info.getOperations();
   
         for (int i = 0; i < operations.length; ++i)
  @@ -68,6 +71,8 @@
                  strBuf.append(params[j].getType());
               }
   
  +            // FIXME: separate operation and attribute maps!
  +            
               methodMap.put(name + strBuf.toString(), invocationInterface.getMethod(
                             name, getSignatureAsClassArray(params, 
resource.getClass().getClassLoader())));
            }
  @@ -184,35 +189,86 @@
      // Interceptor overrides ----------------------------------------------
      public Object invoke(Invocation invocation) throws InvocationException 
      {
  +      Method method = null;
  +      
         try
         {
  -         Method m = (Method)methodMap.get(invocation.getOperationWithSignature());
  -         return m.invoke(resource, invocation.getArgs());
  +         method = (Method)methodMap.get(invocation.getOperationWithSignature());
  +         return method.invoke(resource, invocation.getArgs());
         }
  +      
         catch (IllegalAccessException e)
         {
  -         throw new InvocationException(e, "Illegal access to method " + 
invocation.getName());
  +         throw new InvocationException(new ReflectionException(e,"Illegal access to 
method " + method.getName()));
         }
  +      
         catch (IllegalArgumentException e)
         {
  -         throw new InvocationException(e, "Illegal operation arguments in " + 
invocation.getName() + ": " + e.getMessage());
  +         throw new InvocationException(new ReflectionException(e, 
  +               "Illegal arguments for " + ((invocation.getInvocationType() == 
Invocation.ATTRIBUTE)
  +                                        ? "attribute "
  +                                        : "operation ")
  +                                        + invocation.getName()
  +                                        + ": " + e.getMessage())
  +         );
         }
  +      
         catch (InvocationTargetException e)
         {
  -         if (e.getTargetException() instanceof Exception)
  -         {
  -            Exception e2 = (Exception)e.getTargetException();
  -            throw new InvocationException(e2, "Operation " + invocation.getName() + 
" on MBean " + info.getClassName() + " has thrown an exception: " + e2.toString());
  +         // exception or error from the MBean implementation ('business exception')
  +         
  +         if (e.getTargetException() instanceof RuntimeException)
  +         {
  +            // runtime exceptions from mbean wrapped in RuntimeMBeanException
  +            throw new InvocationException(new 
RuntimeMBeanException((RuntimeException)e.getTargetException(),
  +                  "RuntimeException thrown by " + ((invocation.getInvocationType() 
== Invocation.ATTRIBUTE)
  +                                                ? "attribute "
  +                                                : "operation ")
  +                                                + invocation.getName() + " in MBean 
"
  +                                                + info.getClassName() + ": "
  +                                                + e.getTargetException().toString())
  +            );                  
  +         }
  +         
  +         else if (e.getTargetException() instanceof Exception)
  +         {
  +            // checked exceptions from mbean wrapped in MBeanException
  +            throw new InvocationException(new 
MBeanException((Exception)e.getTargetException(),
  +                  "Exception thrown by " + ((invocation.getInvocationType() == 
Invocation.ATTRIBUTE)
  +                                         ? "attribute "
  +                                         : "operation ")
  +                                         + invocation.getName() + " in MBean " 
  +                                         + info.getClassName() + ": "
  +                                         + e.getTargetException().toString())
  +            );
  +         }
  +         
  +         else if (e.getTargetException() instanceof Error)
  +         {
  +            // errors from mbean wrapped in RuntimeErrorException
  +            throw new InvocationException(new 
RuntimeErrorException((Error)e.getTargetException(),
  +                  "Error thrown by " + ((invocation.getInvocationType() == 
Invocation.ATTRIBUTE)
  +                                     ? "attribute "
  +                                     : "operation ")
  +                                     + invocation.getName() + " in MBean "
  +                                     + info.getClassName() + ": "
  +                                     + e.getTargetException().toString())
  +            );
            }
  +         
            else
            {
  -            Error err = (Error)e.getTargetException();
  -            throw new InvocationException(err, "Operation " + invocation.getName() 
+ " on MBean " + info.getClassName() + " has thrown an errpr: " + err.toString());
  +            throw new InvocationException(e.getTargetException());
            }
         }
         catch (NullPointerException e)
         {
  -         throw new InvocationException(e, "Operation " + invocation.getName() + " 
is not a declared management operation in " + invocationInterface + " interface.");
  +         // method (operation or getter/setter) is not part of the management 
interface
  +         
  +         if (invocation.getInvocationType() == Invocation.ATTRIBUTE)
  +            throw new InvocationException(new AttributeNotFoundException("Attribute 
'" + invocation.getName() + "' not found on " + info.getClassName() + "."), "plaah");
  +         else 
  +            throw new InvocationException(new ReflectionException(new 
NullPointerException("Operation '" + invocation.getName() + "' not found on " + 
info.getClassName() + ".")));
         }
      }
      
  
  
  
  1.2       +7 -1      jmx/src/main/org/jboss/mx/interceptor/InvocationException.java
  
  Index: InvocationException.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/jmx/src/main/org/jboss/mx/interceptor/InvocationException.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- InvocationException.java  2001/12/06 23:02:41     1.1
  +++ InvocationException.java  2002/01/24 22:13:33     1.2
  @@ -10,8 +10,14 @@
       extends Exception
   {
   
  -   private Throwable t;
  +   private Throwable t = null;
   
  +   public InvocationException(Throwable t)
  +   {
  +      super();
  +      this.t = t;
  +   }
  +   
      public InvocationException(Throwable t, String msg)
      {
         super(msg);
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to