add javalibrarypath function
Allan Cornet [Sat, 9 Jun 2007 14:11:12 +0000 (14:11 +0000)]
to set and get java.library.path

add SCI/etc/librarypath.xml
bootloader for java.library.path (default)

27 files changed:
scilab/Makefile.am
scilab/etc/librarypath.xml [new file with mode: 0644]
scilab/modules/javasci/build.xml
scilab/modules/javasci/src/java/javasci/ClassPath.java
scilab/modules/javasci/src/java/javasci/LibraryPath.java [new file with mode: 0644]
scilab/modules/jvm/.checkstyle
scilab/modules/jvm/Makefile.am
scilab/modules/jvm/help/en_US/javalibrarypath.xml [new file with mode: 0644]
scilab/modules/jvm/includes/addToLibrarypath.h [new file with mode: 0644]
scilab/modules/jvm/includes/gw_jvm.h
scilab/modules/jvm/jvm.iss
scilab/modules/jvm/libjvm.vcproj
scilab/modules/jvm/sci_gateway/c/gw_jvm.c
scilab/modules/jvm/sci_gateway/c/sci_javalibrarypath.c [new file with mode: 0644]
scilab/modules/jvm/sci_gateway/jvm_gateway.xml
scilab/modules/jvm/src/c/InitializeJVM.c
scilab/modules/jvm/src/c/JVM_commons.h
scilab/modules/jvm/src/c/JVM_functions.h
scilab/modules/jvm/src/c/addToLibrarypath.c [new file with mode: 0644]
scilab/modules/jvm/src/c/getLibrarypath.c [new file with mode: 0644]
scilab/modules/jvm/src/c/getLibrarypath.h [new file with mode: 0644]
scilab/modules/jvm/src/c/loadClasspath.c
scilab/modules/jvm/src/c/loadLibrarypath.c [new file with mode: 0644]
scilab/modules/jvm/src/c/loadLibrarypath.h [new file with mode: 0644]
scilab/modules/jvm/src/java/jvm-JAVA.vcproj
scilab/modules/jvm/src/java/org/scilab/modules/jvm/ClassPath.java
scilab/modules/jvm/src/java/org/scilab/modules/jvm/LibraryPath.java [new file with mode: 0644]

index ce5a50f..44008cd 100644 (file)
@@ -28,6 +28,7 @@ root_DATA = $(top_srcdir)/etc/scilab.start \
 $(top_srcdir)/etc/scilab.quit \
 $(top_srcdir)/etc/modules.xml \
 $(top_srcdir)/etc/classpath.xml \
+$(top_srcdir)/etc/librarypath.xml \
 $(top_srcdir)/etc/toolboxes.xml
 
 all: macros
diff --git a/scilab/etc/librarypath.xml b/scilab/etc/librarypath.xml
new file mode 100644 (file)
index 0000000..c3a2f58
--- /dev/null
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>\r
+<!DOCTYPE librarypaths SYSTEM "../modules/jvm/xml/librarypath.dtd">\r
+<!-- =================== -->\r
+<!--\r
+ default java.library.path for Scilab\r
+ add path only if path exists\r
+ @author Allan CORNET\r
+ @date INRIA 2007\r
+ ===================\r
+ Don't touch if you do not know what you are doing\r
+-->\r
+<!-- =================== -->\r
+\r
+<librarypaths>\r
+<librarypath path="$SCILAB/bin"/>\r
+<librarypath path="$SCILAB/.libs"/>\r
+</librarypaths>
\ No newline at end of file
index 8a2fd82..32737c5 100644 (file)
@@ -28,6 +28,7 @@ INRIA 2007
                <attribute name="Scilab" value="javasci.Scilab"/>\r
                <attribute name="SciStringArray" value="javasci.SciStringArray"/>\r
                <attribute name="ClassPath" value="javasci.ClassPath"/>\r
+               <attribute name="LibraryPath" value="javasci.LibraryPath"/>\r
          </manifest>\r
        </jar>\r
   </target>\r
index 1b09286..7e03c30 100644 (file)
@@ -1,11 +1,13 @@
-/*--------------------------------------------------------------------------*/\r
+/*-----------------------------------------------------------------------------------*/\r
 /**\r
- * Loading classes at runtime.\r
- */\r
-/*--------------------------------------------------------------------------*/\r
-package javasci;\r
-/*--------------------------------------------------------------------------*/\r
+* Loading classes at runtime.\r
+* @author Allan CORNET - INRIA 2007\r
+*/\r
+/*-----------------------------------------------------------------------------------*/\r
+package org.scilab.modules.jvm;\r
+/*-----------------------------------------------------------------------------------*/ \r
 import java.lang.reflect.Method;\r
+import java.lang.reflect.InvocationTargetException;\r
 import java.io.File;\r
 import java.io.IOException;\r
 import java.net.URL;\r
@@ -20,6 +22,14 @@ public class ClassPath {
 private static final Class[] parameters = new Class[]{URL.class};\r
 \r
 /**\r
+ * Constructor\r
+ */\r
+protected ClassPath() {\r
+       /*  indicate that the requested operation is not supported */\r
+       throw new UnsupportedOperationException();              \r
+}\r
+\r
+/**\r
  * add a filename to java classpath.\r
  * @param s a filename\r
  * @throws IOException if an error occurs\r
@@ -54,9 +64,12 @@ try {
 Method method = sysclass.getDeclaredMethod("addURL", parameters);\r
 method.setAccessible(true);\r
 method.invoke(sysloader , new Object[] {u });\r
-} catch (Throwable t) {\r
-t.printStackTrace();\r
-throw new IOException("Error, could not add URL to system classloader");\r
+} catch (NoSuchMethodException e) {\r
+       throw new IOException("Error NoSuchMethodException, could not add URL to system classloader");\r
+} catch (IllegalAccessException e) {\r
+       throw new IOException("Error IllegalAccessException, could not add URL to system classloader");\r
+} catch (InvocationTargetException e) {\r
+       throw new IOException("Error InvocationTargetException, could not add URL to system classloader");\r
 }\r
 \r
 }\r
diff --git a/scilab/modules/javasci/src/java/javasci/LibraryPath.java b/scilab/modules/javasci/src/java/javasci/LibraryPath.java
new file mode 100644 (file)
index 0000000..253e877
--- /dev/null
@@ -0,0 +1,78 @@
+/*-----------------------------------------------------------------------------------*/\r
+/**\r
+* Modify java.library.path at runtime.\r
+* @author Allan CORNET - INRIA 2007\r
+*/\r
+/*-----------------------------------------------------------------------------------*/\r
+package org.scilab.modules.jvm;\r
+/*-----------------------------------------------------------------------------------*/\r
+import java.io.IOException;\r
+import java.io.File;\r
+import java.lang.reflect.Field;\r
+/*-----------------------------------------------------------------------------------*/\r
+/*http://forum.java.sun.com/thread.jspa?threadID=135560&start=15&tstart=0 */\r
+/*-----------------------------------------------------------------------------------*/\r
+\r
+/**\r
+ * LibraryPath to overload java.library.path.\r
+ */\r
+public class LibraryPath {\r
+\r
+       private static final String JAVALIBRARYPATH = "java.library.path";\r
+       /**\r
+        * Constructor\r
+        */\r
+       protected LibraryPath() {\r
+               /*  indicate that the requested operation is not supported */\r
+               throw new UnsupportedOperationException();              \r
+       }\r
+/*--------------------------------------------------------------------------*/ \r
+       /**\r
+        * checks if pathToAdd already exists\r
+     * @param currentpaths list of current paths\r
+     * @param pathToAdd path to add\r
+     * @return a boolean true if path already exists\r
+     */\r
+       private static boolean pathAlreadyExists(String currentpaths, String pathToAdd) {\r
+    String[] paths = currentpaths.split("" + File.pathSeparatorChar);\r
+        for (String libraryPath : paths) {\r
+            if (libraryPath.equalsIgnoreCase(pathToAdd)) { return true; }\r
+        }\r
+        return false;\r
+    }\r
+/*--------------------------------------------------------------------------*/\r
+       /**\r
+        * add a path to java.library.path \r
+        * @param p path to add\r
+        * @throws IOException return a exception\r
+        */\r
+       public static void addPath(final String p) throws IOException {\r
+       if (!pathAlreadyExists(System.getProperty(JAVALIBRARYPATH), p)) {\r
+               String newLibPath = p + File.pathSeparator + System.getProperty(JAVALIBRARYPATH);\r
+               System.setProperty(JAVALIBRARYPATH, newLibPath);\r
+               try {\r
+                       Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");\r
+                       fieldSysPath.setAccessible(true);\r
+                       if (fieldSysPath != null) {\r
+               fieldSysPath.set(System.class.getClassLoader(), null);\r
+               }\r
+           } catch (NoSuchFieldException e) {\r
+               throw new IOException("Error NoSuchFieldException, could not add path to " + JAVALIBRARYPATH);\r
+           } catch (IllegalAccessException e) {\r
+               throw new IOException("Error IllegalAccessException, could not add path to " + JAVALIBRARYPATH);\r
+           }\r
+               }\r
+  }\r
\r
+/*--------------------------------------------------------------------------*/\r
+/**\r
+ * get the scilab java.library.path.\r
+ * @return librarypath\r
+ */\r
+       public static String[] getLibraryPath() {\r
+               String librarypath = System.getProperty(JAVALIBRARYPATH);\r
+               String[] paths = librarypath.split("" + File.pathSeparatorChar);\r
+               return paths;\r
+       }\r
+}\r
+/*-----------------------------------------------------------------------------------*/\r
index ea6caf7..b891c58 100644 (file)
@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>\r
 <fileset-config file-format-version="1.2.0" simple-config="true">\r
-    <fileset name="tous" enabled="true" check-config-name="Scilab CheckStyle" local="false">\r
+    <local-check-config name="Scilab CheckStyle" location="internal_config__1181381231358.xml" type="internal" description=""/>\r
+    <fileset name="tous" enabled="true" check-config-name="Scilab CheckStyle" local="true">\r
         <file-match-pattern match-pattern="." include-pattern="true"/>\r
     </fileset>\r
 </fileset-config>\r
index c029097..9ce5bd6 100644 (file)
@@ -35,14 +35,17 @@ includes/getScilabJNIEnv.h \
 includes/InitializeJVM.h \
 includes/gw_jvm.h \
 includes/JniUtils.h \
-includes/addToClasspath.h
+includes/addToClasspath.h \
+includes/addToLibrarypath.h \
 
 if WITH_JAVA
 
 GATEWAY_C_SOURCES = sci_gateway/c/sci_system_getproperty.c \
 sci_gateway/c/sci_with_embedded_jre.c \
 sci_gateway/c/sci_javaclasspath.c \
-sci_gateway/c/gw_jvm.c
+sci_gateway/c/gw_jvm.c \
+sci_gateway/c/sci_javalibrarypath.c
+
 
 JVM_C_SOURCES = src/c/InitializeJVM.c \
 src/c/TerminateJVM.c \
@@ -55,7 +58,10 @@ src/c/system_getproperty.c \
 src/c/loadClasspath.c \
 src/c/createMainScilabObject.c \
 src/c/JVM_commons.c \
-src/c/catchIfJavaException.c
+src/c/catchIfJavaException.c \
+src/c/loadLibrarypath.c \
+src/c/getLibrarypath.c \
+src/c/addToLibrarypath.c
 
 USEANT=1
 
diff --git a/scilab/modules/jvm/help/en_US/javalibrarypath.xml b/scilab/modules/jvm/help/en_US/javalibrarypath.xml
new file mode 100644 (file)
index 0000000..c220935
--- /dev/null
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE MAN SYSTEM "../../../../modules/helptools/help.dtd">
+<MAN>
+  <LANGUAGE>eng</LANGUAGE>
+
+  <TITLE>javalibrarypath</TITLE>
+
+  <TYPE>Scilab Function</TYPE>
+
+  <DATE>$LastChangedDate: 2007-05-10 09:26:08 +0200$</DATE>
+
+  <SHORT_DESCRIPTION name="javalibrarypath">set and get dynamic java.library.path</SHORT_DESCRIPTION>
+
+  <CALLING_SEQUENCE>
+    <CALLING_SEQUENCE_ITEM>res=javalibrarypath()</CALLING_SEQUENCE_ITEM>
+    <CALLING_SEQUENCE_ITEM>javalibrarypath(path)</CALLING_SEQUENCE_ITEM>
+  </CALLING_SEQUENCE>
+
+  <PARAM>
+    <PARAM_INDENT>
+      <PARAM_ITEM>
+        <PARAM_NAME>res</PARAM_NAME>
+
+        <PARAM_DESCRIPTION>
+          <SP>: a string matrix</SP>
+        </PARAM_DESCRIPTION>
+      </PARAM_ITEM>
+    </PARAM_INDENT>
+  </PARAM>
+
+  <DESCRIPTION>
+    <P>set and get the dynamic Java Library path to one or more directory given in path.</P>
+    <P>When you use java classes with native methods, you need to define path where is dynamic library.</P>
+  </DESCRIPTION>
+
+  <EXAMPLE><![CDATA[res=javalibrarypath();
+javalibrarypath(SCI);
+javalibrarypath([SCI,SCI+'/.lib']);
+]]></EXAMPLE>
+
+  <SEE_ALSO>
+    <SEE_ALSO_ITEM>
+      <LINK>javaclasspath</LINK>
+    </SEE_ALSO_ITEM>
+  </SEE_ALSO>
+
+  <AUTHOR>A.C</AUTHOR>
+</MAN>
diff --git a/scilab/modules/jvm/includes/addToLibrarypath.h b/scilab/modules/jvm/includes/addToLibrarypath.h
new file mode 100644 (file)
index 0000000..99209e0
--- /dev/null
@@ -0,0 +1,18 @@
+/*-----------------------------------------------------------------------------------*/\r
+/* INRIA 2007 */\r
+/* Allan CORNET */\r
+/*-----------------------------------------------------------------------------------*/ \r
+#ifndef __ADDTOLIBRARYPATH_H__\r
+#define __ADDTOLIBRARYPATH_H__\r
+\r
+#include "machine.h" /* BOOL */\r
+\r
+/**\r
+* add a path to java.library.path\r
+* @param a new path to add\r
+* @return TRUE or FALSE\r
+*/\r
+BOOL addToLibrarypath(char *librarypathstring);\r
+\r
+#endif /* __ADDTOLIBRARYPATH_H__ */\r
+/*-----------------------------------------------------------------------------------*/ \r
index 4d68210..60b3670 100644 (file)
@@ -15,6 +15,7 @@ int C2F(gw_jvm)(void);
 int C2F(sci_with_embedded_jre) _PARAMS((char *fname,unsigned long fname_len));\r
 int C2F(sci_system_getproperty) _PARAMS((char *fname,unsigned long fname_len));\r
 int C2F(sci_javaclasspath) _PARAMS((char *fname,unsigned long fname_len));\r
+int C2F(sci_javalibrarypath) _PARAMS((char *fname,unsigned long fname_len));\r
 /*-----------------------------------------------------------------------------------*/\r
 #endif /*  __GW_JVM_H__ */\r
 \r
index d20e13f..9016d92 100644 (file)
@@ -13,6 +13,7 @@
 Source: bin\libjvm.dll; DestDir: {app}\bin; Components: {#COMPN_SCILAB}
 ;
 Source: etc\classpath.xml; DestDir: {app}\etc; Components: {#COMPN_SCILAB}
+Source: etc\librarypath.xml; DestDir: {app}\etc; Components: {#COMPN_SCILAB}
 ;
 Source: modules\{#JVM}\jar\org.scilab.modules.jvm.jar;DestDir: {app}\modules\{#JVM}\jar; Components: {#COMPN_SCILAB}
 ;
index 1eab6af..47e0671 100644 (file)
                                >\r
                        </File>\r
                        <File\r
+                               RelativePath=".\src\c\addToLibrarypath.c"\r
+                               >\r
+                       </File>\r
+                       <File\r
+                               RelativePath=".\src\c\catchIfJavaException.c"\r
+                               >\r
+                       </File>\r
+                       <File\r
                                RelativePath=".\src\c\createMainScilabObject.c"\r
                                >\r
                        </File>\r
                                >\r
                        </File>\r
                        <File\r
+                               RelativePath=".\src\c\getLibrarypath.c"\r
+                               >\r
+                       </File>\r
+                       <File\r
                                RelativePath=".\sci_gateway\c\gw_jvm.c"\r
                                >\r
                        </File>\r
                                >\r
                        </File>\r
                        <File\r
+                               RelativePath=".\src\c\loadLibrarypath.c"\r
+                               >\r
+                       </File>\r
+                       <File\r
                                RelativePath=".\sci_gateway\c\sci_javaclasspath.c"\r
                                >\r
                        </File>\r
                        <File\r
+                               RelativePath=".\sci_gateway\c\sci_javalibrarypath.c"\r
+                               >\r
+                       </File>\r
+                       <File\r
                                RelativePath=".\sci_gateway\c\sci_system_getproperty.c"\r
                                >\r
                        </File>\r
                                RelativePath=".\src\c\TerminateJVM.c"\r
                                >\r
                        </File>\r
-                       <File\r
-                               RelativePath=".\src\c\catchIfJavaException.c"\r
-                               >\r
-                       </File>\r
                </Filter>\r
                <Filter\r
                        Name="Header Files"\r
                                >\r
                        </File>\r
                        <File\r
+                               RelativePath=".\includes\addToLibrarypath.h"\r
+                               >\r
+                       </File>\r
+                       <File\r
+                               RelativePath=".\includes\catchIfJavaException.h"\r
+                               >\r
+                       </File>\r
+                       <File\r
                                RelativePath=".\src\c\createMainScilabObject.h"\r
                                >\r
                        </File>\r
                                >\r
                        </File>\r
                        <File\r
+                               RelativePath=".\src\c\getLibrarypath.h"\r
+                               >\r
+                       </File>\r
+                       <File\r
                                RelativePath=".\includes\getScilabJavaVM.h"\r
                                >\r
                        </File>\r
                                >\r
                        </File>\r
                        <File\r
-                               RelativePath=".\src\c\resource.h"\r
+                               RelativePath=".\src\c\loadLibrarypath.h"\r
                                >\r
                        </File>\r
                        <File\r
-                               RelativePath=".\src\c\system_getproperty.h"\r
+                               RelativePath=".\src\c\resource.h"\r
                                >\r
                        </File>\r
                        <File\r
-                               RelativePath=".\includes\TerminateJVM.h"\r
+                               RelativePath=".\src\c\system_getproperty.h"\r
                                >\r
                        </File>\r
                        <File\r
-                               RelativePath=".\includes\catchIfJavaException.h"\r
+                               RelativePath=".\includes\TerminateJVM.h"\r
                                >\r
                        </File>\r
                </Filter>\r
index 1ab940a..92acf66 100644 (file)
@@ -25,7 +25,8 @@ static jvmTable Tab[]=
 {\r
        {C2F(sci_with_embedded_jre),"with_embedded_jre"},\r
        {C2F(sci_system_getproperty),"system_getproperty"},\r
-       {C2F(sci_javaclasspath),"javaclasspath"}\r
+       {C2F(sci_javaclasspath),"javaclasspath"},\r
+       {C2F(sci_javalibrarypath),"javalibrarypath"}\r
 };\r
 /*-----------------------------------------------------------------------------------*/\r
 int C2F(gw_jvm)(void)\r
diff --git a/scilab/modules/jvm/sci_gateway/c/sci_javalibrarypath.c b/scilab/modules/jvm/sci_gateway/c/sci_javalibrarypath.c
new file mode 100644 (file)
index 0000000..a260558
--- /dev/null
@@ -0,0 +1,78 @@
+/*-----------------------------------------------------------------------------------*/\r
+/**\r
+ * @author Allan CORNET INRIA 2007\r
+ */\r
+/*-----------------------------------------------------------------------------------*/\r
+#include "gw_jvm.h"\r
+#include "MALLOC.h"\r
+#include "machine.h"\r
+#include "stack-c.h"\r
+#include "sciprint.h"\r
+#include "Scierror.h"\r
+#include "addToLibrarypath.h"\r
+#include "getLibrarypath.h"\r
+/*-----------------------------------------------------------------------------------*/\r
+int C2F(sci_javalibrarypath) _PARAMS((char *fname,unsigned long fname_len))\r
+{\r
+       Rhs = Max(Rhs,0);\r
+       CheckRhs(0,1);\r
+       CheckLhs(0,1);\r
+\r
+       if (Rhs == 0)\r
+       {\r
+               int nbRow=0;\r
+               int nbCol=1;\r
+               int i=0;\r
+               char **Strings=NULL;\r
+\r
+               Strings=getLibrarypath(&nbRow);\r
+               CreateVarFromPtr( Rhs+1, "S", &nbRow, &nbCol,Strings ) ;\r
+\r
+               LhsVar(1)=Rhs+1;\r
+               C2F(putlhsvar)();\r
+               if (Strings)\r
+               {\r
+                       for (i=0;i<nbRow;i++)\r
+                       {\r
+                               if (Strings[i])\r
+                               {\r
+                                       FREE(Strings[i]);\r
+                                       Strings[i]=NULL;\r
+                               }\r
+                       }\r
+                       FREE(Strings);\r
+                       Strings=NULL;\r
+               }\r
+\r
+       }\r
+       else\r
+       {\r
+               if ( GetType(1) == sci_strings )\r
+               {\r
+                       static int n1=0,m1=0;\r
+                       int i=0;\r
+                       BOOL bOK=FALSE;\r
+                       char **LIBRARYPATHS=NULL;\r
+\r
+                       GetRhsVar(1,"S",&m1,&n1,&LIBRARYPATHS);\r
+\r
+                       for (i = 0; i<m1*n1 ;i++)\r
+                       {\r
+                               bOK=addToLibrarypath(LIBRARYPATHS[i]);\r
+                               if (!bOK)\r
+                               {\r
+                                       Scierror(999,"could not add path to java.library.path : %s.\r\n",LIBRARYPATHS[i]);\r
+                                       return 0;\r
+                               }\r
+                       }\r
+                       LhsVar(1) = 0;\r
+                       C2F(putlhsvar)();       \r
+               }\r
+               else\r
+               {\r
+                       Scierror(999,"invalid parameter(s).\r\n");\r
+               }\r
+       }\r
+       return 0;\r
+}\r
+/*-----------------------------------------------------------------------------------*/\r
index bc2e93c..d86963e 100644 (file)
@@ -26,4 +26,5 @@
 <PRIMITIVE gatewayId="52" primitiveId="1" primitiveName="with_embedded_jre" />\r
 <PRIMITIVE gatewayId="52" primitiveId="2" primitiveName="system_getproperty" />\r
 <PRIMITIVE gatewayId="52" primitiveId="3" primitiveName="javaclasspath" />\r
+<PRIMITIVE gatewayId="52" primitiveId="4" primitiveName="javalibrarypath" />\r
 </GATEWAY>\r
index c6163a2..c6cb183 100644 (file)
@@ -7,11 +7,15 @@
 #include <string.h>
 #include "InitializeJVM.h"
 #include "loadClasspath.h"
+#include "loadLibrarypath.h"
 #include "setgetSCIpath.h"
 #include "MALLOC.h"
 #include "JVM.h"
 #include "createMainScilabObject.h"
 /*-----------------------------------------------------------------------------------*/ 
+static void DoLoadClasspathInEtc(char *SCIPATH);
+static void DoLoadLibrarypathInEtc(char *SCIPATH);
+/*-----------------------------------------------------------------------------------*/ 
 BOOL InitializeJVM(void)
 {
        BOOL bOK=FALSE;
@@ -30,14 +34,9 @@ BOOL InitializeJVM(void)
 #endif
        }
        else
-
        {
-               #define XMLCLASSPATH "%s/etc/classpath.xml"
-               char *classpathfile = NULL;
-               classpathfile = (char*)MALLOC(sizeof(char)*(strlen(SCIPATH)+strlen(XMLCLASSPATH)+1));
-               sprintf(classpathfile,XMLCLASSPATH,SCIPATH);
-               LoadClasspath(classpathfile);
-               if (classpathfile) {FREE(classpathfile); classpathfile = NULL;}
+               DoLoadLibrarypathInEtc(SCIPATH);
+               DoLoadClasspathInEtc(SCIPATH);
 
                bOK = createMainScilabObject();
 
@@ -58,4 +57,25 @@ BOOL InitializeJVM(void)
        return bOK;
 }
 /*-----------------------------------------------------------------------------------*/ 
+static void DoLoadClasspathInEtc(char *SCIPATH)
+{
+       #define XMLCLASSPATH "%s/etc/classpath.xml"
+       char *classpathfile = NULL;
+       classpathfile = (char*)MALLOC(sizeof(char)*(strlen(SCIPATH)+strlen(XMLCLASSPATH)+1));
+       sprintf(classpathfile,XMLCLASSPATH,SCIPATH);
+       LoadClasspath(classpathfile);
+       if (classpathfile) {FREE(classpathfile); classpathfile = NULL;}
+}
+/*-----------------------------------------------------------------------------------*/ 
+static void DoLoadLibrarypathInEtc(char *SCIPATH)
+{
+       #define XMLLIBRARYPATH "%s/etc/librarypath.xml"
+       char *librarypathfile = NULL;
+       librarypathfile = (char*)MALLOC(sizeof(char)*(strlen(SCIPATH)+strlen(XMLLIBRARYPATH)+1));
+       sprintf(librarypathfile,XMLLIBRARYPATH,SCIPATH);
+       LoadLibrarypath(librarypathfile);
+       if (librarypathfile) {FREE(librarypathfile); librarypathfile = NULL;}
+}
+/*-----------------------------------------------------------------------------------*/ 
+
 
index cd4e484..ca81e07 100644 (file)
@@ -7,6 +7,7 @@
 /*-----------------------------------------------------------------------------------*/ \r
 #include <jni.h>\r
 #include "machine.h"\r
+#include "JVM_functions.h"\r
 /*-----------------------------------------------------------------------------------*/ \r
 #ifdef _MSC_VER\r
 #define JRE_PATH "/java/jre"\r
@@ -25,7 +26,7 @@
 #define DEFAULT_SCILAB_CLASSPATH "/bin" /* SCI/bin */\r
 #define DEFAULT_SCILAB_LIBRARYPATH "/bin" /* SCI/bin */\r
 #else\r
-/* not really used , LD_LIBRARY_PATH is used in fact */\r
+/* not really used , java.library.path is used in fact */\r
 #define DEFAULT_SCILAB_CLASSPATH "/.libs/" /* SCI/.libs */\r
 #define DEFAULT_SCILAB_LIBRARYPATH "/.libs/" /* SCI/.libs */\r
 #endif\r
 #define USER_CLASSPATH "."\r
 #define JVM_CLASSPATH "/modules/jvm/jar/org.scilab.modules.jvm.jar"\r
 /*-----------------------------------------------------------------------------------*/ \r
-\r
-/**\r
- * @TODO : add comments...\r
- *\r
- * @param args  \r
- * @return <ReturnValue>\r
- */\r
-jint SciJNI_GetDefaultJavaVMInitArgs(void *args);\r
-\r
-/**\r
- * @TODO : add comments...\r
- *\r
- * @param pvm   \r
- * @param penv  \r
- * @param args  \r
- * @return <ReturnValue>\r
- */\r
-jint SciJNI_CreateJavaVM(JavaVM **pvm, void **penv, void *args);\r
-\r
-/**\r
- * @TODO : add comments...\r
- *\r
- * @param vmBuf \r
- * @param BufLen    \r
- * @param nVMs  \r
- * @return <ReturnValue>\r
- */\r
-jint SciJNI_GetCreatedJavaVMs(JavaVM **vmBuf, jsize BufLen, jsize *nVMs);\r
-\r
-/**\r
- * @TODO : add comments...\r
- *\r
- * @param filedynlib    \r
- * @return <ReturnValue>\r
- */\r
-BOOL LoadFuntionsJVM(char *filedynlib);\r
-\r
-\r
-/**\r
- * @TODO : add comments...\r
- *\r
- * @return <ReturnValue>\r
- */\r
-BOOL FreeDynLibJVM(void);\r
-\r
 #endif /*  __JVM_COMMONS_H__ */\r
 /*-----------------------------------------------------------------------------------*/ \r
index 9b0b38e..cdaf5e4 100644 (file)
@@ -16,28 +16,38 @@ BOOL LoadDynLibJVM(char *SCILAB_PATH);
 \r
 /**\r
 * Free JVM dynamic library\r
-* @return TRUE or FALSE\r
+* @return <ReturnValue> TRUE or FALSE\r
 */\r
 BOOL FreeDynLibJVM(void);\r
 \r
 /**\r
 * wrapping JNI_CreateJavaVM\r
+* @param pvm   \r
+* @param penv  \r
+* @param args  \r
+* @return <ReturnValue>\r
 */\r
 jint SciJNI_CreateJavaVM(JavaVM **pvm, void **penv, void *args);\r
 \r
 /**\r
 * wrapping JNI_GetCreatedJavaVMs\r
+* @param vmBuf \r
+* @param BufLen    \r
+* @param nVMs  \r
+* @return <ReturnValue>\r
 */\r
 jint SciJNI_GetCreatedJavaVMs(JavaVM **vmBuf, jsize BufLen, jsize *nVMs);\r
 \r
 /**\r
 * wrapping JNI_GetDefaultJavaVMInitArgs\r
+* @param args  \r
+* @return <ReturnValue>\r
 */\r
 jint SciJNI_GetDefaultJavaVMInitArgs(void *args);\r
 \r
 /**\r
 * Check if Scilab uses embedded JRE\r
-* @return TRUE or FALSE\r
+* @return <ReturnValue> TRUE or FALSE\r
 */\r
 BOOL withEmbeddedJRE(void);\r
 \r
diff --git a/scilab/modules/jvm/src/c/addToLibrarypath.c b/scilab/modules/jvm/src/c/addToLibrarypath.c
new file mode 100644 (file)
index 0000000..a607790
--- /dev/null
@@ -0,0 +1,52 @@
+/*-----------------------------------------------------------------------------------*/\r
+/* INRIA 2007 */\r
+/* Allan CORNET */\r
+/*-----------------------------------------------------------------------------------*/ \r
+#include <jni.h>\r
+#include "addToLibrarypath.h"\r
+#include "getScilabJNIEnv.h"\r
+#include "../../io/includes/directories.h"\r
+#include "fromjava.h"\r
+/*-----------------------------------------------------------------------------------*/ \r
+BOOL addToLibrarypath(char *librarypathstring)\r
+{\r
+       BOOL bOK=FALSE;\r
+\r
+       if (librarypathstring)\r
+       {\r
+               if ( isdir(librarypathstring) )\r
+               {\r
+                       JNIEnv * currentENV = getScilabJNIEnv();\r
+                       if (currentENV)\r
+                       {\r
+                               jclass cls=NULL;\r
+                               if (IsFromJava())\r
+                               {\r
+                                       /* Boot loader for scilab and javasci */\r
+                                       /* if scilab is called from java (javasci), we need to update standard java.library.path */\r
+                                       /* doesn't require to add -cp SCI/jar/modules/jvm.jar when you use javasci */\r
+                                       cls = (*currentENV)->FindClass(currentENV, "javasci/LibraryPath");\r
+                               }\r
+                               else\r
+                               {\r
+                                       cls = (*currentENV)->FindClass(currentENV, "org/scilab/modules/jvm/LibraryPath");\r
+                               }\r
+                               \r
+                               if (cls)\r
+                               {\r
+                                       jmethodID mid=NULL;\r
+                                       mid = (*currentENV)->GetStaticMethodID(currentENV, cls, "addPath","(Ljava/lang/String;)V");\r
+                                       if (mid)\r
+                                       {\r
+                                               jstring jstr;\r
+                                               jstr = (*currentENV)->NewStringUTF(currentENV,librarypathstring);\r
+                                               (*currentENV)->CallStaticObjectMethod(currentENV,cls, mid,jstr);\r
+                                               bOK=TRUE;\r
+                                       }\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+       return bOK;\r
+}\r
+/*-----------------------------------------------------------------------------------*/ \r
diff --git a/scilab/modules/jvm/src/c/getLibrarypath.c b/scilab/modules/jvm/src/c/getLibrarypath.c
new file mode 100644 (file)
index 0000000..c0724a6
--- /dev/null
@@ -0,0 +1,65 @@
+/*-----------------------------------------------------------------------------------*/\r
+/* INRIA 2007 */\r
+/* Allan CORNET */\r
+/*-----------------------------------------------------------------------------------*/ \r
+#include <jni.h>\r
+#include <string.h>\r
+#include "getLibrarypath.h"\r
+#include "getScilabJNIEnv.h"\r
+#include "fromjava.h"\r
+#include "MALLOC.h"\r
+/*-----------------------------------------------------------------------------------*/ \r
+char **getLibrarypath(int *sizeLibrarypathArray)\r
+{\r
+       char **LibrarypathArray=NULL;\r
+       JNIEnv * currentENV = getScilabJNIEnv();\r
+       if (currentENV)\r
+       {\r
+               jclass cls=NULL;\r
+               if (IsFromJava())\r
+               {\r
+                       /* Boot loader for scilab and javasci */\r
+                       /* if scilab is called from java (javasci), we need to update standard java.library.path */\r
+                       /* doesn't require to add -cp SCI/modules/jvm/jar/org.scilab.modules.jvm.jar when you use javasci */\r
+                       cls = (*currentENV)->FindClass(currentENV, "javasci/LibraryPath");\r
+               }\r
+               else\r
+               {\r
+                       cls = (*currentENV)->FindClass(currentENV, "org/scilab/modules/jvm/LibraryPath");\r
+               }\r
+\r
+               if (cls)\r
+               {\r
+                       jmethodID mid=NULL;\r
+                       mid = (*currentENV)->GetStaticMethodID(currentENV, cls, "getLibraryPath","()[Ljava/lang/String;");\r
+                       if (mid)\r
+                       {\r
+                               jobjectArray jStrings;\r
+                               \r
+                               jStrings=(*currentENV)->CallStaticObjectMethod(currentENV,cls, mid,NULL);\r
+                               *sizeLibrarypathArray=(*currentENV)->GetArrayLength(currentENV,jStrings);\r
+                               if (*sizeLibrarypathArray>0)\r
+                               {\r
+                                       int i=0;\r
+\r
+                                       LibrarypathArray=(char**)MALLOC(sizeof(char*)*(*sizeLibrarypathArray));\r
+                                       for (i=0;i<*sizeLibrarypathArray;i++)\r
+                                       {\r
+                                               jstring jelement;\r
+                                               const char *str = NULL;\r
+                                               char *string = NULL;\r
+\r
+                                               jelement = (jstring)(*currentENV)->GetObjectArrayElement(currentENV, jStrings, i);\r
+                                               str = (*currentENV)->GetStringUTFChars(currentENV,jelement, 0);\r
+                                               string = (char*)MALLOC(sizeof(char)*(strlen(str)+1));\r
+                                               strcpy(string,str);\r
+                                               LibrarypathArray[i]=string;\r
+                                               (*currentENV)->ReleaseStringUTFChars(currentENV, jelement, str);\r
+                                       }\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+       return LibrarypathArray;\r
+}\r
+/*-----------------------------------------------------------------------------------*/ \r
diff --git a/scilab/modules/jvm/src/c/getLibrarypath.h b/scilab/modules/jvm/src/c/getLibrarypath.h
new file mode 100644 (file)
index 0000000..e6a5fdc
--- /dev/null
@@ -0,0 +1,16 @@
+/*-----------------------------------------------------------------------------------*/\r
+/* INRIA 2007 */\r
+/* Allan CORNET */\r
+/*-----------------------------------------------------------------------------------*/ \r
+#ifndef __GETLIBRARYPATH_H__\r
+#define __GETLIBRARYPATH_H__\r
+\r
+/**\r
+* get java.library.path\r
+* @param return size of Librarypath Array\r
+* @return char ** Classpath Array\r
+*/\r
+char **getLibrarypath(int *sizeLibrarypathArray);\r
+\r
+#endif /* __GETLIBRARYPATH_H__ */\r
+/*-----------------------------------------------------------------------------------*/ \r
index 010c25a..3df1d30 100644 (file)
@@ -90,7 +90,7 @@ BOOL LoadClasspath(char *xmlfilename)
                                bOK = TRUE;\r
                        }\r
                        else\r
-                               {\r
+                       {\r
                        printf("Wrong format for %s. \n", xmlfilename);\r
                        }\r
                }\r
diff --git a/scilab/modules/jvm/src/c/loadLibrarypath.c b/scilab/modules/jvm/src/c/loadLibrarypath.c
new file mode 100644 (file)
index 0000000..4928e69
--- /dev/null
@@ -0,0 +1,105 @@
+/*-----------------------------------------------------------------------------------*/\r
+/* INRIA 2007 */\r
+/* Allan CORNET */\r
+/*-----------------------------------------------------------------------------------*/ \r
+#include <libxml/xpath.h>\r
+#include <libxml/xmlreader.h>\r
+#include "loadLibrarypath.h"\r
+#include "GetXmlFileEncoding.h"\r
+#include "../../fileio/includes/FileExist.h"\r
+#include "addToLibrarypath.h"\r
+#include "setgetSCIpath.h"\r
+#include "MALLOC.h"\r
+/*-----------------------------------------------------------------------------------*/ \r
+BOOL LoadLibrarypath(char *xmlfilename)\r
+{\r
+       BOOL bOK = FALSE;\r
+       if ( FileExist(xmlfilename) )\r
+       {\r
+               char *encoding=GetXmlFileEncoding(xmlfilename);\r
+\r
+               /* Don't care about line return / empty line */\r
+               xmlKeepBlanksDefault(0);\r
+               /* check if the XML file has been encoded with utf8 (unicode) or not */\r
+               if ( (strcmp("utf-8", encoding)!=0) || (strcmp("UTF-8", encoding)==0) )\r
+               {\r
+                       xmlDocPtr doc;\r
+                       xmlXPathContextPtr xpathCtxt = NULL;\r
+                       xmlXPathObjectPtr xpathObj = NULL;\r
+                       char *LIBRARYPATH=NULL;\r
+\r
+                       doc = xmlParseFile (xmlfilename);\r
+\r
+                       if (doc == NULL) \r
+                       {\r
+                               printf("Error: could not parse file %s\n", xmlfilename);\r
+                               if (encoding) {FREE(encoding);encoding=NULL;}\r
+                               return bOK;\r
+                       }\r
+\r
+                       xpathCtxt = xmlXPathNewContext(doc);\r
+                       xpathObj = xmlXPathEval((const xmlChar*)"//librarypaths/librarypath", xpathCtxt);\r
+\r
+                       if(xpathObj && xpathObj->nodesetval->nodeMax) \r
+                       {\r
+                               /* the Xpath has been understood and there are node */\r
+                               int     i;\r
+                               for(i = 0; i < xpathObj->nodesetval->nodeNr; i++)\r
+                               {\r
+\r
+                                       xmlAttrPtr attrib=xpathObj->nodesetval->nodeTab[i]->properties;\r
+                                       /* Get the properties of <librarypath>  */\r
+                                       while (attrib != NULL)\r
+                                       {\r
+                                               /* loop until when have read all the attributes */\r
+                                               if (xmlStrEqual (attrib->name, (const xmlChar*) "path"))\r
+                                               { \r
+                                                       /* we found the tag primitiveName */\r
+                                                       const char *str=(const char*)attrib->children->content;\r
+                                                       LIBRARYPATH=(char*)MALLOC(sizeof(char)*(strlen((const char*)str)+1));\r
+                                                       strcpy(LIBRARYPATH,str);\r
+                                               }\r
+                                               attrib = attrib->next;\r
+                                       }\r
+\r
+                                       if ( (LIBRARYPATH) && (strlen(LIBRARYPATH) > 0) )\r
+                                       {\r
+                                               #define KEYWORDSCILAB "$SCILAB" \r
+                                               char firstchars[8];\r
+                                               char *SCIPATH=NULL;\r
+                                               SCIPATH=getSCIpath();\r
+                                               \r
+                                               strncpy(firstchars,LIBRARYPATH,strlen(KEYWORDSCILAB));\r
+                                               firstchars[strlen(KEYWORDSCILAB)]='\0';\r
+\r
+                                               if (strcmp(firstchars,KEYWORDSCILAB)==0)\r
+                                               {\r
+                                                       char *modifypath = (char*)MALLOC(sizeof(char)*(strlen(SCIPATH)+strlen(LIBRARYPATH)+1));\r
+                                                       strcpy(modifypath,SCIPATH);\r
+                                                       strcat(modifypath,&LIBRARYPATH[strlen(KEYWORDSCILAB)]);\r
+                                                       FREE(LIBRARYPATH);\r
+                                                       LIBRARYPATH = modifypath;\r
+                                               }\r
+\r
+                                           if (SCIPATH) {FREE(SCIPATH);SCIPATH=NULL;}\r
+                        addToLibrarypath(LIBRARYPATH);\r
+                                               FREE(LIBRARYPATH);\r
+                                               LIBRARYPATH = NULL;\r
+                                       }\r
+                               }\r
+                               bOK = TRUE;\r
+                       }\r
+                       else\r
+                               {\r
+                       printf("Wrong format for %s. \n", xmlfilename);\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       printf("Error : Not a valid path file %s (encoding not 'utf-8') Encoding '%s' found\n", xmlfilename, encoding);\r
+               }\r
+               if (encoding) {FREE(encoding);encoding=NULL;}\r
+       }\r
+       return bOK;\r
+}\r
+/*-----------------------------------------------------------------------------------*/ \r
diff --git a/scilab/modules/jvm/src/c/loadLibrarypath.h b/scilab/modules/jvm/src/c/loadLibrarypath.h
new file mode 100644 (file)
index 0000000..e16db28
--- /dev/null
@@ -0,0 +1,18 @@
+/*-----------------------------------------------------------------------------------*/\r
+/* INRIA 2007 */\r
+/* Allan CORNET */\r
+/*-----------------------------------------------------------------------------------*/ \r
+#ifndef __LOADLIBRARYPATH_H__\r
+#define __LOADLIBRARYPATH_H__\r
+\r
+#include "machine.h" /* BOOL */\r
+\r
+/**\r
+* add paths to java.library.path\r
+* @param a filename SCI/etc/librarypath.xml\r
+* @return TRUE or FALSE\r
+*/\r
+BOOL LoadLibrarypath(char *xmlfilename);\r
+\r
+#endif /* __LOADLIBRARYPATH_H__ */\r
+/*-----------------------------------------------------------------------------------*/ \r
index 501aafe..f8dc350 100644 (file)
                                >\r
                        </File>\r
                        <File\r
+                               RelativePath=".\org\scilab\modules\jvm\LibraryPath.java"\r
+                               >\r
+                       </File>\r
+                       <File\r
                                RelativePath=".\org\scilab\modules\jvm\Scilab.java"\r
                                >\r
                        </File>\r
index 60c5fd0..7e03c30 100644 (file)
@@ -1,11 +1,13 @@
 /*-----------------------------------------------------------------------------------*/\r
 /**\r
 * Loading classes at runtime.\r
+* @author Allan CORNET - INRIA 2007\r
 */\r
 /*-----------------------------------------------------------------------------------*/\r
 package org.scilab.modules.jvm;\r
 /*-----------------------------------------------------------------------------------*/ \r
 import java.lang.reflect.Method;\r
+import java.lang.reflect.InvocationTargetException;\r
 import java.io.File;\r
 import java.io.IOException;\r
 import java.net.URL;\r
@@ -20,6 +22,14 @@ public class ClassPath {
 private static final Class[] parameters = new Class[]{URL.class};\r
 \r
 /**\r
+ * Constructor\r
+ */\r
+protected ClassPath() {\r
+       /*  indicate that the requested operation is not supported */\r
+       throw new UnsupportedOperationException();              \r
+}\r
+\r
+/**\r
  * add a filename to java classpath.\r
  * @param s a filename\r
  * @throws IOException if an error occurs\r
@@ -54,9 +64,12 @@ try {
 Method method = sysclass.getDeclaredMethod("addURL", parameters);\r
 method.setAccessible(true);\r
 method.invoke(sysloader , new Object[] {u });\r
-} catch (Throwable t) {\r
-t.printStackTrace();\r
-throw new IOException("Error, could not add URL to system classloader");\r
+} catch (NoSuchMethodException e) {\r
+       throw new IOException("Error NoSuchMethodException, could not add URL to system classloader");\r
+} catch (IllegalAccessException e) {\r
+       throw new IOException("Error IllegalAccessException, could not add URL to system classloader");\r
+} catch (InvocationTargetException e) {\r
+       throw new IOException("Error InvocationTargetException, could not add URL to system classloader");\r
 }\r
 \r
 }\r
diff --git a/scilab/modules/jvm/src/java/org/scilab/modules/jvm/LibraryPath.java b/scilab/modules/jvm/src/java/org/scilab/modules/jvm/LibraryPath.java
new file mode 100644 (file)
index 0000000..253e877
--- /dev/null
@@ -0,0 +1,78 @@
+/*-----------------------------------------------------------------------------------*/\r
+/**\r
+* Modify java.library.path at runtime.\r
+* @author Allan CORNET - INRIA 2007\r
+*/\r
+/*-----------------------------------------------------------------------------------*/\r
+package org.scilab.modules.jvm;\r
+/*-----------------------------------------------------------------------------------*/\r
+import java.io.IOException;\r
+import java.io.File;\r
+import java.lang.reflect.Field;\r
+/*-----------------------------------------------------------------------------------*/\r
+/*http://forum.java.sun.com/thread.jspa?threadID=135560&start=15&tstart=0 */\r
+/*-----------------------------------------------------------------------------------*/\r
+\r
+/**\r
+ * LibraryPath to overload java.library.path.\r
+ */\r
+public class LibraryPath {\r
+\r
+       private static final String JAVALIBRARYPATH = "java.library.path";\r
+       /**\r
+        * Constructor\r
+        */\r
+       protected LibraryPath() {\r
+               /*  indicate that the requested operation is not supported */\r
+               throw new UnsupportedOperationException();              \r
+       }\r
+/*--------------------------------------------------------------------------*/ \r
+       /**\r
+        * checks if pathToAdd already exists\r
+     * @param currentpaths list of current paths\r
+     * @param pathToAdd path to add\r
+     * @return a boolean true if path already exists\r
+     */\r
+       private static boolean pathAlreadyExists(String currentpaths, String pathToAdd) {\r
+    String[] paths = currentpaths.split("" + File.pathSeparatorChar);\r
+        for (String libraryPath : paths) {\r
+            if (libraryPath.equalsIgnoreCase(pathToAdd)) { return true; }\r
+        }\r
+        return false;\r
+    }\r
+/*--------------------------------------------------------------------------*/\r
+       /**\r
+        * add a path to java.library.path \r
+        * @param p path to add\r
+        * @throws IOException return a exception\r
+        */\r
+       public static void addPath(final String p) throws IOException {\r
+       if (!pathAlreadyExists(System.getProperty(JAVALIBRARYPATH), p)) {\r
+               String newLibPath = p + File.pathSeparator + System.getProperty(JAVALIBRARYPATH);\r
+               System.setProperty(JAVALIBRARYPATH, newLibPath);\r
+               try {\r
+                       Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");\r
+                       fieldSysPath.setAccessible(true);\r
+                       if (fieldSysPath != null) {\r
+               fieldSysPath.set(System.class.getClassLoader(), null);\r
+               }\r
+           } catch (NoSuchFieldException e) {\r
+               throw new IOException("Error NoSuchFieldException, could not add path to " + JAVALIBRARYPATH);\r
+           } catch (IllegalAccessException e) {\r
+               throw new IOException("Error IllegalAccessException, could not add path to " + JAVALIBRARYPATH);\r
+           }\r
+               }\r
+  }\r
\r
+/*--------------------------------------------------------------------------*/\r
+/**\r
+ * get the scilab java.library.path.\r
+ * @return librarypath\r
+ */\r
+       public static String[] getLibraryPath() {\r
+               String librarypath = System.getProperty(JAVALIBRARYPATH);\r
+               String[] paths = librarypath.split("" + File.pathSeparatorChar);\r
+               return paths;\r
+       }\r
+}\r
+/*-----------------------------------------------------------------------------------*/\r