1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
/*****************************************************************************
Copyright 2004 Steve Mnard
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*****************************************************************************/
#ifndef _JAVA_ENV_H_
#define _JAVA_ENV_H_
/**
* Simple exception class to wrap java-sourced exceptions
*/
class JavaException
{
public :
JavaException(const char* msg, const char* f, int l) : file(f), line(l) {message = msg;}
JavaException(const JavaException& ex) : file(ex.file), line(ex.line) {message = ex.message;}
virtual ~JavaException() {}
const char* file;
int line;
string message;
};
class HostException
{
public :
HostException() {}
virtual ~HostException() {}
virtual const char* getFile() { return "";}
virtual int getLine() {return 0;}
virtual string getMessage() { return ""; }
};
/**
* the platform adapter's implementation is chosen by the JPYPE_??? macros
*/
class JPPlatformAdapter
{
public :
virtual ~JPPlatformAdapter() {};
virtual void loadLibrary(const char* path) = 0;
virtual void unloadLibrary() = 0;
virtual void* getSymbol(const char* name)= 0;
};
/**
* Wrap all the needed parts of JNI to provide centralized error detection and dynamic loading
*/
class JPJavaEnv
{
public :
JPJavaEnv(JavaVM* vm) :
jvm(vm),
referenceQueue(NULL),
convertStringObjects(true)
{
}
virtual ~JPJavaEnv() {}
private :
static JPPlatformAdapter* GetAdapter();
// static JPPlatformAdapter* adapter;
static jint (JNICALL *CreateJVM_Method)(JavaVM **pvm, void **penv, void *args);
static jint (JNICALL *GetCreatedJVMs_Method)(JavaVM **pvm, jsize size, jsize* nVms);
JavaVM* jvm;
jobject referenceQueue;
bool convertStringObjects;
JNIEnv* getJNIEnv();
public :
static void load(const string& path);
static JPJavaEnv* CreateJavaVM(void* arg);
static JPJavaEnv* GetCreatedJavaVM();
jint AttachCurrentThread();
jint AttachCurrentThreadAsDaemon();
static bool isThreadAttached();
void setConvertStringObjects(bool flag)
{
convertStringObjects = flag;
}
bool getConvertStringObjects()
{
return convertStringObjects;
}
void setReferenceQueue(jobject obj)
{
referenceQueue = NewGlobalRef(obj);
}
jobject getReferenceQueue()
{
return referenceQueue;
}
void shutdown();
void checkInitialized();
int DestroyJavaVM();
jobject NewGlobalRef(jobject obj);
void DeleteGlobalRef(jobject obj);
jobject NewLocalRef(jobject obj);
void DeleteLocalRef(jobject obj);
bool ExceptionCheck();
void ExceptionDescribe();
void ExceptionClear();
jthrowable ExceptionOccurred();
jint DetachCurrentThread();
jint GetEnv(JNIEnv** env);
jint ThrowNew(jclass clazz, const char* msg);
jint Throw(jthrowable th);
jobject NewDirectByteBuffer(void* address, jlong capacity);
/** NewObjectA */
jobject NewObjectA(jclass a0, jmethodID a1, jvalue* a2);
/** NewObject */
jobject NewObject(jclass a0, jmethodID a1);
/** get/release primitive arrays critical (direct pointer access) */
void* GetPrimitiveArrayCritical(jarray array, jboolean *isCopy);
void ReleasePrimitiveArrayCritical(jarray array, void *carray, jint mode);
jint PushLocalFrame(jint);
jobject PopLocalFrame(jobject);
#include "jp_javaenv_autogen.h"
};
#endif // _JAVA_ENV_H_
|