/**
 * 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
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.management;

import org.apache.commons.modeler.Registry;
import org.objectweb.jonas.server.Server;

// JOnAS Log
import org.objectweb.jonas.common.Log;

// Monolog
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.BasicLevel;

/**
 * Utility MBean classes.
 * @author Michel-Ange Anton
 */
public class JonasMBeanTools {

// ------------------------------------------------------------- Constants

    private final static String[] PACKAGE_DESCRIPTORS = {
        "org.objectweb.jonas.server", "org.objectweb.jonas.ear",
        "org.objectweb.jonas.container", "org.objectweb.jonas.naming",
        "org.objectweb.jonas.mail",
        "org.objectweb.jonas.resource", "org.objectweb.jonas.jtm",
        "org.objectweb.jonas.dbm", "org.objectweb.jonas.ws.mbean"};

// ------------------------------------------------------------- Privates variables

    private static Registry s_Registry = null;
    private static Logger s_Logger = Log.getLogger(Log.JONAS_MANAGEMENT_PREFIX);

// ------------------------------------------------------------- Public methods

    /**
     * Load the registry of managed object descriptions.
     * To load a new mbean-descriptor, add it in the <code>PACKAGE_DESCRIPTORS</code> array.
     * @return The registry where the descriptors are loaded
     */
    public synchronized static Registry getRegistry() {

        if (s_Registry == null) {
            // Load registry
            s_Registry = Registry.getRegistry(null, null);
            ClassLoader cl = Server.class.getClassLoader();

            // Load descriptors
            for (int i = 0; i < PACKAGE_DESCRIPTORS.length; i++) {
                s_Registry.loadDescriptors(PACKAGE_DESCRIPTORS[i], cl);
            }

            // Log loaded descriptors
            if (s_Logger.isLoggable(BasicLevel.DEBUG)) {
                String s;
                String[] as = s_Registry.findManagedBeans();
                s_Logger.log(BasicLevel.DEBUG, ">>> List of all MBeans descriptors");
                for (int i = 0; i < as.length; i++) {
                    s = ">>> " + i + ") " + as[i];
                    s_Logger.log(BasicLevel.DEBUG, s);
                }
                s_Logger.log(BasicLevel.DEBUG, "<<< List of all MBeans descriptors");
            }
        }
        return (s_Registry);
    }

}
