/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2006 Bull S.A.S.
 * 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_clusterd.lib.wrapper;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.objectweb.jonas_clusterd.api.ClusterDaemonConfiguration;
import org.objectweb.jonas_clusterd.api.ClusterDaemonConfigurationException;

import org.objectweb.jonas.common.Log;
import org.objectweb.jonas.server.LoaderManager;

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


/**
 *
 * Wrapper for accessing to the Digester tool
 * @author Benoit Pelletier
 */
public class ClusterDaemonConfigurationManagerWrapper {

    /**
     * ClusterDaemonManager fully qualified classname
     */
    private static final String CLUSTER_DAEMON_MANAGER_CLASSNAME = "org.objectweb.jonas_clusterd.lib.ClusterDaemonConfigurationManager";

    /**
     * logger
     */
    private static Logger logger = Log.getLogger(Log.JONAS_CLUSTER_DAEMON);

    /**
     * Empty private constructor for utility classes
     */
    private ClusterDaemonConfigurationManagerWrapper() { }

    /**
     * Wrap ClusterDaemonManager.getClusterDaemonConfiguration(fileName, cl)
     *
     * @param fileName name of the file containing the cluster daemon configuration
     * @param cl ClassLoader
     *
     * @return the cluster daemon configuration
     *
     * @throws ClusterDaemonConfigurationException When ClusterDaemonConfiguration can't be created
     */
    public static ClusterDaemonConfiguration getClusterDaemonConfiguration(String fileName, ClassLoader cl)
    throws ClusterDaemonConfigurationException {
        LoaderManager lm = LoaderManager.getInstance();
        ClusterDaemonConfiguration conf = null;
        try {
            ClassLoader tools = lm.getToolsLoader();
            Class manager = tools.loadClass(CLUSTER_DAEMON_MANAGER_CLASSNAME);
            Method m = manager.getDeclaredMethod("getClusterDaemonConfiguration", new Class[] {String.class, ClassLoader.class});
            conf = (ClusterDaemonConfiguration) m.invoke(null, new Object[] {fileName, cl});
        } catch (InvocationTargetException ite) {
            Throwable t = ite.getTargetException();
            if (ClusterDaemonConfigurationException.class.isInstance(t)) {
                throw (ClusterDaemonConfigurationException) ite.getTargetException();
            } else {
                throw new ClusterDaemonConfigurationException("ClusterDaemonManager.getClusterDaemonConfiguration fails", t);
            }
        } catch (Exception e) {
            // TODO add i18n here
            throw new ClusterDaemonConfigurationException("Problems when using reflection on ClusterDaemonManager", e);
        }

        return conf;
    }

    /**
     * Wrap ClusterDaemonConfigurationManager.setParsingWithValidation(b)
     *
     * @param b true/false
     */
    public static void setParsingWithValidation(boolean b) {
        LoaderManager lm = LoaderManager.getInstance();

        try {
            ClassLoader tools = lm.getToolsLoader();
            Class manager = tools.loadClass(CLUSTER_DAEMON_MANAGER_CLASSNAME);
            Method m = manager.getDeclaredMethod("setParsingWithValidation", new Class[] {boolean.class});
            m.invoke(null, new Object[] {new Boolean(b)});
        } catch (Exception e) {
            // Should never occurs
            logger.log(BasicLevel.ERROR, e);
        }
    }

}
