FileDocCategorySizeDatePackage
InProcessMBeanElementChangeEventListenerImpl.javaAPI DocGlassfish v2 API11122Fri May 04 22:24:10 BST 2007com.sun.enterprise.admin.mbeans.custom

InProcessMBeanElementChangeEventListenerImpl

public class InProcessMBeanElementChangeEventListenerImpl extends Object implements com.sun.enterprise.admin.event.MBeanElementChangeEventListener

Fields Summary
private static final Logger
logger
private final MBeanServer
mbs
Constructors Summary
public InProcessMBeanElementChangeEventListenerImpl()

      
    
Methods Summary
public voidhandleCreate(com.sun.enterprise.admin.event.MBeanElementChangeEvent event)

        try {
            final ConfigContext rcc                 = event.getConfigContext();
            final ArrayList<ConfigAdd> additions    = event.getConfigChangeList();
            //the list should is supposed to contain ConfigAdd elements only. Hence the above assignment should be safe
            for (ConfigAdd added : additions) {
                final String xp = added.getXPath();
                if (xp != null) {
                    Object co = rcc.exactLookup(xp);
                    if (co instanceof ApplicationRef) {
                        co = ApplicationHelper.findApplication(rcc,
                                ((ApplicationRef)co).getRef());
                        if (co instanceof Mbean) {
                            Mbean mb = (Mbean)co;
                            if(mb.isEnabled())
                                register(mb);
                        }
                    }
                }
            }
        } catch (final Exception e) {
            throw new AdminEventListenerException(e);
        }
    
public voidhandleDelete(com.sun.enterprise.admin.event.MBeanElementChangeEvent event)

        try {
            final ConfigContext occ                     = event.getOldConfigContext();
            final ArrayList<ConfigDelete> deletions     = event.getConfigChangeList();
            //the list should is supposed to contain Delete elements only. Hence the above assignment should be safe
            for (ConfigDelete deleted : deletions) {
                final String xp = deleted.getXPath();
                if (xp != null) {
                    Object co = occ.exactLookup(xp);
                    if (co instanceof ApplicationRef) {
                        co = ApplicationHelper.findApplication(occ,
                                ((ApplicationRef)co).getRef());
                        if (co instanceof Mbean) {
                            final Mbean mbean = (Mbean)co;
                            unregister(mbean);
                            logger.info(CMBStrings.get("cmb.successfulDelete", mbean.getName()));
                        }
                    }
                }
            }
        } catch (final Exception e) {
            throw new AdminEventListenerException(e);
        }
    
private voidhandleUpdate(com.sun.enterprise.config.ConfigContext rcc, com.sun.enterprise.config.serverbeans.ApplicationRef ref)

        String mbeanName = ref.getRef();
        Mbean mbean = (Mbean)ApplicationHelper.findApplication(rcc, mbeanName);

        // note: register and unregister declares "throws Exception".  The cleanest
        // way to deal with that is to simply wrap "Exception" and throw it back out.
        // Otherwise I'd have to declare "throws Exception" on each method all the way back up the
        // call stack
        
        try {
            if(ref.isEnabled()) {
                register(mbean);
            }
            else {
                unregister(mbean);
            }
        }catch(Exception e) {
            throw new AdminEventListenerException(e);
        }
    
public voidhandleUpdate(com.sun.enterprise.admin.event.MBeanElementChangeEvent event)

            /* Note that none of the attributes of "mbean" element except "enabled" and "object-name" are modifiable dynamically. Only the properties in this element,
            that are actually the attributes of runtime mbean are modifiable dynamically. Also, note that there need
            not be any validation of the attribute changes in this listener, because that is supposed to happen at
            the configuration time. Hence a ConfigChange has to be only creation/deletion/update of a property element or the two attributes discussed above.
            Every such change should result in setting the corresponding attribute on the MBean that is already registered. */
        try {
            final ConfigContext rcc                 = event.getConfigContext();
            final ArrayList<ConfigChange> changes    = event.getConfigChangeList();
            for (ConfigChange change : changes) {
                handleUpdate(rcc, change);
            }
        } catch (final AdminEventListenerException ae) {
            throw ae;
        } catch (final Exception e) {
            throw new AdminEventListenerException(e);
        }
    
private voidhandleUpdate(com.sun.enterprise.config.ConfigContext rcc, com.sun.enterprise.config.ConfigChange change)

        final String xp = change.getXPath();

        if (xp == null) 
            return;

        logger.fine(CMBStrings.get("cmb.gotConfigChange", xp));

        final Object changedObject = rcc.exactLookup(xp);

        final String parentPath = xp.substring(0, xp.lastIndexOf("/"));
        Object parent = null;
        try {
            parent = rcc.exactLookup(parentPath);
        } catch(Exception e) {
            // ignore for now
        }
        
        if (changedObject instanceof Mbean) {
            handleUpdate((Mbean)changedObject);
        } else if (changedObject instanceof ElementProperty) {
            handleUpdate(parent, (ElementProperty)changedObject);
        } else if (changedObject instanceof ApplicationRef) {
            handleUpdate(rcc, (ApplicationRef)changedObject);
        }
        else {
            throw new AdminEventListenerException(CMBStrings.get("InternalError", "Can't handle this: ", changedObject.getClass()));
        }
    
private voidhandleUpdate(com.sun.enterprise.config.serverbeans.Mbean mbean)

        logger.info(CMBStrings.get("cmb.illegalHandleUpdate", mbean.getName()));
    
private voidhandleUpdate(java.lang.Object parent, com.sun.enterprise.config.serverbeans.ElementProperty ep)

        final Mbean mbean               = (Mbean) parent;
        if (! mbean.isEnabled()) {
            logger.info(CMBStrings.get("cmb.mbeanIsDisabled", mbean.getName()));
            return;
        }
        final ObjectName on             = new ObjectName(mbean.getObjectName());
        final MBeanAttributeSetter mas  = new MBeanAttributeSetter(mbs, on);
        mas.setIt(ep.getName(), ep.getValue());
    
protected voidregister(com.sun.enterprise.config.serverbeans.Mbean mbean)

        final ObjectName on = CustomMBeanRegistrationImpl.getCascadingAwareObjectName(mbean);
        if (mbs.isRegistered(on)) {
            logger.info(CMBStrings.get("cmb.unsuccessfulRegistration", on));
        }
        else {
            final CustomMBeanRegistration cmr = new CustomMBeanRegistrationImpl(mbs);
            cmr.registerMBean(mbean);
            logger.info(CMBStrings.get("cmb.successfulRegistration",  mbean.getName())); 
        }
    
protected voidunregister(com.sun.enterprise.config.serverbeans.Mbean mbean)

        // this is the best-case effort
//        final String ons    = mbean.getObjectName();
//        final ObjectName on = new ObjectName(ons);
        final ObjectName on = CustomMBeanRegistrationImpl.getCascadingAwareObjectName(mbean);
        if (mbs.isRegistered(on)) {
            mbs.unregisterMBean(on);
            logger.info(CMBStrings.get("cmb.successfulUnRegistration", on));
        }
        else {
            logger.info(CMBStrings.get("cmb.unsuccessfulUnRegistration", on));
        }