FileDocCategorySizeDatePackage
CustomMBeanRegistrationImpl.javaAPI DocGlassfish v2 API13255Fri May 04 22:24:10 BST 2007com.sun.enterprise.admin.mbeans.custom.loading

CustomMBeanRegistrationImpl

public final class CustomMBeanRegistrationImpl extends Object implements com.sun.enterprise.admin.server.core.CustomMBeanRegistration
Class to register all the MBeans defined in the server's configuration. Registering the MBeans is done as described in the design document. The self-management rules are to be taken into account for this. As it stands now, (AS 9.0) this class is not designed to be thread-safe. The calling code must ensure serial access if needed.
since
SJSAS 9.0

Fields Summary
private final MBeanServer
mbs
Creates a new instance of CustomMBeanRegistrar
private ClassLoader
cl
private static final Logger
logger
Constructors Summary
public CustomMBeanRegistrationImpl(MBeanServer mbs)


          
        this.mbs    = mbs;
        this.cl     = new MBeanClassLoader();
    
Methods Summary
public static javax.management.ObjectNamegetCascadingAwareObjectName(com.sun.enterprise.config.serverbeans.Mbean mbean)

        try {
            final ObjectName configON   = new ObjectName(mbean.getObjectName());
            return (getCascadingAwareObjectName(configON) );
        } catch(final CustomMBeanException cmbe) {
            throw cmbe;
        } catch(final Exception e) {
            throw new CustomMBeanException(e);
        }
    
public static javax.management.ObjectNamegetCascadingAwareObjectName(javax.management.ObjectName configON)

        try {
            final String serverNameKey  = CustomMBeanConstants.SERVER_KEY;
            final String serverNameVal  = System.getProperty(SystemPropertyConstants.SERVER_NAME);
            final Hashtable properties  = configON.getKeyPropertyList();
            properties.put(serverNameKey, serverNameVal);
            final ObjectName ron = new ObjectName(configON.getDomain(), properties);
            return ( ron );
        } catch(final Exception e) {
            throw new CustomMBeanException(e);
        }
    
public static javax.management.ObjectNamegetCascadingUnawareObjectName(javax.management.ObjectName cascadeON)

        if (cascadeON == null) 
            throw new CustomMBeanException(CMBStrings.get("InternalError", "getCascadingUnawareObjectName() received a null argument"));
        try {
            ObjectName ron = cascadeON;
            final String serverNameKey  = CustomMBeanConstants.SERVER_KEY;
            final Hashtable properties  = cascadeON.getKeyPropertyList(); // this may be unmodifiable
            if (properties.containsKey(serverNameKey)) {
                final Hashtable np = new Hashtable(properties);
                np.remove(serverNameKey);
                ron = new ObjectName(cascadeON.getDomain(), np);
            }
            return ( ron );
        } catch(final Exception e) {
            throw new CustomMBeanException(e);
        }        
    
private voidinitIt(com.sun.enterprise.config.serverbeans.Mbean mbc, javax.management.ObjectName on)

        try {
            final MBeanAttributeSetter mas = new MBeanAttributeSetter(mbs, on);
            final ElementProperty[] ats = mbc.getElementProperty();
            for (ElementProperty p : ats) {
                mas.setIt(p.getName(), p.getValue());
                logger.fine(CMBStrings.get("cmb.initMBean",  p.getName(), mbc.getName()));
            }
        }catch(CustomMBeanException cmbe) {
            // If we get a CMBE -- then everything is already documented...
            throw cmbe;
        }catch(Throwable t) {
            // indirect calls to user code -- thus Throwable!
            throw new CustomMBeanException(t);
        }
    
private java.lang.ClassloadIt(com.sun.enterprise.config.serverbeans.Mbean mbean)

        final String classname = mbean.getImplClassName();
        try {
            final Class c = cl.loadClass(classname);
            logger.fine(CMBStrings.get("cmb.loadingMBean8", c.getName(), cl.getClass().getName(), c.getClassLoader().getClass().getName()));
            return ( c );
        } catch (final ClassNotFoundException cnfe) {
            logger.info(CMBStrings.get("cmb.loadingMBean3", mbean.getImplClassName(), cl.getClass().getName()));
            throw new CustomMBeanException(cnfe);
        } catch (final NoClassDefFoundError ncdfe) {
            logger.info(CMBStrings.get("cmb.loadingMBean4", mbean.getImplClassName(), cl.getClass().getName()));
            throw new CustomMBeanException(ncdfe);
        } catch (final Exception e) {
            throw new CustomMBeanException(e);
        }
    
private java.lang.ObjectnewIt(java.lang.Class c)

        String name = null;
        try{
            name = c.getName();
            return c.newInstance();
         } catch (final InstantiationException ie) {
            logger.info(CMBStrings.get("cmb.loadingMBean5", name));
            throw new CustomMBeanException(ie);
        } catch (final IllegalAccessException iae) {
            logger.info(CMBStrings.get("cmb.loadingMBean6", name));
            throw new CustomMBeanException(iae);
        } catch (final ExceptionInInitializerError eie) {
            logger.info(CMBStrings.get("cmb.loadingMBean7", name));
            throw new CustomMBeanException(eie);
        }catch(Throwable t) {
            // yes -- we are catching a Throwable here.  Normally this is highly 
            // discouraged.  But this is a special case.  We are calling the user's
            // code and he may be throwing a Throwable in the ctor...
            
            String message = CMBStrings.get("cmb.newingMBean", c.getName(), t);
            logger.warning(message);
            throw new CustomMBeanException(message, t);
        }
    
private javax.management.ObjectNameregisterIt(java.lang.Object mo, javax.management.ObjectName on)

        if(mo == null)
            throw new CustomMBeanException(CMBStrings.get("objNameNull"));
        try {
            final ObjectInstance oi = mbs.registerMBean(mo, on);
            return ( oi.getObjectName() );
        }catch(Exception e){
            throw new CustomMBeanException(e);
        }
    
public javax.management.ObjectNameregisterMBean(com.sun.enterprise.config.serverbeans.Mbean mbean)

        /* This is the only place where the "registration of the MBean happens.
         */
        if (mbean == null)
            throw new CustomMBeanException(CMBStrings.get("InternalError", "registerMBean() received a null argument"));
        
        ObjectName ron = null;
        try {
            logger.fine(CMBStrings.get("cmb.loadingMBean1", mbean.getName()));
            final ObjectName mon    = getCascadingAwareObjectName(mbean);
            logger.fine(CMBStrings.get("cmb.loadingMBean2", mon.toString()));
            final Class mc          = loadIt(mbean);
            final Object mo         = newIt(mc);
            ron = registerIt(mo, mon);
            
            // if the MBean implements MBeanRegistration -- it can set the ON to 
            // anything at all...
            if(!ObjectNameSelectionAlgorithm.implementsMBeanRegistrationInterface(mbean.getImplClassName()))
            {
                if(!mon.equals(ron))
                    throw new CustomMBeanException(CMBStrings.get("objNameMismatch", mon, ron));
            }
            initIt(mbean, ron);
   
            // WBN 12-15-2005
            // this is just defensive programming -- the Listener should not be
            // calling us on a disabled mbean.  In the case we are being called
            // as part of pre-reg in order to get an objectname, this will make
            // one extra unneeded call to unregisterIt()
            // I say, safety first, performance second!
            if(!mbean.isEnabled())
                unregisterIt(mbean, ron);
            return ( ron );

        }catch (final CustomMBeanException cmbe) {
            if(ron != null) {
                try {
                    // paranoia...
                    unregisterIt(mbean, ron);
                }catch(Throwable t) {
                }
            }
            throw cmbe;
        }catch (final Throwable e) {
            if(ron != null) {
                try {
                    // paranoia...
                    unregisterIt(mbean, ron);
                }catch(Throwable t) {
                }
            }
            throw new CustomMBeanException(e);
        }
    
public voidregisterMBeans(java.util.List mbeans, boolean continueReg)

        if (mbeans == null)
            throw new IllegalArgumentException(
                CMBStrings.get("InternalError", "registerMBeans() received a null mbeans argument"));
        for (Mbean mbean : mbeans) {
            try {
                registerMBean(mbean);
            } catch(final Throwable t) {
                if (continueReg) {
                    logger.info(CMBStrings.get("cmb.registerError", mbean.getName()));
                }
                else {
                    throw new RuntimeException(t);
                }
            }
        }
    
public voidregisterMBeans(java.util.List mbeans)

        this.registerMBeans(mbeans, true);
    
public voidsetClassLoader(java.lang.ClassLoader cl)

        if (cl == null)
            throw new IllegalArgumentException(CMBStrings.get("InternalError", "setClassLoader() received a null argument"));
        this.cl = cl;
    
private voidunregisterIt(com.sun.enterprise.config.serverbeans.Mbean m, javax.management.ObjectName ron)

        //attempt to unregister the mbean, not being able to do so is not fatal
        // DO NOT ALLOW Exceptions out of here -- this method gets called from
        // within catch blocks -- which would be a tragic java problem!
        try {
            if (mbs.isRegistered(ron))
                mbs.unregisterMBean(ron);
        } catch (final Exception e) { 
            logger.warning(CMBStrings.get("cmb.unloadMBeanError", m.getName()));
        }