From d683dc4b30c79348b5e73f0f11b1cfd8ce4b9812 Mon Sep 17 00:00:00 2001 From: rfeng Date: Wed, 19 May 2010 00:53:52 +0000 Subject: Merge branch 'jaxrs' into trunk git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@945980 13f79535-47bb-0310-9956-ffa450edef68 --- .../jaxrs/impl/JAXRSImplementationImpl.java | 9 ---- .../jaxrs/xml/JAXRSImplementationProcessor.java | 55 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 9 deletions(-) (limited to 'sca-java-2.x/trunk/modules/implementation-jaxrs/src/main') diff --git a/sca-java-2.x/trunk/modules/implementation-jaxrs/src/main/java/org/apache/tuscany/sca/implementation/jaxrs/impl/JAXRSImplementationImpl.java b/sca-java-2.x/trunk/modules/implementation-jaxrs/src/main/java/org/apache/tuscany/sca/implementation/jaxrs/impl/JAXRSImplementationImpl.java index 99f6f8df59..4c19f1b52e 100644 --- a/sca-java-2.x/trunk/modules/implementation-jaxrs/src/main/java/org/apache/tuscany/sca/implementation/jaxrs/impl/JAXRSImplementationImpl.java +++ b/sca-java-2.x/trunk/modules/implementation-jaxrs/src/main/java/org/apache/tuscany/sca/implementation/jaxrs/impl/JAXRSImplementationImpl.java @@ -18,10 +18,6 @@ */ package org.apache.tuscany.sca.implementation.jaxrs.impl; -import java.util.Collections; -import java.util.List; - -import org.apache.tuscany.sca.assembly.Service; import org.apache.tuscany.sca.assembly.impl.ImplementationImpl; import org.apache.tuscany.sca.implementation.jaxrs.JAXRSImplementation; @@ -40,11 +36,6 @@ public class JAXRSImplementationImpl extends ImplementationImpl implements JAXRS super(TYPE); } - public List getServices() { - // The Web implementation does not offer services - return Collections.emptyList(); - } - public String getApplication() { return application; } diff --git a/sca-java-2.x/trunk/modules/implementation-jaxrs/src/main/java/org/apache/tuscany/sca/implementation/jaxrs/xml/JAXRSImplementationProcessor.java b/sca-java-2.x/trunk/modules/implementation-jaxrs/src/main/java/org/apache/tuscany/sca/implementation/jaxrs/xml/JAXRSImplementationProcessor.java index 037d8f385a..da25b6edd5 100644 --- a/sca-java-2.x/trunk/modules/implementation-jaxrs/src/main/java/org/apache/tuscany/sca/implementation/jaxrs/xml/JAXRSImplementationProcessor.java +++ b/sca-java-2.x/trunk/modules/implementation-jaxrs/src/main/java/org/apache/tuscany/sca/implementation/jaxrs/xml/JAXRSImplementationProcessor.java @@ -20,11 +20,17 @@ package org.apache.tuscany.sca.implementation.jaxrs.xml; import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import javax.ws.rs.Path; +import javax.ws.rs.core.Application; import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.binding.rest.RESTBinding; +import org.apache.tuscany.sca.binding.rest.RESTBindingFactory; import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; @@ -37,6 +43,11 @@ import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.implementation.jaxrs.JAXRSImplementation; import org.apache.tuscany.sca.implementation.jaxrs.JAXRSImplementationFactory; +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.oasisopen.sca.ServiceRuntimeException; /** * Implements a StAX artifact processor for Web implementations. @@ -45,11 +56,17 @@ public class JAXRSImplementationProcessor extends BaseStAXArtifactProcessor impl StAXArtifactProcessor { private static final QName IMPLEMENTATION_JAXRS = JAXRSImplementation.TYPE; + private AssemblyFactory assemblyFactory; private JAXRSImplementationFactory implementationFactory; + private RESTBindingFactory restBindingFactory; + private JavaInterfaceFactory javaInterfaceFactory; public JAXRSImplementationProcessor(ExtensionPointRegistry extensionPoints) { FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); + this.assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); this.implementationFactory = modelFactories.getFactory(JAXRSImplementationFactory.class); + this.restBindingFactory = modelFactories.getFactory(RESTBindingFactory.class); + this.javaInterfaceFactory = modelFactories.getFactory(JavaInterfaceFactory.class); } public QName getArtifactType() { @@ -91,6 +108,44 @@ public class JAXRSImplementationProcessor extends BaseStAXArtifactProcessor impl classReference = resolver.resolveModel(ClassReference.class, classReference, context); implementation.setApplicationClass(classReference.getJavaClass()); implementation.setUnresolved(false); + + Application application; + try { + application = (Application) implementation.getApplicationClass().newInstance(); + } catch (Exception e) { + throw new ContributionResolveException(e); + } + + for(Class rootResourceClass: application.getClasses()) { + addService(implementation, rootResourceClass); + } + for(Object rootResource: application.getSingletons()) { + addService(implementation, rootResource.getClass()); + } + + } + + private void addService(JAXRSImplementation implementation, Class rootResourceClass) { + Service service = assemblyFactory.createService(); + JavaInterfaceContract contract = javaInterfaceFactory.createJavaInterfaceContract(); + JavaInterface javaInterface; + try { + javaInterface = javaInterfaceFactory.createJavaInterface(rootResourceClass); + } catch (InvalidInterfaceException e) { + throw new ServiceRuntimeException(e); + } + contract.setInterface(javaInterface); + service.setInterfaceContract(contract); + RESTBinding binding = restBindingFactory.createRESTBinding(); + // FIXME: The @ApplicationPath is available for JAX-RS 1.1 + // binding.setURI("/"); + Path path = rootResourceClass.getAnnotation(Path.class); + if (path != null) { + binding.setURI(path.value()); + } + service.getBindings().add(binding); + service.setName(rootResourceClass.getSimpleName()); + implementation.getServices().add(service); } public void write(JAXRSImplementation implementation, XMLStreamWriter writer, ProcessorContext context) -- cgit v1.2.3