summaryrefslogtreecommitdiffstats
path: root/java/sca/modules/binding-corba-runtime/src/main
diff options
context:
space:
mode:
authorrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2008-06-20 17:57:47 +0000
committerrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2008-06-20 17:57:47 +0000
commit80c2130923497ebb90a51e8c7839e9ac39c1febf (patch)
treead54d6f6fd1a6648eba7d828411df903763826f2 /java/sca/modules/binding-corba-runtime/src/main
parentfa66067486ac7fb8e141778b0f5c3de356069254 (diff)
Apply the patch from Wojtek for TUSCANY-2357. Thanks.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@670012 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/binding-corba-runtime/src/main')
-rw-r--r--java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/exceptions/RequestConfigurationException.java4
-rw-r--r--java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/reference/DynaCorbaRequest.java12
-rw-r--r--java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/NodeType.java2
-rw-r--r--java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/TypeTreeCreator.java158
-rw-r--r--java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/util/EnumTypeHelper.java42
-rw-r--r--java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/util/TypeHelpersProxy.java2
6 files changed, 208 insertions, 12 deletions
diff --git a/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/exceptions/RequestConfigurationException.java b/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/exceptions/RequestConfigurationException.java
index c4d78a5027..61de0496e3 100644
--- a/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/exceptions/RequestConfigurationException.java
+++ b/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/exceptions/RequestConfigurationException.java
@@ -32,6 +32,10 @@ public class RequestConfigurationException extends Exception {
this.objectId = objectId;
}
+ public RequestConfigurationException(String message) {
+ super(message);
+ }
+
public String getType() {
return objectId;
}
diff --git a/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/reference/DynaCorbaRequest.java b/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/reference/DynaCorbaRequest.java
index 759f9756b6..dc2224d7a8 100644
--- a/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/reference/DynaCorbaRequest.java
+++ b/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/reference/DynaCorbaRequest.java
@@ -19,9 +19,7 @@
package org.apache.tuscany.sca.binding.corba.impl.reference;
-import java.util.ArrayList;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
import org.apache.tuscany.sca.binding.corba.impl.exceptions.CorbaException;
@@ -43,7 +41,6 @@ import org.omg.CORBA.portable.OutputStream;
public class DynaCorbaRequest {
private TypeTree returnTree;
- private List<TypeTree> arguments = new ArrayList<TypeTree>();
private Map<String, TypeTree> exceptions = new HashMap<String, TypeTree>();
private OutputStream outputStream;
private ObjectImpl remoteObject;
@@ -69,7 +66,8 @@ public class DynaCorbaRequest {
*
* @param argument
*/
- public void addArgument(java.lang.Object argument) {
+ public void addArgument(java.lang.Object argument)
+ throws RequestConfigurationException {
TypeTree tree = TypeTreeCreator.createTypeTree(argument.getClass());
TypeHelpersProxy.write(tree.getRootNode(), outputStream, argument);
}
@@ -79,7 +77,8 @@ public class DynaCorbaRequest {
*
* @param forClass
*/
- public void setOutputType(Class<?> forClass) {
+ public void setOutputType(Class<?> forClass)
+ throws RequestConfigurationException {
returnTree = TypeTreeCreator.createTypeTree(forClass);
}
@@ -99,7 +98,8 @@ public class DynaCorbaRequest {
*
* @param forClass
*/
- public void addExceptionType(Class<?> forClass) {
+ public void addExceptionType(Class<?> forClass)
+ throws RequestConfigurationException {
TypeTree tree = TypeTreeCreator.createTypeTree(forClass);
String exceptionId = getExceptionId(forClass);
exceptions.put(exceptionId, tree);
diff --git a/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/NodeType.java b/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/NodeType.java
index 155bfb5444..cb1987cb4b 100644
--- a/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/NodeType.java
+++ b/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/NodeType.java
@@ -24,5 +24,5 @@ package org.apache.tuscany.sca.binding.corba.impl.types;
* Types of CORBA objects.
*/
public enum NodeType {
- primitive, struct, union, array, sequence, reference
+ primitive, struct, union, array, sequence, reference, idl_enum, exception
}
diff --git a/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/TypeTreeCreator.java b/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/TypeTreeCreator.java
index d370c5cd7e..24bd8dcbe7 100644
--- a/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/TypeTreeCreator.java
+++ b/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/TypeTreeCreator.java
@@ -19,9 +19,17 @@
package org.apache.tuscany.sca.binding.corba.impl.types;
+import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
+
+import org.apache.tuscany.sca.binding.corba.impl.exceptions.RequestConfigurationException;
/**
* @version $Rev$ $Date$
@@ -134,7 +142,8 @@ public class TypeTreeCreator {
* @param forClass
* @return type tree
*/
- public static TypeTree createTypeTree(Class<?> forClass) {
+ public static TypeTree createTypeTree(Class<?> forClass)
+ throws RequestConfigurationException {
TypeTree tree = new TypeTree();
TypeTreeNode rootNode = null;
rootNode = inspectClassHierarchy(forClass, tree);
@@ -151,7 +160,7 @@ public class TypeTreeCreator {
* @return
*/
private static TypeTreeNode inspectClassHierarchy(Class<?> forClass,
- TypeTree tree) {
+ TypeTree tree) throws RequestConfigurationException {
// //remains of type tree caching
// TypeTreeNode existingNode = tree.getNodeForType(forClass);
// if (existingNode != null) {
@@ -175,7 +184,8 @@ public class TypeTreeCreator {
Class<?> reduced = reduceArrayDimension(node.getJavaClass());
children = new TypeTreeNode[1];
children[0] = inspectClassHierarchy(reduced, tree);
- } else if (nodeType.equals(NodeType.struct)) {
+ } else if (nodeType.equals(NodeType.struct)
+ || nodeType.equals(NodeType.exception)) {
// inspect types for every structure member
Field[] fields = node.getJavaClass().getFields();
children = new TypeTreeNode[fields.length];
@@ -185,6 +195,8 @@ public class TypeTreeCreator {
child.setName(fields[i].getName());
children[i] = child;
}
+ } else if (nodeType.equals(NodeType.idl_enum)) {
+
} else if (nodeType.equals(NodeType.union)) {
// TODO: unions
} else if (nodeType.equals(NodeType.reference)) {
@@ -201,8 +213,10 @@ public class TypeTreeCreator {
* @param forClass
* class
* @return node
+ * @throws RequestConfigurationException
*/
- private static TypeTreeNode createTypeNode(Class<?> forClass) {
+ private static TypeTreeNode createTypeNode(Class<?> forClass)
+ throws RequestConfigurationException {
TypeTreeNode node = new TypeTreeNode();
if (forClass.isArray()) {
node.setNodeType(NodeType.sequence);
@@ -215,10 +229,144 @@ public class TypeTreeCreator {
node.setNodeType(NodeType.reference);
node.setJavaClass(forClass);
node.setChildren(null);
- } else {
+ } else if (isStructType(forClass)) {
node.setNodeType(NodeType.struct);
node.setJavaClass(forClass);
+ } else if (isEnumType(forClass)) {
+ node.setNodeType(NodeType.idl_enum);
+ node.setJavaClass(forClass);
+ } else if (isUserException(forClass)) {
+ node.setNodeType(NodeType.exception);
+ node.setJavaClass(forClass);
+ } else {
+ RequestConfigurationException e = new RequestConfigurationException(
+ "User defined type which cannot be handler: "
+ + forClass.getCanonicalName());
+ throw e;
}
return node;
}
+
+ /**
+ * Tells whether given class is structure
+ *
+ * @param forClass
+ * @return
+ */
+ private static boolean isStructType(Class<?> forClass) {
+ int classMods = forClass.getModifiers();
+ if (!Modifier.isFinal(classMods)) {
+ return false;
+ }
+ boolean areCtorsValid = false;
+ Class<?>[] fieldsTypes = null;
+ Constructor<?>[] ctors = forClass.getConstructors();
+ /*
+ * Do we have 2 ctors and one of them is null ctor?
+ */
+ if (ctors.length != 2) {
+ return false;
+ }
+ for (int i = 0; i < ctors.length; i++) {
+ Class<?>[] params = ctors[i].getParameterTypes();
+ if (params.length == 0) {
+ areCtorsValid = true;
+ } else {
+ fieldsTypes = params;
+ }
+ }
+ if (!areCtorsValid) {
+ return false;
+ }
+ /*
+ * Are constructor args declared as class fields?
+ */
+ Field[] fields = forClass.getFields();
+ Set<Class<?>> fieldsSet = new HashSet<Class<?>>(Arrays
+ .asList(fieldsTypes));
+ for (int i = 0; i < fields.length && !fieldsSet.isEmpty(); i++) {
+ int mods = fields[i].getModifiers();
+ if (Modifier.isPublic(mods) && !Modifier.isStatic(mods)
+ && !Modifier.isFinal(mods)) {
+ fieldsSet.remove(fields[i].getType());
+ }
+
+ }
+ return fieldsSet.isEmpty();
+ }
+
+ /**
+ * Tells whether given class is enum
+ *
+ * @param forClass
+ * @return
+ */
+ private static boolean isEnumType(Class<?> forClass) {
+ boolean isValueMethod = false;
+ boolean isFromIntMethod = false;
+ /*
+ * enum type should have value and from_int methods
+ */
+ try {
+ Method valueMet = forClass.getMethod("value", new Class[] {});
+ int modValueMet = valueMet.getModifiers();
+ if (valueMet.getReturnType().equals(int.class)
+ && Modifier.isPublic(modValueMet)) {
+ isValueMethod = true;
+ }
+ Method fromIntMet = forClass.getMethod("from_int",
+ new Class[] { int.class });
+ int modFromIntMet = fromIntMet.getModifiers();
+ if ((fromIntMet.getReturnType().equals(forClass)
+ && Modifier.isPublic(modFromIntMet) && Modifier
+ .isStatic(modFromIntMet))) {
+ isFromIntMethod = true;
+ }
+ } catch (NoSuchMethodException e) {
+ }
+ if (!isFromIntMethod && !isValueMethod) {
+ return false;
+ }
+ /*
+ * enum type should also contain minimum one pair of fields: EnumType
+ * field and int _field
+ */
+ int enumCount = 0;
+ Field[] fields = forClass.getFields();
+ for (int i = 0; i < fields.length; i++) {
+ if (fields[i].getType().equals(forClass)) {
+ int modifiers = fields[i].getModifiers();
+ if (Modifier.isStatic(modifiers)
+ && Modifier.isPublic(modifiers)
+ && Modifier.isFinal(modifiers)) {
+ try {
+ Field field = forClass.getField("_"
+ + fields[i].getName());
+ if (field.getType().equals(int.class)) {
+ enumCount++;
+ }
+ } catch (NoSuchFieldException e) {
+ }
+
+ }
+ }
+ }
+ return enumCount > 0;
+ }
+
+ /**
+ * Tells whether given class is corba user exception
+ * @param forClass
+ * @return
+ */
+ private static boolean isUserException(Class<?> forClass) {
+ do {
+ if (forClass.equals(Exception.class)) {
+ return true;
+ } else {
+ forClass = forClass.getSuperclass();
+ }
+ } while (!forClass.equals(Object.class));
+ return false;
+ }
}
diff --git a/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/util/EnumTypeHelper.java b/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/util/EnumTypeHelper.java
new file mode 100644
index 0000000000..5cb68433a2
--- /dev/null
+++ b/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/util/EnumTypeHelper.java
@@ -0,0 +1,42 @@
+package org.apache.tuscany.sca.binding.corba.impl.types.util;
+
+import java.lang.reflect.Method;
+
+import org.apache.tuscany.sca.binding.corba.impl.types.TypeTreeNode;
+import org.omg.CORBA.TypeCode;
+import org.omg.CORBA.portable.InputStream;
+import org.omg.CORBA.portable.OutputStream;
+
+public class EnumTypeHelper implements TypeHelper {
+
+ public TypeCode getType(TypeTreeNode node) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Object read(TypeTreeNode node, InputStream is) {
+ int value = is.read_long();
+ Object result = null;
+ try {
+ Method method = node.getJavaClass().getMethod("from_int",
+ new Class[] { int.class });
+ result = method.invoke(null, new Object[] { value });
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return result;
+ }
+
+ public void write(TypeTreeNode node, OutputStream os, Object data) {
+ int value = 0;
+ try {
+ Method method = data.getClass().getMethod("value", new Class[] {});
+ value = (Integer) method.invoke(data, new Object[] {});
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ os.write_long(value);
+ }
+}
diff --git a/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/util/TypeHelpersProxy.java b/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/util/TypeHelpersProxy.java
index 467287382e..77dff7bab9 100644
--- a/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/util/TypeHelpersProxy.java
+++ b/java/sca/modules/binding-corba-runtime/src/main/java/org/apache/tuscany/sca/binding/corba/impl/types/util/TypeHelpersProxy.java
@@ -71,6 +71,8 @@ public class TypeHelpersProxy {
complexTypes.put(NodeType.struct, new StructTypeHelper());
complexTypes.put(NodeType.reference, new ReferenceTypeHelper());
complexTypes.put(NodeType.sequence, new SequenceTypeHelper());
+ complexTypes.put(NodeType.idl_enum, new EnumTypeHelper());
+ complexTypes.put(NodeType.exception, new StructTypeHelper());
}
/**