FileDocCategorySizeDatePackage
MBeanRegistry.javaAPI DocGlassfish v2 API42742Fri May 04 22:25:36 BST 2007com.sun.enterprise.admin.meta

MBeanRegistry

public class MBeanRegistry extends Object
Provides naming support for Mbeans

Fields Summary
private static final String
MSG_FINDREGISTRYENTRY_FAILED
private final MBeanServer
mMBeanServer
protected static final Logger
_sLogger
private volatile MBeanRegistryEntry[]
entries
private static final ObjectName
REGISTRATION_MBEAN_OBJECTNAME
private static final String
REGISTRATION_OPERATION_NAME
private static final String
UNREGISTRATION_OPERATION_NAME
public static final int
SORT_BY_NAME
public static final int
SORT_BY_XPATH
Constructors Summary
public MBeanRegistry()

    
      
        mMBeanServer    = FeatureAvailability.getInstance().getMBeanServer();
    
Methods Summary
private voidaddDottedName(java.lang.String dottedName, javax.management.ObjectName objectName)

        _sLogger.log(Level.FINE, "******regDottedName******* "+dottedName +" ObjectName="+objectName);
        mMBeanServer.invoke(REGISTRATION_MBEAN_OBJECTNAME, REGISTRATION_OPERATION_NAME, 
                       new Object[]{dottedName, objectName},
                       new String[]{dottedName.getClass().getName(), objectName.getClass().getName()});
    
public voidadoptConfigBeanAdd(com.sun.enterprise.config.ConfigBean bean, java.lang.String domainName)

        _sLogger.log(Level.FINEST, "****** MBeanRegistry.adoptConfigBeanAddDelete->add element:"+bean);
        instantiateAndRegisterConfigMBeans(bean, domainName);
        registerDottedName(bean, domainName); //this one is already recursive
        return;
   
public voidadoptConfigBeanDelete(com.sun.enterprise.config.ConfigBean bean, java.lang.String domainName)

        //delete only 
        // we need to unregister MBeans as well as unregister dotted name
        _sLogger.log(Level.FINEST, "****** MBeanRegistry.adoptConfigBeanAddDelete->delete element"+bean);
        ArrayList arr = new ArrayList();
        collectChildrenObjectNames(bean, domainName, arr);
        for(int i=0; i<arr.size(); i++)
        {
            ObjectName objectName = (ObjectName)arr.get(i);
            if(objectName!=null)
            {
                // first try to unregister dotted name
                try
                {
                    removeDottedName(objectName);
                }
                catch (Throwable t)
                {
                    //this is ok, some elements has no dotted name
                    _sLogger.fine("!!!!!!!!!!!!!! Can not unregister dotted name for MBean: "+objectName);
                }
                
                // now, try to unregister MBean
                try
                {
                    if( mMBeanServer.isRegistered(objectName))
                        mMBeanServer.unregisterMBean(objectName);
//                    else
//                        //just to unregister dotted name
//                        notifyUnregisterMBean(objectName, getConfigContext());
                }
                catch (Throwable t)
                {
                    //this is ok, some elements has no dotted name
                    _sLogger.fine("!!!!!!!!!!!!!! Can not unregister MBean: "+objectName);
                }
            }
        }
   
private voidcollectChildrenObjectNames(com.sun.enterprise.config.ConfigBean bean, java.lang.String domainName, java.util.ArrayList arr)

        try 
        {
            ConfigBean[] beans = bean.getAllChildBeans();
            if(beans!=null)
            {
                for(int i=0; i<beans.length; i++)
                {
                    collectChildrenObjectNames(beans[i], domainName, arr);
                }
            }
        }
        catch(Exception e)
        {
            //ignore exceptions (mabe there is no correspondent ObjectName
        } 
        try 
        {
            ObjectName name = getObjectNameForConfigBean(bean, domainName);
            if(name!=null)
            {
               arr.add(name);
               _sLogger.log(Level.FINEST, "******collectChildrenObjectNames.add-> ");
            }
        }
        catch(MalformedObjectNameException e)
        {
            String xpath = bean.getAbsoluteXPath("");
            _sLogger.log(Level.FINE, "Object name malformed for bean: "+ xpath, e);
        } 
   
public MBeanRegistryEntry[]findMBeanRegistryEntriesByDottedName(java.lang.String dottedName)

        Name name;
        try {
             name = new Name(dottedName);
        } catch (Exception e)
        {
            return null;
        }
        ArrayList list = new ArrayList();
        for(int i=0; i<entries.length; i++)
        {
            if(entries[i].getNamingDescriptor().isDottedPatternMatch(name))
                list.add(entries[i]);
        }
        if(list.size()==0)
            return null;
        return (MBeanRegistryEntry[])list.toArray(new MBeanRegistryEntry[]{});
    
public MBeanRegistryEntryfindMBeanRegistryEntry(javax.management.ObjectName objectName)

        try
        {
            Hashtable ht = objectName.getKeyPropertyList();
            ht.put(":",objectName.getDomain()); //add domain name pseudo pair
            
            for(int i=0; i<entries.length; i++)
            {
                if(entries[i]!=null && entries[i].isObjectNamePatternMatch(ht))
                    return entries[i];
            }
        }
        catch (Exception e)
        {
            _sLogger.log(Level.FINE, MSG_FINDREGISTRYENTRY_FAILED, e);
        }
        return null;
    
public MBeanRegistryEntryfindMBeanRegistryEntryByType(java.lang.String type)

        for(int i=0; i<entries.length; i++)
        {
            if(entries[i]!=null && entries[i].getName().equals(type))
                return entries[i];
        }
        return null;
    
public MBeanRegistryEntryfindMBeanRegistryEntryByXPath(java.lang.String xpath)

        if(xpath!=null)
            while(xpath.length()>1 && xpath.endsWith("/"))
                xpath = xpath.substring(0, xpath.length()-1);
        Object[] tokens = MBeanNamingDescriptor.getXPathTokens(xpath);
 
        for(int i=0; i<entries.length; i++)
        {
            if(entries[i].getNamingDescriptor().isXpathTokensMatch(tokens))
                return entries[i];
        }
        return null;
    
public MBeanRegistryEntryfindMBeanRegistryEntryByXPathPattern(java.lang.String patternPrefix)

        // bring prefix into accord of inner naming descriptor's pattern style (double apostrophies)
        patternPrefix = MBeanNamingDescriptor.XPATH_TO_MASK(patternPrefix);
        int len = patternPrefix.length();
        String pattern;
        //enum entries to find matching pattern
        for(int i=0; i<entries.length; i++)
        {
            pattern = entries[i].namingDescriptor.getXPathPattern();
            if( pattern!= null &&
                pattern.startsWith(patternPrefix) &&
                (pattern.length()==len || pattern.indexOf('/", len)<0 ) )
               return entries[i];
        }
        return null;
    
public voidgenerateAndRegisterAllDottedNames(com.sun.enterprise.config.ConfigContext ctx, java.lang.String domainName)

        ConfigBean bean = null;
        try
        {
            bean = (ConfigBean)ConfigBeansFactory.getConfigBeanByXPath(ctx, "/domain");
            registerDottedName(bean, domainName);
        }
        catch (Exception e)
        {
            throw new MBeanMetaException("DottedNamesRegistration: Could not get '/domain' ConfigMBean");
        }
    
public java.lang.StringgetConfigElementPrintName(java.lang.String xpath, boolean bIncludingKey, boolean bReplaceRefByParentElem)
returns element name description like "custom resource myResource"

       xpath = MBeanMetaHelper.cutAttributeTokenFromXPath(xpath.trim());
       MBeanRegistryEntry entry = findMBeanRegistryEntryByXPath(xpath);
       String printName;
       String elemName = MBeanMetaHelper.extractLastElemNameFromXPath(xpath);
       if(bReplaceRefByParentElem && elemName.endsWith("-ref"))
       {
          xpath = MBeanMetaHelper.cutLastElementFromXPath(xpath);
          elemName = MBeanMetaHelper.extractLastElemNameFromXPath(xpath);
       }
       if(entry==null ||
          (printName = entry.getElementPrintName())==null)
       {
           //generic name convertion to print name
           printName = elemName.replace('-", ' ");
       }
       String key = MBeanMetaHelper.getMultipleElementKeyValue(xpath);
       if(key!=null && bIncludingKey)
           printName = printName + " '" + key + "'";
       return printName;
   
private static javax.management.ObjectNamegetDottedRegMBeanName()

    //****************************************************************************************************
       
    
        try {
            return new ObjectName("com.sun.appserv:name=dotted-name-registry,type=dotted-name-support"); //CHANGE_ME
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    
private java.lang.StringgetFirstValidDottedName(com.sun.enterprise.admin.meta.naming.MBeanNamingInfo namingInfo)

        ArrayList arr = getValidDottedNames(namingInfo);
        if(arr!=null && arr.size()>0)
            return (String)arr.get(0);
        return null;
    
public com.sun.enterprise.admin.meta.naming.MBeanNamingInfogetMBeanNamingInfo(javax.management.ObjectName objectName)

        MBeanRegistryEntry entry = this.findMBeanRegistryEntry(objectName);
        return new MBeanNamingInfo(entry.getNamingDescriptor(), objectName);
    
public javax.management.ObjectNamegetMbeanObjectName(java.lang.String type, java.lang.String[] location)

        return getMbeanObjectName(findMBeanRegistryEntryByType(type), type, location);
    
private javax.management.ObjectNamegetMbeanObjectName(MBeanRegistryEntry entry, java.lang.String type, java.lang.String[] location)

 
        if(entry!=null)
        {
            try {
                MBeanNamingInfo namingInfo = new MBeanNamingInfo(entry.getNamingDescriptor(), type, location);
                if(namingInfo!=null)
                    return namingInfo.getObjectName();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return null;
    
public com.sun.enterprise.admin.meta.naming.MBeanNamingInfogetNamingInfoForConfigBean(com.sun.enterprise.config.ConfigBean bean, java.lang.String domainName)

        if(bean == null) 
            return null;
        try   
        {
            String xpath = bean.getAbsoluteXPath("");
            MBeanRegistryEntry entry = findMBeanRegistryEntryByXPath(xpath);
            MBeanNamingDescriptor descr = entry.getNamingDescriptor();
            String [] parms = descr.extractParmListFromXPath(xpath);
            if(parms!=null && parms.length>0 && parms[0]==null)
                parms[0] = domainName;
            return new MBeanNamingInfo(descr, descr.getType(), parms);
        }
        catch (Exception e)
        {
            // it is ok. not all config beans have entries in registry
            return null;
        }
   
public javax.management.ObjectNamegetObjectNameForConfigBean(com.sun.enterprise.config.ConfigBean bean, java.lang.String domainName)

        MBeanNamingInfo namingInfo = getNamingInfoForConfigBean(bean, domainName);
        if(namingInfo!=null)
            return namingInfo.getObjectName();
        return null;
   
public static java.util.ArrayListgetValidDottedNames(com.sun.enterprise.admin.meta.naming.MBeanNamingInfo namingInfo)

        return getValidDottedNames(namingInfo, null);
    
public static java.util.ArrayListgetValidDottedNames(com.sun.enterprise.admin.meta.naming.MBeanNamingInfo namingInfo, java.lang.String idPrefix)

        ArrayList arr = new ArrayList();
        try {
            String [] dottedNames = namingInfo.getDottedNames(idPrefix);
            for(int i=0; i<dottedNames.length; i++)    
            {
                if( dottedNames[i].length()>0 &&
                    !dottedNames[i].startsWith("@"))
                    arr.add(dottedNames[i]);
            }
        } catch (Exception e)
        {}
        return arr;
    
public voidinstantiateAndRegisterAllConfigMBeans(com.sun.enterprise.config.ConfigContext ctx, java.lang.String domainName)
This method can be used to register all config mbeans that are available in the system. It recursively descends the configbean tree and instantiates the mbean based on the descriptor file and registers the mbeans. If a configbean is null, the mbean is skipped and recursion is continued. Also, if the mbean is already created, then the InstanceAlreadyException is ignored (or, alternatively, the registration is not even attempted). No exception is thrown from the recursive method and is a best effort solution. The only exception thrown is when domain bean cannot be obtained.

param
ctx ConfigContext used for creating config mbeans
param
domainName domainName used for registering the mbeans
exception
MBeanMetaException. ConfigException is wrapped as MBeanMetaException when domain bean is not obtained and the ConfigException is also chained.
see
generateAndRegisterAllDottedNames

        ConfigBean bean = null;
        try
        {
            bean = (ConfigBean)ConfigBeansFactory.getConfigBeanByXPath(ctx, 
                                                ServerXPathHelper.XPATH_DOMAIN);
        }
        catch (ConfigException e)
        {
            _sLogger.log(Level.FINEST, 
                "Exception during Instantiating All Config MBeans in MBeanRegistry", e);
            throw new MBeanMetaException
                ("ConfigException in getting Domain bean from config context", e);
        }  
        instantiateAndRegisterConfigMBeans(bean, domainName);
        _sLogger.log(Level.FINEST, 
            "Instantiated All Config MBeans in MBeanRegistry Successfully");
    
private voidinstantiateAndRegisterConfigMBeans(com.sun.enterprise.config.ConfigBean bean, java.lang.String domainName)
A recursive method to register all mbeans from the bean specified Step 1, Get mbean for that bean. Step 2, Find all children for bean Step 3, call this method for each bean If bean is null, return silently. This method is designed to instantiate all possible mbeans without throwing any exceptions--a best effort solution.

        if(bean == null) 
            return;
        String xpath = bean.getAbsoluteXPath("");
        ConfigContext ctx = bean.getConfigContext();
        try   
        {
            ObjectName objectName = getObjectNameForConfigBean(bean, domainName);
            if(objectName!=null)
            {
                if (! mMBeanServer.isRegistered(objectName)) 
                {
//register here
                    BaseAdminMBean mbean = instantiateConfigMBean(objectName, null, ctx) ;
                    registerMBean(mbean, objectName);
                }
            }    
        }
        catch(Exception e)
        {
            if(xpath!=null && xpath.indexOf("/"+ServerTags.ELEMENT_PROPERTY+"[")==0)
            {
                _sLogger.log(Level.FINEST,"---- Exception for xpath="+xpath, e);            
            }
        } 
        try 
        {
            ConfigBean[] beans = bean.getAllChildBeans();
            if(beans!=null)
            {
                for(int i=0; i<beans.length; i++)
                {
                    if(beans[i]!=null && beans[i].getConfigContext()==null)
                    { //temporary patch for bug #6171788
                        beans[i].setConfigContext(ctx);
                    }

                    try {
                        instantiateAndRegisterConfigMBeans(beans[i], domainName);
                    } catch(Exception e1) {
                        //ignore
                         _sLogger.log(Level.FINEST,
                            "Exception in instantiateAndRegisterConfigMBeans:", e1); 
                    }
                }
            }
        }
        catch(Exception e)
        {
             _sLogger.log(Level.FINEST,
                        "Exception in instantiateAndRegisterConfigMBeans:", e); 
        } 
    
public com.sun.enterprise.admin.BaseAdminMBeaninstantiateConfigMBean(javax.management.ObjectName objectName, java.lang.Object managedResource)

        return instantiateConfigMBean( objectName, managedResource, null);
    
public com.sun.enterprise.admin.BaseAdminMBeaninstantiateConfigMBean(javax.management.ObjectName objectName, java.lang.Object managedResource, com.sun.enterprise.config.ConfigContext ctx)

        MBeanRegistryEntry entry = findMBeanRegistryEntry(objectName);
        if(entry!=null)
        {
            MBeanNamingDescriptor descr = entry.getNamingDescriptor();
            return entry.instantiateMBean(objectName, managedResource, ctx);
        }
        return null;
    
public com.sun.enterprise.admin.BaseAdminMBeaninstantiateMBean(java.lang.String type, java.lang.String[] location)

        return instantiateMBean( type, location, null, null);
    
public com.sun.enterprise.admin.BaseAdminMBeaninstantiateMBean(java.lang.String type, java.lang.String[] location, java.lang.Object managedResource, com.sun.enterprise.config.ConfigContext ctx)

        return instantiateMBean( type, location, managedResource, ctx, false);
    
public com.sun.enterprise.admin.BaseAdminMBeaninstantiateMBean(java.lang.String type, java.lang.String[] location, java.lang.Object managedResource, com.sun.enterprise.config.ConfigContext ctx, boolean bRegister)

        MBeanRegistryEntry entry = findMBeanRegistryEntryByType(type);
        BaseAdminMBean mbean = null;
        if(entry!=null)
        {
            mbean =  entry.instantiateMBean(type, location, managedResource, location[0], ctx);
            if(bRegister && mbean!=null)
            {
                ObjectName objectName = getMbeanObjectName(entry, type, location);
                if(objectName!=null)
                {
                    registerMBean(mbean, objectName);
                }
            }
        }
        return mbean;
    
public voidloadMBeanRegistry(java.io.InputStream stream)

        loadMBeanRegistry(stream, true);
    
public voidloadMBeanRegistry(java.io.InputStream stream, boolean bMergeWithMbean)

        MyRegistry registry = new MyRegistry();
        try {
            registry.loadDescriptors("MbeansDescriptorsDOMSource", stream, null);
        } catch (Exception e)
        {
            throw new MBeanMetaException(e.getMessage());
        }
        // naming init
        String[] names = registry.findManagedBeans();
        entries = new MBeanRegistryEntry[names.length]; //loose the prev ?
        for(int i=0; i<names.length; i++)
        {
            entries[i] = new MBeanRegistryEntry(new MyManagedBean(registry.findManagedBean(names[i])), bMergeWithMbean);
        }
//        MBeansNaming.initMBeanNaming();
        
    
public voidnotifyRegisterMBean(javax.management.ObjectName objectName, com.sun.enterprise.config.ConfigContext ctx)

        try {
            MBeanNamingInfo namingInfo = getMBeanNamingInfo(objectName);
            ArrayList arr = getValidDottedNames(namingInfo);
            if(arr!=null)
                for(int i=0; i<arr.size(); i++) //enumerate all dotted names aliases 
                    addDottedName((String)arr.get(i), namingInfo.getObjectName());
            //FIXME: only if no XPath -> notifying Lloyd's MBean
        } catch (Exception e)
        {}
    
public voidnotifyUnregisterMBean(javax.management.ObjectName objectName, com.sun.enterprise.config.ConfigContext ctx)

        try {
            MBeanNamingInfo namingInfo = getMBeanNamingInfo(objectName);
            ConfigBean bean = null;
            try
            {
                bean = (ConfigBean)ConfigBeansFactory.getConfigBeanByXPath(ctx, namingInfo.getXPath());
            }
            catch (Exception e)
            {}
            if(bean==null)
               removeDottedName(objectName);
        } catch (Exception e)
        {}
    
private voidregisterDottedName(com.sun.enterprise.config.ConfigBean bean, java.lang.String domainName)

        if(bean==null)
            return;
        String xpath = bean.getAbsoluteXPath("");
        try   
        {
            MBeanNamingInfo namingInfo = getNamingInfoForConfigBean(bean, domainName);
            ObjectName objectName = namingInfo.getObjectName();
            ArrayList arr = getValidDottedNames(namingInfo);
            if(arr!=null)
                for(int i=0; i<arr.size(); i++) //enum aliases
                {
                    // register dotted name/alias
                    addDottedName((String)arr.get(i), objectName);
                }
       }
        catch(Exception e)
        {
            if(xpath!=null && xpath.indexOf("/"+ServerTags.ELEMENT_PROPERTY+"[")==0)
            {
                _sLogger.log(Level.FINE,"---- Exception for xpath="+xpath, e);            
            }
        } 
        try 
        {
            ConfigBean[] beans = bean.getAllChildBeans();
            if(beans!=null)
            {
                for(int i=0; i<beans.length; i++)
                {
                    registerDottedName(beans[i], domainName);
                }
            }
        }
        catch(Exception e)
        {
        } 
    
private voidregisterMBean(com.sun.enterprise.admin.BaseAdminMBean mbean, javax.management.ObjectName name)
Note the check for isRegistered(). This is to prevent calling of preRegister in registerMBean. There can potentially be infinite loop in the case where preRegister() goes queryNames or queryMBeans which inturn will call registerMBeans We need to silently move on when InstanceAlreadyException is thrown anyway. This does not change functionality in that sense.

        if (!  mMBeanServer.isRegistered(name)) {
            mMBeanServer.registerMBean(mbean, name);
        } 
    
private voidremoveDottedName(javax.management.ObjectName objectName)

        _sLogger.log(Level.FINE, "******unregDottedName******* for ObjectName="+objectName);
        mMBeanServer.invoke(REGISTRATION_MBEAN_OBJECTNAME, UNREGISTRATION_OPERATION_NAME, 
                       new Object[]{objectName},
                       new String[]{objectName.getClass().getName()});
    
public voidsortRegistryEntries(int sortType)

        
    
        // veeery slooow
        TreeMap map  = new TreeMap();
        String emptyKey = " ";
        String  key;
        for(int i=0; i<entries.length; i++)
        {
            if(sortType==SORT_BY_XPATH)
                key = entries[i].getNamingDescriptor().getXPathPattern();
            else
                key = entries[i].getName();
            if(key==null || key.length()==0)
            {
                key = emptyKey;
                emptyKey = emptyKey + " ";
            }    
            map.put(key, entries[i]);
        }
        entries = (MBeanRegistryEntry[])map.values().toArray((Object[])(new MBeanRegistryEntry[0]));
    
public java.lang.StringtoFormatString()

        String str =  "MBeanRegistry("+entries.length+" entries):\n";
        for(int i=0; i<entries.length; i++)
        {
            str = str + entries[i].toFormatString();
        }
        return str;
    
public java.lang.StringtoString()

    
      
    
        String str =  "MBeanRegistry("+entries.length+" entries):\n";
        for(int i=0; i<entries.length; i++)
        {
            str = str + entries[i].toString();
        }
        return str;