FileDocCategorySizeDatePackage
ConfigMBeansNaming.javaAPI DocGlassfish v2 API13457Fri May 04 22:34:08 BST 2007com.sun.enterprise.admin.server.core.mbean.config.naming

ConfigMBeansNaming

public class ConfigMBeansNaming extends MBeansDescriptions
Provides naming support for ConfigMbeans

Fields Summary
static MBeanNamingDescriptor[]
m_mbeanDescr
public static final Logger
sLogger
private static final String
MSG_FINDNAMINDESCRIPTOR_FAILED
private static final String
MSG_MALFORMED_DOTTED_NAME
private static final String
MSG_EXCEPTION_DURING_LIST_NAMES
Constructors Summary
Methods Summary
public static java.lang.String[]findNameContinuation(java.lang.String instanceName, java.lang.String dottedName)

        HashSet hs = new HashSet();
        int wildDescrIndex = -1;
        Name name = null;
        int  nNameTokens = 0;

        //if there is no such instance - next statement will throw runtime exception
        InstanceEnvironment instanceEnvironment= new InstanceEnvironment(instanceName);
        
        //First: add "static" continuations
        try
        {
            name = new Name(dottedName);
            nNameTokens = name.getNumParts();
        }
        catch (Exception e)
        {
            sLogger.log(Level.FINE, MSG_MALFORMED_DOTTED_NAME, e);
            return new String[0];
        }

        for(int i=0; i<m_mbeanDescr.length; i++) //enumerate all descriptors
        {
            Object[][] tokens = m_mbeanDescr[i].getDottedTokens();
            if(tokens!=null)
            {
                for(int j=0; j<tokens.length; j++) //enum different dotted patterns presentations
                {
                    if(MBeanNamingDescriptor.isDottedPatternMatch(name, tokens[j], false) && tokens[j].length>nNameTokens)
                    {
                        //dotted pattern beginning matches to sample
                        if(!(tokens[j][nNameTokens] instanceof String)) //wildcard?
                        {
                            if(tokens[j].length==nNameTokens+1) //only if wildcard at the end; otherwise - ignore
                                wildDescrIndex = i;
                        }
                        else
                        {
                            hs.add(dottedName+"."+tokens[j][nNameTokens]);
                        }
                    }
                }
            }
        }
        //Now try to add childrens names
        String xpath = null;
        if(wildDescrIndex>=0)
        {
            try
            {
                ConfigMBeanNamingInfo info = new ConfigMBeanNamingInfo(dottedName + ".fake");
                xpath = info.getXPath();
            }
            catch (Exception e)
            {
               sLogger.log(Level.FINE, MSG_EXCEPTION_DURING_LIST_NAMES, e);
            }
        }
        if(xpath!=null)
        {
            String attributeName = null;
            String elementName= null;
            // seek for elemname[@attrname=]
            xpath = xpath.trim();
            if(xpath.length()>0 && xpath.endsWith("]"))
            {
                int i = xpath.lastIndexOf('@") + 1;
                int j = xpath.indexOf('=",i) ;
                if(i>0 && j>i)
                {
                    attributeName = xpath.substring(i,j).trim();
                    j = xpath.lastIndexOf('[");
                    if(j>0 && j<i)
                    {
                        xpath = xpath.substring(0,j);
                        j = xpath.lastIndexOf('/");
                        if(j>0 && j<xpath.length()-2)
                        {
                            elementName = xpath.substring(j+1).trim();
                            xpath = xpath.substring(0,j);
                        }
                    }
                }
                
            }
            
            if(attributeName!=null && elementName!=null) //is parsed successfully
            {
                //here we are to call ConfiBeans methods
                ConfigContext configContext;
                try
                {
                    String fileUrl  = instanceEnvironment.getConfigFilePath();
                    configContext   = ConfigFactory.createConfigContext(fileUrl);
                    ConfigBean bean = ConfigBeansFactory.getConfigBeanByXPath(configContext, xpath);
                    ConfigBean[] childs =  bean.getChildBeansByName(elementName);
                    for(int i=0; i<childs.length; i++)
                    {
                        String next = childs[i].getAttributeValue(attributeName);
                        if(next!=null)
                            hs.add(dottedName+"."+next);
                    }
                    
                }
                catch (ConfigException ce)
                {
                   sLogger.log(Level.FINE, MSG_EXCEPTION_DURING_LIST_NAMES, ce);
                }
                catch (NullPointerException npe) //ConfigBean returns this exception by many reasons
                {
                   sLogger.log(Level.FINE, MSG_EXCEPTION_DURING_LIST_NAMES, npe);
                }
            }
        }
        return (String[])hs.toArray(new String[hs.size()]);
    
static MBeanNamingDescriptorfindNamingDescriptor(java.lang.String dottedName)

        try
        {
            Name name = new Name(dottedName);
            for(int i=0; i<m_mbeanDescr.length; i++)
            {
                if(m_mbeanDescr[i].isDottedPatternMatch(name))
                    return m_mbeanDescr[i];
            }
        }
        catch (Exception e)
        {
            sLogger.log(Level.FINE, MSG_FINDNAMINDESCRIPTOR_FAILED, e);
        }
        return null;
    
static MBeanNamingDescriptorfindNamingDescriptor(javax.management.ObjectName objectName)

        try
        {
           Hashtable ht = objectName.getKeyPropertyList();
           for(int i=0; i<m_mbeanDescr.length; i++)
           {
               if(m_mbeanDescr[i].isObjectNamePatternMatch(ht))
                   return m_mbeanDescr[i];
           }
        }
        catch (Exception e)
        {
            sLogger.log(Level.FINE, MSG_FINDNAMINDESCRIPTOR_FAILED, e);
        }
        return null;
    
static MBeanNamingDescriptorfindNamingDescriptorByType(java.lang.String type)

    //**************************************************************************
       
    
        try
        {
            for(int i=0; i<m_mbeanDescr.length; i++)
            {
                if(type.equals(m_mbeanDescr[i].getType()))
                    return m_mbeanDescr[i];
            }
        }
        catch (Exception e)
        {
            sLogger.log(Level.FINE, MSG_FINDNAMINDESCRIPTOR_FAILED, e);
        }
        return null;
    
private static synchronized MBeanNamingDescriptor[]initConfigMBeanNaming()

       MBeanNamingDescriptor[] descrs = null;
       try
       {
               descrs = new MBeanNamingDescriptor[mbean_descriptions.length];
               for(int i=0; i<mbean_descriptions.length; i++)
               {
                   descrs[i] = new MBeanNamingDescriptor(mbean_descriptions[i]);
               }
       }
       catch (MBeanConfigException e)
       {
       }
       return descrs;
    
public static voidmain(java.lang.String[] args)

       testDottedName("myInstance.http-listener.myListener");
       testDottedName("myInstance.http-listener");
       testDottedName("myInstance.http-server");
       testDottedName("myInstance.http-server.mymy");
       testDottedName("myInstance.http-listeners.myListener");
       testDottedName("myInstance.http-serve");
       testDottedName("myInstance.http-server.http-listener.myListener");

        try
        {
           final String dmn = "com.sun.appserv";
            
           testObjectName( JMXUtil.newObjectName(dmn, "type=http-listener,instance-name=myServer,name=myListener"));
           testObjectName( JMXUtil.newObjectName(dmn, "type=http-listener,instance-name=myServer,name=myListener,chtoto=to"));
           testObjectName( JMXUtil.newObjectName(dmn, "type=http-service,instance-name=myServer,name=jhgv"));
           testObjectName( JMXUtil.newObjectName(dmn, "type=http-service,server-instances=myServer"));
           testObjectName( JMXUtil.newObjectName(dmn, "type=http-service,instance-name=myServer"));
           testObjectName( JMXUtil.newObjectName(dmn, "type=http-listener,name=myListener,instance-name=myServer"));
        }
        catch (Throwable e)
        {
            print(">>>>>>EXCEPTION: " + e);
            e.printStackTrace();
        }
    
private static voidprint(java.lang.String str)

        System.out.println(str);
    
private static voidtestDottedName(java.lang.String dottedName)

        try
        {
            print("\n\n\n>>>>>>test for dotted name: " + dottedName);
            ConfigMBeanNamingInfo mbi = new ConfigMBeanNamingInfo(dottedName);
            print("       ConfigMBeanNamingInfo =" + mbi);
            print("       ObjectName =" + mbi.getObjectName());
            print("       XPath      =" + mbi.getXPath());
        }
        catch (Throwable e)
        {
            print(">>>>>>EXCEPTION: " + e);
            //e.printStackTrace();
        }
            
    
private static voidtestObjectName(javax.management.ObjectName name)

        try
        {
            print("\n\n\n>>>>>>test for object name: " + name);
            ConfigMBeanNamingInfo mbi = new ConfigMBeanNamingInfo(name);
            print("       ConfigMBeanNamingInfo =" + mbi);
            print("       ObjectName =" + mbi.getObjectName());
            print("       XPath      =" + mbi.getXPath());
        }
        catch (Throwable e)
        {
            print(">>>>>>EXCEPTION: " + e);
            e.printStackTrace();
        }