javaembed.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "platform.h"
  14. #include <jni.h>
  15. #include "jexcept.hpp"
  16. #include "jthread.hpp"
  17. #include "hqlplugins.hpp"
  18. #include "deftype.hpp"
  19. #include "eclrtl.hpp"
  20. #include "eclrtl_imp.hpp"
  21. #include "jprop.hpp"
  22. #include "build-config.h"
  23. #ifdef _WIN32
  24. #define EXPORT __declspec(dllexport)
  25. #else
  26. #define EXPORT
  27. #endif
  28. static const char * compatibleVersions[] = {
  29. "Java Embed Helper 1.0.0",
  30. NULL };
  31. static const char *version = "Java Embed Helper 1.0.0";
  32. extern "C" EXPORT bool getECLPluginDefinition(ECLPluginDefinitionBlock *pb)
  33. {
  34. if (pb->size == sizeof(ECLPluginDefinitionBlockEx))
  35. {
  36. ECLPluginDefinitionBlockEx * pbx = (ECLPluginDefinitionBlockEx *) pb;
  37. pbx->compatibleVersions = compatibleVersions;
  38. }
  39. else if (pb->size != sizeof(ECLPluginDefinitionBlock))
  40. return false;
  41. pb->magicVersion = PLUGIN_VERSION;
  42. pb->version = version;
  43. pb->moduleName = "java";
  44. pb->ECL = NULL;
  45. pb->flags = PLUGIN_MULTIPLE_VERSIONS;
  46. pb->description = "Java Embed Helper";
  47. return true;
  48. }
  49. namespace javaembed {
  50. // Use a global object to ensure that the Java VM is initialized once only.
  51. // We would like to create it lazily for two reasons:
  52. // 1. So that we only get a JVM if we need one (even if we have loaded the plugin)
  53. // 2. It's important for the JVM to be initialized AFTER we have set up signal handlers, as it
  54. // likes to set its own (in particular, it seems to intercept and ignore some SIGSEGV during the
  55. // garbage collection).
  56. // Unfortunately, it seems that the design of the JNI interface is such that JNI_CreateJavaVM has to be called on the 'main thread'.
  57. // So we can't achieve 1, and 2 requires that we create via the INIT_MODULE mechanism (rather than just a static object), and that
  58. // any engines that call InitModuleObjects() or load plugins dynamically do so AFTER setting any signal handlers or calling
  59. // EnableSEHtoExceptionMapping
  60. //
  61. static class JavaGlobalState
  62. {
  63. public:
  64. JavaGlobalState()
  65. {
  66. JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
  67. StringArray optionStrings;
  68. const char* origPath = getenv("CLASSPATH");
  69. StringBuffer newPath;
  70. newPath.append("-Djava.class.path=");
  71. if (origPath && *origPath)
  72. {
  73. newPath.append(origPath).append(ENVSEPCHAR);
  74. }
  75. StringBuffer envConf;
  76. envConf.append(CONFIG_DIR).append(PATHSEPSTR).append("environment.conf");
  77. Owned<IProperties> conf = createProperties(envConf.str(), true);
  78. if (conf && conf->hasProp("classpath"))
  79. {
  80. conf->getProp("classpath", newPath);
  81. newPath.append(ENVSEPCHAR);
  82. }
  83. else
  84. {
  85. newPath.append(INSTALL_DIR).append(PATHSEPCHAR).append("classes").append(ENVSEPCHAR);
  86. }
  87. newPath.append(".");
  88. optionStrings.append(newPath);
  89. if (conf && conf->hasProp("jvmlibpath"))
  90. {
  91. StringBuffer libPath;
  92. libPath.append("-Djava.library.path=");
  93. conf->getProp("jvmlibpath", libPath);
  94. optionStrings.append(libPath);
  95. }
  96. if (conf && conf->hasProp("jvmoptions"))
  97. {
  98. optionStrings.appendList(conf->queryProp("jvmoptions"), ENVSEPSTR);
  99. }
  100. // Options we know we always want set
  101. optionStrings.append("-Xrs");
  102. // These may be useful for debugging
  103. // optionStrings.append("-Xcheck:jni");
  104. // optionStrings.append("-verbose:jni");
  105. JavaVMOption* options = new JavaVMOption[optionStrings.length()];
  106. ForEachItemIn(idx, optionStrings)
  107. {
  108. DBGLOG("javaembed: Setting JVM option: %s",(char *)optionStrings.item(idx));
  109. options[idx].optionString = (char *) optionStrings.item(idx);
  110. options[idx].extraInfo = NULL;
  111. }
  112. vm_args.nOptions = optionStrings.length();
  113. vm_args.options = options;
  114. vm_args.ignoreUnrecognized = true;
  115. vm_args.version = JNI_VERSION_1_6;
  116. /* load and initialize a Java VM, return a JNI interface pointer in env */
  117. JNIEnv *env; /* receives pointer to native method interface */
  118. int createResult = JNI_CreateJavaVM(&javaVM, (void**)&env, &vm_args);
  119. delete [] options;
  120. if (createResult != 0)
  121. throw MakeStringException(0, "javaembed: Unable to initialize JVM (%d)",createResult);
  122. }
  123. ~JavaGlobalState()
  124. {
  125. // We don't attempt to destroy the Java VM, as it's buggy...
  126. }
  127. JavaVM *javaVM; /* denotes a Java VM */
  128. } *globalState;
  129. #ifdef _WIN32
  130. EXTERN_C IMAGE_DOS_HEADER __ImageBase;
  131. #endif
  132. MODULE_INIT(INIT_PRIORITY_STANDARD)
  133. {
  134. globalState = new JavaGlobalState;
  135. // Make sure we are never unloaded (as JVM does not support it)
  136. // we do this by doing a dynamic load of the javaembed library
  137. #ifdef _WIN32
  138. char path[_MAX_PATH];
  139. ::GetModuleFileName((HINSTANCE)&__ImageBase, path, _MAX_PATH);
  140. if (strstr(path, "javaembed"))
  141. {
  142. HINSTANCE h = LoadSharedObject(path, false, false);
  143. DBGLOG("LoadSharedObject returned %p", h);
  144. }
  145. #else
  146. FILE *diskfp = fopen("/proc/self/maps", "r");
  147. if (diskfp)
  148. {
  149. char ln[_MAX_PATH];
  150. while (fgets(ln, sizeof(ln), diskfp))
  151. {
  152. if (strstr(ln, "libjavaembed"))
  153. {
  154. const char *fullName = strchr(ln, '/');
  155. if (fullName)
  156. {
  157. char *tail = (char *) strstr(fullName, SharedObjectExtension);
  158. if (tail)
  159. {
  160. tail[strlen(SharedObjectExtension)] = 0;
  161. HINSTANCE h = LoadSharedObject(fullName, false, false);
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. fclose(diskfp);
  168. }
  169. #endif
  170. return true;
  171. }
  172. MODULE_EXIT()
  173. {
  174. // We don't attempt to destroy the Java VM, as it's buggy...
  175. // delete globalState;
  176. // globalState = NULL;
  177. }
  178. static void checkType(type_t javatype, size32_t javasize, type_t ecltype, size32_t eclsize)
  179. {
  180. if (javatype != ecltype || javasize != eclsize)
  181. throw MakeStringException(0, "javaembed: Type mismatch"); // MORE - could provide some details!
  182. }
  183. // There is a singleton JavaThreadContext per thread. This allows us to
  184. // ensure that we can make repeated calls to a Java function efficiently.
  185. class JavaThreadContext
  186. {
  187. public:
  188. JNIEnv *JNIenv; /* receives pointer to native method interface */
  189. public:
  190. JavaThreadContext()
  191. {
  192. jint res = globalState->javaVM->AttachCurrentThread((void **) &JNIenv, NULL);
  193. assertex(res >= 0);
  194. javaClass = NULL;
  195. javaMethodID = NULL;
  196. contextClassLoaderChecked = false;
  197. }
  198. ~JavaThreadContext()
  199. {
  200. if (javaClass)
  201. JNIenv->DeleteGlobalRef(javaClass);
  202. // According to the Java VM 1.7 docs, "A native thread attached to
  203. // the VM must call DetachCurrentThread() to detach itself before
  204. // exiting."
  205. globalState->javaVM->DetachCurrentThread();
  206. }
  207. void checkException()
  208. {
  209. if (JNIenv->ExceptionCheck())
  210. {
  211. jthrowable exception = JNIenv->ExceptionOccurred();
  212. JNIenv->ExceptionClear();
  213. jclass throwableClass = JNIenv->FindClass("java/lang/Throwable");
  214. jmethodID throwableToString = JNIenv->GetMethodID(throwableClass, "toString", "()Ljava/lang/String;");
  215. jstring cause = (jstring) JNIenv->CallObjectMethod(exception, throwableToString);
  216. const char *text = JNIenv->GetStringUTFChars(cause, 0);
  217. VStringBuffer message("javaembed: In method %s: %s", prevtext.get(), text);
  218. JNIenv->ReleaseStringUTFChars(cause, text);
  219. rtlFail(0, message.str());
  220. }
  221. }
  222. void ensureContextClassLoaderAvailable ()
  223. {
  224. // JVMs that are created by native threads have a context class loader set to the
  225. // bootstrap class loader, which is not very useful because the bootstrap class
  226. // loader is interested only in getting the JVM up to speed. In particular,
  227. // the classpath is ignored. The idea here is to set, if needed, the context
  228. // class loader to another loader that does recognize classpath. What follows
  229. // is the equivalent of the following Java code:
  230. //
  231. // if (Thread.currentThread().getContextClassLoader == NULL)
  232. // Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
  233. if (!contextClassLoaderChecked)
  234. {
  235. JNIenv->ExceptionClear();
  236. // Get the current context class loader
  237. jclass javaLangThreadClass = JNIenv->FindClass("java/lang/Thread");
  238. checkException();
  239. jmethodID currentThreadMethod = JNIenv->GetStaticMethodID(javaLangThreadClass, "currentThread", "()Ljava/lang/Thread;");
  240. checkException();
  241. jobject threadObj = JNIenv->CallStaticObjectMethod(javaLangThreadClass, currentThreadMethod);
  242. checkException();
  243. jmethodID getContextClassLoaderMethod = JNIenv->GetMethodID(javaLangThreadClass, "getContextClassLoader", "()Ljava/lang/ClassLoader;");
  244. checkException();
  245. jobject contextClassLoaderObj = JNIenv->CallObjectMethod(threadObj, getContextClassLoaderMethod);
  246. checkException();
  247. if (!contextClassLoaderObj)
  248. {
  249. // No context class loader, so use the system class loader (hopefully it's present)
  250. jclass javaLangClassLoaderClass = JNIenv->FindClass("java/lang/ClassLoader");
  251. checkException();
  252. jmethodID getSystemClassLoaderMethod = JNIenv->GetStaticMethodID(javaLangClassLoaderClass, "getSystemClassLoader", "()Ljava/lang/ClassLoader;");
  253. checkException();
  254. jobject systemClassLoaderObj = JNIenv->CallStaticObjectMethod(javaLangClassLoaderClass, getSystemClassLoaderMethod);
  255. checkException();
  256. if (systemClassLoaderObj)
  257. {
  258. jmethodID setContextClassLoaderMethod = JNIenv->GetMethodID(javaLangThreadClass, "setContextClassLoader", "(Ljava/lang/ClassLoader;)V");
  259. checkException();
  260. JNIenv->CallObjectMethod(threadObj, setContextClassLoaderMethod, systemClassLoaderObj);
  261. checkException();
  262. }
  263. }
  264. contextClassLoaderChecked = true;
  265. }
  266. }
  267. inline void importFunction(size32_t lenChars, const char *utf)
  268. {
  269. size32_t bytes = rtlUtf8Size(lenChars, utf);
  270. StringBuffer text(bytes, utf);
  271. if (!prevtext || strcmp(text, prevtext) != 0)
  272. {
  273. prevtext.clear();
  274. // Make sure there is a context class loader available; we need to
  275. // do this before calling FindClass() on the class we need
  276. ensureContextClassLoaderAvailable();
  277. // Name should be in the form class.method:signature
  278. const char *funcname = strchr(text, '.');
  279. if (!funcname)
  280. throw MakeStringException(MSGAUD_user, 0, "javaembed: Invalid import name %s - Expected classname.methodname:signature", text.str());
  281. const char *signature = strchr(funcname, ':');
  282. if (!signature)
  283. throw MakeStringException(MSGAUD_user, 0, "javaembed: Invalid import name %s - Expected classname.methodname:signature", text.str());
  284. StringBuffer classname(funcname-text, text);
  285. funcname++; // skip the '.'
  286. StringBuffer methodname(signature-funcname, funcname);
  287. signature++; // skip the ':'
  288. if (javaClass)
  289. JNIenv->DeleteGlobalRef(javaClass);
  290. javaClass = (jclass) JNIenv->NewGlobalRef(JNIenv->FindClass(classname));
  291. if (!javaClass)
  292. throw MakeStringException(MSGAUD_user, 0, "javaembed: Failed to resolve class name %s", classname.str());
  293. javaMethodID = JNIenv->GetStaticMethodID(javaClass, methodname, signature);
  294. if (!javaMethodID)
  295. throw MakeStringException(MSGAUD_user, 0, "javaembed: Failed to resolve method name %s with signature %s", methodname.str(), signature);
  296. const char *returnSig = strrchr(signature, ')');
  297. assertex(returnSig); // Otherwise how did Java accept it??
  298. returnSig++;
  299. returnType.set(returnSig);
  300. argsig.set(signature);
  301. prevtext.set(text);
  302. }
  303. }
  304. inline void callFunction(jvalue &result, const jvalue * args)
  305. {
  306. JNIenv->ExceptionClear();
  307. switch (returnType.get()[0])
  308. {
  309. case 'C': result.c = JNIenv->CallStaticCharMethodA(javaClass, javaMethodID, args); break;
  310. case 'Z': result.z = JNIenv->CallStaticBooleanMethodA(javaClass, javaMethodID, args); break;
  311. case 'J': result.j = JNIenv->CallStaticLongMethodA(javaClass, javaMethodID, args); break;
  312. case 'F': result.f = JNIenv->CallStaticFloatMethodA(javaClass, javaMethodID, args); break;
  313. case 'D': result.d = JNIenv->CallStaticDoubleMethodA(javaClass, javaMethodID, args); break;
  314. case 'I': result.i = JNIenv->CallStaticIntMethodA(javaClass, javaMethodID, args); break;
  315. case 'S': result.s = JNIenv->CallStaticShortMethodA(javaClass, javaMethodID, args); break;
  316. case 'B': result.s = JNIenv->CallStaticByteMethodA(javaClass, javaMethodID, args); break;
  317. case '[':
  318. case 'L': result.l = JNIenv->CallStaticObjectMethodA(javaClass, javaMethodID, args); break;
  319. default: throwUnexpected();
  320. }
  321. checkException();
  322. }
  323. inline __int64 getSignedResult(jvalue & result)
  324. {
  325. switch (returnType.get()[0])
  326. {
  327. case 'B': return result.b;
  328. case 'S': return result.s;
  329. case 'I': return result.i;
  330. case 'J': return result.j;
  331. case 'L':
  332. {
  333. // Result should be of class 'Number'
  334. if (!result.l)
  335. return 0;
  336. jmethodID getVal = JNIenv->GetMethodID(JNIenv->GetObjectClass(result.l), "longValue", "()J");
  337. if (!getVal)
  338. throw MakeStringException(MSGAUD_user, 0, "javaembed: Type mismatch on result");
  339. return JNIenv->CallLongMethod(result.l, getVal);
  340. }
  341. default:
  342. throw MakeStringException(MSGAUD_user, 0, "javaembed: Type mismatch on result");
  343. }
  344. }
  345. inline double getDoubleResult(jvalue &result)
  346. {
  347. switch (returnType.get()[0])
  348. {
  349. case 'D': return result.d;
  350. case 'F': return result.f;
  351. case 'L':
  352. {
  353. // Result should be of class 'Number'
  354. if (!result.l)
  355. return 0;
  356. jmethodID getVal = JNIenv->GetMethodID(JNIenv->GetObjectClass(result.l), "doubleValue", "()D");
  357. if (!getVal)
  358. throw MakeStringException(MSGAUD_user, 0, "javaembed: Type mismatch on result");
  359. return JNIenv->CallDoubleMethod(result.l, getVal);
  360. }
  361. default:
  362. throw MakeStringException(MSGAUD_user, 0, "javaembed: Type mismatch on result");
  363. }
  364. }
  365. inline bool getBooleanResult(jvalue &result)
  366. {
  367. switch (returnType.get()[0])
  368. {
  369. case 'Z': return result.z;
  370. case 'L':
  371. {
  372. // Result should be of class 'Boolean'
  373. if (!result.l)
  374. return false;
  375. jmethodID getVal = JNIenv->GetMethodID(JNIenv->GetObjectClass(result.l), "booleanValue", "()Z");
  376. if (!getVal)
  377. throw MakeStringException(MSGAUD_user, 0, "javaembed: Type mismatch on result");
  378. return JNIenv->CallBooleanMethod(result.l, getVal);
  379. }
  380. default:
  381. throw MakeStringException(MSGAUD_user, 0, "javaembed: Type mismatch on result");
  382. }
  383. }
  384. inline void getDataResult(jvalue &result, size32_t &__len, void * &__result)
  385. {
  386. if (strcmp(returnType, "[B")!=0)
  387. throw MakeStringException(MSGAUD_user, 0, "javaembed: Type mismatch on result");
  388. jbyteArray array = (jbyteArray) result.l;
  389. __len = (array != NULL ? JNIenv->GetArrayLength(array) : 0);
  390. __result = (__len > 0 ? rtlMalloc(__len) : NULL);
  391. if (__result)
  392. JNIenv->GetByteArrayRegion(array, 0, __len, (jbyte *) __result);
  393. }
  394. inline void getStringResult(jvalue &result, size32_t &__len, char * &__result)
  395. {
  396. switch (returnType.get()[0])
  397. {
  398. case 'C': // Single char returned, prototyped as STRING or STRING1 in ECL
  399. rtlUnicodeToStrX(__len, __result, 1, &result.c);
  400. break;
  401. case 'L':
  402. {
  403. jstring sresult = (jstring) result.l;
  404. if (sresult)
  405. {
  406. size_t size = JNIenv->GetStringUTFLength(sresult); // in bytes
  407. const char *text = JNIenv->GetStringUTFChars(sresult, NULL);
  408. size32_t chars = rtlUtf8Length(size, text);
  409. rtlUtf8ToStrX(__len, __result, chars, text);
  410. JNIenv->ReleaseStringUTFChars(sresult, text);
  411. }
  412. else
  413. {
  414. __len = 0;
  415. __result = NULL;
  416. }
  417. break;
  418. }
  419. default:
  420. throw MakeStringException(MSGAUD_user, 0, "javaembed: Type mismatch on result");
  421. }
  422. }
  423. inline void getUTF8Result(jvalue &result, size32_t &__chars, char * &__result)
  424. {
  425. switch (returnType.get()[0])
  426. {
  427. case 'C': // Single jchar returned, prototyped as UTF8 in ECL
  428. rtlUnicodeToUtf8X(__chars, __result, 1, &result.c);
  429. break;
  430. case 'L':
  431. {
  432. jstring sresult = (jstring) result.l;
  433. if (sresult)
  434. {
  435. size_t size = JNIenv->GetStringUTFLength(sresult); // Returns length in bytes (not chars)
  436. const char * text = JNIenv->GetStringUTFChars(sresult, NULL);
  437. rtlUtf8ToUtf8X(__chars, __result, rtlUtf8Length(size, text), text);
  438. JNIenv->ReleaseStringUTFChars(sresult, text);
  439. }
  440. else
  441. {
  442. __chars = 0;
  443. __result = NULL;
  444. }
  445. break;
  446. }
  447. default:
  448. throw MakeStringException(MSGAUD_user, 0, "javaembed: Type mismatch on result");
  449. }
  450. }
  451. inline void getUnicodeResult(jvalue &result, size32_t &__chars, UChar * &__result)
  452. {
  453. switch (returnType.get()[0])
  454. {
  455. case 'C': // Single jchar returned, prototyped as UNICODE or UNICODE1 in ECL
  456. rtlUnicodeToUnicodeX(__chars, __result, 1, &result.c);
  457. break;
  458. case 'L':
  459. {
  460. jstring sresult = (jstring) result.l;
  461. if (sresult)
  462. {
  463. size_t size = JNIenv->GetStringUTFLength(sresult); // in bytes
  464. const char *text = JNIenv->GetStringUTFChars(sresult, NULL);
  465. size32_t chars = rtlUtf8Length(size, text);
  466. rtlUtf8ToUnicodeX(__chars, __result, chars, text);
  467. JNIenv->ReleaseStringUTFChars(sresult, text);
  468. }
  469. else
  470. {
  471. __chars = 0;
  472. __result = NULL;
  473. }
  474. break;
  475. }
  476. default:
  477. throw MakeStringException(MSGAUD_user, 0, "javaembed: Type mismatch on result");
  478. }
  479. }
  480. inline void getSetResult(jvalue &result, bool & __isAllResult, size32_t & __resultBytes, void * & __result, int _elemType, size32_t elemSize)
  481. {
  482. if (returnType.get()[0] != '[')
  483. throw MakeStringException(MSGAUD_user, 0, "javaembed: Type mismatch on result (array expected)");
  484. type_t elemType = (type_t) _elemType;
  485. jarray array = (jarray) result.l;
  486. int numResults = (array != NULL ? JNIenv->GetArrayLength(array) : 0);
  487. rtlRowBuilder out;
  488. byte *outData = NULL;
  489. size32_t outBytes = 0;
  490. if (numResults > 0)
  491. {
  492. if (elemSize != UNKNOWN_LENGTH)
  493. {
  494. out.ensureAvailable(numResults * elemSize); // MORE - check for overflow?
  495. outData = out.getbytes();
  496. }
  497. switch(returnType.get()[1])
  498. {
  499. case 'Z':
  500. checkType(type_boolean, sizeof(jboolean), elemType, elemSize);
  501. JNIenv->GetBooleanArrayRegion((jbooleanArray) array, 0, numResults, (jboolean *) outData);
  502. break;
  503. case 'B':
  504. checkType(type_int, sizeof(jbyte), elemType, elemSize);
  505. JNIenv->GetByteArrayRegion((jbyteArray) array, 0, numResults, (jbyte *) outData);
  506. break;
  507. case 'C':
  508. // we COULD map to a set of string1, but is there any point?
  509. throw MakeStringException(0, "javaembed: Return type mismatch (char[] not supported)");
  510. break;
  511. case 'S':
  512. checkType(type_int, sizeof(jshort), elemType, elemSize);
  513. JNIenv->GetShortArrayRegion((jshortArray) array, 0, numResults, (jshort *) outData);
  514. break;
  515. case 'I':
  516. checkType(type_int, sizeof(jint), elemType, elemSize);
  517. JNIenv->GetIntArrayRegion((jintArray) array, 0, numResults, (jint *) outData);
  518. break;
  519. case 'J':
  520. checkType(type_int, sizeof(jlong), elemType, elemSize);
  521. JNIenv->GetLongArrayRegion((jlongArray) array, 0, numResults, (jlong *) outData);
  522. break;
  523. case 'F':
  524. checkType(type_real, sizeof(jfloat), elemType, elemSize);
  525. JNIenv->GetFloatArrayRegion((jfloatArray) array, 0, numResults, (jfloat *) outData);
  526. break;
  527. case 'D':
  528. checkType(type_real, sizeof(jdouble), elemType, elemSize);
  529. JNIenv->GetDoubleArrayRegion((jdoubleArray) array, 0, numResults, (jdouble *) outData);
  530. break;
  531. case 'L':
  532. if (strcmp(returnType, "[Ljava/lang/String;") == 0)
  533. {
  534. for (int i = 0; i < numResults; i++)
  535. {
  536. jstring elem = (jstring) JNIenv->GetObjectArrayElement((jobjectArray) array, i);
  537. size_t lenBytes = JNIenv->GetStringUTFLength(elem); // in bytes
  538. const char *text = JNIenv->GetStringUTFChars(elem, NULL);
  539. switch (elemType)
  540. {
  541. case type_string:
  542. if (elemSize == UNKNOWN_LENGTH)
  543. {
  544. out.ensureAvailable(outBytes + lenBytes + sizeof(size32_t));
  545. outData = out.getbytes() + outBytes;
  546. * (size32_t *) outData = lenBytes;
  547. rtlStrToStr(lenBytes, outData+sizeof(size32_t), lenBytes, text);
  548. outBytes += lenBytes + sizeof(size32_t);
  549. }
  550. else
  551. rtlStrToStr(elemSize, outData, lenBytes, text);
  552. break;
  553. case type_varstring:
  554. if (elemSize == UNKNOWN_LENGTH)
  555. {
  556. out.ensureAvailable(outBytes + lenBytes + 1);
  557. outData = out.getbytes() + outBytes;
  558. rtlStrToVStr(0, outData, lenBytes, text);
  559. outBytes += lenBytes + 1;
  560. }
  561. else
  562. rtlStrToVStr(elemSize, outData, lenBytes, text); // Fixed size null terminated strings... weird.
  563. break;
  564. case type_utf8:
  565. case type_unicode:
  566. {
  567. size32_t numchars = rtlUtf8Length(lenBytes, text);
  568. if (elemType == type_utf8)
  569. {
  570. assertex (elemSize == UNKNOWN_LENGTH);
  571. out.ensureAvailable(outBytes + lenBytes + sizeof(size32_t));
  572. outData = out.getbytes() + outBytes;
  573. * (size32_t *) outData = numchars;
  574. rtlStrToStr(lenBytes, outData+sizeof(size32_t), lenBytes, text);
  575. outBytes += lenBytes + sizeof(size32_t);
  576. }
  577. else
  578. {
  579. if (elemSize == UNKNOWN_LENGTH)
  580. {
  581. // You can't assume that number of chars in utf8 matches number in unicode16 ...
  582. size32_t numchars16;
  583. rtlDataAttr unicode16;
  584. rtlUtf8ToUnicodeX(numchars16, unicode16.refustr(), numchars, text);
  585. out.ensureAvailable(outBytes + numchars16*sizeof(UChar) + sizeof(size32_t));
  586. outData = out.getbytes() + outBytes;
  587. * (size32_t *) outData = numchars16;
  588. rtlUnicodeToUnicode(numchars16, (UChar *) (outData+sizeof(size32_t)), numchars16, unicode16.getustr());
  589. outBytes += numchars16*sizeof(UChar) + sizeof(size32_t);
  590. }
  591. else
  592. rtlUtf8ToUnicode(elemSize / sizeof(UChar), (UChar *) outData, numchars, text);
  593. }
  594. break;
  595. }
  596. default:
  597. JNIenv->ReleaseStringUTFChars(elem, text);
  598. throw MakeStringException(0, "javaembed: Return type mismatch (ECL string type expected)");
  599. }
  600. JNIenv->ReleaseStringUTFChars(elem, text);
  601. JNIenv->DeleteLocalRef(elem);
  602. if (elemSize != UNKNOWN_LENGTH)
  603. outData += elemSize;
  604. }
  605. }
  606. else
  607. throw MakeStringException(0, "javaembed: Return type mismatch (%s[] not supported)", returnType.get()+2);
  608. break;
  609. }
  610. }
  611. __isAllResult = false;
  612. __resultBytes = elemSize == UNKNOWN_LENGTH ? outBytes : elemSize * numResults;
  613. __result = out.detachdata();
  614. }
  615. inline const char *querySignature()
  616. {
  617. return argsig.get();
  618. }
  619. private:
  620. StringAttr returnType;
  621. StringAttr argsig;
  622. StringAttr prevtext;
  623. jclass javaClass;
  624. jmethodID javaMethodID;
  625. bool contextClassLoaderChecked;
  626. };
  627. // Each call to a Java function will use a new JavaEmbedScriptContext object
  628. #define MAX_JNI_ARGS 10
  629. class JavaEmbedImportContext : public CInterfaceOf<IEmbedFunctionContext>
  630. {
  631. public:
  632. JavaEmbedImportContext(JavaThreadContext *_sharedCtx, const char *options)
  633. : sharedCtx(_sharedCtx)
  634. {
  635. argcount = 0;
  636. argsig = NULL;
  637. // Create a new frame for local references and increase the capacity
  638. // of those references to 64 (default is 16)
  639. sharedCtx->JNIenv->PushLocalFrame(64);
  640. }
  641. ~JavaEmbedImportContext()
  642. {
  643. // Pop local reference frame; explicitly frees all local
  644. // references made during that frame's lifetime
  645. sharedCtx->JNIenv->PopLocalFrame(NULL);
  646. }
  647. virtual bool getBooleanResult()
  648. {
  649. return sharedCtx->getBooleanResult(result);
  650. }
  651. virtual void getDataResult(size32_t &__len, void * &__result)
  652. {
  653. sharedCtx->getDataResult(result, __len, __result);
  654. }
  655. virtual double getRealResult()
  656. {
  657. return sharedCtx->getDoubleResult(result);
  658. }
  659. virtual __int64 getSignedResult()
  660. {
  661. return sharedCtx->getSignedResult(result);
  662. }
  663. virtual unsigned __int64 getUnsignedResult()
  664. {
  665. throw MakeStringException(MSGAUD_user, 0, "javaembed: Unsigned results not supported"); // Java doesn't support unsigned
  666. }
  667. virtual void getStringResult(size32_t &__len, char * &__result)
  668. {
  669. sharedCtx->getStringResult(result, __len, __result);
  670. }
  671. virtual void getUTF8Result(size32_t &__chars, char * &__result)
  672. {
  673. sharedCtx->getUTF8Result(result, __chars, __result);
  674. }
  675. virtual void getUnicodeResult(size32_t &__chars, UChar * &__result)
  676. {
  677. sharedCtx->getUnicodeResult(result, __chars, __result);
  678. }
  679. virtual void getSetResult(bool & __isAllResult, size32_t & __resultBytes, void * & __result, int elemType, size32_t elemSize)
  680. {
  681. sharedCtx->getSetResult(result, __isAllResult, __resultBytes, __result, elemType, elemSize);
  682. }
  683. virtual IRowStream *getDatasetResult(IEngineRowAllocator * _resultAllocator)
  684. {
  685. UNIMPLEMENTED;
  686. }
  687. virtual byte * getRowResult(IEngineRowAllocator * _resultAllocator)
  688. {
  689. UNIMPLEMENTED;
  690. }
  691. virtual size32_t getTransformResult(ARowBuilder & builder)
  692. {
  693. UNIMPLEMENTED;
  694. }
  695. virtual void bindBooleanParam(const char *name, bool val)
  696. {
  697. if (*argsig != 'B')
  698. typeError("BOOLEAN");
  699. argsig++;
  700. jvalue v;
  701. v.z = val;
  702. addArg(v);
  703. }
  704. virtual void bindDataParam(const char *name, size32_t len, const void *val)
  705. {
  706. if (argsig[0] != '[' || argsig[1] != 'B')
  707. typeError("DATA");
  708. argsig += 2;
  709. jvalue v;
  710. jbyteArray javaData = sharedCtx->JNIenv->NewByteArray(len);
  711. sharedCtx->JNIenv->SetByteArrayRegion(javaData, 0, len, (jbyte *) val);
  712. v.l = javaData;
  713. addArg(v);
  714. }
  715. virtual void bindRealParam(const char *name, double val)
  716. {
  717. jvalue v;
  718. switch(*argsig)
  719. {
  720. case 'D':
  721. v.d = val;
  722. break;
  723. case 'F':
  724. v.f = val;
  725. break;
  726. default:
  727. typeError("REAL");
  728. break;
  729. }
  730. argsig++;
  731. addArg(v);
  732. }
  733. virtual void bindSignedParam(const char *name, __int64 val)
  734. {
  735. jvalue v;
  736. switch(*argsig)
  737. {
  738. case 'I':
  739. v.i = val;
  740. break;
  741. case 'J':
  742. v.j = val;
  743. break;
  744. case 'S':
  745. v.s = val;
  746. break;
  747. case 'B':
  748. v.b = val;
  749. break;
  750. default:
  751. typeError("INTEGER");
  752. break;
  753. }
  754. argsig++;
  755. addArg(v);
  756. }
  757. virtual void bindUnsignedParam(const char *name, unsigned __int64 val)
  758. {
  759. throw MakeStringException(MSGAUD_user, 0, "javaembed: Unsigned parameters not supported"); // Java doesn't support unsigned
  760. }
  761. virtual void bindStringParam(const char *name, size32_t len, const char *val)
  762. {
  763. jvalue v;
  764. switch(*argsig)
  765. {
  766. case 'C':
  767. rtlStrToUnicode(1, &v.c, len, val);
  768. argsig++;
  769. break;
  770. case 'L':
  771. if (strncmp(argsig, "Ljava/lang/String;", 18) == 0)
  772. {
  773. argsig += 18;
  774. unsigned unicodeChars;
  775. UChar *unicode;
  776. rtlStrToUnicodeX(unicodeChars, unicode, len, val);
  777. v.l = sharedCtx->JNIenv->NewString(unicode, unicodeChars);
  778. rtlFree(unicode);
  779. break;
  780. }
  781. // fall into ...
  782. default:
  783. typeError("STRING");
  784. break;
  785. }
  786. addArg(v);
  787. }
  788. virtual void bindVStringParam(const char *name, const char *val)
  789. {
  790. bindStringParam(name, strlen(val), val);
  791. }
  792. virtual void bindUTF8Param(const char *name, size32_t numchars, const char *val)
  793. {
  794. jvalue v;
  795. switch(*argsig)
  796. {
  797. case 'C':
  798. rtlUtf8ToUnicode(1, &v.c, numchars, val);
  799. argsig++;
  800. break;
  801. case 'L':
  802. if (strncmp(argsig, "Ljava/lang/String;", 18) == 0)
  803. {
  804. argsig += 18;
  805. unsigned unicodeChars;
  806. UChar *unicode;
  807. rtlUtf8ToUnicodeX(unicodeChars, unicode, numchars, val);
  808. v.l = sharedCtx->JNIenv->NewString(unicode, unicodeChars);
  809. rtlFree(unicode);
  810. break;
  811. }
  812. // fall into ...
  813. default:
  814. typeError("UTF8");
  815. break;
  816. }
  817. addArg(v);
  818. }
  819. virtual void bindUnicodeParam(const char *name, size32_t numchars, const UChar *val)
  820. {
  821. jvalue v;
  822. switch(*argsig)
  823. {
  824. case 'C':
  825. rtlUnicodeToUnicode(1, &v.c, numchars, val);
  826. argsig++;
  827. break;
  828. case 'L':
  829. if (strncmp(argsig, "Ljava/lang/String;", 18) == 0)
  830. {
  831. argsig += 18;
  832. v.l = sharedCtx->JNIenv->NewString(val, numchars);
  833. break;
  834. }
  835. // fall into ...
  836. default:
  837. typeError("UNICODE");
  838. break;
  839. }
  840. addArg(v);
  841. }
  842. virtual void bindSetParam(const char *name, int _elemType, size32_t elemSize, bool isAll, size32_t totalBytes, void *setData)
  843. {
  844. jvalue v;
  845. if (*argsig != '[')
  846. typeError("SET");
  847. argsig++;
  848. type_t elemType = (type_t) _elemType;
  849. int numElems = totalBytes / elemSize;
  850. switch(*argsig)
  851. {
  852. case 'Z':
  853. checkType(type_boolean, sizeof(jboolean), elemType, elemSize);
  854. v.l = sharedCtx->JNIenv->NewBooleanArray(numElems);
  855. sharedCtx->JNIenv->SetBooleanArrayRegion((jbooleanArray) v.l, 0, numElems, (jboolean *) setData);
  856. break;
  857. case 'B':
  858. checkType(type_int, sizeof(jbyte), elemType, elemSize);
  859. v.l = sharedCtx->JNIenv->NewByteArray(numElems);
  860. sharedCtx->JNIenv->SetByteArrayRegion((jbyteArray) v.l, 0, numElems, (jbyte *) setData);
  861. break;
  862. case 'C':
  863. // we COULD map to a set of string1, but is there any point?
  864. typeError("");
  865. break;
  866. case 'S':
  867. checkType(type_int, sizeof(jshort), elemType, elemSize);
  868. v.l = sharedCtx->JNIenv->NewShortArray(numElems);
  869. sharedCtx->JNIenv->SetShortArrayRegion((jshortArray) v.l, 0, numElems, (jshort *) setData);
  870. break;
  871. case 'I':
  872. checkType(type_int, sizeof(jint), elemType, elemSize);
  873. v.l = sharedCtx->JNIenv->NewIntArray(numElems);
  874. sharedCtx->JNIenv->SetIntArrayRegion((jintArray) v.l, 0, numElems, (jint *) setData);
  875. break;
  876. case 'J':
  877. checkType(type_int, sizeof(jlong), elemType, elemSize);
  878. v.l = sharedCtx->JNIenv->NewLongArray(numElems);
  879. sharedCtx->JNIenv->SetLongArrayRegion((jlongArray) v.l, 0, numElems, (jlong *) setData);
  880. break;
  881. case 'F':
  882. checkType(type_real, sizeof(jfloat), elemType, elemSize);
  883. v.l = sharedCtx->JNIenv->NewFloatArray(numElems);
  884. sharedCtx->JNIenv->SetFloatArrayRegion((jfloatArray) v.l, 0, numElems, (jfloat *) setData);
  885. break;
  886. case 'D':
  887. checkType(type_real, sizeof(jdouble), elemType, elemSize);
  888. v.l = sharedCtx->JNIenv->NewDoubleArray(numElems);
  889. sharedCtx->JNIenv->SetDoubleArrayRegion((jdoubleArray) v.l, 0, numElems, (jdouble *) setData);
  890. break;
  891. case 'L':
  892. if (strncmp(argsig, "Ljava/lang/String;", 18) == 0)
  893. {
  894. argsig += 17; // Yes, 17, because we increment again at the end of the case
  895. const byte *inData = (const byte *) setData;
  896. const byte *endData = inData + totalBytes;
  897. if (elemSize == UNKNOWN_LENGTH)
  898. {
  899. numElems = 0;
  900. // Will need 2 passes to work out how many elements there are in the set :(
  901. while (inData < endData)
  902. {
  903. int thisSize;
  904. switch (elemType)
  905. {
  906. case type_varstring:
  907. thisSize = strlen((const char *) inData) + 1;
  908. break;
  909. case type_string:
  910. thisSize = * (size32_t *) inData + sizeof(size32_t);
  911. break;
  912. case type_unicode:
  913. thisSize = (* (size32_t *) inData) * sizeof(UChar) + sizeof(size32_t);
  914. break;
  915. case type_utf8:
  916. thisSize = rtlUtf8Size(* (size32_t *) inData, inData + sizeof(size32_t)) + sizeof(size32_t);;
  917. break;
  918. default:
  919. typeError("STRING");
  920. }
  921. inData += thisSize;
  922. numElems++;
  923. }
  924. inData = (const byte *) setData;
  925. }
  926. int idx = 0;
  927. v.l = sharedCtx->JNIenv->NewObjectArray(numElems, sharedCtx->JNIenv->FindClass("java/lang/String"), NULL);
  928. while (inData < endData)
  929. {
  930. jstring thisElem;
  931. size32_t thisSize = elemSize;
  932. switch (elemType)
  933. {
  934. case type_varstring:
  935. {
  936. size32_t numChars = strlen((const char *) inData);
  937. unsigned unicodeChars;
  938. rtlDataAttr unicode;
  939. rtlStrToUnicodeX(unicodeChars, unicode.refustr(), numChars, (const char *) inData);
  940. thisElem = sharedCtx->JNIenv->NewString(unicode.getustr(), unicodeChars);
  941. if (elemSize == UNKNOWN_LENGTH)
  942. thisSize = numChars + 1;
  943. break;
  944. }
  945. case type_string:
  946. {
  947. if (elemSize == UNKNOWN_LENGTH)
  948. {
  949. thisSize = * (size32_t *) inData;
  950. inData += sizeof(size32_t);
  951. }
  952. unsigned unicodeChars;
  953. rtlDataAttr unicode;
  954. rtlStrToUnicodeX(unicodeChars, unicode.refustr(), thisSize, (const char *) inData);
  955. thisElem = sharedCtx->JNIenv->NewString(unicode.getustr(), unicodeChars);
  956. break;
  957. }
  958. case type_unicode:
  959. {
  960. if (elemSize == UNKNOWN_LENGTH)
  961. {
  962. thisSize = (* (size32_t *) inData) * sizeof(UChar); // NOTE - it's in chars...
  963. inData += sizeof(size32_t);
  964. }
  965. thisElem = sharedCtx->JNIenv->NewString((const UChar *) inData, thisSize / sizeof(UChar));
  966. //checkJPythonError();
  967. break;
  968. }
  969. case type_utf8:
  970. {
  971. assertex (elemSize == UNKNOWN_LENGTH);
  972. size32_t numChars = * (size32_t *) inData;
  973. inData += sizeof(size32_t);
  974. unsigned unicodeChars;
  975. rtlDataAttr unicode;
  976. rtlUtf8ToUnicodeX(unicodeChars, unicode.refustr(), numChars, (const char *) inData);
  977. thisElem = sharedCtx->JNIenv->NewString(unicode.getustr(), unicodeChars);
  978. thisSize = rtlUtf8Size(numChars, inData);
  979. break;
  980. }
  981. default:
  982. typeError("STRING");
  983. }
  984. sharedCtx->checkException();
  985. inData += thisSize;
  986. sharedCtx->JNIenv->SetObjectArrayElement((jobjectArray) v.l, idx, thisElem);
  987. sharedCtx->JNIenv->DeleteLocalRef(thisElem);
  988. idx++;
  989. }
  990. }
  991. else
  992. typeError("");
  993. break;
  994. }
  995. argsig++;
  996. addArg(v);
  997. }
  998. virtual void importFunction(size32_t lenChars, const char *utf)
  999. {
  1000. sharedCtx->importFunction(lenChars, utf);
  1001. argsig = sharedCtx->querySignature();
  1002. assertex(*argsig == '(');
  1003. argsig++;
  1004. }
  1005. virtual void callFunction()
  1006. {
  1007. sharedCtx->callFunction(result, args);
  1008. }
  1009. virtual void compileEmbeddedScript(size32_t lenChars, const char *script)
  1010. {
  1011. throwUnexpected(); // The java language helper supports only imported functions, not embedding java code in ECL.
  1012. }
  1013. protected:
  1014. JavaThreadContext *sharedCtx;
  1015. jvalue result;
  1016. private:
  1017. void typeError(const char *ECLtype) __attribute__((noreturn))
  1018. {
  1019. const char *javaType;
  1020. int javaLen = 0;
  1021. switch (*argsig)
  1022. {
  1023. case 'Z': javaType = "boolean"; break;
  1024. case 'B': javaType = "byte"; break;
  1025. case 'C': javaType = "char"; break;
  1026. case 'S': javaType = "short"; break;
  1027. case 'I': javaType = "int"; break;
  1028. case 'J': javaType = "long"; break;
  1029. case 'F': javaType = "float"; break;
  1030. case 'D': javaType = "double"; break;
  1031. case '[': javaType = "array"; break;
  1032. case 'L':
  1033. {
  1034. javaType = argsig+1;
  1035. const char *semi = strchr(argsig, ';');
  1036. if (semi)
  1037. javaLen = semi - javaType;
  1038. break;
  1039. }
  1040. case ')':
  1041. throw MakeStringException(0, "javaembed: Too many ECL parameters passed for Java signature %s", sharedCtx->querySignature());
  1042. default:
  1043. throw MakeStringException(0, "javaembed: Unrecognized character %c in java signature %s", *argsig, sharedCtx->querySignature());
  1044. }
  1045. if (!javaLen)
  1046. javaLen = strlen(argsig);
  1047. throw MakeStringException(0, "javaembed: ECL type %s cannot be passed to Java type %.*s", ECLtype, javaLen, javaType);
  1048. }
  1049. void addArg(jvalue &arg)
  1050. {
  1051. assertex(argcount < MAX_JNI_ARGS);
  1052. args[argcount] = arg;
  1053. argcount++;
  1054. }
  1055. jvalue args[MAX_JNI_ARGS];
  1056. int argcount;
  1057. const char *argsig;
  1058. };
  1059. static __thread JavaThreadContext* threadContext; // We reuse per thread, for speed
  1060. static __thread ThreadTermFunc threadHookChain;
  1061. static void releaseContext()
  1062. {
  1063. if (threadContext)
  1064. {
  1065. delete threadContext;
  1066. threadContext = NULL;
  1067. }
  1068. if (threadHookChain)
  1069. {
  1070. (*threadHookChain)();
  1071. threadHookChain = NULL;
  1072. }
  1073. }
  1074. class JavaEmbedContext : public CInterfaceOf<IEmbedContext>
  1075. {
  1076. public:
  1077. virtual IEmbedFunctionContext *createFunctionContext(bool isImport, const char *options)
  1078. {
  1079. if (!threadContext)
  1080. {
  1081. threadContext = new JavaThreadContext;
  1082. threadHookChain = addThreadTermFunc(releaseContext);
  1083. }
  1084. assertex(isImport);
  1085. return new JavaEmbedImportContext(threadContext, options);
  1086. }
  1087. };
  1088. extern IEmbedContext* getEmbedContext()
  1089. {
  1090. return new JavaEmbedContext;
  1091. }
  1092. } // namespace