Add support for static resources method information
Project: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/repo Commit: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/commit/8ef88acf Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/8ef88acf Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/8ef88acf Branch: refs/heads/master Commit: 8ef88acf2db60b1b6ed232d07c3d88a4ecb217f2 Parents: 0c2fbeb Author: Carlos Sierra <csie...@apache.org> Authored: Tue Jan 30 09:16:39 2018 +0100 Committer: Carlos Sierra <csie...@apache.org> Committed: Tue Jan 30 09:16:39 2018 +0100 ---------------------------------------------------------------------- jax-rs.itests/src/main/java/test/JaxrsTest.java | 50 ++++++++++- .../internal/AriesJaxRSServiceRuntime.java | 93 ++++++++++++++++---- .../internal/CXFJaxRsServiceRegistrator.java | 39 ++++++-- .../jax/rs/whiteboard/internal/Whiteboard.java | 47 +++++----- .../introspection/ClassIntrospector.java | 5 +- .../introspection/ClassIntrospectorTest.java | 24 +++-- 6 files changed, 204 insertions(+), 54 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/8ef88acf/jax-rs.itests/src/main/java/test/JaxrsTest.java ---------------------------------------------------------------------- diff --git a/jax-rs.itests/src/main/java/test/JaxrsTest.java b/jax-rs.itests/src/main/java/test/JaxrsTest.java index 9bb74c7..d2aa9f3 100644 --- a/jax-rs.itests/src/main/java/test/JaxrsTest.java +++ b/jax-rs.itests/src/main/java/test/JaxrsTest.java @@ -121,6 +121,29 @@ public class JaxrsTest extends TestHelper { assertEquals("Hello application", response.readEntity(String.class)); + + RuntimeDTO runtimeDTO = _runtime.getRuntimeDTO(); + + ApplicationDTO[] applicationDTOs = runtimeDTO.applicationDTOs; + + assertEquals(1, applicationDTOs.length); + + ApplicationDTO applicationDTO = applicationDTOs[0]; + ResourceMethodInfoDTO[] resourceMethods = + applicationDTO.resourceMethods; + + assertEquals(1, resourceMethods.length); + + ResourceMethodInfoDTO resourceMethod = resourceMethods[0]; + assertEquals(HttpMethod.GET, resourceMethod.method); + assertEquals("/", resourceMethod.path); + assertArrayEquals( + new String[]{MediaType.WILDCARD}, resourceMethod.consumingMimeType); + assertArrayEquals( + new String[]{MediaType.TEXT_PLAIN}, + resourceMethod.producingMimeType); + assertArrayEquals(new String[0], resourceMethod.nameBindings); + } @Test @@ -193,6 +216,29 @@ public class JaxrsTest extends TestHelper { assertEquals( "Hello extended", webTarget.request().get().readEntity(String.class)); + + RuntimeDTO runtimeDTO = _runtime.getRuntimeDTO(); + + ApplicationDTO[] applicationDTOs = runtimeDTO.applicationDTOs; + + assertEquals(1, applicationDTOs.length); + + ApplicationDTO applicationDTO = applicationDTOs[0]; + + ResourceMethodInfoDTO[] resourceMethods = + applicationDTO.resourceMethods; + + assertEquals(1, resourceMethods.length); + + ResourceMethodInfoDTO resourceMethod = resourceMethods[0]; + assertEquals(HttpMethod.GET, resourceMethod.method); + assertEquals("/", resourceMethod.path); + assertArrayEquals( + new String[]{MediaType.WILDCARD}, resourceMethod.consumingMimeType); + assertArrayEquals( + new String[]{MediaType.TEXT_PLAIN}, + resourceMethod.producingMimeType); + assertArrayEquals(new String[0], resourceMethod.nameBindings); } @Test @@ -653,12 +699,12 @@ public class JaxrsTest extends TestHelper { ServiceRegistration<?> erroredRegistration = registerApplication( new TestApplication() { - + @Override public Set<Object> getSingletons() { throw new RuntimeException(); } - + }, "service.ranking", 10); runtimeDTO = getRuntimeDTO(); http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/8ef88acf/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AriesJaxRSServiceRuntime.java ---------------------------------------------------------------------- diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AriesJaxRSServiceRuntime.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AriesJaxRSServiceRuntime.java index c08ee69..ebfeb1a 100644 --- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AriesJaxRSServiceRuntime.java +++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/AriesJaxRSServiceRuntime.java @@ -27,7 +27,6 @@ import static org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants.JAX_RS_ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; @@ -50,6 +49,7 @@ import org.osgi.service.jaxrs.runtime.dto.FailedApplicationDTO; import org.osgi.service.jaxrs.runtime.dto.FailedExtensionDTO; import org.osgi.service.jaxrs.runtime.dto.FailedResourceDTO; import org.osgi.service.jaxrs.runtime.dto.ResourceDTO; +import org.osgi.service.jaxrs.runtime.dto.ResourceMethodInfoDTO; import org.osgi.service.jaxrs.runtime.dto.RuntimeDTO; import org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants; import org.slf4j.Logger; @@ -338,24 +338,32 @@ public class AriesJaxrsServiceRuntime implements JaxrsServiceRuntime { return _shadowedApplications.remove(serviceReference); } - public Map<String, Object> setApplicationForPath( - String path, Map<String, Object> properties) { + public ApplicationRuntimeInformation setApplicationForPath( + String path, + CachingServiceReference<Application> serviceReference, + CXFJaxRsServiceRegistrator cxfJaxRsServiceRegistrator) { + + ApplicationRuntimeInformation ari = new ApplicationRuntimeInformation( + serviceReference, cxfJaxRsServiceRegistrator); return _applications.compute( path, (__, prop) -> { - if (DEFAULT_NAME.equals(getApplicationName(properties::get))) { - _defaultApplicationProperties = properties; + if (DEFAULT_NAME.equals( + getApplicationName( + ari._cachingServiceReference::getProperty))) { + + _defaultApplicationProperties = ari; } - return properties; + return ari; }); } - public Map<String, Object> unsetApplicationForPath(String path) { + public ApplicationRuntimeInformation unsetApplicationForPath(String path) { return _applications.remove(path); } - private ConcurrentHashMap<String, Map<String, Object>> + private ConcurrentHashMap<String, ApplicationRuntimeInformation> _applications = new ConcurrentHashMap<>(); private ConcurrentHashMap<String, Collection<EndpointRuntimeInformation>> _applicationEndpoints = new ConcurrentHashMap<>(); @@ -389,7 +397,7 @@ public class AriesJaxrsServiceRuntime implements JaxrsServiceRuntime { new CopyOnWriteArrayList<>(); private Collection<CachingServiceReference<?>> _invalidExtensions = new CopyOnWriteArrayList<>(); - private volatile Map<String, Object> _defaultApplicationProperties; + private volatile ApplicationRuntimeInformation _defaultApplicationProperties; private static FailedApplicationDTO buildFailedApplicationDTO( int reason, CachingServiceReference<Application> serviceReference) { @@ -467,7 +475,10 @@ public class AriesJaxrsServiceRuntime implements JaxrsServiceRuntime { resourceDTO.resourceMethods = ClassIntrospector.getResourceMethodInfos( endpointRuntimeInformation._class, - endpointRuntimeInformation._bus); + endpointRuntimeInformation._bus + ).toArray( + new ResourceMethodInfoDTO[0] + ); return resourceDTO; } @@ -490,7 +501,8 @@ public class AriesJaxrsServiceRuntime implements JaxrsServiceRuntime { private Stream<ApplicationDTO> applicationDTOStream() { return _applications.values().stream(). - filter(p -> !(".default".equals(p.get(JAX_RS_NAME)))). + filter(p -> !(".default".equals( + p._cachingServiceReference.getProperty(JAX_RS_NAME)))). map( this::buildApplicationDTO ); @@ -511,13 +523,16 @@ public class AriesJaxrsServiceRuntime implements JaxrsServiceRuntime { } private ApplicationDTO buildApplicationDTO( - Map<String, Object> properties) { + ApplicationRuntimeInformation ari) { ApplicationDTO applicationDTO = new ApplicationDTO(){}; - applicationDTO.name = getApplicationName(properties::get); - applicationDTO.base = getApplicationBase(properties::get); - applicationDTO.serviceId = (Long)properties.get("service.id"); + applicationDTO.name = getApplicationName( + ari._cachingServiceReference::getProperty); + applicationDTO.base = getApplicationBase( + ari._cachingServiceReference::getProperty); + applicationDTO.serviceId = + (Long)ari._cachingServiceReference.getProperty("service.id"); applicationDTO.resourceDTOs = getApplicationEndpointsStream( applicationDTO.name).toArray( @@ -529,6 +544,24 @@ public class AriesJaxrsServiceRuntime implements JaxrsServiceRuntime { ExtensionDTO[]::new ); + CXFJaxRsServiceRegistrator cxfJaxRsServiceRegistrator = + ari._cxfJaxRsServiceRegistrator; + + Bus bus = cxfJaxRsServiceRegistrator.getBus(); + Iterable<Class<?>> resourceClasses = + cxfJaxRsServiceRegistrator.getStaticResourceClasses(); + + ArrayList<ResourceMethodInfoDTO> resourceMethodInfoDTOS = + new ArrayList<>(); + + for (Class<?> resourceClass : resourceClasses) { + resourceMethodInfoDTOS.addAll( + ClassIntrospector.getResourceMethodInfos(resourceClass, bus)); + } + + applicationDTO.resourceMethods = resourceMethodInfoDTOS.toArray( + new ResourceMethodInfoDTO[0]); + return applicationDTO; } @@ -700,4 +733,34 @@ public class AriesJaxrsServiceRuntime implements JaxrsServiceRuntime { Class<?> _class; } + private static class ApplicationRuntimeInformation { + public ApplicationRuntimeInformation( + CachingServiceReference cachingServiceReference, + CXFJaxRsServiceRegistrator cxfJaxRsServiceRegistrator) { + + _cachingServiceReference = cachingServiceReference; + _cxfJaxRsServiceRegistrator = cxfJaxRsServiceRegistrator; + } + + @Override + public int hashCode() { + return _cachingServiceReference.hashCode(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + EndpointRuntimeInformation that = (EndpointRuntimeInformation) o; + + return _cachingServiceReference.equals( + that._cachingServiceReference); + } + + CachingServiceReference _cachingServiceReference; + CXFJaxRsServiceRegistrator _cxfJaxRsServiceRegistrator; + + } + } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/8ef88acf/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/CXFJaxRsServiceRegistrator.java ---------------------------------------------------------------------- diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/CXFJaxRsServiceRegistrator.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/CXFJaxRsServiceRegistrator.java index 2adf92b..a92f714 100644 --- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/CXFJaxRsServiceRegistrator.java +++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/CXFJaxRsServiceRegistrator.java @@ -23,6 +23,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.TreeSet; import java.util.stream.Collectors; @@ -40,6 +41,7 @@ import org.apache.aries.osgi.functional.CachingServiceReference; import org.apache.cxf.Bus; import org.apache.cxf.endpoint.Server; import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; +import org.apache.cxf.jaxrs.JAXRSServiceFactoryBean; import org.apache.cxf.jaxrs.ext.ResourceComparator; import org.apache.cxf.jaxrs.impl.ConfigurableImpl; import org.apache.cxf.jaxrs.lifecycle.ResourceProvider; @@ -159,6 +161,7 @@ public class CXFJaxRsServiceRegistrator { private Map<String, Object> _properties; private volatile boolean _closed = false; private Server _server; + private JAXRSServerFactoryBean _jaxRsServerFactoryBean; private static class ComparableResourceComparator implements ResourceComparator { @@ -217,10 +220,10 @@ public class CXFJaxRsServiceRegistrator { return; } - JAXRSServerFactoryBean jaxRsServerFactoryBean = createEndpoint( + _jaxRsServerFactoryBean = createEndpoint( _application, JAXRSServerFactoryBean.class); - jaxRsServerFactoryBean.setBus(_bus); + _jaxRsServerFactoryBean.setBus(_bus); _bus.setExtension( context -> { @@ -237,9 +240,9 @@ public class CXFJaxRsServiceRegistrator { }, ServerConfigurableFactory.class); - jaxRsServerFactoryBean.setStart(false); + _jaxRsServerFactoryBean.setStart(false); - jaxRsServerFactoryBean.setProvider( + _jaxRsServerFactoryBean.setProvider( (Feature) featureContext -> { for (ServiceTuple<?> provider : _providers) { CachingServiceReference<?> cachingServiceReference = @@ -290,17 +293,17 @@ public class CXFJaxRsServiceRegistrator { }); for (ResourceProvider resourceProvider: _services) { - jaxRsServerFactoryBean.setResourceProvider(resourceProvider); + _jaxRsServerFactoryBean.setResourceProvider(resourceProvider); } - if (jaxRsServerFactoryBean.getResourceClasses().isEmpty()) { + if (_jaxRsServerFactoryBean.getResourceClasses().isEmpty()) { return; } - jaxRsServerFactoryBean.setResourceComparator( + _jaxRsServerFactoryBean.setResourceComparator( new ComparableResourceComparator()); - _server = jaxRsServerFactoryBean.create(); + _server = _jaxRsServerFactoryBean.create(); ApplicationInfo applicationInfo = (ApplicationInfo) _server.getEndpoint().get(Application.class.getName()); @@ -312,4 +315,24 @@ public class CXFJaxRsServiceRegistrator { _server.start(); } + protected Iterable<Class<?>> getStaticResourceClasses() { + JAXRSServiceFactoryBean serviceFactory = + _jaxRsServerFactoryBean.getServiceFactory(); + + List<ClassResourceInfo> classResourceInfo = + serviceFactory.getClassResourceInfo(); + + ArrayList<Class<?>> classes = new ArrayList<>(); + + for (ClassResourceInfo resourceInfo : classResourceInfo) { + if (!ServiceReferenceResourceProvider.class.isAssignableFrom( + resourceInfo.getResourceProvider().getClass())) { + + classes.add(resourceInfo.getResourceClass()); + } + } + + return classes; + } + } http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/8ef88acf/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java ---------------------------------------------------------------------- diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java index 01ce73c..17e9d80 100644 --- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java +++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Whiteboard.java @@ -257,17 +257,19 @@ public class Whiteboard { ).then( nothing() ) - ).flatMap( - this::deployApplication - ).map( - ServiceTuple::getCachingServiceReference - ).map( - Utils::getProperties - ).foreach( - p -> _runtime.setApplicationForPath( - getApplicationBase(p::get), p), - p -> _runtime.unsetApplicationForPath( - getApplicationBase(p::get)) + ).flatMap(at -> + deployApplication(at). + foreach( + registrator -> + _runtime.setApplicationForPath( + getApplicationBase( + at.getCachingServiceReference()::getProperty), + at.getCachingServiceReference(), registrator), + registrator -> + _runtime.unsetApplicationForPath( + getApplicationBase( + at.getCachingServiceReference()::getProperty)) + ) ); } @@ -300,7 +302,7 @@ public class Whiteboard { ); } - private OSGi<ServiceTuple<Application>> deployApplication( + private OSGi<CXFJaxRsServiceRegistrator> deployApplication( ServiceTuple<Application> tuple) { return @@ -318,21 +320,24 @@ public class Whiteboard { return properties; }).flatMap(properties -> - deployRegistrator(bus, tuple, properties).then( - registerCXFServletService(bus, properties)).then( - just(tuple) - ))); + deployRegistrator(bus, tuple, properties).flatMap(registrator -> + registerCXFServletService(bus, properties).then( + just(registrator) + )))); } - private OSGi<?> deployRegistrator( - Bus bus, ServiceTuple<Application> tuple, Map<String, Object> props) { + private OSGi<CXFJaxRsServiceRegistrator> deployRegistrator( + Bus bus, ServiceTuple<Application> tuple, + Map<String, Object> props) { return - just(() -> new CXFJaxRsServiceRegistrator( - bus, tuple.getService(), props)). + just(() -> + new CXFJaxRsServiceRegistrator(bus, tuple.getService(), props)). flatMap(registrator -> onClose(registrator::close).then( - register(CXFJaxRsServiceRegistrator.class, registrator, props))); + register(CXFJaxRsServiceRegistrator.class, registrator, props).then( + just(registrator) + ))); } private OSGi<CachingServiceReference<Object>> http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/8ef88acf/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospector.java ---------------------------------------------------------------------- diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospector.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospector.java index 6a3ff7f..e596b30 100644 --- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospector.java +++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospector.java @@ -29,6 +29,7 @@ import javax.ws.rs.Path; import javax.ws.rs.core.MediaType; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -41,7 +42,7 @@ public class ClassIntrospector { private static final List<MediaType> _ALL_TYPES_LIST = Collections.singletonList(JAXRSUtils.ALL_TYPES); - public static ResourceMethodInfoDTO[] getResourceMethodInfos( + public static Collection<ResourceMethodInfoDTO> getResourceMethodInfos( Class<?> clazz, Bus bus) { ClassResourceInfo classResourceInfo = @@ -52,7 +53,7 @@ public class ClassIntrospector { new HashSet<>(), "/", null, _ALL_TYPES_LIST, _ALL_TYPES_LIST, Collections.emptySet(), true, classResourceInfo); - return convert.toArray(ResourceMethodInfoDTO[]::new); + return convert.collect(Collectors.toList()); } private static Stream<ResourceMethodInfoDTO> convert( http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/8ef88acf/jax-rs.whiteboard/src/test/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospectorTest.java ---------------------------------------------------------------------- diff --git a/jax-rs.whiteboard/src/test/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospectorTest.java b/jax-rs.whiteboard/src/test/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospectorTest.java index de35ab6..2cfea49 100644 --- a/jax-rs.whiteboard/src/test/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospectorTest.java +++ b/jax-rs.whiteboard/src/test/java/org/apache/aries/jax/rs/whiteboard/internal/introspection/ClassIntrospectorTest.java @@ -49,7 +49,9 @@ public class ClassIntrospectorTest { Bus bus = BusFactory.getDefaultBus(true); ResourceMethodInfoDTO[] resourceMethodInfoDTOS = - ClassIntrospector.getResourceMethodInfos(PlainResource.class, bus); + ClassIntrospector.getResourceMethodInfos(PlainResource.class, bus).toArray( + new ResourceMethodInfoDTO[0] + ); assertEquals(1, resourceMethodInfoDTOS.length); @@ -71,7 +73,9 @@ public class ClassIntrospectorTest { ResourceMethodInfoDTO[] resourceMethodInfoDTOS = ClassIntrospector.getResourceMethodInfos( - PlainResourceSeveralOperationsWithNameBinding.class, bus); + PlainResourceSeveralOperationsWithNameBinding.class, bus).toArray( + new ResourceMethodInfoDTO[0] + ); assertEquals(2, resourceMethodInfoDTOS.length); @@ -108,7 +112,9 @@ public class ClassIntrospectorTest { ResourceMethodInfoDTO[] resourceMethodInfoDTOS = ClassIntrospector.getResourceMethodInfos( - PlainResourceSeveralOperations.class, bus); + PlainResourceSeveralOperations.class, bus).toArray( + new ResourceMethodInfoDTO[0] + ); assertEquals(2, resourceMethodInfoDTOS.length); @@ -140,7 +146,9 @@ public class ClassIntrospectorTest { ResourceMethodInfoDTO[] resourceMethodInfoDTOS = ClassIntrospector.getResourceMethodInfos( - PlainResourceSeveralOperationsCommonPath.class, bus); + PlainResourceSeveralOperationsCommonPath.class, bus).toArray( + new ResourceMethodInfoDTO[0] + ); assertEquals(2, resourceMethodInfoDTOS.length); @@ -172,7 +180,9 @@ public class ClassIntrospectorTest { ResourceMethodInfoDTO[] resourceMethodInfoDTOS = ClassIntrospector.getResourceMethodInfos( - PlainResourceSeveralOperationsDifferentPath.class, bus); + PlainResourceSeveralOperationsDifferentPath.class, bus).toArray( + new ResourceMethodInfoDTO[0] + ); assertEquals(2, resourceMethodInfoDTOS.length); @@ -204,7 +214,9 @@ public class ClassIntrospectorTest { ResourceMethodInfoDTO[] resourceMethodInfoDTOS = ClassIntrospector.getResourceMethodInfos( - ResourceWithSubResource.class, bus); + ResourceWithSubResource.class, bus).toArray( + new ResourceMethodInfoDTO[0] + ); assertEquals(5, resourceMethodInfoDTOS.length);