/**
 * 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_domain.lib.wrapper;

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

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

import org.objectweb.jonas_domain.api.DomainMap;
import org.objectweb.jonas_domain.api.DomainMapException;

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


/**
 *
 *
 * @author Adriana Danes
 */
public class DomainManagerWrapper {

    /**
     * DomainMapManager fully qualified classname
     */
    private static final String DOMAIN_MANAGER_CLASSNAME = "org.objectweb.jonas_domain.lib.DomainMapManager";

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

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

    /**
     * Wrap DomainMapManager.getDeploymentDesc(domainFileName, cl)
     *
     * @param domainFileName name of the file containing the domain map
     * @param cl Domain ClassLoader
     *
     * @return the DomainMap containing the map description
     *
     * @throws DomainMapException When DomainMap can't be created
     */
    public static DomainMap getDomainMap(String domainFileName, ClassLoader cl)
    throws DomainMapException {
        LoaderManager lm = LoaderManager.getInstance();
        DomainMap map = null;
        try {
            ClassLoader tools = lm.getToolsLoader();
            Class manager = tools.loadClass(DOMAIN_MANAGER_CLASSNAME);
            Method m = manager.getDeclaredMethod("getDomainMap", new Class[] {String.class, ClassLoader.class});
            map = (DomainMap) m.invoke(null, new Object[] {domainFileName, cl});
        } catch (InvocationTargetException ite) {
            Throwable t = ite.getTargetException();
            if (DomainMapException.class.isInstance(t)) {
                throw (DomainMapException) ite.getTargetException();
            } else {
                throw new DomainMapException("DomainMapManager.getDomainMap fails", t);
            }
        } catch (Exception e) {
            // TODO add i18n here
            throw new DomainMapException("Problems when using reflection on DomainMapManager", e);
        }

        return map;
    }

    /**
     * Wrap DomainMapManager.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(DOMAIN_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);
        }
    }

}
