From 4fa9cb86b99f147ab8f42588dd03383fd317b090 Mon Sep 17 00:00:00 2001 From: slaws Date: Mon, 2 Aug 2010 16:27:23 +0000 Subject: 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. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@981585 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/introspect/impl/JAXWSProcessor.java | 131 +++++++++++++++++++++ ...implementation.java.introspect.JavaClassVisitor | 3 +- .../introspect/impl/JAXWSProcessorTestCase.java | 96 +++++++++++++++ 3 files changed, 229 insertions(+), 1 deletion(-) create mode 100644 sca-java-2.x/trunk/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessor.java create mode 100644 sca-java-2.x/trunk/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessorTestCase.java (limited to 'sca-java-2.x/trunk/modules') diff --git a/sca-java-2.x/trunk/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessor.java b/sca-java-2.x/trunk/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessor.java new file mode 100644 index 0000000000..db824d658e --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessor.java @@ -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 void visitClass(Class 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; + } + +} diff --git a/sca-java-2.x/trunk/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor b/sca-java-2.x/trunk/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor index 676dd272e1..d0723f466c 100644 --- a/sca-java-2.x/trunk/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor +++ b/sca-java-2.x/trunk/modules/implementation-java/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor @@ -29,5 +29,6 @@ org.apache.tuscany.sca.implementation.java.introspect.impl.ReferenceProcessor;ra 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 diff --git a/sca-java-2.x/trunk/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessorTestCase.java b/sca-java-2.x/trunk/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessorTestCase.java new file mode 100644 index 0000000000..3fd113f761 --- /dev/null +++ b/sca-java-2.x/trunk/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/JAXWSProcessorTestCase.java @@ -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; + } + } + + +} -- cgit v1.2.3