From d8164a0012e17f6a83340ff994566db15f0bf294 Mon Sep 17 00:00:00 2001 From: Rajith Muditha Attapattu Date: Thu, 31 May 2012 18:11:14 +0000 Subject: 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 --- qpid/cpp/bindings/qpid/java/VaraintMapWrapper.cpp | 115 ++++++++++ qpid/cpp/bindings/qpid/java/VaraintMapWrapper.h | 49 +++++ qpid/cpp/bindings/qpid/java/java.i | 5 +- qpid/cpp/bindings/qpid/java/jmap.cpp | 92 -------- qpid/cpp/bindings/qpid/java/jmap.h | 40 ---- qpid/cpp/bindings/swig_java_typemaps.i | 240 ++++++++++++++++++--- .../org/apache/qpid/messaging/cpp/CppTest.java | 1 - qpid/java/lib/qpid_cpp_jni.jar | Bin 23847 -> 22280 bytes 8 files changed, 373 insertions(+), 169 deletions(-) create mode 100644 qpid/cpp/bindings/qpid/java/VaraintMapWrapper.cpp create mode 100644 qpid/cpp/bindings/qpid/java/VaraintMapWrapper.h delete mode 100644 qpid/cpp/bindings/qpid/java/jmap.cpp delete mode 100644 qpid/cpp/bindings/qpid/java/jmap.h diff --git a/qpid/cpp/bindings/qpid/java/VaraintMapWrapper.cpp b/qpid/cpp/bindings/qpid/java/VaraintMapWrapper.cpp new file mode 100644 index 0000000000..1f7cc54550 --- /dev/null +++ b/qpid/cpp/bindings/qpid/java/VaraintMapWrapper.cpp @@ -0,0 +1,115 @@ +/* + * + * 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. + * + */ + +#include "qpid/types/Variant.h" +#include "jni.h" + +#include + +using namespace qpid::types; + +VaraintMapWrapper::VaraintMapWrapper(): env(0), varMap(new Variant::Map()) + +VaraintMapWrapper::VaraintMapWrapper(JNIEnv*& jniEnv, Variant::Map*& map): env(jniEnv), varMap(map) +{ +} + +VaraintMapWrapper::jobject get(const std::string key) const +{ + jobject result; + Varient::Map::iterator iter = varMap->find(key); + if (iter == varMap->end()){ + return NULL; + } + + Variant v = iter->first(); + + try { + switch (v->getType()) { + case VAR_VOID: { + result = NULL; + break; + } + case VAR_BOOL : { + result = v->asBool() ? JNI_TRUE : JNI_FALSE; + break; + } + case VAR_UINT8 : + case VAR_UINT16 : + case VAR_UINT32 : { + result = (jnit) v->asUint32(); + break; + } + case VAR_UINT64 : { + result = (jlong) v->asUint64(); + break; + } + case VAR_INT8 : + case VAR_INT16 : + case VAR_INT32 : { + result = (jnit) v->asUint32(); + break; + } + case VAR_INT64 : { + result = (jlong) v->asUint64(); + break; + } + case VAR_FLOAT : { + result = (jfloat) v->asFloat(); + break; + } + case VAR_DOUBLE : { + result = (jdouble) v->asDouble(); + break; + } + case VAR_STRING : { + const std::string val(v->asString()); + result = env->NewStringUTF(val.data()); + break; + } + } + } catch (Exception& ex) { + result = NULL; // need to throw exception + } + + return result; +} + +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/VaraintMapWrapper.h b/qpid/cpp/bindings/qpid/java/VaraintMapWrapper.h new file mode 100644 index 0000000000..6272bbc49b --- /dev/null +++ b/qpid/cpp/bindings/qpid/java/VaraintMapWrapper.h @@ -0,0 +1,49 @@ +/* + * + * 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. + * + */ + +#include "qpid/types/Variant.h" +#include "jni.h" + +#include + +/** + * Only primitives types are supported as values. + */ +class VaraintMapWrapper { + + public: + VaraintMapWrapper(); + VaraintMapWrapper(JNIEnv*& jniEnv, qpid::types::Variant::Map*& map); + jobject get(const std::string key) const; + 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; + JNIEnv env; +}; + + + 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 +#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/qpid/java/jmap.cpp b/qpid/cpp/bindings/qpid/java/jmap.cpp deleted file mode 100644 index da1df4794b..0000000000 --- a/qpid/cpp/bindings/qpid/java/jmap.cpp +++ /dev/null @@ -1,92 +0,0 @@ -/* - * - * 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. - * - */ - -#include "qpid/types/Variant.h" -#include "jni.h" - -#include - -jmap::jmap(JNIEnv*& jniEnv, qpid::types::Variant::Map*& map): env(jniEnv), varMap(map) -{ -} - -jmap::jobject get(const std::string key) const -{ - jobject result; - qpid::types::Variant v = varMap[key]; - try { - switch (v->getType()) { - case qpid::types::VAR_VOID: { - result = NULL; - break; - } - case qpid::types::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 : { - result = (jnit) v->asUint32(); - break; - } - case qpid::types::VAR_UINT64 : { - result = (jlong) v->asUint64(); - break; - } - case qpid::types::VAR_INT8 : - case qpid::types::VAR_INT16 : - case qpid::types::VAR_INT32 : { - result = (jnit) v->asUint32(); - break; - } - case qpid::types::VAR_INT64 : { - result = (jlong) v->asUint64(); - break; - } - case qpid::types::VAR_FLOAT : { - result = (jfloat) v->asFloat(); - break; - } - case qpid::types::VAR_DOUBLE : { - result = (jdouble) v->asDouble(); - break; - } - case qpid::types::VAR_STRING : { - const std::string val(v->asString()); - result = env->NewStringUTF(val.data()); - break; - } - } - } catch (qpid::types::Exception& ex) { - result = NULL; // need to throw exception - } - - return result; -} - -jmap::void put (const jstring key, jobject object) -{ - -} - - - diff --git a/qpid/cpp/bindings/qpid/java/jmap.h b/qpid/cpp/bindings/qpid/java/jmap.h deleted file mode 100644 index 4b1e76f0ea..0000000000 --- a/qpid/cpp/bindings/qpid/java/jmap.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * - * 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. - * - */ - -#include "qpid/types/Variant.h" -#include "jni.h" - -#include - -class jmap { - - public: - jmap(JNIEnv*& jniEnv, qpid::types::Variant::Map*& map); - jobject get(const std::string key) const; - void put (const jstring key, jobject object); - - private: - qpid::types::Variant::Map varMap; - JNIEnv env; -}; - - - 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 map) + { + return new VaraintMapWrapper(); + } + + static Map getJavaMap(final VaraintMapWrapper map) + { + return new Map() + { + @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 m) + { + throw new UnsupportedOperationException("This map is read-only"); + } + + @Override + public void clear() + { + throw new UnsupportedOperationException("This map is read-only"); + } + + @Override + public Set keySet() + { + Set keys = new HashSet(); + for (String key:(String[])map.keys()) + { + keys.add(key); + } + + return keys; + } + + @Override + public Collection values() + { + return Arrays.asList(map.values()); + } + + @Override + public Set> entrySet() + { + Set> entries = new HashSet>(); + for (final String key: keySet()) + { + final Object value = map.get(key); + entries.add(new Entry() + { + @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 index 3be238dbb0..52ea2dea47 100644 Binary files a/qpid/java/lib/qpid_cpp_jni.jar and b/qpid/java/lib/qpid_cpp_jni.jar differ -- cgit v1.2.1