Douglas Ferguson <[EMAIL PROTECTED]> writes:

> Sure, I'd love to see that script. 

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Lanceur implements Runnable {

        private static final Log LOG = LogFactory.getLog(Lanceur.class);
        
        /*
         * the command line arguments array
         */
        private String[] arguments;

        /*
         * the exit status
         * 
         */
        private int status = 0;

        /*
         * class loader constructed by this lanceur
         */
        private URLClassLoader loader;

        public static void main(String[] argv) {
                if(argv.length == 0)
                   System.exit(1); 
                /* first argument is real lanucher class */
                String real = argv[0];
                Class realC = Class.forName(real);
                /* shift les arguments */
                String[] nargs = new String[argv.length - 1];
                if (nargs.length != 0) {
                        System.arraycopy(arguments, 1, nargs, 0, nargs.length);
                }       
                Lanceur l = (Lanceur)realC.newInstance();
                l.setArguments(argv);
                l.run();
        }

        public void run() {
                /* extraction du repertoire */
                String libdir = System.getProperty("libdir","..\\lib");
                if (libdir == null) {
                        LOG.error("Definir le repertoire contenant les 
bibliotheques : -Dlibdir=<xxxxx>");
                        setStatus(1);
                        return;
                }
                /* verifications */
                File f = new File(libdir);
                if (!f.exists()) {
                        LOG.error("Le repertoire" + libdir + " n'existe pas");
                        setStatus(2);
                        return;
                }
                if (!f.isDirectory()) {
                        LOG.error(libdir + " n'est pas un repertoire");
                        setStatus(2);
                        return;
                }
                if (!f.canRead()) {
                        LOG.error("Le contenu de " + libdir + " ne peut etre 
lu");
                        setStatus(2);
                        return;
                }
                /* extraction du nom de la classe principale */
                String clname = System.getProperty("main","Main");
                if (clname == null)
                        clname = "Main";
                /* construction du classpath */
                File[] jars = f.listFiles(new FileFilter() {

                        public boolean accept(File pathname) {
                                return pathname.getName().endsWith(".jar")
                                                || 
pathname.getName().endsWith(".zip");
                        }

                });
                int ln = jars.length;
                URL[] urls = new URL[ln + 1];
                /* ajout du repertoire courant par defaut*/
                try {
                        urls[0] = new URL("file://.");
                } catch (MalformedURLException e) {
                        LOG.error("Impossible d'ajouter . au classpath : "
                                        + e.getLocalizedMessage());
                }
                for (int i = 0; i < ln; i++)
                        try {
                                handleJar(jars[i]);
                                urls[i] = new URL("file://" + 
jars[i].getPath());
                        } catch (MalformedURLException e) {
                                LOG.error("Impossible d'ajouter . au classpath 
: "
                                                + e.getLocalizedMessage());
                        }
                /* chargement */
                loader = new URLClassLoader(urls, 
ClassLoader.getSystemClassLoader());
                try {
                        Class cls = loader.loadClass(clname);
                        Method main = cls.getDeclaredMethod("main", new Class[] 
{ nargs
                                        .getClass() });
                        LOG.debug("Execution de "+clname +".main(");
                        for(int i=0;i<arguments.length;i++) {
                                LOG.debug(arguments[i]);
                                if(i < arguments.length -1)
                                        LOG.debug(','+"");
                        }
                        LOG.debug(")");
                        main.invoke(null, new Object[] { nargs });
                } catch (ClassNotFoundException e) {
                        LOG.error("Impossible de trouver la classe " + clname
                                        + " : " + e.getLocalizedMessage());
                        setStatus(3);
                } catch (SecurityException e) {
                        LOG.error("Erreur de securite pour charger la methode 
main dans "
                                                        + clname + " : " + 
e.getLocalizedMessage());
                        setStatus(3);
                } catch (NoSuchMethodException e) {
                        LOG.error("Impossible de trouver la methode main dans  "
                                        + clname + " : " + 
e.getLocalizedMessage());
                        setStatus(3);
                } catch (IllegalArgumentException e) {
                        LOG.error("Argument incorrecte a l'execution de " + 
clname
                                        + ".main() : " + 
e.getLocalizedMessage());
                        e.printStackTrace();
                        setStatus(3);
                } catch (IllegalAccessException e) {
                        LOG.error("Acces interdit a l'execution de " + clname
                                        + ".main() : " + 
e.getLocalizedMessage());
                        setStatus(3);
                } catch (InvocationTargetException e) {
                        setStatus(4);
                        e.printStackTrace();
                }
        }

        /**
         * This method may be used to do something with the
         * content of each loaded jar file before they are put into
         * the classpath.
         * 
         * @param file the File object representing a jar or zip file.
         */
        protected void handleJar(File file) {
                // NOP
        }

        public String[] getArguments() {
                return arguments;
        }

        public void setArguments(String[] arguments) {
                this.arguments = arguments;
        }

        /**
         * @return Returns the status.
         */
        public int getStatus() {
                return status;
        }

        /**
         * @param status The status to set.
         */
        public void setStatus(int status) {
                this.status = status;
        }

        /**
         * @return Returns the loader.
         */
        public URLClassLoader getLoader() {
                return loader;
        }

        /**
         * @param loader The loader to set.
         */
        public void setLoader(URLClassLoader loader) {
                this.loader = loader;
        }
}

-- 
OQube < software engineering \ génie logiciel >
Arnaud Bailly, Dr.
\web> http://www.oqube.com


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to