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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef __CYGWIN__
#include "w32api/basetyps.h"
#endif
/* Include the JNI header file */
#include "jni.h"
/* JNI structure */
typedef struct {
JavaVM *jvm ;
jclass ijs_class ;
jobject ijs ;
jmethodID jni_main_mid ;
jmethodID process_command_mid ;
jint debug ;
int embedded ;
int native_doubles ;
int destroyed ;
} InlineJavaJNIVM ;
void shutdown_JVM(InlineJavaJNIVM *this){
if ((! this->embedded)&&(! this->destroyed)){
(*(this->jvm))->DestroyJavaVM(this->jvm) ;
this->destroyed = 1 ;
}
}
JNIEnv *get_env(InlineJavaJNIVM *this){
JNIEnv *env ;
(*(this->jvm))->AttachCurrentThread(this->jvm, ((void **)&env), NULL) ;
return env ;
}
/*
This is only used to trap exceptions from Perl.
*/
void check_exception_from_perl(JNIEnv *env, char *msg){
if ((*(env))->ExceptionCheck(env)){
(*(env))->ExceptionDescribe(env) ;
(*(env))->ExceptionClear(env) ;
croak("%s", msg) ;
}
}
void throw_ije(JNIEnv *env, char *msg){
jclass ije ;
ije = (*(env))->FindClass(env, "org/perl/inline/java/InlineJavaException") ;
if ((*(env))->ExceptionCheck(env)){
(*(env))->ExceptionDescribe(env) ;
(*(env))->ExceptionClear(env) ;
(*(env))->FatalError(env, "Can't find class InlineJavaException: exiting...") ;
}
(*(env))->ThrowNew(env, ije, msg) ;
}
jstring JNICALL jni_callback(JNIEnv *env, jobject obj, jstring cmd){
dSP ;
jstring resp ;
char *c = (char *)((*(env))->GetStringUTFChars(env, cmd, NULL)) ;
char *r = NULL ;
int count = 0 ;
SV *hook = NULL ;
char msg[128] ;
ENTER ;
SAVETMPS ;
PUSHMARK(SP) ;
XPUSHs(&PL_sv_undef) ;
XPUSHs(sv_2mortal(newSVpv(c, 0))) ;
PUTBACK ;
(*(env))->ReleaseStringUTFChars(env, cmd, c) ;
count = perl_call_pv("Inline::Java::Callback::InterceptCallback",
G_ARRAY|G_EVAL) ;
SPAGAIN ;
/* Check the eval */
if (SvTRUE(ERRSV)){
STRLEN n_a ;
throw_ije(env, SvPV(ERRSV, n_a)) ;
}
else{
if (count != 2){
sprintf(msg, "Invalid return value from Inline::Java::Callback::InterceptCallback: %d",
count) ;
throw_ije(env, msg) ;
}
}
/*
The first thing to pop is a reference to the returned object,
which we must keep around long enough so that it is not deleted
before control gets back to Java. This is because this object
may be returned be the callback, and when it gets back to Java
it will already be deleted.
*/
hook = perl_get_sv("Inline::Java::Callback::OBJECT_HOOK", FALSE) ;
sv_setsv(hook, POPs) ;
r = (char *)POPp ;
resp = (*(env))->NewStringUTF(env, r) ;
PUTBACK ;
FREETMPS ;
LEAVE ;
return resp ;
}
/*****************************************************************************/
MODULE = Inline::Java::JNI PACKAGE = Inline::Java::JNI
PROTOTYPES: DISABLE
InlineJavaJNIVM *
new(CLASS, classpath, args, embedded, debug, native_doubles)
char * CLASS
char * classpath
SV * args
int embedded
int debug
int native_doubles
PREINIT:
JavaVMInitArgs vm_args ;
JavaVMOption *options ;
JNIEnv *env ;
JNINativeMethod nm ;
jint res ;
char *cp ;
int args_len ;
int i ;
SV ** val = NULL ;
STRLEN n_a ;
CODE:
args = SvRV(args) ;
RETVAL = (InlineJavaJNIVM *)safemalloc(sizeof(InlineJavaJNIVM)) ;
if (RETVAL == NULL){
croak("Can't create InlineJavaJNIVM") ;
}
RETVAL->ijs = NULL ;
RETVAL->debug = debug ;
RETVAL->embedded = embedded ;
RETVAL->native_doubles = native_doubles ;
RETVAL->destroyed = 0 ;
/* Figure out the length of the args array */
args_len = av_len((AV *)args) + 1 ;
vm_args.version = JNI_VERSION_1_2 ;
options = (JavaVMOption *)malloc((2 + args_len) * sizeof(JavaVMOption)) ;
vm_args.options = options ;
vm_args.nOptions = 0 ;
vm_args.ignoreUnrecognized = JNI_FALSE ;
options[vm_args.nOptions++].optionString =
((RETVAL->debug > 5) ? "-verbose" : "-verbose:") ;
cp = (char *)malloc((strlen(classpath) + 32) * sizeof(char)) ;
sprintf(cp, "-Djava.class.path=%s", classpath) ;
options[vm_args.nOptions++].optionString = cp ;
for (i = 0 ; i < args_len ; i++){
val = av_fetch((AV *)args, i, 0) ;
if (val != NULL){
options[vm_args.nOptions++].optionString = SvPV(*val, n_a) ;
}
}
/* Embedded patch and idea by Doug MacEachern */
if (RETVAL->embedded) {
/* We are already inside a JVM */
jint n = 0 ;
res = JNI_GetCreatedJavaVMs(&(RETVAL->jvm), 1, &n) ;
if (n <= 0) {
/* res == 0 even if no JVMs are alive */
res = -1;
}
if (res < 0) {
croak("Can't find any created Java JVMs") ;
}
env = get_env(RETVAL) ;
}
else {
/* Create the Java VM */
res = JNI_CreateJavaVM(&(RETVAL->jvm), (void **)&(env), &vm_args) ;
if (res < 0) {
croak("Can't create Java JVM using JNI") ;
}
}
free(options) ;
free(cp) ;
/* Load the classes that we will use */
RETVAL->ijs_class = (*(env))->FindClass(env, "org/perl/inline/java/InlineJavaServer") ;
check_exception_from_perl(env, "Can't find class InlineJavaServer") ;
RETVAL->ijs_class = (*(env))->NewGlobalRef(env, RETVAL->ijs_class) ;
/* Get the method ids that are needed later */
RETVAL->jni_main_mid = (*(env))->GetStaticMethodID(env, RETVAL->ijs_class, "jni_main",
"(IZ)Lorg/perl/inline/java/InlineJavaServer;") ;
check_exception_from_perl(env, "Can't find method jni_main in class InlineJavaServer") ;
RETVAL->process_command_mid = (*(env))->GetMethodID(env, RETVAL->ijs_class, "ProcessCommand",
"(Ljava/lang/String;)Ljava/lang/String;") ;
check_exception_from_perl(env, "Can't find method ProcessCommand in class InlineJavaServer") ;
/* Register the callback function */
nm.name = "jni_callback" ;
nm.signature = "(Ljava/lang/String;)Ljava/lang/String;" ;
nm.fnPtr = jni_callback ;
(*(env))->RegisterNatives(env, RETVAL->ijs_class, &nm, 1) ;
check_exception_from_perl(env, "Can't register method jni_callback in class InlineJavaServer") ;
OUTPUT:
RETVAL
void
shutdown(this)
InlineJavaJNIVM * this
CODE:
shutdown_JVM(this) ;
void
DESTROY(this)
InlineJavaJNIVM * this
CODE:
shutdown_JVM(this) ;
safefree(this) ;
void
create_ijs(this)
InlineJavaJNIVM * this
PREINIT:
JNIEnv *env ;
CODE:
env = get_env(this) ;
this->ijs = (*(env))->CallStaticObjectMethod(env, this->ijs_class, this->jni_main_mid, this->debug, this->native_doubles) ;
check_exception_from_perl(env, "Can't call jni_main in class InlineJavaServer") ;
this->ijs = (*(env))->NewGlobalRef(env, this->ijs) ;
char *
process_command(this, data)
InlineJavaJNIVM * this
char * data
PREINIT:
JNIEnv *env ;
jstring cmd ;
jstring resp ;
SV *hook = NULL ;
CODE:
env = get_env(this) ;
cmd = (*(env))->NewStringUTF(env, data) ;
check_exception_from_perl(env, "Can't create java.lang.String") ;
resp = (*(env))->CallObjectMethod(env, this->ijs, this->process_command_mid, cmd) ;
/* Thanks Dave Blob for spotting this. This is necessary since this code never really returns to Java
It simply calls into Java and comes back. */
(*(env))->DeleteLocalRef(env, cmd);
check_exception_from_perl(env, "Can't call ProcessCommand in class InlineJavaServer") ;
hook = perl_get_sv("Inline::Java::Callback::OBJECT_HOOK", FALSE) ;
sv_setsv(hook, &PL_sv_undef) ;
RETVAL = (char *)((*(env))->GetStringUTFChars(env, resp, NULL)) ;
OUTPUT:
RETVAL
CLEANUP:
(*(env))->ReleaseStringUTFChars(env, resp, RETVAL) ;
(*(env))->DeleteLocalRef(env, resp) ;
|