User: juhalindfors
  Date: 02/04/12 16:02:25

  Modified:    src/main/org/jboss/mx/util MBeanProxy.java
  Log:
  concentrating expception handling on create methods + javadoc
  
  Revision  Changes    Path
  1.2       +107 -29   jmx/src/main/org/jboss/mx/util/MBeanProxy.java
  
  Index: MBeanProxy.java
  ===================================================================
  RCS file: /cvsroot/jboss/jmx/src/main/org/jboss/mx/util/MBeanProxy.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MBeanProxy.java   23 Jan 2002 22:01:24 -0000      1.1
  +++ MBeanProxy.java   12 Apr 2002 23:02:25 -0000      1.2
  @@ -37,7 +37,7 @@
    * @see java.lang.reflect.Proxy
    *
    * @author  <a href="mailto:[EMAIL PROTECTED]";>Juha Lindfors</a>.
  - * @version $Revision: 1.1 $
  + * @version $Revision: 1.2 $
    *   
    */
   public class MBeanProxy
  @@ -46,61 +46,142 @@
      // Static --------------------------------------------------------
      
      // FIXME: unify the exceptions thrown by the static get and create methods
  -   public static Object get(Class intrface, ObjectName name, String agentID) 
  -         throws InstanceNotFoundException, IntrospectionException, 
ReflectionException
  +   
  +   /**
  +    * Creates a proxy to an MBean in the given (local) MBean server.
  +    *
  +    * @param   intrface    the interface this proxy implements
  +    * @param   name        object name of the MBean this proxy connects to
  +    * @param   agentID     agent ID of the MBean server this proxy connects to
  +    *
  +    * @return  proxy instance
  +    *
  +    * @throws MBeanProxyCreationException if the proxy could not be created
  +    */
  +   public static Object get(Class intrface, ObjectName name, String agentID) throws 
MBeanProxyCreationException
      {
         return get(intrface, name, 
(MBeanServer)MBeanServerFactory.findMBeanServer(agentID).get(0));
      }
   
  -   public static Object get(Class intrface, ObjectName name, MBeanServer server) 
  -         throws InstanceNotFoundException, IntrospectionException, 
ReflectionException
  +   /**
  +    * Creates a proxy to an MBean in the given (local) MBean server.
  +    *
  +    * @param   intrface the interface this proxy implements
  +    * @param   name     object name of the MBean this proxy connects to
  +    * @param   server   MBean server this proxy connects to
  +    *
  +    * @return proxy instance
  +    *
  +    * @throws MBeanProxyCreationException if the proxy could not be created
  +    */
  +   public static Object get(Class intrface, ObjectName name, MBeanServer server)  
throws MBeanProxyCreationException
      {
         return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
               new Class[] {intrface}, new JMXInvocationHandler(server, name));
      }
   
  -   public static Object create(Class instance, Class intrface, ObjectName name, 
MBeanServer server) 
  -         throws InstanceNotFoundException, IntrospectionException, 
ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, 
MBeanException, NotCompliantMBeanException
  -   {
  -      server.createMBean(instance.getName(), name);
  -      return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
  -            new Class[] {intrface}, new JMXInvocationHandler(server, name));
  -   }
   
  -   public static Object create(Class instance, Class intrface, ObjectName name, 
String agentID) 
  -         throws InstanceNotFoundException, IntrospectionException, 
ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, 
MBeanException, NotCompliantMBeanException
  +   /**
  +    * Convenience method for registering an MBean and retrieving a proxy for it.
  +    *
  +    * @param   instance MBean instance to be registered
  +    * @param   intrface the interface this proxy implements
  +    * @param   name     object name of the MBean
  +    * @param   agentID  agent ID of the MBean server this proxy connects to
  +    *
  +    * @return proxy instance
  +    *
  +    * @throws MBeanProxyCreationException if the proxy could not be created
  +    */
  +   public static Object create(Class instance, Class intrface, ObjectName name, 
String agentID) throws MBeanProxyCreationException
      {
         return create(instance, intrface, name, 
(MBeanServer)MBeanServerFactory.findMBeanServer(agentID).get(0));
  +   }   
  +   
  +   /**
  +    * Convenience method for registering an MBean and retrieving a proxy for it.
  +    *
  +    * @param   instance MBean instance to be registered
  +    * @param   intrface the interface this proxy implements
  +    * @param   name     object name of the MBean
  +    * @param   server   MBean server this proxy connects to
  +    *
  +    * @throws MBeanProxyCreationException if the proxy could not be created
  +    */
  +   public static Object create(Class instance, Class intrface, ObjectName name, 
MBeanServer server) throws MBeanProxyCreationException
  +   {
  +      try
  +      {
  +         server.createMBean(instance.getName(), name);
  +         return 
Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
  +               new Class[] {intrface}, new JMXInvocationHandler(server, name));
  +      }
  +      catch (ReflectionException e) {
  +         throw new MBeanProxyCreationException("Creating the MBean failed: " + 
e.toString());
  +      }
  +      catch (InstanceAlreadyExistsException e) {
  +         throw new MBeanProxyCreationException("Instance already exists: " + name);
  +      }
  +      catch (MBeanRegistrationException e) {
  +         throw new MBeanProxyCreationException("Error registering the MBean to the 
server: " + e.toString());
  +      }
  +      catch (MBeanException e) {
  +         throw new MBeanProxyCreationException(e.toString());
  +      }
  +      catch (NotCompliantMBeanException e) {
  +         throw new MBeanProxyCreationException("Not a compliant MBean " + 
instance.getClass().getName() + ": " + e.toString());
  +      }
      }
  +
      
      // Inner Classes -------------------------------------------------
  +   
  +   /**
  +    * Proxy invocation handler
  +    */
      private static class JMXInvocationHandler implements InvocationHandler
  -   {   
  +   {  
  +      
         // Attributes -------------------------------------------------
         private MBeanServer server    = null;
         private ObjectName objectName = null;
         private HashMap attributeMap  = new HashMap();
         
         // Constructors -----------------------------------------------
  -      public JMXInvocationHandler(MBeanServer server, ObjectName name) 
  -            throws InstanceNotFoundException, IntrospectionException, 
ReflectionException
  +      public JMXInvocationHandler(MBeanServer server, ObjectName name) throws 
MBeanProxyCreationException
         {
  -         this.server     = server;
  -         this.objectName = name;
  -        
  -         MBeanInfo info = server.getMBeanInfo(objectName);
  -         MBeanAttributeInfo[] attributes = info.getAttributes();
  -
  -         for (int i = 0; i < attributes.length; ++i)
  +         try
  +         {
  +            // make sure we were able to resolve the server
  +            if (server == null)
  +               throw new InstanceNotFoundException("null agent reference");
  +               
  +            this.server     = server;
  +            this.objectName = name;
  +           
  +            MBeanInfo info = server.getMBeanInfo(objectName);
  +            MBeanAttributeInfo[] attributes = info.getAttributes();
  +   
  +            for (int i = 0; i < attributes.length; ++i)
  +               attributeMap.put(attributes[i].getName(), attributes[i]);
  +         }
  +         catch (InstanceNotFoundException e)
            {
  -            attributeMap.put(attributes[i].getName(), attributes[i]);
  +            throw new MBeanProxyCreationException("Object name " + name + " not 
found: " + e.toString());
  +         }
  +         catch (IntrospectionException e)
  +         {
  +            throw new MBeanProxyCreationException(e.toString());
  +         }
  +         catch (ReflectionException e)
  +         {
  +            throw new MBeanProxyCreationException(e.toString());
            }
         }
         
         // InvocationHandler implementation ---------------------------
         public Object invoke(Object proxy, Method method, Object[] args) throws 
Exception
         {
  -
            try {
               String methodName = method.getName();
               
  @@ -214,13 +295,10 @@
               // just unwrap and throw the actual error
               throw e.getTargetError();
            }
  -         catch (NullPointerException e) { System.out.println("HERE"); return null;}
         }
  -      
      }
      
   }
  -      
   
   
   
  
  
  

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

Reply via email to