/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2004 Bull S.A.
 * Contact: jonas-team@ow2.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.ow2.jonas.generators.genic.wrapper;

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

import org.ow2.jonas.lib.bootstrap.LoaderManager;
import org.ow2.jonas.service.ServiceException;


/**
 * GenIC wrapper
 *
 * @author Camilleri Jerome : Initial developer
 */
public class GenicServiceWrapper {


    /**
     * GenIC fully qualified classname
     */
    private static final String GENIC_CLASSNAME = "org.ow2.jonas.generators.genic.GenIC";

    /**
     * Private empty constructor for utility class
     */
    private GenicServiceWrapper() { };

    /**
     * Wrapper around GenIC.main(String[]) method
     * @param args GenIC args
     * @throws ServiceException If GenIC fails or if Reflection errors occurs
     */
    public static void callGenic(final String[] args) throws ServiceException {
        LoaderManager lm = LoaderManager.getInstance();
        ClassLoader old = null;

        try {
            ClassLoader ext = lm.getExternalLoader();
            if (ext == null) {
                throw new ServiceException("cannot get tools classloader");
            }
            old = Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(ext);

            Class manager = ext.loadClass(GENIC_CLASSNAME);
            Method m = manager.getDeclaredMethod("main", new Class[] {String[].class});
            m.invoke(null, new Object[] {args});
        } catch (InvocationTargetException e) {
            if (e.getTargetException() instanceof Error) {
                throw (Error) e.getTargetException();
            } else if (e.getTargetException() instanceof ServiceException) {
                throw (ServiceException) e.getTargetException();
            } else {
                throw new ServiceException("Problems when invoking main method from GenIC", e.getTargetException());
            }
        } catch (Exception e) {
            throw new ServiceException("Problems when invoking main method from GenIC" , e);
        } finally {
            if (old != null) {
                Thread.currentThread().setContextClassLoader(old);
            }
        }
    }
}

