diff options
author | Rajith Muditha Attapattu <rajith@apache.org> | 2012-05-31 18:11:14 +0000 |
---|---|---|
committer | Rajith Muditha Attapattu <rajith@apache.org> | 2012-05-31 18:11:14 +0000 |
commit | d8164a0012e17f6a83340ff994566db15f0bf294 (patch) | |
tree | 5e0d78dd9c73370484d51265dd60588880b56ef6 | |
parent | a0acb74a76686aa459959fcabd0acd9b4601976f (diff) | |
download | qpid-python-d8164a0012e17f6a83340ff994566db15f0bf294.tar.gz |
QPID-4027 Changed the typemaps to provide a java.util.Map based on the
wrapped c++ Variant::Map. Maps returned to the java code are read only.
Need to add code to translate a java.util.Map into a Variant::Map
git-svn-id: https://svn.apache.org/repos/asf/qpid/branches/address-refactor2@1344841 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | qpid/cpp/bindings/qpid/java/VaraintMapWrapper.cpp (renamed from qpid/cpp/bindings/qpid/java/jmap.cpp) | 63 | ||||
-rw-r--r-- | qpid/cpp/bindings/qpid/java/VaraintMapWrapper.h (renamed from qpid/cpp/bindings/qpid/java/jmap.h) | 19 | ||||
-rw-r--r-- | qpid/cpp/bindings/qpid/java/java.i | 5 | ||||
-rw-r--r-- | qpid/cpp/bindings/swig_java_typemaps.i | 240 | ||||
-rw-r--r-- | qpid/java/client-api/src/main/java/org/apache/qpid/messaging/cpp/CppTest.java | 1 | ||||
-rw-r--r-- | qpid/java/lib/qpid_cpp_jni.jar | bin | 23847 -> 22280 bytes |
6 files changed, 266 insertions, 62 deletions
diff --git a/qpid/cpp/bindings/qpid/java/jmap.cpp b/qpid/cpp/bindings/qpid/java/VaraintMapWrapper.cpp index da1df4794b..1f7cc54550 100644 --- a/qpid/cpp/bindings/qpid/java/jmap.cpp +++ b/qpid/cpp/bindings/qpid/java/VaraintMapWrapper.cpp @@ -7,9 +7,9 @@ * 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 @@ -24,69 +24,92 @@ #include <string> -jmap::jmap(JNIEnv*& jniEnv, qpid::types::Variant::Map*& map): env(jniEnv), varMap(map) +using namespace qpid::types; + +VaraintMapWrapper::VaraintMapWrapper(): env(0), varMap(new Variant::Map()) + +VaraintMapWrapper::VaraintMapWrapper(JNIEnv*& jniEnv, Variant::Map*& map): env(jniEnv), varMap(map) { } -jmap::jobject get(const std::string key) const +VaraintMapWrapper::jobject get(const std::string key) const { jobject result; - qpid::types::Variant v = varMap[key]; + Varient::Map::iterator iter = varMap->find(key); + if (iter == varMap->end()){ + return NULL; + } + + Variant v = iter->first(); + try { switch (v->getType()) { - case qpid::types::VAR_VOID: { + case VAR_VOID: { result = NULL; break; } - case qpid::types::VAR_BOOL : { + case VAR_BOOL : { result = v->asBool() ? JNI_TRUE : JNI_FALSE; break; } - case qpid::types::VAR_UINT8 : - case qpid::types::VAR_UINT16 : - case qpid::types::VAR_UINT32 : { + case VAR_UINT8 : + case VAR_UINT16 : + case VAR_UINT32 : { result = (jnit) v->asUint32(); break; } - case qpid::types::VAR_UINT64 : { + case VAR_UINT64 : { result = (jlong) v->asUint64(); break; } - case qpid::types::VAR_INT8 : - case qpid::types::VAR_INT16 : - case qpid::types::VAR_INT32 : { + case VAR_INT8 : + case VAR_INT16 : + case VAR_INT32 : { result = (jnit) v->asUint32(); break; } - case qpid::types::VAR_INT64 : { + case VAR_INT64 : { result = (jlong) v->asUint64(); break; } - case qpid::types::VAR_FLOAT : { + case VAR_FLOAT : { result = (jfloat) v->asFloat(); break; } - case qpid::types::VAR_DOUBLE : { + case VAR_DOUBLE : { result = (jdouble) v->asDouble(); break; } - case qpid::types::VAR_STRING : { + case VAR_STRING : { const std::string val(v->asString()); result = env->NewStringUTF(val.data()); break; } } - } catch (qpid::types::Exception& ex) { + } catch (Exception& ex) { result = NULL; // need to throw exception } return result; } -jmap::void put (const jstring key, jobject object) +VaraintMapWrapper::jboolean containsKey (const jstring key) const { + return (jboolean)true; +} +VaraintMapWrapper::jboolean containsValue (const jobject value) const +{ + return (jboolean)true; } +VaraintMapWrapper::jboolean isEmpty() const +{ + return (jboolean)true; +} +/* jstring[] keys() const; + jobject[] values() const; + jinit size() const; +*/ diff --git a/qpid/cpp/bindings/qpid/java/jmap.h b/qpid/cpp/bindings/qpid/java/VaraintMapWrapper.h index 4b1e76f0ea..6272bbc49b 100644 --- a/qpid/cpp/bindings/qpid/java/jmap.h +++ b/qpid/cpp/bindings/qpid/java/VaraintMapWrapper.h @@ -7,9 +7,9 @@ * 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 @@ -24,12 +24,21 @@ #include <string> -class jmap { +/** + * Only primitives types are supported as values. + */ +class VaraintMapWrapper { public: - jmap(JNIEnv*& jniEnv, qpid::types::Variant::Map*& map); + VaraintMapWrapper(); + VaraintMapWrapper(JNIEnv*& jniEnv, qpid::types::Variant::Map*& map); jobject get(const std::string key) const; - void put (const jstring key, jobject object); + jboolean containsKey (const jstring key) const; + jboolean containsValue (const jobject value) const; + jboolean isEmpty() const; + jobjectArray keys() const; + jobjectArray values() const; + jint size() const; private: qpid::types::Variant::Map varMap; diff --git a/qpid/cpp/bindings/qpid/java/java.i b/qpid/cpp/bindings/qpid/java/java.i index 5e2eb39234..1f6006d955 100644 --- a/qpid/cpp/bindings/qpid/java/java.i +++ b/qpid/cpp/bindings/qpid/java/java.i @@ -18,15 +18,14 @@ */ %{ -#include "jmap.h" -#include <qpid/sys/IntegerTypes.h> +#include "VaraintMapWrapper.h" %} %module cqpid %include "std_string.i" %include "../../swig_java_typemaps.i" -%include "jmap.h" +%include "VaraintMapWrapper.h" %include "../qpid.i" diff --git a/qpid/cpp/bindings/swig_java_typemaps.i b/qpid/cpp/bindings/swig_java_typemaps.i index e32897a5bb..344247d0d9 100644 --- a/qpid/cpp/bindings/swig_java_typemaps.i +++ b/qpid/cpp/bindings/swig_java_typemaps.i @@ -17,6 +17,152 @@ * under the License. */ +/* Module code used by some of the wrapper classes */ + +%pragma(java) moduleimports=%{ +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +%} + +%pragma(java) modulecode=%{ + /** Checks if the buffer passed is a direct buffer + * This method could also convert a non direct buffer into a direct buffer. + * However the extra copying deafeats the purpose of the binding. + */ + static protected java.nio.ByteBuffer isBufferDirect(java.nio.ByteBuffer buff) + { + if (buff.isDirect()) + { + return buff; + } + else + { + throw new RuntimeException("The ByteBuffer passed is not allocated direct"); + } + } + + /** We don't support setting maps into C++ atm, but adding here to get around swig **/ + static VaraintMapWrapper getVariantMap(final Map<String,Object> map) + { + return new VaraintMapWrapper(); + } + + static Map<String, Object> getJavaMap(final VaraintMapWrapper map) + { + return new Map<String, Object>() + { + @Override + public int size() + { + return map.size(); + } + + @Override + public boolean isEmpty() + { + return map.isEmpty(); + } + + @Override + public boolean containsKey(Object key) + { + return map.containsKey((String)key); + } + + @Override + public boolean containsValue(Object value) + { + return map.containsValue(value); + } + + @Override + public Object get(Object key) + { + return map.get((String)key); + } + + @Override + public Object put(String key, Object value) + { + throw new UnsupportedOperationException("This map is read-only"); + } + + @Override + public Object remove(Object key) + { + throw new UnsupportedOperationException("This map is read-only"); + } + + @Override + public void putAll(Map<? extends String, ? extends Object> m) + { + throw new UnsupportedOperationException("This map is read-only"); + } + + @Override + public void clear() + { + throw new UnsupportedOperationException("This map is read-only"); + } + + @Override + public Set<String> keySet() + { + Set<String> keys = new HashSet<String>(); + for (String key:(String[])map.keys()) + { + keys.add(key); + } + + return keys; + } + + @Override + public Collection<Object> values() + { + return Arrays.asList(map.values()); + } + + @Override + public Set<Entry<String, Object>> entrySet() + { + Set<Entry<String, Object>> entries = new HashSet<Entry<String, Object>>(); + for (final String key: keySet()) + { + final Object value = map.get(key); + entries.add(new Entry<String, Object>() + { + @Override + public String getKey() + { + return key; + } + + @Override + public Object getValue() + { + return value; + } + + @Override + public Object setValue(Object value) + { + throw new UnsupportedOperationException("This set is read-only"); + } + + }); + } + return entries; + } + + }; + } +%} + + /* These 3 typemaps tell SWIG what JNI and Java types to use */ %typemap(jni) qpid::messaging::Message::BYTE_BUFFER "jobject" %typemap(jtype) qpid::messaging::Message::BYTE_BUFFER "java.nio.ByteBuffer" @@ -30,25 +176,8 @@ %typemap(javain) (qpid::messaging::Message::BYTE_BUFFER) "$module.isBufferDirect($javainput)" -%pragma(java) modulecode=%{ - /** Checks if the buffer passed is a direct buffer - * This method could also convert a non direct buffer into a direct buffer. - * However the extra copying deafeats the purpose of the binding. - */ - static protected java.nio.ByteBuffer isBufferDirect(java.nio.ByteBuffer buff) { - if (buff.isDirect()) - { - return buff; - } - else - { - throw new RuntimeException("The ByteBuffer passed is not allocated direct"); - } - } -%} - %typemap(out) qpid::messaging::Message::BYTE_BUFFER { - jresult = jenv->NewDirectByteBuffer($1.getStart(), $1.getSize()); + jresult = jenv->NewDirectByteBuffer($1.getStart(), $1.getSize()); } %typemap(javaout) qpid::messaging::Message::BYTE_BUFFER { @@ -56,30 +185,75 @@ } %typemap(jni) qpid::types::Variant::Map& "jobject" -%typemap(jtype) qpid::types::Variant::Map& "jmap" -%typemap(jstype) qpid::types::Variant::Map& "jmap" +%typemap(jtype) qpid::types::Variant::Map& "VaraintMapWrapper" +%typemap(jstype) qpid::types::Variant::Map& "java.util.Map" %typemap(out) qpid::types::Variant::Map& { - *(jmap **)&jresult = new jmap(jenv,$1); + *(VaraintMapWrapper **)&jresult = new VaraintMapWrapper(jenv,$1); } %typemap(javaout) qpid::types::Variant::Map& { - return $jnicall; + return $module.getJavaMap($jnicall); } %typemap(in) (qpid::types::Variant::Map&){ - $1 = NULL; + $1 = new qpid::types::Variant::Map(); } -%pragma(java) modulecode=%{ - /** temp hack to get the code compiling. - * We are currently not using any of the methods - * That require us to set a map. - */ - static protected jmap convertToJMap(Object map) { - return null; - } -%} +%typemap(javain) (qpid::types::Variant::Map&) "$module.getVariantMap($javainput)" + +%typemap(in) uint8_t { + $1 = (uint8_t)$input; +} + +%typemap(out) uint8_t { + $result = (jbyte)$1; +} + +%typemap(javaout) uint8_t { + return $jnicall; +} + +%typemap(javain) uint8_t "$javainput" + + +%typemap(in) uint32_t { + $1 = (uint32_t)$input; +} + +%typemap(out) uint32_t { + $result = (jint)$1; +} + +%typemap(javaout) uint32_t { + return $jnicall; +} + +%typemap(javain) uint32_t "$javainput" + +%typemap(in) uint64_t { + $1 = (uint64_t)$input; +} + +%typemap(out) uint64_t { + $result = (jlong)$1; +} + +%typemap(javaout) uint64_t { + return $jnicall; +} + +%typemap(javain) uint64_t "$javainput" + +%typemap(jni) uint8_t "jbyte" +%typemap(jtype) uint8_t "byte" +%typemap(jstype) uint8_t "byte" + +%typemap(jni) uint32_t "jint" +%typemap(jtype) uint32_t "int" +%typemap(jstype) uint32_t "int" -%typemap(javain) (qpid::types::Variant::Map&) "$module.convertToJMap($javainput)" +%typemap(jni) uint64_t "jlong" +%typemap(jtype) uint64_t "long" +%typemap(jstype) uint64_t "long" diff --git a/qpid/java/client-api/src/main/java/org/apache/qpid/messaging/cpp/CppTest.java b/qpid/java/client-api/src/main/java/org/apache/qpid/messaging/cpp/CppTest.java index f06637637c..e74cba908d 100644 --- a/qpid/java/client-api/src/main/java/org/apache/qpid/messaging/cpp/CppTest.java +++ b/qpid/java/client-api/src/main/java/org/apache/qpid/messaging/cpp/CppTest.java @@ -9,7 +9,6 @@ import org.apache.qpid.messaging.Sender; public class CppTest { - public static void main(String[] args) { System.out.println(System.getProperty("sys.path")); diff --git a/qpid/java/lib/qpid_cpp_jni.jar b/qpid/java/lib/qpid_cpp_jni.jar Binary files differindex 3be238dbb0..52ea2dea47 100644 --- a/qpid/java/lib/qpid_cpp_jni.jar +++ b/qpid/java/lib/qpid_cpp_jni.jar |