Skip to content

Commit

Permalink
LibLoader 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jerome-jouvie committed Feb 18, 2017
1 parent 5b9d932 commit 5663264
Show file tree
Hide file tree
Showing 15 changed files with 3,743 additions and 0 deletions.
Binary file added distrib/1.0.0/LibLoader-linux.jar
Binary file not shown.
Binary file added distrib/1.0.0/LibLoader-linux64.jar
Binary file not shown.
Binary file added distrib/1.0.0/LibLoader.jar
Binary file not shown.
144 changes: 144 additions & 0 deletions src-c-linux32/LibLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/**
* LibLoader
* Copyright © 2007-2017 Jérôme Jouvie
*
* Created on 25 mar. 2007
* @version file v1.0.0
*
* To contact me:
* [email protected]
* http://jerome.jouvie.free.fr/
*
* INTRODUCTION
* This project enhance the Java's System.loadLibrary and allows:
* - library loading without loading dependencies
* - search libraries from "java.library.path", "sun.jnlp.applet.launcher", "org.lwjgl.librarypath"
* - library loading from applet
*
*
* GNU LESSER GENERAL PUBLIC LICENSE
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of the License,
* or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*/

#include <stdio.h>
#include <dlfcn.h>

#include "org_jouvieje_libloader_LibLoaderJNI.h"

#define POINTER_TYPE unsigned long
#define N2J_CAST_PTR(X,Y) (*(Y*)&(X))

const char * NULL_HANDLE = "";
void ThrowNullPointerException(JNIEnv *jenv, const char *message) {
jclass exception = jenv->FindClass("java/lang/NullPointerException");

if(exception) {
jenv->ThrowNew(exception, message);
jenv->DeleteLocalRef(exception);
}
}

bool CheckAllocation(JNIEnv *jenv, void *memAllocated) {
if(memAllocated == NULL) {
ThrowNullPointerException(jenv, "");
return false;
}
else {
return true;
}
}

char *getByteArrayElements(JNIEnv *jenv, jbyteArray array) {
if(array) {
const jsize length = jenv->GetArrayLength(array);
const jbyte *chars = jenv->GetByteArrayElements(array, 0);
char *copy = new char[length+1]; //Allocate memory
if(!CheckAllocation(jenv, copy)) {
return 0;
}

for(int i = 0; i < length; i++) {
copy[i] = (char)chars[i];
}
copy[length] = 0; //End of the string
jenv->ReleaseByteArrayElements(array, (jbyte *)chars, 0);

return copy;
}
return 0;
}

void releaseByteArrayElements(JNIEnv *jenv, jbyteArray array, const char *chars) {
if(chars) {
delete [] chars; //Deallocate memory
chars = NULL;
}
}

JNIEXPORT jlong JNICALL Java_org_jouvieje_libloader_LibLoaderJNI_dlopen(JNIEnv *jenv, jclass jcls, jbyteArray jfilename, jint jflag) {
char *filename = getByteArrayElements(jenv, jfilename);
int flag = (int)jflag;

void *handle = dlopen(filename, flag);

if(!handle) {
printf("dlopen error: %s\n", dlerror());
}

releaseByteArrayElements(jenv, jfilename, (const char *)filename);

POINTER_TYPE jresult/* = 0*/;
N2J_CAST_PTR(jresult, void *) = handle;
return (jlong)jresult;
}

JNIEXPORT jstring JNICALL Java_org_jouvieje_libloader_LibLoaderJNI_dlerror(JNIEnv *jenv, jclass jcls) {
char *result = dlerror();
if (!result) {
return NULL;
}
return jenv->NewStringUTF(result);
}

JNIEXPORT jlong JNICALL Java_org_jouvieje_libloader_LibLoaderJNI_dlsym(JNIEnv *jenv, jclass jcls, jlong jhandle, jbyteArray jsymbol) {
if(!jhandle) {
ThrowNullPointerException(jenv, NULL_HANDLE);
return 0;
}
void *handle = *(void **)&jhandle;
char *symbol = getByteArrayElements(jenv, jsymbol);

void *result = dlsym(handle, symbol);

releaseByteArrayElements(jenv, jsymbol, (const char *)symbol);

POINTER_TYPE jresult/* = 0*/;
N2J_CAST_PTR(jresult, void *) = result;
return (jlong)jresult;
}

JNIEXPORT jint JNICALL Java_org_jouvieje_libloader_LibLoaderJNI_dlclose(JNIEnv *jenv, jclass jcls, jlong jhandle) {
if(!jhandle) {
ThrowNullPointerException(jenv, NULL_HANDLE);
return 0;
}
void *handle = *(void **)&jhandle;

int result = dlclose(handle);
return (jint)result;
}

3 changes: 3 additions & 0 deletions src-c-linux32/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
echo <jni.h> to "jni.h" in jni headers
rm ./libLibLoader.so
g++ -I../../../jni-headers/linux/sun/ -O3 -Wall -fPIC *.cpp -pthread -shared -o libLibLoader.so
45 changes: 45 additions & 0 deletions src-c-linux32/org_jouvieje_libloader_LibLoaderJNI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class org_jouvieje_libloader_LibLoaderJNI */

#ifndef _Included_org_jouvieje_libloader_LibLoaderJNI
#define _Included_org_jouvieje_libloader_LibLoaderJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: org_jouvieje_libloader_LibLoaderJNI
* Method: dlopen
* Signature: ([BI)J
*/
JNIEXPORT jlong JNICALL Java_org_jouvieje_libloader_LibLoaderJNI_dlopen
(JNIEnv *, jclass, jbyteArray, jint);

/*
* Class: org_jouvieje_libloader_LibLoaderJNI
* Method: dlerror
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_org_jouvieje_libloader_LibLoaderJNI_dlerror
(JNIEnv *, jclass);

/*
* Class: org_jouvieje_libloader_LibLoaderJNI
* Method: dlsym
* Signature: (J[B)J
*/
JNIEXPORT jlong JNICALL Java_org_jouvieje_libloader_LibLoaderJNI_dlsym
(JNIEnv *, jclass, jlong, jbyteArray);

/*
* Class: org_jouvieje_libloader_LibLoaderJNI
* Method: dlclose
* Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_org_jouvieje_libloader_LibLoaderJNI_dlclose
(JNIEnv *, jclass, jlong);

#ifdef __cplusplus
}
#endif
#endif
144 changes: 144 additions & 0 deletions src-c-linux64/LibLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/**
* LibLoader
* Copyright © 2007-2017 Jérôme Jouvie
*
* Created on 25 mar. 2007
* @version file v1.0.0
*
* To contact me:
* [email protected]
* http://jerome.jouvie.free.fr/
*
* INTRODUCTION
* This project enhance the Java's System.loadLibrary and allows:
* - library loading without loading dependencies
* - search libraries from "java.library.path", "sun.jnlp.applet.launcher", "org.lwjgl.librarypath"
* - library loading from applet
*
*
* GNU LESSER GENERAL PUBLIC LICENSE
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of the License,
* or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*/

#include <stdio.h>
#include <dlfcn.h>

#include "org_jouvieje_libloader_LibLoaderJNI.h"

#define POINTER_TYPE unsigned long
#define N2J_CAST_PTR(X,Y) (*(Y*)&(X))

const char * NULL_HANDLE = "";
void ThrowNullPointerException(JNIEnv *jenv, const char *message) {
jclass exception = jenv->FindClass("java/lang/NullPointerException");

if(exception) {
jenv->ThrowNew(exception, message);
jenv->DeleteLocalRef(exception);
}
}

bool CheckAllocation(JNIEnv *jenv, void *memAllocated) {
if(memAllocated == NULL) {
ThrowNullPointerException(jenv, "");
return false;
}
else {
return true;
}
}

char *getByteArrayElements(JNIEnv *jenv, jbyteArray array) {
if(array) {
const jsize length = jenv->GetArrayLength(array);
const jbyte *chars = jenv->GetByteArrayElements(array, 0);
char *copy = new char[length+1]; //Allocate memory
if(!CheckAllocation(jenv, copy)) {
return 0;
}

for(int i = 0; i < length; i++) {
copy[i] = (char)chars[i];
}
copy[length] = 0; //End of the string
jenv->ReleaseByteArrayElements(array, (jbyte *)chars, 0);

return copy;
}
return 0;
}

void releaseByteArrayElements(JNIEnv *jenv, jbyteArray array, const char *chars) {
if(chars) {
delete [] chars; //Deallocate memory
chars = NULL;
}
}

JNIEXPORT jlong JNICALL Java_org_jouvieje_libloader_LibLoaderJNI_dlopen(JNIEnv *jenv, jclass jcls, jbyteArray jfilename, jint jflag) {
char *filename = getByteArrayElements(jenv, jfilename);
int flag = (int)jflag;

void *handle = dlopen(filename, flag);

if(!handle) {
printf("dlopen error: %s\n", dlerror());
}

releaseByteArrayElements(jenv, jfilename, (const char *)filename);

POINTER_TYPE jresult/* = 0*/;
N2J_CAST_PTR(jresult, void *) = handle;
return (jlong)jresult;
}

JNIEXPORT jstring JNICALL Java_org_jouvieje_libloader_LibLoaderJNI_dlerror(JNIEnv *jenv, jclass jcls) {
char *result = dlerror();
if (!result) {
return NULL;
}
return jenv->NewStringUTF(result);
}

JNIEXPORT jlong JNICALL Java_org_jouvieje_libloader_LibLoaderJNI_dlsym(JNIEnv *jenv, jclass jcls, jlong jhandle, jbyteArray jsymbol) {
if(!jhandle) {
ThrowNullPointerException(jenv, NULL_HANDLE);
return 0;
}
void *handle = *(void **)&jhandle;
char *symbol = getByteArrayElements(jenv, jsymbol);

void *result = dlsym(handle, symbol);

releaseByteArrayElements(jenv, jsymbol, (const char *)symbol);

POINTER_TYPE jresult/* = 0*/;
N2J_CAST_PTR(jresult, void *) = result;
return (jlong)jresult;
}

JNIEXPORT jint JNICALL Java_org_jouvieje_libloader_LibLoaderJNI_dlclose(JNIEnv *jenv, jclass jcls, jlong jhandle) {
if(!jhandle) {
ThrowNullPointerException(jenv, NULL_HANDLE);
return 0;
}
void *handle = *(void **)&jhandle;

int result = dlclose(handle);
return (jint)result;
}

3 changes: 3 additions & 0 deletions src-c-linux64/build64.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
echo <jni.h> to "jni.h" in jni headers
rm ./libLibLoader64.so
g++ -I../../../jni-headers/linux64/sun/ -O3 -Wall -fPIC *.cpp -pthread -shared -o libLibLoader64.so
45 changes: 45 additions & 0 deletions src-c-linux64/org_jouvieje_libloader_LibLoaderJNI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class org_jouvieje_libloader_LibLoaderJNI */

#ifndef _Included_org_jouvieje_libloader_LibLoaderJNI
#define _Included_org_jouvieje_libloader_LibLoaderJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: org_jouvieje_libloader_LibLoaderJNI
* Method: dlopen
* Signature: ([BI)J
*/
JNIEXPORT jlong JNICALL Java_org_jouvieje_libloader_LibLoaderJNI_dlopen
(JNIEnv *, jclass, jbyteArray, jint);

/*
* Class: org_jouvieje_libloader_LibLoaderJNI
* Method: dlerror
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_org_jouvieje_libloader_LibLoaderJNI_dlerror
(JNIEnv *, jclass);

/*
* Class: org_jouvieje_libloader_LibLoaderJNI
* Method: dlsym
* Signature: (J[B)J
*/
JNIEXPORT jlong JNICALL Java_org_jouvieje_libloader_LibLoaderJNI_dlsym
(JNIEnv *, jclass, jlong, jbyteArray);

/*
* Class: org_jouvieje_libloader_LibLoaderJNI
* Method: dlclose
* Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_org_jouvieje_libloader_LibLoaderJNI_dlclose
(JNIEnv *, jclass, jlong);

#ifdef __cplusplus
}
#endif
#endif
Loading

0 comments on commit 5663264

Please sign in to comment.