summaryrefslogtreecommitdiff
path: root/qpid/cpp/bindings/swig_java_cpp_helper.i
blob: 7a31ff62212f3f319b6f83244d5db6e0cb47a7c6 (plain)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

/*
 * ======================== Contents on this file ===========================
 * 1. Static C++ Helper Methods.
 * 2. JNICALL JNI_OnLoad method for initializing global variables.
 * 2. Wrappers for C++ Objects (handwritten c++ classes).
 * 3. SWIG Interface definitions for the above classes.
 * ==========================================================================
 */




/*
 * ===================== Static C++ Helper Methods ==========================
 * Static helper methods in support of typemaps,
 * for converting between C++ and Java.
 * ==========================================================================
 */

%begin %{

/*
 * These are initialized during library loading via JNI_OnLoad.
 * This is done for performance reasons.
 * Thee jclass variables are marked as global references.
 * The jmethod variables remain valid until the corresponding jclass ref is valid.
 */
static JavaVM* cachedJVM=0;

static std::string STRING_ENCODING("utf8");

static jclass JAVA_STRING_CLASS;

static jclass JAVA_BOOLEAN_CLASS;
static jmethodID JAVA_BOOLEAN_CTOR;
static jmethodID JAVA_BOOLEAN_VALUE_METHOD;

static jclass JAVA_BYTE_CLASS;
static jmethodID JAVA_BYTE_CTOR;
static jmethodID JAVA_BYTE_VALUE_METHOD;

static jclass JAVA_SHORT_CLASS;
static jmethodID JAVA_SHORT_CTOR;
static jmethodID JAVA_SHORT_VALUE_METHOD;

static jclass JAVA_INT_CLASS;
static jmethodID JAVA_INT_CTOR;
static jmethodID JAVA_INT_VALUE_METHOD;

static jclass JAVA_LONG_CLASS;
static jmethodID JAVA_LONG_CTOR;
static jmethodID JAVA_LONG_VALUE_METHOD;

static jclass JAVA_FLOAT_CLASS;
static jmethodID JAVA_FLOAT_CTOR;
static jmethodID JAVA_FLOAT_VALUE_METHOD;

static jclass JAVA_DOUBLE_CLASS;
static jmethodID JAVA_DOUBLE_CTOR;
static jmethodID JAVA_DOUBLE_VALUE_METHOD;

static jclass JAVA_ILLEGAL_ARGUMENT_EXP;
static jclass JAVA_JNI_LAYER_EXP;
static jmethodID JAVA_JNI_LAYER_EXP_CTOR1; // takes a msg.
static jmethodID JAVA_JNI_LAYER_EXP_CTOR2; // takes a msg and a throwable.

static jobject createGlobalRef(JNIEnv* env,jobject obj)
{
    return env->NewGlobalRef(obj);
}

static jclass findClass(JNIEnv* env,const char* name)
{
    return env->FindClass(name);
}

static jmethodID getMethodID(JNIEnv* env, jclass clazz, const char* name, const char* sig)
{
    return env->GetMethodID(clazz,name,sig);
}

// TODO link up with log4j
static void printLog(const char* msg)
{
    std::cout << msg << std::endl;
}

/*
 * ========================= init() for the module ==========================
 *
 * Pre loads class, method and except objects for performance.
 * WARNING *** This needs to be called immediately after loading the library.
 *
 * ==========================================================================
 */
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved)
{
   cachedJVM = jvm;
   JNIEnv* env;
   if (jvm->GetEnv((void **)&env, JNI_VERSION_1_4)) {
         return JNI_ERR; /* JNI version not supported */
   }

   printLog("--- ** Initializing cqpid_java ** ---");
   if (!env)
   {
      printLog("JNI env ref is null!, aborting");
      printLog("--- ** cqpid_java library loading failed! ** ---");
      return JNI_ERR;
   }

   // The standard class loading should not fail :)
   JAVA_STRING_CLASS = static_cast<jclass>(createGlobalRef(env,findClass(env,"java/lang/String")));

   JAVA_BOOLEAN_CLASS = static_cast<jclass>(createGlobalRef(env,findClass(env,"java/lang/Boolean")));
   JAVA_BOOLEAN_CTOR = getMethodID(env,JAVA_BOOLEAN_CLASS, "<init>", "(Z)V");
   JAVA_BOOLEAN_VALUE_METHOD = getMethodID(env,JAVA_BOOLEAN_CLASS,"booleanValue", "()Z");

   JAVA_BYTE_CLASS = static_cast<jclass>(createGlobalRef(env,findClass(env,"java/lang/Byte")));
   JAVA_BYTE_CTOR = getMethodID(env,JAVA_BYTE_CLASS, "<init>", "(B)V");
   JAVA_BYTE_VALUE_METHOD = getMethodID(env,JAVA_BYTE_CLASS,"byteValue", "()B");

   JAVA_SHORT_CLASS = static_cast<jclass>(createGlobalRef(env,findClass(env,"java/lang/Short")));
   JAVA_SHORT_CTOR = getMethodID(env,JAVA_SHORT_CLASS, "<init>", "(S)V");
   JAVA_SHORT_VALUE_METHOD = getMethodID(env,JAVA_SHORT_CLASS,"shortValue", "()S");

   JAVA_INT_CLASS = static_cast<jclass>(createGlobalRef(env,findClass(env,"java/lang/Integer")));
   JAVA_INT_CTOR = getMethodID(env,JAVA_INT_CLASS, "<init>", "(I)V");
   JAVA_INT_VALUE_METHOD = getMethodID(env,JAVA_INT_CLASS,"intValue", "()I");

   JAVA_LONG_CLASS = static_cast<jclass>(createGlobalRef(env,findClass(env,"java/lang/Long")));
   JAVA_LONG_CTOR = getMethodID(env,JAVA_LONG_CLASS, "<init>", "(J)V");
   JAVA_LONG_VALUE_METHOD = getMethodID(env,JAVA_LONG_CLASS,"longValue", "()J");

   JAVA_DOUBLE_CLASS = static_cast<jclass>(createGlobalRef(env,findClass(env,"java/lang/Double")));
   JAVA_DOUBLE_CTOR = getMethodID(env,JAVA_DOUBLE_CLASS, "<init>", "(D)V");
   JAVA_DOUBLE_VALUE_METHOD = getMethodID(env,JAVA_DOUBLE_CLASS,"doubleValue", "()D");

   JAVA_FLOAT_CLASS = static_cast<jclass>(createGlobalRef(env,findClass(env,"java/lang/Float")));
   JAVA_FLOAT_CTOR = getMethodID(env,JAVA_FLOAT_CLASS, "<init>", "(F)V");
   JAVA_FLOAT_VALUE_METHOD = getMethodID(env,JAVA_FLOAT_CLASS,"floatValue", "()F");

   JAVA_ILLEGAL_ARGUMENT_EXP = static_cast<jclass>(createGlobalRef(env,findClass(env,"java/lang/IllegalArgumentException")));
   JAVA_JNI_LAYER_EXP = static_cast<jclass>(createGlobalRef(env,findClass(env,"org/apache/qpid/messaging/cpp/JNILayerException")));
   JAVA_JNI_LAYER_EXP_CTOR1 = getMethodID(env,JAVA_JNI_LAYER_EXP, "<init>", "(Ljava/lang/String;)V");
   JAVA_JNI_LAYER_EXP_CTOR2 = getMethodID(env,JAVA_JNI_LAYER_EXP, "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V");

   if (env->ExceptionCheck())
   {
      // The JVM will throw an exception on the Java side.
      printLog("Loading of one or more class or constructor definitions failed. See exception for details");
      printLog("--- ** cqpid_java library loading failed! ** ---");
      return JNI_ERR;
   }

   printLog("--- ** cqpid_java library was loaded successfully ** ---");
   return JNI_VERSION_1_4;
}

static JNIEnv* getJNIEnvPointer()
{
    JNIEnv* env;
    cachedJVM->GetEnv((void **)&env, JNI_VERSION_1_4);
    return env;
}

static bool checkAndThrowJNILaylerException(JNIEnv* env, const char* msg)
{
    jthrowable cause = env->ExceptionOccurred();
    if (cause)
    {
        jthrowable ex = static_cast<jthrowable>(env->NewObject(JAVA_JNI_LAYER_EXP, JAVA_JNI_LAYER_EXP_CTOR2, msg, cause));
        env->Throw(ex);
        env->DeleteLocalRef(cause);
        env->DeleteLocalRef(ex);
        return true;
    }
    return false;
}

static void throwJNILayerException(JNIEnv* env, const char* msg)
{
    jthrowable ex = static_cast<jthrowable>(env->NewObject(JAVA_JNI_LAYER_EXP, JAVA_JNI_LAYER_EXP_CTOR1, msg));
    env->Throw(ex);
    env->DeleteLocalRef(ex);
}

static jobject newJavaBoolean(JNIEnv* env, jboolean arg)
{
    return env->NewObject(JAVA_BOOLEAN_CLASS, JAVA_BOOLEAN_CTOR,arg);
}

static jobject newJavaByte(JNIEnv* env, jbyte arg)
{
    return env->NewObject(JAVA_BYTE_CLASS, JAVA_BYTE_CTOR,arg);
}

static jobject newJavaShort(JNIEnv* env, jshort arg)
{
    return env->NewObject(JAVA_SHORT_CLASS, JAVA_SHORT_CTOR,arg);
}

static jobject newJavaInt(JNIEnv* env, jint arg)
{
    return env->NewObject(JAVA_INT_CLASS, JAVA_INT_CTOR,arg);
}

static jobject newJavaLong(JNIEnv* env, jlong arg)
{
    return env->NewObject(JAVA_LONG_CLASS, JAVA_LONG_CTOR,arg);
}

static jobject newJavaFloat(JNIEnv* env, jfloat arg)
{
    return env->NewObject(JAVA_FLOAT_CLASS, JAVA_FLOAT_CTOR,arg);
}

static jobject newJavaDouble(JNIEnv* env, jdouble arg)
{
    return env->NewObject(JAVA_DOUBLE_CLASS, JAVA_DOUBLE_CTOR,arg);
}

static jstring newJavaString(JNIEnv* env, const std::string val)
{
    return env->NewStringUTF(val.data());
}

class NativeStringCreator
{
    public:
       NativeStringCreator(JNIEnv* env, jstring javaString)
          : env_(env), javaString_(javaString), data_(0),str_()
       {
           data_ = (const char *)env_->GetStringUTFChars(javaString_, 0);
           size_t count = (size_t)env_->GetStringUTFLength(javaString_);
           str_.assign(data_,count);
       }

       std::string getStr() { return str_; }

       ~NativeStringCreator()
       {
           env_->ReleaseStringUTFChars(javaString_,data_);
       }

    private:
       JNIEnv* env_;
       jstring javaString_;
       const char* data_;
       std::string str_;
};

//Only primitive types, Strings and UUIDs are supported. No maps or lists.
static qpid::types::Variant convertJavaObjectToVariant(JNIEnv* env, jobject obj)
{
   qpid::types::Variant result;

   if (env->IsInstanceOf(obj,JAVA_STRING_CLASS))
   {
       NativeStringCreator strCreator(env,(jstring)obj);
       result = qpid::types::Variant(strCreator.getStr());
       result.setEncoding(STRING_ENCODING);
   }
   else if (env->IsInstanceOf(obj,JAVA_LONG_CLASS))
   {
       uint64_t v = env->CallLongMethod(obj,JAVA_LONG_VALUE_METHOD);
       result = qpid::types::Variant(v);
   }
   else if (env->IsInstanceOf(obj,JAVA_INT_CLASS))
   {
       uint32_t v = env->CallIntMethod(obj,JAVA_INT_VALUE_METHOD);
       result = qpid::types::Variant(v);
   }
   else if (env->IsInstanceOf(obj,JAVA_SHORT_CLASS))
   {
       uint16_t v = env->CallShortMethod(obj,JAVA_SHORT_VALUE_METHOD);
       result = qpid::types::Variant(v);
   }
   else if (env->IsInstanceOf(obj,JAVA_BYTE_CLASS))
   {
       uint8_t v = env->CallByteMethod(obj,JAVA_BYTE_VALUE_METHOD);
       result = qpid::types::Variant(v);
   }
   else if (env->IsInstanceOf(obj,JAVA_BOOLEAN_CLASS))
   {
       bool v = env->CallBooleanMethod(obj,JAVA_BOOLEAN_VALUE_METHOD);
       result = qpid::types::Variant(v);
   }
   else if (env->IsInstanceOf(obj,JAVA_DOUBLE_CLASS))
   {
       double v = env->CallDoubleMethod(obj,JAVA_DOUBLE_VALUE_METHOD);
       result = qpid::types::Variant(v);
   }
   else if (env->IsInstanceOf(obj,JAVA_FLOAT_CLASS))
   {
       float v = env->CallFloatMethod(obj,JAVA_FLOAT_VALUE_METHOD);
       result = qpid::types::Variant(v);
   }
   else
   {
       env->ThrowNew(JAVA_ILLEGAL_ARGUMENT_EXP,"Only primitive types and strings are allowed");
   }

   checkAndThrowJNILaylerException(env,"Exception occured when converting Java object to Variant");

   return result;
}
%}

/*
 * ====================== Wrappers for C++ Objects ==========================
 * Defines wrappers for C++ objects in support of typemaps,
 * for converting between C++ and Java.
 *
 * Note: If you define Classes that needs a corresponding Java class, you need
 * to add the function definitions under the swig section defined
 * at the end of this file.
 * ==========================================================================
 */

%inline %{
/* ======================== ReadOnlyVariantMapWrapper ==================== */

/**
 * Provides a read-only view of the underlying Variant::Map
 */
class ReadOnlyVariantMapWrapper {

  public:
    ReadOnlyVariantMapWrapper(qpid::types::Variant::Map& map);
    ~ReadOnlyVariantMapWrapper();
    jobject get(const std::string& key) const;
    jboolean containsKey (const std::string& key) const;
    jboolean isEmpty() const;
    // keys_ is populated on demand (i.e if the keys() method is called).
    // since this method modifies keys_ it cannot be marked const.
    jobjectArray keys();
    jint size() const;

  private:
    qpid::types::Variant::Map varMap_;
    jobjectArray keys_;
};

/* ======================== WriteOnlyVariantMapWrapper ==================== */
/**
 * Provides a write-only Variant::Map
 * This is only intended to be used with code that is not in the critical path.
 * Ex: not be used with setting message properties.
 * This is intended to be used when setting connection properties
 * or address properties.
 */
class WriteOnlyVariantMapWrapper {

  public:
    WriteOnlyVariantMapWrapper();
    ~WriteOnlyVariantMapWrapper(){}
    void put(const std::string& key, jobject obj);
    qpid::types::Variant::Map getVariantMap() const { return varMap_; }

  private:
    qpid::types::Variant::Map varMap_;
};


ReadOnlyVariantMapWrapper::ReadOnlyVariantMapWrapper(qpid::types::Variant::Map& map): varMap_(map), keys_(0){}

ReadOnlyVariantMapWrapper::~ReadOnlyVariantMapWrapper()
{
    if(keys_ != 0)
    {
        JNIEnv* env = getJNIEnvPointer();
        int size = varMap_.size(); // the map is immutable, so this is a safe assumption.
        for (int i=0; i < size; i++)
        {
            // All though this object is deleted, the string may still be referenced on the java side.
            // Therefore the object is only deleting it's reference to these strings.
            // The JVM will cleanup once the Strings goes out of scope on the Java side.
	        env->DeleteLocalRef(env->GetObjectArrayElement(keys_,size));
	    }
    }
}

jobject ReadOnlyVariantMapWrapper::get(const std::string& key) const
{
    JNIEnv* env = getJNIEnvPointer();
    using namespace qpid::types;
    Variant::Map::const_iterator iter;
    Variant v;

    iter = varMap_.find(key);
    if (iter == varMap_.end()){
        return 0;
    }

    v = iter->second;

    switch (v.getType()) {
        case VAR_VOID: {
            return 0;
        }

        case VAR_BOOL : {
            return newJavaBoolean(env,v.asBool() ? JNI_TRUE : JNI_FALSE);
        }

        case VAR_INT8 :
        case VAR_UINT8 : {
            return newJavaByte(env,(jbyte)v.asUint8());
        }

        case VAR_INT16 :
        case VAR_UINT16 : {
            return newJavaShort(env,(jshort) v.asUint16());
        }


        case VAR_INT32 :
        case VAR_UINT32 : {
            return newJavaInt(env,(jint)v.asUint32());
        }

        case VAR_INT64 :
        case VAR_UINT64 : {
            return newJavaLong(env,(jlong)v.asUint64());
        }

        case VAR_FLOAT : {
            return newJavaFloat(env,(jfloat)v.asFloat());
        }

        case VAR_DOUBLE : {
            return newJavaDouble(env,(jdouble)v.asDouble());
        }

        case VAR_STRING : {
            return newJavaString(env,v.asString());
        }

        case VAR_MAP : {
            throwJNILayerException(env,"The key maps to a Varient::Map. Unsupported type for message properties");
            break;
        }

        case VAR_LIST : {
            throwJNILayerException(env,"The key maps to a Varient::List. Unsupported type for message properties");
            break;
        }
    }
}

jboolean ReadOnlyVariantMapWrapper::containsKey (const std::string& key) const
{
    return (jboolean)(varMap_.find(key) != varMap_.end());
}

jboolean ReadOnlyVariantMapWrapper::isEmpty() const
{
    return (jboolean)varMap_.empty();
}

jint ReadOnlyVariantMapWrapper::size() const
{
    return (jint)varMap_.size();
}

jobjectArray ReadOnlyVariantMapWrapper::keys()
{
    if (keys_ == 0)
    {
        JNIEnv* env = getJNIEnvPointer();
        qpid::types::Variant::Map::const_iterator iter;
        int size = varMap_.size();
        int index = 0;
        keys_ = (jobjectArray)env->NewObjectArray(size,env->FindClass("java/lang/String"),env->NewStringUTF(""));
        for(iter = varMap_.begin(); iter != varMap_.end(); iter++)
        {
            const std::string val((iter->first));
            env->SetObjectArrayElement(keys_,index,newJavaString(env,val));
            index++;
        }
    }
    return keys_;
}

WriteOnlyVariantMapWrapper::WriteOnlyVariantMapWrapper()
{
    varMap_ = qpid::types::Variant::Map();
}

void WriteOnlyVariantMapWrapper::put(const std::string& key, jobject obj)
{
  JNIEnv* env = getJNIEnvPointer();
  if (key.empty())
  {
      env->ThrowNew(JAVA_ILLEGAL_ARGUMENT_EXP,"Key cannot be empty.");
  }

  qpid::types::Variant v = convertJavaObjectToVariant(env,obj);

  if (v)
  {
      varMap_[key] = v;
  }
  else
  {
      // There will be an exception on the java side,
      // thrown by convertJavaObjectToVariant method.
      return;
  }
}

%}

/** =================== SWIG Interface definitions ============================
  * This is for swig to generate the jni/java code
  * ===========================================================================
  */
ReadOnlyVariantMapWrapper::ReadOnlyVariantMapWrapper(qpid::types::Variant::Map& map);
ReadOnlyVariantMapWrapper::~ReadOnlyVariantMapWrapper();
jobject ReadOnlyVariantMapWrapper::get(const std::string& key) const;
jboolean ReadOnlyVariantMapWrapper::containsKey (const std::string& key) const;
jboolean ReadOnlyVariantMapWrapper::isEmpty() const;
jobjectArray ReadOnlyVariantMapWrapper::keys();
jint ReadOnlyVariantMapWrapper::size() const;

WriteOnlyVariantMapWrapper::WriteOnlyVariantMapWrapper();
WriteOnlyVariantMapWrapper::~WriteOnlyVariantMapWrapper();
void WriteOnlyVariantMapWrapper::put(const std::string& key, jobject obj);
qpid::types::Variant::Map WriteOnlyVariantMapWrapper::getVariantMap() const;