/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2005 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.Iterator;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

/**
 * A set of static classes used to build the names of proprietary MBeans used in Joram.
 * @author Adriana Danes
 */
public class JoramObjectName {

    static final String JORAM_DOMAIN = "Joram";
    static final String JORAM_CLIENT_DOMAIN = "joramClient";
    /**
     * The name of the JORAM rar used by JOnAS
     */
    static final String JORAM_RAR_NAME = "joram_for_jonas_ra";

    /**
     * Tests if a ResourceAdapterModule corresponding to the Joram RAR is deployed by testing the
     * corresponding MBean in the MBean server.
     */
    public static boolean isJoramRarDeployed(String domainName, String serverName) {
        ObjectName resAdaptersOn = J2eeObjectName.getResourceAdapterModules(domainName, serverName);
        Iterator it = JonasManagementRepr.queryNames(resAdaptersOn, serverName).iterator();
        while (it.hasNext()) {
            ObjectName aResAdapterOn = (ObjectName) it.next();
            String name = aResAdapterOn.getKeyProperty("name");
            if (name.equals(JORAM_RAR_NAME)) {
                return true;
            }
        }
        return false;
    }

    public static ObjectName joramClientMBeans() throws MalformedObjectNameException {
        return ObjectName.getInstance(JORAM_CLIENT_DOMAIN + ":*");
    }

    /**
     * @return ObjectName for the current JoramAdapter MBean
     * @exception MalformedObjectNameException Could not create ObjectName with the given String
     */
    public static ObjectName joramAdapter() throws MalformedObjectNameException {
        return ObjectName.getInstance(JORAM_CLIENT_DOMAIN + ":type=JoramAdapter");
    }

    /**
     * Create ObjectName for a Joram managed queue
     * @param name queue name
     * @return ObjectName for a Joram managed queue
     * @exception MalformedObjectNameException Could not create ObjectName with the given String
     */
    public static ObjectName joramQueue(String name) throws MalformedObjectNameException {
        return ObjectName.getInstance(JORAM_CLIENT_DOMAIN + ":type=queue,name=" + name);
    }

    /**
     * Create ObjectName for all Joram managed queues
     * @return ObjectName for a Joram managed queue
     * @exception MalformedObjectNameException Could not create ObjectName with the given String
     */
    public static ObjectName joramQueues() throws MalformedObjectNameException {
        return ObjectName.getInstance(JORAM_CLIENT_DOMAIN + ":type=queue,*");
    }

    /**
     * Create ObjectName for a Joram managed topic
     * @param name topic name
     * @return ObjectName for a Joram managed topic
     * @exception MalformedObjectNameException Could not create ObjectName with the given String
     */
    public static ObjectName joramTopic(String name) throws MalformedObjectNameException {
        return ObjectName.getInstance(JORAM_CLIENT_DOMAIN + ":type=topic,name=" + name);
    }

    /**
     * Create ObjectName for all Joram managed topics
     * @return ObjectName for a Joram managed topic
     * @exception MalformedObjectNameException Could not create ObjectName with the given String
     */
    public static ObjectName joramTopics() throws MalformedObjectNameException {
        return ObjectName.getInstance(JORAM_CLIENT_DOMAIN + ":type=topic,*");
    }

    /**
     * Create ObjectName for all Joram DMQs
     * @return ObjectName for a Joram DMQs
     * @exception MalformedObjectNameException Could not create ObjectName with the given String
     */
    public static ObjectName joramDMQs() throws MalformedObjectNameException {
        return ObjectName.getInstance(JORAM_CLIENT_DOMAIN + ":type=queue.dmq,*");
    }

    /**
     * Create ObjectName for a Joram DMQ
     * @param name DMQ name
     * @return ObjectName for a Joram DMQ
     * @exception MalformedObjectNameException Could not create ObjectName with the given String
     */
    public static ObjectName joramDMQ(String name) throws MalformedObjectNameException {
        return ObjectName.getInstance(JORAM_CLIENT_DOMAIN + ":type=queue.dmq,name=" + name);
    }
    /**
     * Create ObjectName for a Joram managed user
     * @param name user name
     * @return ObjectName for a Joram managed user
     * @exception MalformedObjectNameException Could not create ObjectName with the given String
     */
    public static ObjectName joramUser(String name) throws MalformedObjectNameException {
        return ObjectName.getInstance(JORAM_CLIENT_DOMAIN + ":type=User,name=" + name);
    }

    /**
     * Create ObjectName for all Joram managed users
     * @return ObjectName for a Joram managed user
     * @exception MalformedObjectNameException Could not create ObjectName with the given String
     */
    public static ObjectName joramUsers() throws MalformedObjectNameException {
        return ObjectName.getInstance(JORAM_CLIENT_DOMAIN + ":type=User,*");
    }

    /**
     * Unique point giving JORAM rar name to JOnAS management
     * @return name of the JORAM rar used by JOnAS
     */
    public static String getJORAM_RAR_NAME() {
        return JORAM_RAR_NAME;
    }

}
