/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *  2010 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS 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 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS 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 XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.xemelios.plugin.generatorPesPjs;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

/**
 *
 * @author cmarchand
 */
public class Starter {
    private static final Class[] PARAMETERS = new Class[] { URL.class };

    public static void main(String args[]) {
        try {
            File localDir = new File(System.getProperty("user.dir"));
            // chargement des jars pour reconstituer le classpath
            File[] jars = localDir.listFiles(new FileFilter() {
                public boolean accept(File pathname) {
                    return pathname.getName().toLowerCase().endsWith(".jar") && !"plugin-gen-pes-pjs.jar".equals(pathname.getName());
                }
            });
            for(File f:jars) addFile(f);

            localDir = localDir.getParentFile();
            jars = localDir.listFiles(new FileFilter() {
                public boolean accept(File pathname) {
                    return pathname.getName().toLowerCase().endsWith(".jar") && !"plugin-gen-pes-pjs.jar".equals(pathname.getName());
                }
            });
            for(File f:jars) addFile(f);

            localDir = new File(localDir,"lib");
            jars = localDir.listFiles(new FileFilter() {
                public boolean accept(File pathname) {
                    return pathname.getName().toLowerCase().endsWith(".jar") && !"plugin-gen-pes-pjs.jar".equals(pathname.getName());
                }
            });
            for(File f:jars) addFile(f);
        } catch(Exception ex) {
            ex.printStackTrace();
        }
        new GenPesPjs().run();
        System.exit(0);
    }

    public static void addFile(File f) throws IOException {
        addURL(f.toURL());
    }

    // bitonio trouvé sur le net
    public static void addURL(URL u) throws IOException {
        URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader();
        Class sysclass = URLClassLoader.class;

        try {
            Method method = sysclass.getDeclaredMethod("addURL", PARAMETERS);
            method.setAccessible(true); // voyou
            method.invoke(sysloader, new Object[] { u });
        } catch (Throwable t) {
            t.printStackTrace();
            throw new IOException("Error, could not add URL to system classloader");
        }

    }

}
