Merged revisions 8245-8298 via svnmerge from
[cacao.git] / src / native / vm / java_lang_Runtime.c
index c6c7329b6c921fafd4e34c88f5da17c43ec3e83f..4a363e368392965dc34c310086674e199ef910c6 100644 (file)
 
 
 #include "config.h"
+
+#if defined(ENABLE_LTDL) && defined(HAVE_LTDL_H)
+# include <ltdl.h>
+#endif
+
 #include "vm/types.h"
 
 #include "mm/gc-common.h"
 
+#include "native/jni.h"
+#include "native/native.h"
+
+#include "native/include/java_lang_String.h"
+
+#include "toolbox/logging.h"
+
+#include "vm/exceptions.h"
+#include "vm/stringlocal.h"
 #include "vm/vm.h"
 
+#include "vmcore/options.h"
+
 
 /* should we run all finalizers on exit? */
 static bool finalizeOnExit = false;
@@ -86,6 +102,84 @@ void _Jv_java_lang_Runtime_gc(void)
 }
 
 
+/*
+ * Class:     java/lang/Runtime
+ * Method:    loadLibrary
+ * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
+ */
+#if defined(ENABLE_JNI)
+s4 _Jv_java_lang_Runtime_loadLibrary(JNIEnv *env, java_lang_String *libname, classloader *cl)
+#else
+s4 _Jv_java_lang_Runtime_loadLibrary(java_lang_String *libname, classloader *cl)
+#endif
+{
+#if defined(ENABLE_LTDL)
+       utf               *name;
+       lt_dlhandle        handle;
+# if defined(ENABLE_JNI)
+       lt_ptr             onload;
+       s4                 version;
+# endif
+
+       if (libname == NULL) {
+               exceptions_throw_nullpointerexception();
+               return 0;
+       }
+
+       name = javastring_toutf((java_handle_t *) libname, false);
+
+       /* is the library already loaded? */
+
+       if (native_library_find(name, cl) != NULL)
+               return 1;
+
+       /* open the library */
+
+       handle = native_library_open(name);
+
+       if (handle == NULL)
+               return 0;
+
+# if defined(ENABLE_JNI)
+       /* resolve JNI_OnLoad function */
+
+       onload = lt_dlsym(handle, "JNI_OnLoad");
+
+       if (onload != NULL) {
+               JNIEXPORT s4 (JNICALL *JNI_OnLoad) (JavaVM *, void *);
+               JavaVM *vm;
+
+               JNI_OnLoad = (JNIEXPORT s4 (JNICALL *)(JavaVM *, void *)) (ptrint) onload;
+
+               (*env)->GetJavaVM(env, &vm);
+
+               version = JNI_OnLoad(vm, NULL);
+
+               /* if the version is not 1.2 and not 1.4 the library cannot be loaded */
+
+               if ((version != JNI_VERSION_1_2) && (version != JNI_VERSION_1_4)) {
+                       lt_dlclose(handle);
+
+                       return 0;
+               }
+       }
+# endif
+
+       /* insert the library name into the library hash */
+
+       native_library_add(name, cl, handle);
+
+       return 1;
+#else
+       vm_abort("_Jv_java_lang_Runtime_loadLibrary: not available");
+
+       /* keep compiler happy */
+
+       return 0;
+#endif
+}
+
+
 #if defined(ENABLE_JAVASE)
 
 /*