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

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

import org.objectweb.jonas_ear.deployment.api.EarDeploymentDesc;
import org.objectweb.jonas_ear.deployment.api.EarDeploymentDescException;

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;


/**
 *
 *
 * @author Guillaume Sauthier
 */
public class EarManagerWrapper {

    /**
     * EarDeploymentDescManager fully qualified classname
     */
    private static final String EAR_MANAGER_CLASSNAME = "org.objectweb.jonas_ear.deployment.lib.EarDeploymentDescManager";

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

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

    /**
     * Wrap EarDeploymentDescManager.getDeploymentDesc(ear, cl)
     *
     * @param ear Ear filename
     * @param cl Application ClassLoader
     *
     * @return the EarDeploymentDesc of the ear filename
     *
     * @throws EarDeploymentDescException When Descriptor cannot be instanciated
     */
    public static EarDeploymentDesc getDeploymentDesc(String ear, ClassLoader cl)
    throws EarDeploymentDescException {
        LoaderManager lm = LoaderManager.getInstance();
        EarDeploymentDesc earDD = null;
        try {
            ClassLoader tools = lm.getToolsLoader();
            Class manager = tools.loadClass(EAR_MANAGER_CLASSNAME);
            Method m = manager.getDeclaredMethod("getDeploymentDesc", new Class[] {String.class, ClassLoader.class});
            earDD = (EarDeploymentDesc) m.invoke(null, new Object[] {ear, cl});
        } catch (InvocationTargetException ite) {
            Throwable t = ite.getTargetException();
            if (EarDeploymentDescException.class.isInstance(t)) {
                throw (EarDeploymentDescException) ite.getTargetException();
            } else {
                throw new EarDeploymentDescException("EarDeploymentDescManager.getDeploymentDesc fails", t);
            }
        } catch (Exception e) {
            // TODO add i18n here
            throw new EarDeploymentDescException("Problems when using reflection on EarDeploymentDescManager", e);
        }

        return earDD;
    }

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

}
