lucene-pylucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Toivo Henningsson <toivo.hennings...@modelon.com>
Subject Using JCC from pure C++, patches
Date Tue, 05 Feb 2013 10:23:05 GMT
Hi!

I've been using JCC to interact with Java code from C++ for a few weeks now. (no python involved)
It's been working well, but I've had to patch the C++ code in sources/ a little. I thought
I'd share the patches; hopefully at least some of them can make it into mainline JCC. I pulled
the source from http://svn.apache.org/repos/asf/lucene/pylucene/trunk/jcc/jcc/sources/ this
morning, so I hope that the files should be up to date.

=======================================================
First, in JArray.h there was a missing typedef when not using python:

--- JArray.h.0    2013-02-05 10:49:05.650684100 +0100
+++ JArray.h.1 2013-02-05 10:49:40.901160300 +0100
@@ -37,6 +37,8 @@
extern PyTypeObject *PY_TYPE(JArrayLong);
extern PyTypeObject *PY_TYPE(JArrayShort);
+#else
+typedef int Py_ssize_t;
#endif
 #include "JCCEnv.h"

=====================================================================
There was a missing #include in JCCEnv.cpp (free, realloc, and calloc were not found):

--- JCCEnv.cpp.0                         2013-02-05 10:49:22.497323300 +0100
+++ JCCEnv.cpp.1                       2013-02-05 10:49:48.613639700 +0100
@@ -13,6 +13,7 @@
  */
 #include <map>
+#include <stdlib.h>
#include <string.h>
#include <jni.h>

========================================================================
I got a slew of warnings for non-const char pointers initialized with string literals, this
patch seems to fix it (created on top of the last patch):

--- JCCEnv.cpp.1                         2013-02-05 10:49:48.613639700 +0100
+++ JCCEnv.cpp.2                       2013-02-05 10:52:20.451420600 +0100
@@ -818,10 +818,10 @@
     jmethodID mu = vm_env->GetMethodID(_fil, "toURL", "()Ljava/net/URL;");
     jmethodID ma = vm_env->GetMethodID(_ucl, "addURL", "(Ljava/net/URL;)V");
#if defined(_MSC_VER) || defined(__WIN32)
-    char *pathsep = ";";
+    const char *pathsep = ";";
     char *path = _strdup(classPath);
#else
-    char *pathsep = ":";
+    const char *pathsep = ":";
     char *path = strdup(classPath);
#endif
@@ -848,9 +848,9 @@
     jmethodID gu = vm_env->GetMethodID(_ucl, "getURLs", "()[Ljava/net/URL;");
     jmethodID gp = vm_env->GetMethodID(_url, "getPath", "()Ljava/lang/String;");
#if defined(_MSC_VER) || defined(__WIN32)
-    char *pathsep = ";";
+    const char *pathsep = ";";
#else
-    char *pathsep = ":";
+    const char *pathsep = ":";
#endif
     jobjectArray array = (jobjectArray)
         vm_env->CallObjectMethod(classLoader, gu);

========================================================================
Lastly, I got some pretty nasty crashes when I failed to supply a proper classpath to the
JVM. The following patch to JCCEnv.cpp (built on top of the previous one) exits with an error
instead. You might want to handle the error in another way, but I do think that there is a
value in catching it:

--- JCCEnv.cpp.2                         2013-02-05 10:52:20.451420600 +0100
+++ JCCEnv.cpp.3                       2013-02-05 11:00:24.911939300 +0100
@@ -15,6 +15,7 @@
#include <map>
#include <stdlib.h>
#include <string.h>
+#include <iostream>
#include <jni.h>
 #include "JCCEnv.h"
@@ -259,8 +260,13 @@
     {
         JNIEnv *vm_env = get_vm_env();
-        if (vm_env)
+        if (vm_env) {
             cls = vm_env->FindClass(className);
+                             if (cls == NULL) {
+                                                    std::cerr << "Could not find class
" << className << std::endl;
+                                                    exit(1);
+                             }
+                         }
#ifdef PYTHON
         else
         {

Best,

Toivo Henningsson, PhD
Senior Software Engineer
Simulation & Optimization Technology

Phone direct: +46 46 286 22 11
Email: toivo.henningsson@modelon.com<mailto:toivo.henningsson@modelon.com>

[Description: Description: Modelon_2011_Gradient_RGB_400]
________________________________
Modelon AB
Ideon Science Park
SE-223 70 Lund, Sweden

Phone: +46 46 286 2200
Fax: +46 46 286 2201


Web: http://www.modelon.com<http://www.modelon.com/>



This email and any attachments are intended solely for the use of the individual or entity
to whom it is addressed and may be confidential and/or privileged. If you are not one of the
named recipients or have received this email in error, (i) you should not read, disclose,
or copy it, (ii) please notify sender of your receipt by reply email and delete this email
and all attachments, (iii) Modelon does not accept or assume any liability or responsibility
for any use of or reliance on this email.

Mime
  • Unnamed multipart/related (inline, None, 0 bytes)
View raw message