add javalibrarypath function
[scilab.git] / scilab / modules / jvm / src / java / org / scilab / modules / jvm / LibraryPath.java
1 /*-----------------------------------------------------------------------------------*/\r
2 /**\r
3 * Modify java.library.path at runtime.\r
4 * @author Allan CORNET - INRIA 2007\r
5 */\r
6 /*-----------------------------------------------------------------------------------*/\r
7 package org.scilab.modules.jvm;\r
8 /*-----------------------------------------------------------------------------------*/\r
9 import java.io.IOException;\r
10 import java.io.File;\r
11 import java.lang.reflect.Field;\r
12 /*-----------------------------------------------------------------------------------*/\r
13 /*http://forum.java.sun.com/thread.jspa?threadID=135560&start=15&tstart=0 */\r
14 /*-----------------------------------------------------------------------------------*/\r
15 \r
16 /**\r
17  * LibraryPath to overload java.library.path.\r
18  */\r
19 public class LibraryPath {\r
20 \r
21         private static final String JAVALIBRARYPATH = "java.library.path";\r
22         /**\r
23          * Constructor\r
24          */\r
25         protected LibraryPath() {\r
26                 /*  indicate that the requested operation is not supported */\r
27                 throw new UnsupportedOperationException();              \r
28         }\r
29 /*--------------------------------------------------------------------------*/  \r
30         /**\r
31          * checks if pathToAdd already exists\r
32      * @param currentpaths list of current paths\r
33      * @param pathToAdd path to add\r
34      * @return a boolean true if path already exists\r
35      */\r
36         private static boolean pathAlreadyExists(String currentpaths, String pathToAdd) {\r
37     String[] paths = currentpaths.split("" + File.pathSeparatorChar);\r
38         for (String libraryPath : paths) {\r
39             if (libraryPath.equalsIgnoreCase(pathToAdd)) { return true; }\r
40         }\r
41         return false;\r
42     }\r
43 /*--------------------------------------------------------------------------*/\r
44         /**\r
45          * add a path to java.library.path \r
46          * @param p path to add\r
47          * @throws IOException return a exception\r
48          */\r
49         public static void addPath(final String p) throws IOException {\r
50         if (!pathAlreadyExists(System.getProperty(JAVALIBRARYPATH), p)) {\r
51                 String newLibPath = p + File.pathSeparator + System.getProperty(JAVALIBRARYPATH);\r
52                 System.setProperty(JAVALIBRARYPATH, newLibPath);\r
53                 try {\r
54                         Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");\r
55                         fieldSysPath.setAccessible(true);\r
56                         if (fieldSysPath != null) {\r
57                 fieldSysPath.set(System.class.getClassLoader(), null);\r
58                 }\r
59             } catch (NoSuchFieldException e) {\r
60                 throw new IOException("Error NoSuchFieldException, could not add path to " + JAVALIBRARYPATH);\r
61             } catch (IllegalAccessException e) {\r
62                 throw new IOException("Error IllegalAccessException, could not add path to " + JAVALIBRARYPATH);\r
63             }\r
64                 }\r
65   }\r
66  \r
67 /*--------------------------------------------------------------------------*/\r
68 /**\r
69  * get the scilab java.library.path.\r
70  * @return librarypath\r
71  */\r
72         public static String[] getLibraryPath() {\r
73                 String librarypath = System.getProperty(JAVALIBRARYPATH);\r
74                 String[] paths = librarypath.split("" + File.pathSeparatorChar);\r
75                 return paths;\r
76         }\r
77 }\r
78 /*-----------------------------------------------------------------------------------*/\r