From 132aa8a77685ec92bc90c03f987650d275a7b639 Mon Sep 17 00:00:00 2001 From: lresende Date: Mon, 30 Sep 2013 06:59:11 +0000 Subject: 2.0.1 RC1 release tag git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1527464 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/jaxrs/CodeGenerationHelper.java | 280 +++++++++++++++++++++ .../java/jaxrs/GeneratedClassLoader.java | 86 +++++++ .../java/jaxrs/JAXRSJavaInterfaceProcessor.java | 116 +++++++++ .../java/jaxrs/RootResourceClassGenerator.java | 237 +++++++++++++++++ ...terfacedef.java.introspect.JavaInterfaceVisitor | 17 ++ .../tuscany/sca/interfacedef/java/jaxrs/Bean.java | 31 +++ .../jaxrs/JAXRSInterfaceProcessorTestCase.java | 66 +++++ .../interfacedef/java/jaxrs/MockedResource.java | 51 ++++ .../sca/interfacedef/java/jaxrs/Resource.java | 50 ++++ .../interfacedef/java/jaxrs/ResourceWrapper.java | 59 +++++ .../jaxrs/RootResourceClassGeneratorTestCase.java | 84 +++++++ 11 files changed, 1077 insertions(+) create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/CodeGenerationHelper.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/GeneratedClassLoader.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/JAXRSJavaInterfaceProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/RootResourceClassGenerator.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/resources/META-INF/services/org.apache.tuscany.sca.interfacedef.java.introspect.JavaInterfaceVisitor create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/Bean.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/JAXRSInterfaceProcessorTestCase.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/MockedResource.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/Resource.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/ResourceWrapper.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/RootResourceClassGeneratorTestCase.java (limited to 'sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src') diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/CodeGenerationHelper.java b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/CodeGenerationHelper.java new file mode 100644 index 0000000000..e1891e6c0b --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/CodeGenerationHelper.java @@ -0,0 +1,280 @@ +/* + * 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. + */ + +package org.apache.tuscany.sca.interfacedef.java.jaxrs; + +import java.lang.reflect.Array; +import java.lang.reflect.GenericArrayType; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; +import java.lang.reflect.WildcardType; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.objectweb.asm.Opcodes; + +/** + * @version $Rev$ $Date$ + */ +public class CodeGenerationHelper { + /** + * @param type + * @return + */ + public static Class getErasure(Type type) { + if (type instanceof Class) { + return (Class)type; + } else if (type instanceof GenericArrayType) { + GenericArrayType arrayType = (GenericArrayType)type; + Class componentType = getErasure(arrayType.getGenericComponentType()); + return Array.newInstance(componentType, 0).getClass(); + } else if (type instanceof ParameterizedType) { + ParameterizedType pType = (ParameterizedType)type; + return getErasure(pType.getRawType()); + } else if (type instanceof WildcardType) { + WildcardType wType = (WildcardType)type; + Type[] types = wType.getUpperBounds(); + if (types.length == 0) { + return Object.class; + } + return getErasure(types[0]); + } else if (type instanceof TypeVariable) { + TypeVariable var = (TypeVariable)type; + Type[] types = var.getBounds(); + if (types.length == 0) { + return Object.class; + } + return getErasure(types[0]); + } + return null; + } + + /** + * @param type + * @return + */ + public static String getJAXWSSignature(Type type) { + Class cls = getErasure(type); + if (Collection.class.isAssignableFrom(cls) && (type instanceof ParameterizedType)) { + ParameterizedType pType = (ParameterizedType)type; + Type p = pType.getActualTypeArguments()[0]; + StringBuffer sb = new StringBuffer(); + sb.append(getSignature(cls)); + sb.deleteCharAt(sb.length() - 1); // Remove ; + sb.append('<').append(getSignature(getErasure(p))).append(">;"); + return sb.toString(); + } else if (Map.class.isAssignableFrom(cls) && (type instanceof ParameterizedType)) { + ParameterizedType pType = (ParameterizedType)type; + Type key = pType.getActualTypeArguments()[0]; + Type value = pType.getActualTypeArguments()[1]; + StringBuffer sb = new StringBuffer(); + sb.append(getSignature(cls)); + sb.deleteCharAt(sb.length() - 1); // Remove ; + sb.append('<').append(getSignature(getErasure(key))).append(getSignature(getErasure(value))).append(">;"); + return sb.toString(); + } else { + return getSignature(cls); + } + } + + /** + * @param type + * @return + */ + public static String getSignature(Type type) { + if (!(type instanceof Class)) { + if (type instanceof ParameterizedType) { + ParameterizedType pType = (ParameterizedType)type; + StringBuffer sb = new StringBuffer(); + String rawType = getSignature(pType.getRawType()); + sb.append(rawType.substring(0, rawType.length() - 1)); + sb.append('<'); + for (Type t : pType.getActualTypeArguments()) { + String argType = getSignature(t); + sb.append(argType); + } + sb.append('>'); + sb.append(rawType.substring(rawType.length() - 1)); + return sb.toString(); + } + if (type instanceof TypeVariable) { + return "T" + ((TypeVariable)type).getName() + ";"; + } + if (type instanceof GenericArrayType) { + GenericArrayType arrayType = (GenericArrayType)type; + return "[" + getSignature(arrayType.getGenericComponentType()); + } + if (type instanceof WildcardType) { + WildcardType wType = (WildcardType)type; + Type[] types = wType.getUpperBounds(); + StringBuffer sb = new StringBuffer(); + if (types.length == 0 || !(types.length == 1 && types[0] == Object.class)) { + sb.append('+'); + for (Type t : types) { + sb.append(getSignature(t)); + } + } + types = wType.getLowerBounds(); + if (types.length != 0) { + sb.append('-'); + for (Type t : wType.getLowerBounds()) { + sb.append(getSignature(t)); + } + } + if (sb.length() == 0) { + return "*"; + } + return sb.toString(); + } + } + Class cls = (Class)type; + return org.objectweb.asm.Type.getDescriptor(cls); + } + + /** + * Get the actual type arguments a child class has used to extend a generic base class. + * + * @param baseClass the base class + * @param childClass the child class + * @return a list of the raw classes for the actual type arguments. + */ + public static List> resovleTypeArguments(Class baseClass, Class childClass) { + Map resolvedTypes = new HashMap(); + Type type = childClass; + // start walking up the inheritance hierarchy until we hit baseClass + while (!getErasure(type).equals(baseClass)) { + if (type instanceof Class) { + // there is no useful information for us in raw types, so just keep going. + type = ((Class)type).getGenericSuperclass(); + } else { + ParameterizedType parameterizedType = (ParameterizedType)type; + Class rawType = getErasure(parameterizedType.getRawType()); + + Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); + TypeVariable[] typeParameters = rawType.getTypeParameters(); + for (int i = 0; i < actualTypeArguments.length; i++) { + resolvedTypes.put(typeParameters[i], actualTypeArguments[i]); + } + + if (!rawType.equals(baseClass)) { + type = rawType.getGenericSuperclass(); + } + } + } + + // finally, for each actual type argument provided to baseClass, determine (if possible) + // the raw class for that type argument. + Type[] actualTypeArguments; + if (type instanceof Class) { + actualTypeArguments = ((Class)type).getTypeParameters(); + } else { + actualTypeArguments = ((ParameterizedType)type).getActualTypeArguments(); + } + List> typeArgumentsAsClasses = new ArrayList>(); + // resolve types by chasing down type variables. + for (Type baseType : actualTypeArguments) { + while (resolvedTypes.containsKey(baseType)) { + baseType = resolvedTypes.get(baseType); + } + typeArgumentsAsClasses.add(getErasure(baseType)); + } + return typeArgumentsAsClasses; + } + + /* + signatures.put(boolean.class, "Z"); + signatures.put(byte.class, "B"); + signatures.put(char.class, "C"); + signatures.put(short.class, "S"); + signatures.put(int.class, "I"); + signatures.put(long.class, "J"); + signatures.put(float.class, "F"); + signatures.put(double.class, "D"); + */ + public static int getLoadOPCode(String signature) { + if ("Z".equals(signature) || "B".equals(signature) + || "C".equals(signature) + || "S".equals(signature) + || "I".equals(signature)) { + return Opcodes.ILOAD; + } + + if ("J".equals(signature)) { + return Opcodes.LLOAD; + } + + if ("F".equals(signature)) { + return Opcodes.FLOAD; + } + + if ("D".equals(signature)) { + return Opcodes.DLOAD; + } + + return Opcodes.ALOAD; + + } + + public static int getReturnOPCode(String signature) { + if ("Z".equals(signature) || "B".equals(signature) + || "C".equals(signature) + || "S".equals(signature) + || "I".equals(signature)) { + return Opcodes.IRETURN; + } + + if ("J".equals(signature)) { + return Opcodes.LRETURN; + } + + if ("F".equals(signature)) { + return Opcodes.FRETURN; + } + + if ("D".equals(signature)) { + return Opcodes.DRETURN; + } + if ("V".equals(signature)) { + return Opcodes.RETURN; + } + + return Opcodes.ARETURN; + + } + + /** + * Get the package prefix for generated JAXWS artifacts + * @param cls + * @return + */ + public static String getPackagePrefix(Class cls) { + String name = cls.getName(); + int index = name.lastIndexOf('.'); + if (index == -1) { + return "jaxws."; + } else { + return name.substring(0, index) + ".jaxws."; + } + } + +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/GeneratedClassLoader.java b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/GeneratedClassLoader.java new file mode 100644 index 0000000000..5d296dfe12 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/GeneratedClassLoader.java @@ -0,0 +1,86 @@ +/* + * 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. + */ + +package org.apache.tuscany.sca.interfacedef.java.jaxrs; + +import java.net.URL; +import java.net.URLClassLoader; +import java.util.HashMap; +import java.util.Map; + +public class GeneratedClassLoader extends URLClassLoader { + private class GeneratedClass { + private String className; + private byte[] byteCode; + private Class cls; + + public GeneratedClass(String className, byte[] byteCode) { + super(); + this.className = className; + this.byteCode = byteCode; + } + + public synchronized Class getGeneratedClass() { + if (cls == null) { + cls = defineClass(className, byteCode, 0, byteCode.length); + } + return cls; + } + } + + private Map generatedClasses = new HashMap(); + + public GeneratedClassLoader(ClassLoader parentLoader) { + super(new URL[0], parentLoader); + } + + @Override + protected Class findClass(String className) throws ClassNotFoundException { + GeneratedClass cls = generatedClasses.get(className); + if (cls != null) { + return cls.getGeneratedClass(); + } + return super.findClass(className); + } + + public synchronized Class getGeneratedClass(String className, byte[] byteCode) { + GeneratedClass cls = generatedClasses.get(className); + if (cls == null) { + cls = new GeneratedClass(className, byteCode); + generatedClasses.put(className, cls); + } + return cls.getGeneratedClass(); + } + + @Override + public String toString() { + if( getParent() != null ) { + return "java.net.URLClassLoader:\n" + + "hashcode: " + hashCode() + "\n" + + "URLs: " + java.util.Arrays.asList( + getURLs() ) + "\n" + + "parent { " + getParent() + " }\n"; + } else { + return "java.net.URLClassLoader:\n" + + "hashcode: " + hashCode() + "\n" + + "URLs: " + java.util.Arrays.asList( + getURLs() ) + "\n"; + } + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/JAXRSJavaInterfaceProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/JAXRSJavaInterfaceProcessor.java new file mode 100644 index 0000000000..83f88c98cb --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/JAXRSJavaInterfaceProcessor.java @@ -0,0 +1,116 @@ +/* + * 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. + */ + +package org.apache.tuscany.sca.interfacedef.java.jaxrs; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.HEAD; +import javax.ws.rs.HttpMethod; +import javax.ws.rs.OPTIONS; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; + +import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.JavaOperation; +import org.apache.tuscany.sca.interfacedef.java.introspect.JavaInterfaceVisitor; + +public class JAXRSJavaInterfaceProcessor implements JavaInterfaceVisitor { + private static Map> mapping = new HashMap>(); + static { + mapping.put(HttpMethod.GET, GET.class); + mapping.put(HttpMethod.POST, POST.class); + mapping.put(HttpMethod.PUT, PUT.class); + mapping.put(HttpMethod.DELETE, DELETE.class); + mapping.put(HttpMethod.HEAD, HEAD.class); + mapping.put(HttpMethod.OPTIONS, OPTIONS.class); + } + + private boolean introspectHTTPMethod(JavaOperation operation) { + Method method = operation.getJavaMethod(); + + String methodName = null; + + /** + * A request method designator is a runtime annotation that is annotated with the @HttpMethod annotation. + * JAX-RS defines a set of request method designators for the common HTTP methods: @GET, @POST, @PUT, + * @DELETE, @HEAD. Users may define their own custom request method designators including alternate + * designators for the common HTTP methods. + */ + for (Annotation a : method.getAnnotations()) { + Class annotationType = a.annotationType(); + if (annotationType == HttpMethod.class) { + methodName = ((HttpMethod)a).value(); + break; + } + // Http method related annotations such as @GET, @POST will have itself annotated with + // @HttpMethod + HttpMethod m = a.annotationType().getAnnotation(HttpMethod.class); + if (m != null) { + methodName = m.value(); + break; + } + } + + boolean jaxrs = false; + Class type = mapping.get(methodName); + if (type != null) { + jaxrs = true; + operation.getAttributes().put(type, Boolean.TRUE); + Map attrs = operation.getInterface().getAttributes(); + List operations = (List)attrs.get(type); + if (operations == null) { + operations = new ArrayList(); + attrs.put(type, operations); + operations.add(operation); + } else { + operations.add(operation); + } + } + + return jaxrs; + + } + + public void visitInterface(JavaInterface contract) throws InvalidInterfaceException { + + boolean hasJAXRSAnnotarions = false; + + for (Operation op : contract.getOperations()) { + final JavaOperation operation = (JavaOperation)op; + if (introspectHTTPMethod(operation)) { + hasJAXRSAnnotarions = true; + } + } + + // Always set JAX-RS annotated interfaces as remotables + if (hasJAXRSAnnotarions) { + contract.setRemotable(true); + } + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/RootResourceClassGenerator.java b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/RootResourceClassGenerator.java new file mode 100644 index 0000000000..4209ee92f6 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/RootResourceClassGenerator.java @@ -0,0 +1,237 @@ +/* + * 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. + */ + +package org.apache.tuscany.sca.interfacedef.java.jaxrs; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.objectweb.asm.AnnotationVisitor; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.FieldVisitor; +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; + +public class RootResourceClassGenerator implements Opcodes { + + private static final String DELEGATE_FIELD = "delegate"; + + public static Class generateRootResourceClass(Class interfaze, String path, String consumes, String produces) + throws Exception { + if (!interfaze.isInterface()) { + throw new IllegalArgumentException(interfaze + " is not an interface."); + } + GeneratedClassLoader classLoader = new GeneratedClassLoader(interfaze.getClassLoader()); + String interfaceName = interfaze.getName(); + int index = interfaze.getName().lastIndexOf('.'); + String className = + interfaceName.substring(0, index) + ".Generated" + interfaceName.substring(index + 1) + "Impl"; + + final byte[] content = generate(interfaze, path, consumes, produces); + Class cls = classLoader.getGeneratedClass(className, content); + return cls; + } + + public static void injectProxy(Class generatedResourceClass, Object proxy) throws Exception { + Field field = generatedResourceClass.getField("delegate"); + field.set(null, proxy); + } + + public static byte[] generate(Class interfaze, String path, String consumes, String produces) throws Exception { + String interfaceName = Type.getInternalName(interfaze); + int index = interfaceName.lastIndexOf('/'); + String className = + interfaceName.substring(0, index) + "/Generated" + interfaceName.substring(index + 1) + "Impl"; + + ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); + + declareClass(cw, interfaceName, className); + + annotatePath(cw, path); + annotateContentTypes(cw, consumes, produces); + declareField(cw, interfaceName); + declareConstructor(cw, className); + + for (Method method : interfaze.getMethods()) { + if (!(method.getDeclaringClass() == Object.class)) { + generateMethod(cw, interfaceName, className, method, consumes, produces); + } + } + cw.visitEnd(); + + return cw.toByteArray(); + } + + // public method( arg0, ..., argN) throws , ..., + private static void generateMethod(ClassWriter cw, + String interfaceName, + String className, + Method method, + String consumes, + String produces) { + String methodDescriptor = Type.getMethodDescriptor(method); + + String signatureString = getSignature(method); + + MethodVisitor mv = + cw.visitMethod(ACC_PUBLIC, + method.getName(), + methodDescriptor, + signatureString, + getExceptionInternalNames(method)); + + mv.visitCode(); + mv.visitFieldInsn(GETSTATIC, className, DELEGATE_FIELD, getSignature(interfaceName)); + Class[] paramTypes = method.getParameterTypes(); + int index = 1; + for (int i = 0; i < paramTypes.length; i++) { + String signature = Type.getDescriptor(paramTypes[i]); + mv.visitVarInsn(CodeGenerationHelper.getLoadOPCode(signature), index); + if(paramTypes[i] == long.class || paramTypes[i] == double.class) { + index+=2; // Increase the index by 2 for the 64bit numbers + } else { + index++; + } + } + mv.visitMethodInsn(INVOKEINTERFACE, interfaceName, method.getName(), methodDescriptor); + + Class returnType = method.getReturnType(); + mv.visitInsn(CodeGenerationHelper.getReturnOPCode(Type.getDescriptor(returnType))); + int size = paramTypes.length + 1; + mv.visitMaxs(size, size); + mv.visitEnd(); + } + + /** + * [rfeng] A hack to get the generic method signature + * @param method + * @return + */ + private static String getSignature(Method method) { + try { + Field field = method.getClass().getDeclaredField("signature"); + field.setAccessible(true); + return (String)field.get(method); + } catch (Throwable e) { + return null; + } + } + + private static String[] getExceptionInternalNames(Method method) { + Class[] types = method.getExceptionTypes(); + if (types.length == 0) { + return null; + } + String[] names = new String[types.length]; + for (int i = 0; i < types.length; i++) { + names[i] = Type.getInternalName(types[i]); + } + return names; + } + + private static String getSignature(String interfaceName) { + return "L" + interfaceName + ";"; + } + + private static void declareConstructor(ClassWriter cw, String className) { + MethodVisitor mv; + mv = cw.visitMethod(ACC_PUBLIC, "", "()V", null, null); + mv.visitCode(); + Label l0 = new Label(); + mv.visitLabel(l0); + // mv.visitLineNumber(37, l0); + mv.visitVarInsn(ALOAD, 0); + mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V"); + mv.visitInsn(RETURN); + Label l1 = new Label(); + mv.visitLabel(l1); + mv.visitLocalVariable("this", getSignature(className), null, l0, l1, 0); + mv.visitMaxs(1, 1); + mv.visitEnd(); + } + + // public static delegate; + private static void declareField(ClassWriter cw, String interfaceName) { + FieldVisitor fv = + cw.visitField(ACC_PUBLIC + ACC_STATIC, DELEGATE_FIELD, getSignature(interfaceName), null, null); + fv.visitEnd(); + } + + // public class _Impl implements + private static void declareClass(ClassWriter cw, String interfaceName, String className) { + cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, className, null, "java/lang/Object", new String[] {interfaceName}); + } + + // @Path() + private static void annotatePath(ClassWriter cw, String path) { + AnnotationVisitor av = cw.visitAnnotation("Ljavax/ws/rs/Path;", true); + av.visit("value", path); + av.visitEnd(); + } + + // @Consumes() + // @Provides() + private static void annotateContentTypes(ClassWriter cw, String consumes, String produces) { + AnnotationVisitor av = null; + if (consumes != null) { + av = cw.visitAnnotation("Ljavax/ws/rs/Consumes;", true); + AnnotationVisitor av1 = av.visitArray("value"); + for (String s : consumes.split("(,| )")) { + av1.visit(null, s.trim()); + } + av1.visitEnd(); + av.visitEnd(); + } + if (produces != null) { + av = cw.visitAnnotation("Ljavax/ws/rs/Produces;", true); + AnnotationVisitor av1 = av.visitArray("value"); + for (String s : produces.split("(,| )")) { + av1.visit(null, s.trim()); + } + av1.visitEnd(); + av.visitEnd(); + } + } + + // @Consumes() + // @Provides() + private static void annotateContentTypes(MethodVisitor mv, String consumes, String produces) { + AnnotationVisitor av = null; + if (consumes != null) { + av = mv.visitAnnotation("Ljavax/ws/rs/Consumes;", true); + AnnotationVisitor av1 = av.visitArray("value"); + for (String s : consumes.split("(,| )")) { + av1.visit(null, s.trim()); + } + av1.visitEnd(); + av.visitEnd(); + } + if (produces != null) { + av = mv.visitAnnotation("Ljavax/ws/rs/Produces;", true); + AnnotationVisitor av1 = av.visitArray("value"); + for (String s : produces.split("(,| )")) { + av1.visit(null, s.trim()); + } + av1.visitEnd(); + av.visitEnd(); + } + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/resources/META-INF/services/org.apache.tuscany.sca.interfacedef.java.introspect.JavaInterfaceVisitor b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/resources/META-INF/services/org.apache.tuscany.sca.interfacedef.java.introspect.JavaInterfaceVisitor new file mode 100644 index 0000000000..9101d744b3 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/main/resources/META-INF/services/org.apache.tuscany.sca.interfacedef.java.introspect.JavaInterfaceVisitor @@ -0,0 +1,17 @@ +# 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. +org.apache.tuscany.sca.interfacedef.java.jaxrs.JAXRSJavaInterfaceProcessor;ranking=100 diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/Bean.java b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/Bean.java new file mode 100644 index 0000000000..20a4276b8b --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/Bean.java @@ -0,0 +1,31 @@ +/* + * 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. + */ + +package org.apache.tuscany.sca.interfacedef.java.jaxrs; + +import javax.ws.rs.GET; + +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +public interface Bean { + + @GET + String[] get(); +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/JAXRSInterfaceProcessorTestCase.java b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/JAXRSInterfaceProcessorTestCase.java new file mode 100644 index 0000000000..b680eaa491 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/JAXRSInterfaceProcessorTestCase.java @@ -0,0 +1,66 @@ +package org.apache.tuscany.sca.interfacedef.java.jaxrs; +/* + * 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. + */ + + +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import javax.ws.rs.GET; + +import junit.framework.Assert; + +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.junit.Before; +import org.junit.Test; + + +public class JAXRSInterfaceProcessorTestCase { + private ExtensionPointRegistry registry; + + @Before + public void setUp() throws Exception { + registry = new DefaultExtensionPointRegistry(); + } + + @Test + public final void testProcessor() throws Exception { + DefaultJavaInterfaceFactory iFactory = new DefaultJavaInterfaceFactory(registry); + JavaInterface contract = iFactory.createJavaInterface(Bean.class); + + assertTrue(contract.isRemotable()); + + Assert.assertEquals(1,contract.getOperations().size()); + + List getOperations = (List) contract.getAttributes().get(GET.class); + Assert.assertEquals(1,getOperations.size()); + + //list operation + System.out.println(">>> JAX-RS Operations"); + for(Operation o : contract.getOperations()) { + System.out.println(">>>>>>" + o); + } + + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/MockedResource.java b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/MockedResource.java new file mode 100644 index 0000000000..54516f951d --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/MockedResource.java @@ -0,0 +1,51 @@ +/* + * 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. + */ + +package org.apache.tuscany.sca.interfacedef.java.jaxrs; + +import java.util.List; + +public class MockedResource implements Resource { + private String value; + + public MockedResource() { + super(); + } + + public String get() { + return value; + } + + public void create(long id, String value) { + this.value = value; + } + + public void delete() { + value = null; + } + + public void update(String value) { + this.value = value; + } + + @Override + public String getList(List names) { + return value; + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/Resource.java b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/Resource.java new file mode 100644 index 0000000000..8054abf540 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/Resource.java @@ -0,0 +1,50 @@ +/* + * 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. + */ + +package org.apache.tuscany.sca.interfacedef.java.jaxrs; + +import java.util.List; + +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.QueryParam; + +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +public interface Resource { + + @GET + String get(); + + @GET + String getList(@QueryParam("list") List names); + + @PUT + void update(String value); + + @POST + void create(long id, String value); + + @DELETE + void delete(); + +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/ResourceWrapper.java b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/ResourceWrapper.java new file mode 100644 index 0000000000..55e725f8b9 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/ResourceWrapper.java @@ -0,0 +1,59 @@ +/* + * 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. + */ + +package org.apache.tuscany.sca.interfacedef.java.jaxrs; + +import java.util.List; + +import javax.ws.rs.Consumes; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; + +@Path("myURI") +@Produces({"application/xml", "application/json"}) +@Consumes({"application/xml", "application/json"}) +public class ResourceWrapper implements Resource { + public static Resource delegate; + + public ResourceWrapper() { + super(); + } + + public String get() { + return delegate.get(); + } + + public void create(long id, String value) { + delegate.create(id, value); + } + + public void delete() { + delegate.delete(); + } + + public void update(String value) { + delegate.update(value); + } + + @Override + public String getList(List names) { + return delegate.getList(names); + } + +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/RootResourceClassGeneratorTestCase.java b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/RootResourceClassGeneratorTestCase.java new file mode 100644 index 0000000000..40c70b8d6a --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/interface-java-jaxrs/src/test/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/RootResourceClassGeneratorTestCase.java @@ -0,0 +1,84 @@ +/* + * 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. + */ + +package org.apache.tuscany.sca.interfacedef.java.jaxrs; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.List; + +import javax.ws.rs.Consumes; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; + +import org.junit.Assert; +import org.junit.Test; + +/** + * + */ +public class RootResourceClassGeneratorTestCase { + @Test + public void testGenerator() throws Exception { + Class cls = + RootResourceClassGenerator.generateRootResourceClass(Resource.class, + "myURI", + "application/xml,application/json", + "application/xml,application/json"); + Assert.assertTrue(cls.isAnnotationPresent(Path.class)); + Path path = cls.getAnnotation(Path.class); + Assert.assertEquals("myURI", path.value()); + + Produces produces = cls.getAnnotation(Produces.class); + Assert.assertEquals("application/xml", produces.value()[0]); + + Consumes consumes = cls.getAnnotation(Consumes.class); + Assert.assertEquals("application/json", consumes.value()[1]); + + Field field = cls.getField("delegate"); + Assert.assertSame(Resource.class, field.getType()); + + Assert.assertTrue(Modifier.isPublic(field.getModifiers())); + Assert.assertTrue(Modifier.isStatic(field.getModifiers())); + + Assert.assertTrue(Resource.class.isAssignableFrom(cls)); + + Method m = cls.getMethod("getList", List.class); + System.out.println(m.toGenericString()); + Type type = m.getGenericParameterTypes()[0]; + Assert.assertTrue(type instanceof ParameterizedType); + ParameterizedType pType = (ParameterizedType)type; + Assert.assertEquals(String.class, pType.getActualTypeArguments()[0]); + + Resource resource = new MockedResource(); + field.set(null, resource); + + Resource resourceProxy = (Resource)cls.newInstance(); + Assert.assertNull(resourceProxy.get()); + resourceProxy.create(1l, "123"); + Assert.assertEquals("123", resourceProxy.get()); + resourceProxy.update("ABC"); + Assert.assertEquals("ABC", resourceProxy.get()); + resourceProxy.delete(); + Assert.assertNull(resourceProxy.get()); + } +} -- cgit v1.2.3