/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 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
 *
 * Initial developer(s): ____________________________________.
 * Contributor(s): Bruno Michel and Guillaume Riviere
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jmx;

import java.util.Properties;
import javax.management.ObjectName;

/**
 * A set of static classes used to build the names of MBeans used in JOnAS
 * Each MBean or MBean type should have a method here.
 */
public class JonasObjectName {

    public static String DOMAIN = "jonas";
    
    public static ObjectName ejbServer() {
	try {
	    return new ObjectName(DOMAIN + ":type=service,name=ejbserver");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static ObjectName ejbService() {
	try {
	    return new ObjectName(DOMAIN + ":type=service,name=containers");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static ObjectName databaseService() {
	try {
	    return new ObjectName(DOMAIN + ":type=service,name=database");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static ObjectName resourceService() {
	try {
	    return new ObjectName(DOMAIN + ":type=service,name=resource");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static ObjectName transactionService() {
	try {
	    return new ObjectName(DOMAIN + ":type=service,name=transaction");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static ObjectName logService() {
	try {
	    return new ObjectName(DOMAIN + ":type=service,name=log");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static ObjectName jmsService() {
	try {
	    return new ObjectName(DOMAIN + ":type=service,name=jms");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static ObjectName securityService() {
	try {
	    return new ObjectName(DOMAIN + ":type=service,name=security");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static ObjectName jmxService() {
	try {
	    return new ObjectName("JMImplementation:type=MBeanServerDelegate");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static ObjectName container(String filename) 
	throws javax.management.MalformedObjectNameException 
    {
	return new ObjectName(DOMAIN + ":type=container,fname="+fileNameForObjectName(filename));
    }
    
    public static ObjectName ejb(String type, String filename, String name) 
	throws javax.management.MalformedObjectNameException 
    {
	return new ObjectName(DOMAIN + ":type="+type+",fname="+fileNameForObjectName(filename)+",name="+name);
    }

    public static ObjectName ejbbmp(String filename, String name) 
	throws javax.management.MalformedObjectNameException 
    {
	return new ObjectName(DOMAIN + ":type=ejbbmp,fname="+fileNameForObjectName(filename)+",name="+name);
    }

    public static ObjectName ejbcmp(String filename, String name) 
	throws javax.management.MalformedObjectNameException 
    {
	return new ObjectName(DOMAIN + ":type=ejbcmp,fname="+fileNameForObjectName(filename)+",name="+name);
    }

    public static ObjectName ejbsbf(String filename, String name) 
	throws javax.management.MalformedObjectNameException 
    {
	return new ObjectName(DOMAIN + ":type=ejbsbf,fname="+fileNameForObjectName(filename)+",name="+name);
    }

    public static ObjectName ejbsbl(String filename, String name) 
	throws javax.management.MalformedObjectNameException 
    {
	return new ObjectName(DOMAIN + ":type=ejbsbl,fname="+fileNameForObjectName(filename)+",name="+name);
    }

    public static ObjectName ejbmdb(String filename, String name) 
	throws javax.management.MalformedObjectNameException 
    {
	return new ObjectName(DOMAIN + ":type=ejbmdb,fname="+fileNameForObjectName(filename)+",name="+name);
    }

    public static ObjectName datasource(String name) 
	throws javax.management.MalformedObjectNameException 
    {
	return new ObjectName(DOMAIN + ":type=datasource,name="+name);
    }

    public static ObjectName resourceAdapter(String name) 
	throws javax.management.MalformedObjectNameException 
    {
	return new ObjectName(DOMAIN + ":type=resourceadapter,name="+name);
    }

    public static ObjectName rmiConnector() {
	try {
	    return new ObjectName(DOMAIN + ":type=connector,name=rmiconnector");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static ObjectName allContainers() {
	try {
	    return new ObjectName(DOMAIN + ":type=container,*");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static ObjectName allDatasources() {
	try {
	    return new ObjectName(DOMAIN + ":type=datasource,*");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static ObjectName allResourceAdaptors() {
	try {
	    return new ObjectName(DOMAIN + ":type=resourceadapter,*");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }


    public static ObjectName allEjbBmp(String fileName) {
	try {
	    return new ObjectName(DOMAIN + ":type=ejbbmp,fname="+fileNameForObjectName(fileName)+",*");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static ObjectName allEjbCmp(String fileName) {
	try {
	    return new ObjectName(DOMAIN + ":type=ejbcmp,fname="+fileNameForObjectName(fileName)+",*");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static ObjectName allEjbSbf(String fileName) {
	try {
	    return new ObjectName(DOMAIN + ":type=ejbsbf,fname="+fileNameForObjectName(fileName)+",*");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static ObjectName allEjbSbl(String fileName) {
	try {
	    return new ObjectName(DOMAIN + ":type=ejbsbl,fname="+fileNameForObjectName(fileName)+",*");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static ObjectName allEjbMdb(String fileName) {
	try {
	    return new ObjectName(DOMAIN + ":type=ejbmdb,fname="+fileNameForObjectName(fileName)+",*");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static ObjectName allInContainer (String fileName) {
	try {
	    return new ObjectName(DOMAIN + ":fname="+fileNameForObjectName(fileName)+",*");
	} catch (javax.management.MalformedObjectNameException e) {
	    // this should never occur
	    return null;
	}
    }

    public static String getType(ObjectName obj) {
	return obj.getKeyProperty("type");
    }

    public static String fileNameForObjectName(String fileName) {
	return fileName.replace(':','|');
    }

    public static ObjectName genericEjbObjectName(Properties p) throws javax.management.MalformedObjectNameException  {
	// build objectname for ejb
	String ejbtype = p.getProperty("type");
	String fname = p.getProperty("fname");
	String name = p.getProperty("name");
	ObjectName result = null;
	if ((ejbtype == null)||(fname == null)||(name == null)) {
	    throw new  javax.management.MalformedObjectNameException("ejb properties problem");
	}
	if (ejbtype.equals("ejbbmp")) {
	    result = ejbbmp(fname, name);
	}else if (ejbtype.equals("ejbcmp")) {
	    result = ejbcmp(fname, name);
	} else if (ejbtype.equals("ejbsbf")) {
	    result = ejbsbf(fname, name);
	} else if (ejbtype.equals("ejbsbl")) {
	    result = ejbsbl(fname, name);
	} else if (ejbtype.equals("ejbmdb")) {
	    result = ejbmdb(fname, name);
	} else {
	    throw new  javax.management.MalformedObjectNameException("ejb type properties unknow");
	}
	return result;
    }
}
