You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by sl...@apache.org on 2010/08/02 18:27:24 UTC

svn commit: r981585 - in /tuscany/sca-java-2.x/trunk/modules/implementation-java/src: main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ main/resources/META-INF/services/ test/java/org/apache/tuscany/sca/implementation/java/introspec...

Author: slaws
Date: Mon Aug  2 16:27:23 2010
New Revision: 981585

URL: http://svn.apache.org/viewvc?rev=981585&view=rev
Log:
TUSCANY-3641 - start adding a processor to handle JAXWS annotations in implementation.java classes. I'm a little surprised that this isn't already handled so I'm committing this as a demonstration of intent with a view to asking on the ML is this function is achieved in some way I'm not yet aware of. 

Added:
    tuscany/sca-java-2.x/trunk/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessor.java
    tuscany/sca-java-2.x/trunk/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessorTestCase.java
Modified:
    tuscany/sca-java-2.x/trunk/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor

Added: tuscany/sca-java-2.x/trunk/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessor.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessor.java?rev=981585&view=auto
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessor.java (added)
+++ tuscany/sca-java-2.x/trunk/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessor.java Mon Aug  2 16:27:23 2010
@@ -0,0 +1,131 @@
+/*
+ * 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.implementation.java.introspect.impl;
+
+import static org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper.getAllInterfaces;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.jws.WebService;
+import javax.xml.namespace.QName;
+
+import org.apache.tuscany.sca.assembly.AssemblyFactory;
+import org.apache.tuscany.sca.assembly.Service;
+import org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.implementation.java.IntrospectionException;
+import org.apache.tuscany.sca.implementation.java.JavaElementImpl;
+import org.apache.tuscany.sca.implementation.java.JavaImplementation;
+import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor;
+import org.apache.tuscany.sca.implementation.java.introspect.JavaIntrospectionHelper;
+import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException;
+import org.apache.tuscany.sca.interfacedef.java.JavaInterface;
+import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract;
+import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory;
+import org.apache.tuscany.sca.interfacedef.util.JavaXMLMapper;
+import org.oasisopen.sca.ServiceReference;
+import org.oasisopen.sca.annotation.Callback;
+import org.oasisopen.sca.annotation.Remotable;
+
+/**
+ * Process JAXWS annotations and updates the component type accordingly
+ * 
+ */
+public class JAXWSProcessor extends BaseJavaClassVisitor {
+    
+    public JAXWSProcessor(AssemblyFactory assemblyFactory, JavaInterfaceFactory javaFactory) {
+        super(assemblyFactory);
+        this.javaInterfaceFactory = javaFactory;
+    }
+    
+    public JAXWSProcessor(ExtensionPointRegistry registry) {
+        super(registry);
+    }
+
+    @Override
+    public <T> void visitClass(Class<T> clazz, JavaImplementation type) throws IntrospectionException {
+        WebService webService = clazz.getAnnotation(WebService.class);
+        String tns = JavaXMLMapper.getNamespace(clazz);
+        String localName = clazz.getSimpleName();
+        Class<?> interfaze = clazz;
+        if (webService != null) {
+            tns = getValue(webService.targetNamespace(), tns);
+            localName = getValue(webService.name(), localName);
+            
+            String serviceInterfaceName = webService.endpointInterface();
+            // TODO - how to resolve this interface name 
+            //        needs to be done higher up where we have 
+            //        access to the resolver. 
+
+            Service service;
+            try {
+                service = createService(clazz, interfaze, localName);
+            } catch (InvalidInterfaceException e) {
+                throw new IntrospectionException(e);
+            }
+            
+            if (!type.getServices().contains(service)){
+                type.getServices().add(service);   
+            }
+        }
+    }
+    
+    /**
+     * Utility methods
+     */    
+
+    private static String getValue(String value, String defaultValue) {
+        return "".equals(value) ? defaultValue : value;
+    }
+    
+    public Service createService(Class<?> clazz, Class<?> interfaze, String name) throws InvalidInterfaceException {
+        Service service = assemblyFactory.createService();
+        JavaInterfaceContract interfaceContract = javaInterfaceFactory.createJavaInterfaceContract();
+        service.setInterfaceContract(interfaceContract);
+
+        if (name == null) {
+            service.setName(interfaze.getSimpleName());
+        } else {
+            service.setName(name);
+        }
+
+        JavaInterface callInterface = javaInterfaceFactory.createJavaInterface(interfaze);
+        boolean remotable = clazz.getAnnotation(Remotable.class) != null;
+        if (remotable){
+            callInterface.setRemotable(true);
+        }
+        service.getInterfaceContract().setInterface(callInterface);
+        
+        if (callInterface.getCallbackClass() != null) {
+            JavaInterface callbackInterface = javaInterfaceFactory.createJavaInterface(callInterface.getCallbackClass());
+            if (remotable){
+                callbackInterface.setRemotable(true);
+            }
+            service.getInterfaceContract().setCallbackInterface(callbackInterface);
+        }
+        return service;
+    }    
+
+}

Modified: tuscany/sca-java-2.x/trunk/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor?rev=981585&r1=981584&r2=981585&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor (original)
+++ tuscany/sca-java-2.x/trunk/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor Mon Aug  2 16:27:23 2010
@@ -29,5 +29,6 @@ org.apache.tuscany.sca.implementation.ja
 org.apache.tuscany.sca.implementation.java.introspect.impl.ResourceProcessor;ranking=1100
 org.apache.tuscany.sca.implementation.java.introspect.impl.ScopeProcessor;ranking=1000
 org.apache.tuscany.sca.implementation.java.introspect.impl.ServiceProcessor;ranking=900
+org.apache.tuscany.sca.implementation.java.introspect.impl.JAXWSProcessor;ranking=850
 org.apache.tuscany.sca.implementation.java.introspect.impl.HeuristicPojoProcessor;ranking=800
-org.apache.tuscany.sca.implementation.java.introspect.impl.PolicyProcessor;ranking=700
+org.apache.tuscany.sca.implementation.java.introspect.impl.PolicyProcessor;ranking=700
\ No newline at end of file

Added: tuscany/sca-java-2.x/trunk/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessorTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessorTestCase.java?rev=981585&view=auto
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessorTestCase.java (added)
+++ tuscany/sca-java-2.x/trunk/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessorTestCase.java Mon Aug  2 16:27:23 2010
@@ -0,0 +1,96 @@
+/*
+ * 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.implementation.java.introspect.impl;
+
+import static org.apache.tuscany.sca.implementation.java.introspect.impl.ModelHelper.getService;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+import javax.jws.WebService;
+
+import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory;
+import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry;
+import org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory;
+import org.apache.tuscany.sca.implementation.java.IntrospectionException;
+import org.apache.tuscany.sca.implementation.java.JavaImplementation;
+import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory;
+import org.apache.tuscany.sca.interfacedef.InvalidCallbackException;
+import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory;
+import org.junit.Before;
+import org.junit.Test;
+import org.oasisopen.sca.ServiceReference;
+import org.oasisopen.sca.annotation.Callback;
+import org.oasisopen.sca.annotation.Service;
+
+/**
+ * @version $Rev: 826368 $ $Date: 2009-10-18 08:22:23 +0100 (Sun, 18 Oct 2009) $
+ */
+public class JAXWSProcessorTestCase {
+    private JAXWSProcessor processor;
+    private JavaImplementationFactory javaImplementationFactory;
+
+    @Before
+    public void setUp() throws Exception {
+        ExtensionPointRegistry registry = new DefaultExtensionPointRegistry();
+        processor = new JAXWSProcessor(new DefaultAssemblyFactory(), new DefaultJavaInterfaceFactory(registry));
+        javaImplementationFactory = new DefaultJavaImplementationFactory();
+    }
+
+    @Test
+    public void testWebServiceName() throws Exception {
+        JavaImplementation type = javaImplementationFactory.createJavaImplementation();
+        processor.visitClass(Foo1Impl.class, type);
+        org.apache.tuscany.sca.assembly.Service service = getService(type, "Foo1");
+        assertNotNull(service);
+    }
+    
+    @Test
+    public void testWebServiceEP() throws Exception {
+        JavaImplementation type = javaImplementationFactory.createJavaImplementation();
+        processor.visitClass(Foo2Impl.class, type);
+        org.apache.tuscany.sca.assembly.Service service = getService(type, "Foo2");
+        assertNotNull(service);
+    }    
+
+    @WebService(name="Foo1")
+    private static class Foo1Impl{
+        public String doSomething(String aParam){
+            return null;
+        }
+    }
+    
+    private interface Foo2 {
+        public String doSomething(String aParam);
+    }
+    
+    @WebService(name="Foo2", endpointInterface="Foo2")
+    private static class Foo2Impl{
+        public String doSomething(String aParam){
+            return null;
+        }
+    }    
+    
+
+}