/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999-2004 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jmx;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;

/**
 * A set of static methods used to create object names for J2EE managed object.
 * This implementation is conformant to the JSR77.
 * @author Adriana Danes
 * @author Michel-Ange Anton
 */
public class J2eeObjectName {

    private static MBeanServerConnection localManagementRepr = null;

    /**
     * private constructor
     */
    private J2eeObjectName() {
    }

    // --------------------------------------------------------- Constants
    /**
     * Constant used in pattern ObjectNames
     */
    public static final String ALL = "*";

    /**
     * Constant used in ObjectNames of standalone modules
     */
    public static final String NONE = "none";

    //  --------------------------------------------------------- Public Methods
    /**
     * @param pObjectName Stringified ObjectName
     * @return ObjectName instance corresponding to the received argument
     */
    public static ObjectName getObjectName(String pObjectName) {
        try {
            return ObjectName.getInstance(pObjectName);
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    /**
     * Return the list of <code>ObjectName</code> gotten by the query in the
     * current MbeanServer.
     * @param pOn Query Mbeans to search
     * @return The list of <code>ObjectName</code>
     * @throws ManagementException
     */
    public static List queryObjectNames(ObjectName pOn) throws ManagementException {
        ArrayList al = new ArrayList();
        if (localManagementRepr == null) {
            localManagementRepr = ManagementReprFactory.getLocalManagementRepr();
            if (localManagementRepr == null) {
                return al;
            }
        }
        Iterator itNames = ((MBeanServer) localManagementRepr).queryNames(pOn, null).iterator();
        while (itNames.hasNext()) {
            al.add(itNames.next());
        }
        return al;
    }

    /**
     * Return the list of ObjectName's String gotten by the query in the current
     * MbeanServer.
     * @param pOn Query Mbeans to search
     * @return A list of <code>ObjectName</code> convert to String
     * @throws ManagementException
     */
    public static List queryStrings(ObjectName pOn) throws ManagementException {
        ArrayList al = new ArrayList();
        if (localManagementRepr == null) {
            localManagementRepr = ManagementReprFactory.getLocalManagementRepr();
            if (localManagementRepr == null) {
                return al;
            }
        }
        Iterator itNames = ((MBeanServer) localManagementRepr).queryNames(pOn, null).iterator();
        while (itNames.hasNext()) {
            ObjectName item = (ObjectName) itNames.next();
            al.add(item.toString());
        }
        Collections.sort(al);
        return al;
    }

    /**
     * @param pDomain The Domain name
     * @return ObjectName for a J2EEDomain MBean
     */
    public static ObjectName J2EEDomain(String pDomain) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=J2EEDomain");
            sb.append(",name=");
            sb.append(pDomain);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    /**
     * Return an ObjectName for a J2EEDomain MBean corresponding to a Cluster
     * directly attached to the domain (root cluster)
     * @param pDomain The domain name
     * @param pCluster The cluster name
     * @return ObjectName for a root cluster in the domain
     */
    public static ObjectName J2EECluster(String pDomain, String pCluster) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=J2EEDomain");
            sb.append(",name=");
            sb.append(pDomain);
            sb.append(",clusterName=");
            sb.append(pCluster);
            sb.append(",parentClusterName=");
            sb.append(pDomain);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    /**
     * Return an ObjectName for a J2EEDomain MBean corresponding to a Cluster
     * who's parent is not determined
     * @param pDomain The domain name
     * @param pCluster The cluster name
     * @return ObjectName for all the cluster having the given cluster name
     */
    public static ObjectName J2EEClusters(String pDomain, String pCluster) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=J2EEDomain");
            sb.append(",name=");
            sb.append(pDomain);
            sb.append(",clusterName=");
            sb.append(pCluster);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }
    /**
     * Return an ObjectName for a J2EEDomain MBean corresponding to a sub-cluster
     * attached to a parent-cluster
     * @param pDomain The domain name
     * @param pCluster The cluster name
     * @param pParentCluster The parent cluster name
     * @return
     */
    public static ObjectName J2EECluster(String pDomain, String pCluster, String pParentCluster) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=J2EEDomain");
            sb.append(",name=");
            sb.append(pDomain);
            sb.append(",clusterName=");
            sb.append(pCluster);
            sb.append(",parentClusterName=");
            sb.append(pParentCluster);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }
    public static ObjectName J2EEDomains() {
        try {
            StringBuffer sb = new StringBuffer(ALL);
            sb.append(":j2eeType=J2EEDomain");
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName J2EEServers() {
        try {
            StringBuffer sb = new StringBuffer(ALL);
            sb.append(":j2eeType=J2EEServer");
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName J2EEServers(String pDomain) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=J2EEServer");
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName J2EEServer(String pDomain, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=J2EEServer");
            sb.append(",name=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName JVM(String pDomain, String pServer, String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JVM");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName JVMs(String pDomain, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JVM");
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName J2EEApplications() {
        try {
            StringBuffer sb = new StringBuffer(ALL);
            sb.append(":j2eeType=J2EEApplication");
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName J2EEApplications(String pDomain) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=J2EEApplication");
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName J2EEApplications(String pDomain, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=J2EEApplication");
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName J2EEApplication(String pDomain, String pServer, String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=J2EEApplication");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }
    /**
     * Create generic name for all AppClientModules deployed in a given server, possibly belonging
     * to a given J2EE application
     * @param pDomain domain name
     * @param pServer server name
     * @param pApplication application name
     * @return generic name for the AppClientModules in a given server and application
     */
    public static ObjectName getAppClientModules(String pDomain, String pServer, String pApplication) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=AppClientModule");
            sb.append(",J2EEApplication=");
            if ((pApplication != null) && (pApplication.length() > 0)) {
                sb.append(pApplication);
            } else {
                sb.append(NONE);
            }
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }
    /**
     * Create ObjectName for an AppClientModule
     * @param pDomain domain name
     * @param pServer server name
     * @param pApplication J2ee application name
     * @param pName module name
     * @return ObjectName for an AppClientModule
     */
    public static ObjectName getAppClientModule(String pDomain, String pServer, String pApplication, String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=AppClientModule");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",J2EEApplication=");
            if ((pApplication != null) && (pApplication.length() > 0)) {
                sb.append(pApplication);
            } else {
                sb.append(NONE);
            }
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }
    public static ObjectName getEJBModules() {
        try {
            StringBuffer sb = new StringBuffer(ALL);
            sb.append(":j2eeType=EJBModule");
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getEJBModules(String pDomain) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=EJBModule");
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getEJBModules(String pDomain, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=EJBModule");
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getEJBModules(String pDomain, String pServer, String pApplication) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=EJBModule");
            sb.append(",J2EEApplication=");
            if ((pApplication != null) && (pApplication.length() > 0)) {
                sb.append(pApplication);
            } else {
                sb.append(NONE);
            }
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }
/**
 * Create ObjectName for an EJB Module
 * @param pDomain domain name
 * @param pServer server name
 * @param pApplication j2eeapplication name
 * @param pName module name - currently the container name
 * @return
 */
    public static ObjectName getEJBModule(String pDomain, String pServer, String pApplication, String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=EJBModule");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",J2EEApplication=");
            if ((pApplication != null) && (pApplication.length() > 0)) {
                sb.append(pApplication);
            } else {
                sb.append(NONE);
            }
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getEntityBean(String pDomain, String pModule, String pServer, String pApplication,
            String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=EntityBean");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",EJBModule=");
            sb.append(pModule);
            sb.append(",J2EEApplication=");
            if ((pApplication != null) && (pApplication.length() > 0)) {
                sb.append(pApplication);
            } else {
                sb.append(NONE);
            }
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getEntityBeans(String pDomain) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=EntityBean");
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getEntityBeans(String pDomain, String pModule) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=EntityBean");
            sb.append(",EJBModule=");
            sb.append(pModule);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getEntityBeans(String pDomain, String pModule, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=EntityBean");
            sb.append(",EJBModule=");
            sb.append(pModule);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getStatefulSessionBean(String pDomain, String pModule, String pServer,
            String pApplication, String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=StatefulSessionBean");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",EJBModule=");
            sb.append(pModule);
            sb.append(",J2EEApplication=");
            if ((pApplication != null) && (pApplication.length() > 0)) {
                sb.append(pApplication);
            } else {
                sb.append(NONE);
            }
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getStatefulSessionBeans(String pDomain) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=StatefulSessionBean");
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getStatefulSessionBeans(String pDomain, String pModule) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=StatefulSessionBean");
            sb.append(",EJBModule=");
            sb.append(pModule);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getStatefulSessionBeans(String pDomain, String pModule, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=StatefulSessionBean");
            sb.append(",EJBModule=");
            sb.append(pModule);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getStatelessSessionBean(String pDomain, String pModule, String pServer,
            String pApplication, String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=StatelessSessionBean");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",EJBModule=");
            sb.append(pModule);
            sb.append(",J2EEApplication=");
            if ((pApplication != null) && (pApplication.length() > 0)) {
                sb.append(pApplication);
            } else {
                sb.append(NONE);
            }
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getStatelessSessionBeans(String pDomain, String pModule) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=StatelessSessionBean");
            sb.append(",EJBModule=");
            sb.append(pModule);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getStatelessSessionBeans(String pDomain) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=StatelessSessionBean");
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
             // this should never occur
            return null;
        }
    }


    public static ObjectName getStatelessSessionBeans(String pDomain, String pModule, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=StatelessSessionBean");
            sb.append(",EJBModule=");
            sb.append(pModule);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getMessageDrivenBean(String pDomain, String pModule, String pServer,
            String pApplication, String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=MessageDrivenBean");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",EJBModule=");
            sb.append(pModule);
            sb.append(",J2EEApplication=");
            if ((pApplication != null) && (pApplication.length() > 0)) {
                sb.append(pApplication);
            } else {
                sb.append(NONE);
            }
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getMessageDrivenBeans(String pDomain) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=MessageDrivenBean");
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getMessageDrivenBeans(String pDomain, String pModule, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=MessageDrivenBean");
            sb.append(",EJBModule=");
            sb.append(pModule);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getMessageDrivenBeans(String pDomain, String pModule) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=MessageDrivenBean");
            sb.append(",EJBModule=");
            sb.append(pModule);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getWebServices(String pDomain, String pServer) {
        //TODO: Add a proper web service mbean instead.
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":type=WebService");
            sb.append(",");
            sb.append(ALL);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getWebServices() {
        try {
            StringBuffer sb = new StringBuffer(ALL);
            sb.append(":type=WebService");
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }


    public static ObjectName JavaMailResource(String pDomain, String pName, String pServer, String p_Type) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JavaMailResource");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",type=");
            sb.append(p_Type);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName JavaMailResources(String pDomain, String pServer, String p_Type) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JavaMailResource");
            sb.append(",");
            sb.append(ALL);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",type=");
            sb.append(p_Type);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName JNDIResources(String pDomain, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JNDIResource");
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getWebModules() {
        try {
            return new ObjectName("*:j2eeType=WebModule,*");
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getWebModules(String pDomain) {
        try {
            return new ObjectName(pDomain + ":j2eeType=WebModule,*");
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getWebModules(String pDomain, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=WebModule");
            if ((pServer != null) && (pServer.length() > 0)) {
                sb.append(",J2EEServer=");
                sb.append(pServer);
            }
            sb.append(",*");
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getWebModules(String pDomain, String pServer, String pApplication) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=WebModule");
            if ((pServer != null) && (pServer.length() > 0)) {
                sb.append(",J2EEServer=");
                sb.append(pServer);
            }
            if ((pApplication != null) && (pApplication.length() > 0)) {
                sb.append(",J2EEApplication=");
                sb.append(pApplication);
            }
            sb.append(",*");
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getWebModule(String pDomain, String pServer, String pApplication, String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=WebModule");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",J2EEApplication=");
            if ((pApplication != null) && (pApplication.length() > 0)) {
                sb.append(pApplication);
            } else {
                sb.append(NONE);
            }

            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getResourceAdapterModule(String pDomain, String pServer, String pApplication,
            String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=ResourceAdapterModule");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",J2EEApplication=");
            if ((pApplication == null) || (pApplication.length() == 0)) {
                sb.append(NONE);
            } else {
                sb.append(pApplication);
            }
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getResourceAdapterModules(String pDomain, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=ResourceAdapterModule");
            if ((pServer != null) && (pServer.length() > 0)) {
                sb.append(",J2EEServer=");
                sb.append(pServer);
            }
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getResourceAdapterModules(String pDomain, String pServer, String pApplication) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=ResourceAdapterModule");
            if ((pServer != null) && (pServer.length() > 0)) {
                sb.append(",J2EEServer=");
                sb.append(pServer);
            }
            if ((pApplication != null) && (pApplication.length() > 0)) {
                sb.append(",J2EEApplication=");
                sb.append(pApplication);
            }
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getResourceAdapter(String pDomain, String pResourceAdapterModule, String pApplication,
            String pServer, String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=ResourceAdapter");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",ResourceAdapterModule=");
            sb.append(pResourceAdapterModule);
            sb.append(",J2EEApplication=");
            if ((pApplication == null) || (pApplication.length() == 0)) {
                sb.append(NONE);
            } else {
                sb.append(pApplication);
            }
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getResourceAdapters(String pDomain, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=ResourceAdapter");
            if ((pServer != null) && (pServer.length() > 0)) {
                sb.append(",J2EEServer=");
                sb.append(pServer);
            }
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getResourceAdapters(String pDomain, String pApplication, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=ResourceAdapter");
            sb.append(",J2EEApplication=");
            if ((pApplication == null) || (pApplication.length() == 0)) {
                sb.append(NONE);
            } else {
                sb.append(pApplication);
            }
            if ((pServer != null) && (pServer.length() > 0)) {
                sb.append(",J2EEServer=");
                sb.append(pServer);
            }
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }
    public static ObjectName getJCAResource(String pDomain, String pServer, String pResourceAdapter, String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JCAResource");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",ResourceAdapter=");
            sb.append(pResourceAdapter);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getJCAActivationSpec(String pDomain, String pJCAResource, String pServer, String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JCAActivationSpec");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",JCAResource=");
            sb.append(pJCAResource);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getJCAAdminObject(String pDomain, String pJCAResource, String pServer, String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JCAAdminObject");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",JCAResource=");
            sb.append(pJCAResource);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getJCAConnectionFactory(String pDomain, String pJCAResource, String pServer,
            String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JCAConnectionFactory");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",JCAResource=");
            sb.append(pJCAResource);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }
    public static ObjectName getJCAConnectionFactories(String pDomain, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JCAConnectionFactory");
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }
    public static ObjectName getJCAManagedConnectionFactory(String pDomain, String pServer, String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JCAManagedConnectionFactory");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName ManagementListener(String pDomain, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":type=management,name=managementListener");
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName JTAResource(String pDomain, String pServer, String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JTAResource,name=JTAResource,J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    /**
     * Create ObjectName for a JDBCResource MBean
     * @param pDomain domain name
     * @param pServer server name
     * @param pName MBean name
     * @return ObjectName for a JDBCResource MBean
     */
    public static ObjectName JDBCResource(String pDomain, String pServer, String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JDBCResource");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    /**
     * Create ObjectName for a JDBCResource MBean using name="JDBCResource"
     * @param pDomain
     * @param pServer server name
     * @return ObjectName for a JDBCResource MBean
     */
    public static ObjectName JDBCResource(String pDomain, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JDBCResource");
            sb.append(",name=");
            sb.append("JDBCResource");
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    /**
     * Create ObjectName for a JDBCDataSource MBeans in a JDBCResource named "JDBCResource"
     * @param pDomain domain name
     * @param pServer server name
     * @param pName JDBCDataSource name
     * @return ObjectName for a JDBCDataSource MBean
     */
    public static ObjectName getJDBCDataSource(String pDomain, String pServer, String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JDBCDataSource");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",JDBCResource=");
            sb.append("JDBCResource");
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    /**
     * @param pDomain domain name
     * @param pServer server name
     * @return ObjectName for all JDBCDataSource MBeans in the JDBCResource of a given server
     */
    public static ObjectName getJDBCDataSources(String pDomain, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JDBCDataSource");
            sb.append(",JDBCResource=");
            sb.append("JDBCResource");
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    /**
     * This method is equivalent with the previous as in a server we currently have a
     * sole JDBCResource MBean.
     * @param pDomain domain name
     * @param pServer server name
     * @return ObjectName for all JDBCDataSource MBeans in a given server
     */
    public static ObjectName JDBCDataSources(String pDomain, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JDBCDataSource");
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName JDBCDataSources(String pDomain, String pJDBCResource, String pServer) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JDBCDataSource");
            sb.append(",JDBCResource=");
            sb.append(pJDBCResource);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            sb.append(",");
            sb.append(ALL);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

    public static ObjectName getJDBCDriver(String pDomain, String pServer, String pName) {
        try {
            StringBuffer sb = new StringBuffer(pDomain);
            sb.append(":j2eeType=JDBCDriver");
            sb.append(",name=");
            sb.append(pName);
            sb.append(",J2EEServer=");
            sb.append(pServer);
            return new ObjectName(sb.toString());
        } catch (javax.management.MalformedObjectNameException e) {
            // this should never occur
            return null;
        }
    }

}