report modifications to use with javasci
Allan Cornet [Sun, 17 Aug 2008 16:54:10 +0000 (16:54 +0000)]
scilab/modules/javasci/src/java/javasci/ClassPath.java

index 7fa399a..094a7e2 100644 (file)
@@ -1,6 +1,8 @@
+/*--------------------------------------------------------------------------*/
 /*
  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
- * Copyright (C) 2007 - INRIA - Allan CORNET
+ * Copyright (C) INRIA - Allan CORNET
+ * Copyright (C) 2008 - INRIA - Sylvestre LEDRU
  * 
  * This file must be used under the terms of the CeCILL.
  * This source file is licensed as described in the file COPYING, which
  */
 
 /*--------------------------------------------------------------------------*/
-/**
- * Loading classes at runtime.
- * @author Allan CORNET - INRIA 2007
- */
-/*--------------------------------------------------------------------------*/
 package javasci;
 /*--------------------------------------------------------------------------*/ 
 import java.lang.reflect.Method;
@@ -25,6 +22,8 @@ import java.io.IOException;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.net.URI;
+import java.util.Iterator;
+import java.util.Vector;
 /*--------------------------------------------------------------------------*/
 /**
  * ClassPath to overload java classpath.
@@ -33,62 +32,63 @@ public class ClassPath {
 
        private static final Class[] parameters = new Class[]{URL.class};
 
-       /**
-        * Constructor
-        */
-       protected ClassPath() {
-               /*  indicate that the requested operation is not supported */
-               throw new UnsupportedOperationException();              
-       }
+       private static Vector<URL> queued = new Vector<URL>();
 
        /**
         * add a filename to java classpath.
         * @param s a filename
         * @throws IOException if an error occurs
         */
-       public static void addFile(final String s) throws IOException {
-               File f = new File(s);
-               addFile(f);
+       public static void addFile(final String s,int i) throws IOException {
+               addFile(new File(s), i);
        }
-       /*--------------------------------------------------------------------------*/
+       /*-----------------------------------------------------------------------*/
        /**
         * add a file to java classpath.
         * @param  f a file
         * @throws IOException if an error occurs
         */
-       public static void addFile(final File f) throws IOException {
-       
-               URI uri = f.toURI();
-               addURL(uri.toURL());
+       public static void addFile(final File f, int i) throws IOException {
+               addURL(f.toURI().toURL(), i);
        }
-       /*--------------------------------------------------------------------------*/
+       
+       /*-----------------------------------------------------------------------*/
        /**
-        * add a URL to classpath.
-        * @param u URL
-        * @throws IOException if an error occurs 
+        * Add a URL to classpath.
+        * @param u URL of the classes (jar or path)
+        * @param i the type of load: i=0 startup / i=1 background / i=2 onUse
         */
-       public static void addURL(final URL u) throws IOException {
+       public static void addURL(final URL u, int i) {
 
-               URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
+               final URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
                Class sysclass = URLClassLoader.class;
 
                try {
-                       Method method = sysclass.getDeclaredMethod("addURL", parameters);
+
+                       final Method method = sysclass.getDeclaredMethod("addURL", parameters);
                        method.setAccessible(true);
-                       method.invoke(sysloader , new Object[] {u });
+                       switch (i) {
+                               case 0: /* Load now */
+                                       method.invoke(sysloader , new Object[] { u });
+                                       break;
+                               case 1: /* Load later (background) */
+                                       queued.add(u);
+                                       break;
+                       }
+
                } catch (NoSuchMethodException e) {
-                       throw new IOException("Error NoSuchMethodException, could not add URL to system classloader");
+                       System.err.println("Error: Cannot find the declared method: " + e.getLocalizedMessage());
                } catch (IllegalAccessException e) {
-                       throw new IOException("Error IllegalAccessException, could not add URL to system classloader");
+                       System.err.println("Error: Illegal access: " + e.getLocalizedMessage());
                } catch (InvocationTargetException e) {
-                       throw new IOException("Error InvocationTargetException, could not add URL to system classloader");
+                       System.err.println("Error: Could not invocate target: " + e.getLocalizedMessage());
                }
 
        }
-       /*--------------------------------------------------------------------------*/
+       /*-----------------------------------------------------------------------*/
        /**
-        * get the scilab classpath.
-        * @return classpath
+        * Get the classpath loaded 
+        * @return classpath The list of the classpath
         */
        public static String[] getClassPath() {
        
@@ -100,7 +100,29 @@ public class ClassPath {
                }
                return paths;
        }
-       /*--------------------------------------------------------------------------*/
+
+
+    /**
+     * Load all the classpath in dedicated threads in background
+     */
+       public static void loadBackGroundClassPath(){
+               Thread backgroundLoader = new Thread() {
+                               public void run() { 
+                                       try {
+
+                                               Iterator<URL> urlIt = queued.iterator();
+
+                                               while (urlIt.hasNext()) {
+                                                       ClassPath.addURL(urlIt.next(),0);
+                                               }
+
+                                       }catch (Exception e){
+                                               System.err.println("Error : "+e.getLocalizedMessage());
+                                       }
+                               }
+                       };
+               backgroundLoader.start();
+       }
 }
 /*--------------------------------------------------------------------------*/