report modifications to use with javasci
[scilab.git] / scilab / modules / javasci / src / java / javasci / ClassPath.java
1 /*--------------------------------------------------------------------------*/
2 /*
3  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4  * Copyright (C) INRIA - Allan CORNET
5  * Copyright (C) 2008 - INRIA - Sylvestre LEDRU
6  * 
7  * This file must be used under the terms of the CeCILL.
8  * This source file is licensed as described in the file COPYING, which
9  * you should have received as part of this distribution.  The terms
10  * are also available at    
11  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
12  *
13  */
14
15 /*--------------------------------------------------------------------------*/
16 package javasci;
17 /*--------------------------------------------------------------------------*/ 
18 import java.lang.reflect.Method;
19 import java.lang.reflect.InvocationTargetException;
20 import java.io.File;
21 import java.io.IOException;
22 import java.net.URL;
23 import java.net.URLClassLoader;
24 import java.net.URI;
25 import java.util.Iterator;
26 import java.util.Vector;
27 /*--------------------------------------------------------------------------*/
28 /**
29  * ClassPath to overload java classpath.
30  */
31 public class ClassPath {
32
33         private static final Class[] parameters = new Class[]{URL.class};
34
35         private static Vector<URL> queued = new Vector<URL>();
36
37         /**
38          * add a filename to java classpath.
39          * @param s a filename
40          * @throws IOException if an error occurs
41          */
42         public static void addFile(final String s,int i) throws IOException {
43                 addFile(new File(s), i);
44         }
45         /*-----------------------------------------------------------------------*/
46         /**
47          * add a file to java classpath.
48          * @param  f a file
49          * @throws IOException if an error occurs
50          */
51         public static void addFile(final File f, int i) throws IOException {
52                 addURL(f.toURI().toURL(), i);
53         }
54         
55         /*-----------------------------------------------------------------------*/
56         /**
57          * Add a URL to classpath.
58          * @param u URL of the classes (jar or path)
59          * @param i the type of load: i=0 startup / i=1 background / i=2 onUse
60          */
61         public static void addURL(final URL u, int i) {
62
63                 final URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
64                 Class sysclass = URLClassLoader.class;
65
66                 try {
67
68                         final Method method = sysclass.getDeclaredMethod("addURL", parameters);
69                         method.setAccessible(true);
70                         switch (i) {
71                                 case 0: /* Load now */
72                                         method.invoke(sysloader , new Object[] { u });
73                                         break;
74                                 case 1: /* Load later (background) */
75                                         queued.add(u);
76                                         break;
77                         }
78
79                 } catch (NoSuchMethodException e) {
80                         System.err.println("Error: Cannot find the declared method: " + e.getLocalizedMessage());
81                 } catch (IllegalAccessException e) {
82                         System.err.println("Error: Illegal access: " + e.getLocalizedMessage());
83                 } catch (InvocationTargetException e) {
84                         System.err.println("Error: Could not invocate target: " + e.getLocalizedMessage());
85                 }
86
87         }
88         /*-----------------------------------------------------------------------*/
89         /**
90          * Get the classpath loaded 
91          * @return classpath The list of the classpath
92          */
93         public static String[] getClassPath() {
94         
95                 URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
96                 URL[] path = sysloader.getURLs();
97                 String[] paths = new String[path.length];
98                 for (int i = 0; i < path.length; i++) {
99                         paths[i] = path[i].getFile();
100                 }
101                 return paths;
102         }
103
104
105     /**
106      * Load all the classpath in dedicated threads in background
107      */
108         public static void loadBackGroundClassPath(){
109                 Thread backgroundLoader = new Thread() {
110                                 public void run() { 
111                                         try {
112
113                                                 Iterator<URL> urlIt = queued.iterator();
114
115                                                 while (urlIt.hasNext()) {
116                                                         ClassPath.addURL(urlIt.next(),0);
117                                                 }
118
119                                         }catch (Exception e){
120                                                 System.err.println("Error : "+e.getLocalizedMessage());
121                                         }
122                                 }
123                         };
124                 backgroundLoader.start();
125         }
126 }
127 /*--------------------------------------------------------------------------*/
128