From 047ade832c6f41b92256a968bbb3bcabbaf8f5c0 Mon Sep 17 00:00:00 2001 From: lresende Date: Tue, 27 Apr 2010 21:42:45 +0000 Subject: Initial support for introspecting JAX-RS annotations during interface contract introspection git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@938681 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/jaxrs/JAXRSJavaInterfaceProcessor.java | 107 +++++++++++++++++++++ ...terfacedef.java.introspect.JavaInterfaceVisitor | 17 ++++ 2 files changed, 124 insertions(+) create mode 100644 sca-java-2.x/trunk/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/JAXRSJavaInterfaceProcessor.java create mode 100644 sca-java-2.x/trunk/modules/interface-java-jaxrs/src/main/resources/META-INF/services/org.apache.tuscany.sca.interfacedef.java.introspect.JavaInterfaceVisitor (limited to 'sca-java-2.x/trunk/modules/interface-java-jaxrs/src/main') diff --git a/sca-java-2.x/trunk/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/JAXRSJavaInterfaceProcessor.java b/sca-java-2.x/trunk/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/JAXRSJavaInterfaceProcessor.java new file mode 100644 index 0000000000..563be43a6b --- /dev/null +++ b/sca-java-2.x/trunk/modules/interface-java-jaxrs/src/main/java/org/apache/tuscany/sca/interfacedef/java/jaxrs/JAXRSJavaInterfaceProcessor.java @@ -0,0 +1,107 @@ +/* + * 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.Method; +import java.util.ArrayList; +import java.util.Iterator; +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 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 { + + public void visitInterface(JavaInterface contract) throws InvalidInterfaceException { + + final Class clazz = contract.getJavaClass(); + + List getOperations = new ArrayList(); + List putOperations = new ArrayList(); + List postOperations = new ArrayList(); + List deleteOperations = new ArrayList(); + + boolean hasJAXRSAnnotarions = false; + + for (Iterator it = contract.getOperations().iterator(); it.hasNext();) { + final JavaOperation operation = (JavaOperation)it.next(); + final Method method = operation.getJavaMethod(); + + GET get = method.getAnnotation(GET.class); + if(get != null) { + hasJAXRSAnnotarions = true; + operation.getAttributes().put(GET.class.getClass(), true); + getOperations.add(operation); + } + + if(! getOperations.isEmpty()) { + contract.getAttributes().put(GET.class.getClass(), getOperations); + } + + PUT put = method.getAnnotation(PUT.class); + if(put != null) { + hasJAXRSAnnotarions = true; + operation.getAttributes().put(PUT.class.getClass(), true); + putOperations.add(operation); + } + + if(! putOperations.isEmpty()) { + contract.getAttributes().put(PUT.class.getClass(), putOperations); + } + + + POST post = method.getAnnotation(POST.class); + if(post != null) { + hasJAXRSAnnotarions = true; + operation.getAttributes().put(POST.class.getClass(), true); + postOperations.add(operation); + } + + if(! postOperations.isEmpty()) { + contract.getAttributes().put(POST.class.getClass(), postOperations); + } + + DELETE delete = method.getAnnotation(DELETE.class); + if(delete != null) { + hasJAXRSAnnotarions = true; + operation.getAttributes().put(DELETE.class.getClass(), true); + deleteOperations.add(operation); + } + + if(! deleteOperations.isEmpty()) { + contract.getAttributes().put(GET.class.getClass(), deleteOperations); + } + + } + + // Always set JAX-RS annotated interfaces as remotables + if (hasJAXRSAnnotarions) { + contract.setRemotable(true); + } + } +} diff --git a/sca-java-2.x/trunk/modules/interface-java-jaxrs/src/main/resources/META-INF/services/org.apache.tuscany.sca.interfacedef.java.introspect.JavaInterfaceVisitor b/sca-java-2.x/trunk/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/trunk/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 -- cgit v1.2.3