User: juhalindfors
  Date: 02/02/15 08:42:13

  Modified:    src/main/org/jboss/mx/server MBeanServerImpl.java
                        ServerConstants.java
  Log:
  improved the exception handling from instantiate() + little javadoc
  
  Revision  Changes    Path
  1.13      +134 -30   jmx/src/main/org/jboss/mx/server/MBeanServerImpl.java
  
  Index: MBeanServerImpl.java
  ===================================================================
  RCS file: /cvsroot/jboss/jmx/src/main/org/jboss/mx/server/MBeanServerImpl.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- MBeanServerImpl.java      29 Jan 2002 03:33:21 -0000      1.12
  +++ MBeanServerImpl.java      15 Feb 2002 16:42:13 -0000      1.13
  @@ -31,6 +31,7 @@
   import javax.management.InvalidAttributeValueException;
   import javax.management.ListenerNotFoundException;
   import javax.management.RuntimeErrorException;
  +import javax.management.RuntimeMBeanException;
   import javax.management.RuntimeOperationsException;
   import javax.management.QueryExp;
   import javax.management.MBeanInfo;
  @@ -56,25 +57,76 @@
   
   
   /**
  + * MBean server implementation. This is the default server implementation
  + * returned by the <tt>MBeanServerFactory</tt> class ({@link 
ServerConstants#DEFAULT_MBEAN_SERVER_CLASS DEFAULT_MBEAN_SERVER_CLASS}). <p>
  + *
  + * The MBean server behaviour can be further configured by setting the following
  + * system properties: <ul>
  + *    <li><tt>jbossmx.loader.repository.class</tt> ({@link 
ServerConstants#LOADER_REPOSITORY_CLASS_PROPERTY 
LOADER_REPOSITORY_CLASS_PROPERTY})</li>
  + *    <li><tt>jbossmx.required.modelmbean.class</tt> ({@link 
ServerConstants#REQUIRED_MODELMBEAN_CLASS_PROPERTY 
REQUIRED_MODELMBEAN_CLASS_PROPERTY})</li>
  + * </ul>
  + *
  + * The loader repository is used for managing class loaders in the MBean server.
  + * The default repository uses the <tt>BasicLoaderRepository</tt> implementation
  + * ({@link ServerConstants#DEFAULT_LOADER_REPOSITORY_CLASS 
DEFAULT_LOADER_REPOSITORY_CLASS}).<p>
  + *
  + * The <tt>RequiredModelMBean</tt> uses <tt>XMBean</tt> implementation by default
  + * ({@link ServerConstants#DEFAULT_REQUIRED_MODELMBEAN_CLASS 
DEFAULT_REQUIRED_MODELMBEAN_CLASS}).
    *
    * @see javax.management.MBeanServer
  + * @see javax.management.modelmbean.RequiredModelMBean
  + * @see org.jboss.mx.server.ServerConstants
  + * @see org.jboss.mx.loading.LoaderRepository
  + * @see org.jboss.mx.loading.BasicLoaderRepository
  + * @see org.jboss.mx.modelmbean.XMBean
    *
    * @author  <a href="mailto:[EMAIL PROTECTED]";>Juha Lindfors</a>.
  - * @version $Revision: 1.12 $
  + * @version $Revision: 1.13 $
    */
   public class MBeanServerImpl
      implements MBeanServer, ServerConstants
   {
   
      // Attributes ----------------------------------------------------
  +   
  +   /**
  +    * Sequence number for the MBean server registration notifications. 
  +    */
      protected long registrationNotificationSequence    = 1;
  +   
  +   /**
  +    * Sequence number for the MBean server unregistration notifications.
  +    */
      protected long unregistrationNotificationSequence  = 1;
  -   protected String defaultDomain                     = "DefaultDomain";
  +   
  +   /**
  +   * Default domain name ({@link ServerConstants#DEFAULT_DOMAIN DEFAULT_DOMAIN}) 
used by this server implementation.
  +    */
  +   protected String defaultDomain                     = DEFAULT_DOMAIN;
  +   
  +   /**
  +    * Registry used by this server to map MBean object names to resource references.
  +    */
      protected MBeanRegistry registry                   = null;
  +   
  +   /**
  +    * Direct reference to the mandatory MBean server delegate MBean.
  +    */
      protected MBeanServerDelegate delegate             = null;
  +   
  +   /**
  +    * Class loader repository for this server. The implementation class is 
retrieved via
  +    * {@link LoaderRepository#getDefaultLoaderRepository} call.
  +    */
      protected LoaderRepository loaderRepository        = 
LoaderRepository.getDefaultLoaderRepository();
   
      // Constructors --------------------------------------------------
  +   /**
  +    * Creates an MBean server implementation with a given default domain name and
  +    * registers the mandatory server delegate MBean to the server ({@link 
ServerConstants#MBEAN_SERVER_DELEGATE MBEAN_SERVER_DELEGATE}).
  +    *
  +    * @param defaultDomain default domain name
  +    */
      public MBeanServerImpl(String defaultDomain)
      {
         this.defaultDomain = defaultDomain;
  @@ -109,24 +161,23 @@
         try
         {
            Class clazz = DefaultLoaderRepository.loadClass(className);
  -         return clazz.newInstance();
  +         Constructor constr = clazz.getConstructor(new Class[0]);
  +         
  +         return constr.newInstance(new Object[0]);
         }
  -      catch (ClassNotFoundException e)
  +      catch (Throwable t)
         {
  -         throw new ReflectionException(e, "Class not found: " + className);
  -      }
  -      catch (InstantiationException e)
  -      {
  -         throw new ReflectionException(e, "Cannot instantiate with no-args 
constructor: "  + className);
  -      }
  -      catch (IllegalAccessException e)
  -      {
  -         throw new ReflectionException(e, "Illegal access to default constructor: " 
 + className);
  +         handleInstantiateExceptions(t, className);
  +         
  +         // FIXME: log it
  +         System.out.println("Unhandled exception: " + t.toString());
  +         t.printStackTrace();
  +         
  +         return null;
         }
      }
   
  -   public Object instantiate(String className, ObjectName loaderName)
  -   throws ReflectionException, MBeanException, InstanceNotFoundException
  +   public Object instantiate(String className, ObjectName loaderName) throws 
ReflectionException, MBeanException, InstanceNotFoundException
      {
         try
         {
  @@ -136,28 +187,27 @@
               loader = this.getClass().getClassLoader();
   
            Class clazz = loader.loadClass(className);
  -         return clazz.newInstance();
  +         Constructor constr = clazz.getConstructor(new Class[0]);
  +         
  +         return constr.newInstance(new Object[0]);
         }
         catch (ClassCastException e)
         {
            throw new ReflectionException(e, loaderName + " is not a class loader.");
         }
  -      catch (ClassNotFoundException e)
  -      {
  -         throw new ReflectionException(e, "Class not found: " + className);
  -      }
  -      catch (InstantiationException e)
  +      catch (Throwable t)
         {
  -         throw new ReflectionException(e, "Caanot instantiate with not-args 
constructor: " + className);
  -      }
  -      catch (IllegalAccessException e)
  -      {
  -         throw new ReflectionException(e, "Illegal access to default constructor: " 
+ className);
  +         handleInstantiateExceptions(t, className);
  +         
  +         // FIXME: log it
  +         System.out.println("Unhandled exception: " + t.toString());
  +         t.printStackTrace();
  +         
  +         return null;
         }
      }
   
  -   public Object instantiate(String className, Object[] params, String[] signature)
  -   throws ReflectionException, MBeanException
  +   public Object instantiate(String className, Object[] params, String[] signature) 
throws ReflectionException, MBeanException
      {
         try
         {
  @@ -206,8 +256,7 @@
      }
   
   
  -   public Object instantiate(String className, ObjectName loaderName, Object[] 
params, String[] signature)
  -   throws ReflectionException, MBeanException, InstanceNotFoundException
  +   public Object instantiate(String className, ObjectName loaderName, Object[] 
params, String[] signature) throws ReflectionException, MBeanException, 
InstanceNotFoundException
      {
         try
         {
  @@ -585,6 +634,61 @@
      }
   
      // Protected -----------------------------------------------------
  +   protected void handleInstantiateExceptions(Throwable t, String className) throws 
ReflectionException, MBeanException
  +   {
  +      if (t instanceof ClassNotFoundException)
  +         throw new ReflectionException((Exception)t, "Class not found: " + 
className);
  +      
  +      else if (t instanceof InstantiationException)
  +         throw new ReflectionException((Exception)t, "Cannot instantiate with 
no-args constructor: "  + className);
  +      
  +      else if (t instanceof IllegalAccessException)
  +         throw new ReflectionException((Exception)t, "Illegal access to default 
constructor: "  + className);
  +      
  +      else if (t instanceof NoSuchMethodException)
  +         throw new ReflectionException((Exception)t, className + " does not have a 
public no args constructor.");
  +      
  +      else if (t instanceof SecurityException)
  +         throw new ReflectionException((Exception)t, "Can't access default 
constructor for " + className + ": " + t.toString());
  +      
  +      else if (t instanceof InvocationTargetException)
  +      {
  +         Throwable root = ((InvocationTargetException)t).getTargetException();
  +         
  +         if (root instanceof RuntimeException)
  +            throw new RuntimeMBeanException((RuntimeException)root, className + " 
constructor has thrown an exception: " + root.toString());
  +         else if (root instanceof Error)
  +            throw new RuntimeErrorException((Error)root, className + " constructor 
has thrown an error: " + root.toString());
  +         else if (root instanceof Exception)
  +            throw new MBeanException((Exception)root, className + " constructor has 
thrown an exception: " + root.toString());
  +            
  +         throw new Error("Something went wrong with handling the exception from " + 
className + " default constructor.");
  +      }
  +      
  +      else if (t instanceof ExceptionInInitializerError)
  +      {
  +         Throwable root = ((ExceptionInInitializerError)t).getException();
  +         
  +         // the root cause can be only a runtime exception
  +         if (root instanceof RuntimeException)
  +            throw new RuntimeMBeanException((RuntimeException)root, "Exception in 
class " + className + " static initializer: " + root.toString());
  +         else
  +            // shouldn't get here
  +            throw new Error("ERROR: it turns out the root cause is not always a 
runtime exception!");
  +      }
  +      
  +      else if (t instanceof IllegalArgumentException)
  +      {
  +         // if mismatch between constructor instance args and supplied args -- 
shouldn't happen
  +         throw new Error("Error in the server: mismatch between expected 
constructor arguments and supplied arguments.");
  +      }
  +      
  +      else if (t instanceof Error)
  +      {
  +         throw new RuntimeErrorException((Error)t, "instantiating " + className + " 
failed: " + t.toString());
  +      }      
  +   }      
  +
      protected int getMBeanType(Object object)
      {
   
  
  
  
  1.3       +43 -10    jmx/src/main/org/jboss/mx/server/ServerConstants.java
  
  Index: ServerConstants.java
  ===================================================================
  RCS file: /cvsroot/jboss/jmx/src/main/org/jboss/mx/server/ServerConstants.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ServerConstants.java      28 Jan 2002 23:36:32 -0000      1.2
  +++ ServerConstants.java      15 Feb 2002 16:42:13 -0000      1.3
  @@ -8,12 +8,16 @@
   package org.jboss.mx.server;
   
   /**
  - * Server related constant variables.
  + * Server related constant variables. These are constants that are used internally
  + * by the MBean server implementation or are used to configure the MBean server.
  + * Different JMX service specific constants should be added to the 
<tt>ServiceConstants</tt>
  + * interface.
    *
    * @see org.jboss.mx.service.ServiceConstants
  + * @see org.jboss.mx.server.MBeanServerImpl
    *
    * @author  <a href="mailto:[EMAIL PROTECTED]";>Juha Lindfors</a>.
  - * @version $Revision: 1.2 $
  + * @version $Revision: 1.3 $
    *   
    */
   public interface ServerConstants
  @@ -23,17 +27,46 @@
      /**
       * String representation of the MBean server delegate MBean object name.
       */
  -   final static String MBEAN_SERVER_DELEGATE     = 
"JMImplementation:type=MBeanServerDelegate";
  +   final static String MBEAN_SERVER_DELEGATE              = 
"JMImplementation:type=MBeanServerDelegate";
      
  -   final static String DEFAULT_DOMAIN = "DefaultDomain";
  +   /**
  +    * The default domain name for the MBean server. If a default domain is not 
specified
  +    * when the server is created, this value (<tt>"DefaultDomain"</tt>) is used.
  +    *
  +    * @see  javax.management.MBeanServerFactory
  +    */
  +   final static String DEFAULT_DOMAIN                     = "DefaultDomain";
   
      // Constants for server delegate
  -   final static String SPECIFICATION_NAME        = "Java Management Extensions 
Instrumentation and Agent Specification";
  -   final static String SPECIFICATION_VERSION     = "1.0";
  -   final static String SPECIFICATION_VENDOR      = "Sun Microsystems, Inc.";
  -   final static String IMPLEMENTATION_NAME       = "JBossMX";
  -   final static String IMPLEMENTATION_VERSION    =  "0.8";
  -   final static String IMPLEMENTATION_VENDOR     = "JBoss Organization";
  +   /**
  +    * The specification name of the implementation. This value can be retrieved 
from the MBean server delegate.
  +    */
  +   final static String SPECIFICATION_NAME                 = "Java Management 
Extensions Instrumentation and Agent Specification";
  +   
  +   /**
  +    * The specification version of the implementation. This value can be retrieved 
from the MBean server delegate.
  +    */
  +   final static String SPECIFICATION_VERSION              = "1.0";
  +   
  +   /**
  +    * The specification vendor name. This value can be retrieved from the MBean 
server delegate.
  +    */
  +   final static String SPECIFICATION_VENDOR               = "Sun Microsystems, 
Inc.";
  +   
  +   /**
  +    * The name of the implementation. This value can be retrieved from the MBean 
server delegate.
  +    */
  +   final static String IMPLEMENTATION_NAME                = "JBossMX";
  +   
  +   /**
  +    * The version of the implementation. This value can be retrieved from the MBean 
server delegate.
  +    */
  +   final static String IMPLEMENTATION_VERSION             =  "1.0 Alpha 1";
  +   
  +   /**
  +    * The vendor of the implementation. This value can be retrieved from the MBean 
server delegate.
  +    */
  +   final static String IMPLEMENTATION_VENDOR              = "JBoss Organization";
      
      // system properties
      final static String REQUIRED_MODELMBEAN_CLASS_PROPERTY = 
"jbossmx.required.modelmbean.class";
  
  
  

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

Reply via email to