You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by cs...@apache.org on 2017/08/25 15:25:42 UTC
[1/8] aries-jax-rs-whiteboard git commit: Refactor to reuse logic and
generate DTOs
Repository: aries-jax-rs-whiteboard
Updated Branches:
refs/heads/master 31bf73834 -> 432486ac7
Refactor to reuse logic and generate DTOs
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/216070bb
Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/216070bb
Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/216070bb
Branch: refs/heads/master
Commit: 216070bb24dc179e406af6c6043218f2259a8265
Parents: 31bf738
Author: Carlos Sierra <cs...@apache.org>
Authored: Tue Aug 22 14:02:40 2017 +0200
Committer: Carlos Sierra <cs...@apache.org>
Committed: Tue Aug 22 14:22:23 2017 +0200
----------------------------------------------------------------------
jax-rs.itests/itest.bndrun | 2 +-
jax-rs.itests/src/main/java/test/JaxrsTest.java | 25 +-
.../main/java/test/WhiteboardFactoryTest.java | 2 -
.../internal/AriesJaxRSServiceRuntime.java | 235 +++++++++++++++++--
.../internal/CXFJaxRsServiceRegistrator.java | 8 +-
.../aries/jax/rs/whiteboard/internal/Utils.java | 126 ++++++----
.../jax/rs/whiteboard/internal/Whiteboard.java | 102 ++++----
7 files changed, 374 insertions(+), 126 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/216070bb/jax-rs.itests/itest.bndrun
----------------------------------------------------------------------
diff --git a/jax-rs.itests/itest.bndrun b/jax-rs.itests/itest.bndrun
index c74b820..203616a 100644
--- a/jax-rs.itests/itest.bndrun
+++ b/jax-rs.itests/itest.bndrun
@@ -43,4 +43,4 @@
org.osgi.service.http;version='[1.2.1,1.2.2)',\
org.osgi.service.http.whiteboard;version='[1.0.0,1.0.1)'
-#-runvm: "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
+-runvm: "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/216070bb/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 2f1439e..ecf817a 100644
--- a/jax-rs.itests/src/main/java/test/JaxrsTest.java
+++ b/jax-rs.itests/src/main/java/test/JaxrsTest.java
@@ -188,11 +188,7 @@ public class JaxrsTest extends TestHelper {
ServiceRegistration<?> applicationRegistration = null;
- JaxRSServiceRuntime jaxRSServiceRuntime = getJaxRSServiceRuntime();
-
try {
-
-
applicationRegistration = registerApplication(
new TestApplication());
@@ -306,7 +302,8 @@ public class JaxrsTest extends TestHelper {
applicationRegistration = registerApplication(
new TestApplication());
- filterRegistration = registerFilter(
+ filterRegistration = registerExtension(
+ "filter",
JAX_RS_APPLICATION_SELECT,
"(" + JAX_RS_APPLICATION_BASE + "=/test-application)");
@@ -354,7 +351,8 @@ public class JaxrsTest extends TestHelper {
ServiceRegistration<?> filterRegistration = null;
try {
- filterRegistration = registerFilter(
+ filterRegistration = registerExtension(
+ "Filter",
JAX_RS_APPLICATION_SELECT,
"(" + JAX_RS_APPLICATION_BASE + "=/test-application)");
@@ -785,8 +783,8 @@ public class JaxrsTest extends TestHelper {
try {
serviceRegistration = registerAddon(new TestAddon());
- filterRegistration = registerFilter(
- JAX_RS_EXTENSION, "test-filter");
+ filterRegistration = registerExtension(
+ "Filter", JAX_RS_EXTENSION, "test-filter");
Response response = webTarget.request().get();
@@ -830,8 +828,8 @@ public class JaxrsTest extends TestHelper {
assertNull(response.getHeaders().getFirst("Filtered"));
- filterRegistration = registerFilter(
- JAX_RS_EXTENSION, "test-filter");
+ filterRegistration = registerExtension(
+ "Filter", JAX_RS_EXTENSION, "test-filter");
response = webTarget.request().get();
@@ -955,7 +953,8 @@ public class JaxrsTest extends TestHelper {
Dictionary<String, Object> properties = new Hashtable<>();
- properties.put(JAX_RS_EXTENSION, name);
+ properties.put(JAX_RS_EXTENSION, true);
+ properties.put(JAX_RS_NAME, name);
for (int i = 0; i < keyValues.length; i = i + 2) {
properties.put(keyValues[i].toString(), keyValues[i + 1]);
@@ -965,7 +964,7 @@ public class JaxrsTest extends TestHelper {
Object.class, testFilter, properties);
}
- private ServiceRegistration<?> registerFilter(Object... keyValues) {
+ /*private ServiceRegistration<?> registerFilter(Object... keyValues) {
TestFilter testFilter = new TestFilter();
@@ -977,7 +976,7 @@ public class JaxrsTest extends TestHelper {
return bundleContext.registerService(
Object.class, testFilter, properties);
- }
+ }*/
private ServiceRegistration<Application> registerUngettableApplication(
Object... keyValues) {
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/216070bb/jax-rs.itests/src/main/java/test/WhiteboardFactoryTest.java
----------------------------------------------------------------------
diff --git a/jax-rs.itests/src/main/java/test/WhiteboardFactoryTest.java b/jax-rs.itests/src/main/java/test/WhiteboardFactoryTest.java
index 1534fb9..2c74292 100644
--- a/jax-rs.itests/src/main/java/test/WhiteboardFactoryTest.java
+++ b/jax-rs.itests/src/main/java/test/WhiteboardFactoryTest.java
@@ -22,9 +22,7 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN;
import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.JAX_RS_APPLICATION_BASE;
-import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.JAX_RS_RESOURCE;
-import java.util.Arrays;
import java.util.Dictionary;
import java.util.Hashtable;
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/216070bb/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 67166b0..140e82e 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
@@ -17,18 +17,20 @@
package org.apache.aries.jax.rs.whiteboard.internal;
-import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.jaxrs.runtime.JaxRSServiceRuntime;
import org.osgi.service.jaxrs.runtime.dto.ApplicationDTO;
import org.osgi.service.jaxrs.runtime.dto.DTOConstants;
+import org.osgi.service.jaxrs.runtime.dto.ExtensionDTO;
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.RequestInfoDTO;
+import org.osgi.service.jaxrs.runtime.dto.ResourceDTO;
import org.osgi.service.jaxrs.runtime.dto.RuntimeDTO;
import org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants;
import javax.ws.rs.core.Application;
-import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
@@ -43,28 +45,57 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
private ConcurrentHashMap<String, ServiceReference<Application>>
_applications = new ConcurrentHashMap<>();
+ private ConcurrentHashMap<String, Set<ServiceReference<?>>>
+ _applicationEndpoints = new ConcurrentHashMap<>();
+
+ private ConcurrentHashMap<String, Set<ServiceReference<?>>>
+ _applicationExtensions = new ConcurrentHashMap<>();
+
private TreeSet<ServiceReference<Application>>
- _ungettableApplications;
+ _ungettableApplications = new TreeSet<>();
private Set<ServiceReference<Application>> _shadowedApplications =
new TreeSet<>();
- public AriesJaxRSServiceRuntime(BundleContext bundleContext) {
- _ungettableApplications = new TreeSet<>(Comparator.reverseOrder());
- }
+ private TreeSet<ServiceReference<?>> _ungettableEndpoints = new TreeSet<>();
+ private TreeSet<ServiceReference<?>> _ungettableExtensions = new TreeSet<>();
- public boolean addNotGettable(
+ public boolean addNotGettableApplication(
ServiceReference<Application> serviceReference) {
return _ungettableApplications.add(serviceReference);
}
- public boolean removeNotGettable(
+ public <T> void addNotGettableExtension(
+ ServiceReference<T> serviceReference) {
+
+ _ungettableExtensions.add(serviceReference);
+ }
+
+ public boolean removeNotGettableApplication(
ServiceReference<Application> serviceReference) {
return _ungettableApplications.remove(serviceReference);
}
+ public <T> boolean removeNotGettableEndpoint(
+ ServiceReference<T> serviceReference) {
+
+ return _ungettableEndpoints.remove(serviceReference);
+ }
+
+ public <T> boolean addNotGettableEndpoint(
+ ServiceReference<T> serviceReference) {
+
+ return _ungettableEndpoints.add(serviceReference);
+ }
+
+ public <T> void removeNotGettableExtension(
+ ServiceReference<T> serviceReference) {
+
+ _ungettableExtensions.remove(serviceReference);
+ }
+
public ServiceReference<Application> setApplicationForPath(
String path, ServiceReference<Application> serviceReference) {
@@ -87,6 +118,61 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
return _shadowedApplications.remove(serviceReference);
}
+ public void addApplicationEndpoint(
+ String applicationName, ServiceReference<?> endpointServiceReference) {
+
+ _applicationEndpoints.computeIfAbsent(
+ applicationName, __ -> new TreeSet<>());
+
+ _applicationEndpoints.computeIfPresent(
+ applicationName,
+ (__, set) -> {
+ set.add(endpointServiceReference);
+
+ return set;
+ });
+ }
+
+ public void removeApplicationEndpoint(
+ String applicationName, ServiceReference<?> endpointServiceReference) {
+
+ _applicationEndpoints.computeIfPresent(
+ applicationName,
+ (__, set) -> {
+ set.remove(endpointServiceReference);
+
+ return set;
+ });
+ }
+
+ public void addApplicationExtension(
+ String applicationName,
+ ServiceReference<?> extensionServiceReference) {
+
+ _applicationExtensions.computeIfAbsent(
+ applicationName, __ -> new TreeSet<>());
+
+ _applicationExtensions.computeIfPresent(
+ applicationName,
+ (__, set) -> {
+ set.add(extensionServiceReference);
+
+ return set;
+ });
+ }
+
+ public void removeApplicationExtension(
+ String applicationName, ServiceReference<?> extensionServiceReference) {
+
+ _applicationExtensions.computeIfPresent(
+ applicationName,
+ (__, set) -> {
+ set.remove(extensionServiceReference);
+
+ return set;
+ });
+ }
+
@Override
public RequestInfoDTO calculateRequestInfoDTO(String path) {
return null;
@@ -102,23 +188,69 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
);
runtimeDTO.failedApplicationDTOs = Stream.concat(
- shadowedApplications(),
- unreferenciableApplications()
+ shadowedApplicationsDTOStream(),
+ unreferenciableApplicationsDTOStream()
).toArray(
FailedApplicationDTO[]::new
);
+ runtimeDTO.failedResourceDTOs = unreferenciableEndpointsDTOStream().map(
+ sr -> buildFailedResourceDTO(
+ DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE, sr)
+ ).toArray(
+ FailedResourceDTO[]::new
+ );
+
+ runtimeDTO.failedExtensionDTOs = unreferenciableExtensionsDTOStream().map(
+ sr -> buildFailedExtensionDTO(
+ DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE, sr)
+ ).toArray(
+ FailedExtensionDTO[]::new
+ );
+
return runtimeDTO;
}
+ private FailedExtensionDTO buildFailedExtensionDTO(
+ int reason, ServiceReference<?> serviceReference) {
+
+ FailedExtensionDTO failedExtensionDTO = new FailedExtensionDTO();
+
+ populateExtensionDTO(failedExtensionDTO, serviceReference);
+
+ failedExtensionDTO.failureReason = reason;
+
+ return failedExtensionDTO;
+ }
+
+ private Stream<ServiceReference<?>> unreferenciableExtensionsDTOStream() {
+ return _ungettableExtensions.stream();
+ }
+
+ private FailedResourceDTO buildFailedResourceDTO(
+ int reason, ServiceReference<?> serviceReference) {
+
+ FailedResourceDTO failedResourceDTO = new FailedResourceDTO();
+
+ populateResourceDTO(failedResourceDTO, serviceReference);
+
+ failedResourceDTO.failureReason = reason;
+
+ return failedResourceDTO;
+ }
+
+ private Stream<ServiceReference<?>> unreferenciableEndpointsDTOStream() {
+ return _ungettableEndpoints.stream();
+ }
+
private Stream<ApplicationDTO> applicationDTOStream() {
return _applications.values().stream().
map(
- AriesJaxRSServiceRuntime::buildApplicationDTO
+ this::buildApplicationDTO
);
}
- private Stream<FailedApplicationDTO> unreferenciableApplications() {
+ private Stream<FailedApplicationDTO> unreferenciableApplicationsDTOStream() {
return _ungettableApplications.stream().
map(
sr -> buildFailedApplicationDTO(
@@ -127,7 +259,7 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
);
}
- private Stream<FailedApplicationDTO> shadowedApplications() {
+ private Stream<FailedApplicationDTO> shadowedApplicationsDTOStream() {
return _shadowedApplications.stream().
map(sr -> buildFailedApplicationDTO(
DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE,
@@ -135,16 +267,81 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
);
}
- private static ApplicationDTO buildApplicationDTO(
+ private ApplicationDTO buildApplicationDTO(
ServiceReference<Application> serviceReference) {
ApplicationDTO applicationDTO = new ApplicationDTO(){};
applicationDTO.name = getApplicationName(serviceReference);
+ applicationDTO.base = Whiteboard.getApplicationBase(serviceReference);
+ applicationDTO.serviceId = (Long)serviceReference.getProperty(
+ "service.id");
+
+ applicationDTO.resourceDTOs = getApplicationEndpointsStream(
+ applicationDTO.name).toArray(
+ ResourceDTO[]::new
+ );
+
+ applicationDTO.extensionDTOs = getApplicationExtensionsStream(
+ applicationDTO.name).toArray(
+ ExtensionDTO[]::new
+ );
return applicationDTO;
}
+ private Stream<ResourceDTO> getApplicationEndpointsStream(String name) {
+
+ Set<ServiceReference<?>> applicationEndpoints =
+ _applicationEndpoints.get(name);
+
+ Stream<ServiceReference<?>> applicationEndpointStream =
+ applicationEndpoints != null ?
+ applicationEndpoints.stream() :
+ Stream.empty();
+
+ return
+ applicationEndpointStream.map(
+ sr -> populateResourceDTO(new ResourceDTO(){}, sr)
+ );
+ }
+
+ private Stream<ExtensionDTO> getApplicationExtensionsStream(String name) {
+
+ Set<ServiceReference<?>> applicationExtensions =
+ _applicationEndpoints.get(name);
+
+ Stream<ServiceReference<?>> applicationExtensionStream =
+ applicationExtensions != null ?
+ applicationExtensions.stream() :
+ Stream.empty();
+
+ return
+ applicationExtensionStream.map(
+ sr -> populateExtensionDTO(new ExtensionDTO(){}, sr)
+ );
+ }
+
+ private static ResourceDTO populateResourceDTO(
+ ResourceDTO resourceDTO, ServiceReference<?> serviceReference) {
+
+ resourceDTO.name = serviceReference.getProperty(JAX_RS_NAME).toString();
+ resourceDTO.serviceId = (Long)serviceReference.getProperty(
+ "service.id");
+
+ return resourceDTO;
+ }
+
+ private static ExtensionDTO populateExtensionDTO(
+ ExtensionDTO extensionDTO, ServiceReference<?> serviceReference) {
+
+ extensionDTO.name = serviceReference.getProperty(JAX_RS_NAME).toString();
+ extensionDTO.serviceId = (Long)serviceReference.getProperty(
+ "service.id");
+
+ return extensionDTO;
+ }
+
private static FailedApplicationDTO buildFailedApplicationDTO(
int reason, ServiceReference<Application> serviceReference) {
@@ -162,8 +359,8 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
return failedApplicationDTO;
}
- private static String getApplicationName(
- ServiceReference<Application> serviceReference) {
+ public static String getApplicationName(
+ ServiceReference<?> serviceReference) {
Object property = serviceReference.getProperty(JAX_RS_NAME);
@@ -174,10 +371,10 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
return property.toString();
}
- private static String generateApplicationName(
- ServiceReference<Application> serviceReference) {
+ public static String generateApplicationName(
+ ServiceReference<?> serviceReference) {
- return "jax-rs-application-" +
+ return ".jax-rs-application-" +
serviceReference.getProperty("service.id").toString();
}
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/216070bb/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 91c70f8..a43abba 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
@@ -80,12 +80,12 @@ public class CXFJaxRsServiceRegistrator {
rewire();
}
- public void addProvider(Object provider) {
+ public void addProvider(Utils.ServiceTuple<?> tuple) {
if (_closed) {
return;
}
- _providers.add(provider);
+ _providers.add(tuple.getService());
rewire();
}
@@ -112,12 +112,12 @@ public class CXFJaxRsServiceRegistrator {
rewire();
}
- public void removeProvider(Object provider) {
+ public void removeProvider(Utils.ServiceTuple<?> tuple) {
if (_closed) {
return;
}
- _providers.remove(provider);
+ _providers.remove(tuple.getService());
rewire();
}
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/216070bb/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
index 69310a4..0a343bf 100644
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
@@ -27,7 +27,6 @@ import org.osgi.framework.ServiceReference;
import org.osgi.service.jaxrs.runtime.dto.FailedApplicationDTO;
import javax.ws.rs.core.Application;
-import javax.ws.rs.ext.Provider;
import java.util.Comparator;
import java.util.Map;
import java.util.NavigableSet;
@@ -85,43 +84,79 @@ public class Utils {
public static OSGi<?> safeRegisterGeneric(
ServiceReference<?> serviceReference,
- CXFJaxRsServiceRegistrator registrator) {
-
- return bundleContext().flatMap(bundleContext -> {
- Object service = bundleContext.getService(serviceReference);
- Class<?> serviceClass = service.getClass();
- bundleContext.ungetService(serviceReference);
- if (serviceClass.isAnnotationPresent(Provider.class)) {
- return safeRegisterExtension(serviceReference, registrator);
- }
- else {
- return safeRegisterEndpoint(serviceReference, registrator);
- }
- });
+ String applicationName,
+ CXFJaxRsServiceRegistrator registrator,
+ AriesJaxRSServiceRuntime runtime) {
+
+ if (isExtension(serviceReference)) {
+ return safeRegisterExtension(
+ serviceReference, applicationName, registrator, runtime);
+ }
+ else {
+ return safeRegisterEndpoint(
+ serviceReference, applicationName, registrator, runtime);
+ }
+ }
+
+ private static boolean isExtension(ServiceReference<?> serviceReference) {
+ Object extensionProperty = serviceReference.getProperty(
+ "osgi.jaxrs.extension");
+
+ return
+ (extensionProperty != null) &&
+ (extensionProperty instanceof Boolean) &&
+ ((boolean) extensionProperty);
}
public static OSGi<?> safeRegisterExtension(
ServiceReference<?> serviceReference,
- CXFJaxRsServiceRegistrator registrator) {
+ String applicationName, CXFJaxRsServiceRegistrator registrator,
+ AriesJaxRSServiceRuntime runtime) {
return
- service(serviceReference).flatMap(extension ->
- onClose(() -> registrator.removeProvider(extension)).
- foreach(ign ->
- registrator.addProvider(extension)
- ));
+ onlyGettables(
+ just(serviceReference),
+ runtime::addNotGettableExtension,
+ runtime::removeNotGettableExtension
+ ).foreach(
+ registrator::addProvider,
+ registrator::removeProvider
+ ).foreach(
+ __ -> runtime.addApplicationExtension(
+ applicationName, serviceReference),
+ __ -> runtime.removeApplicationExtension(
+ applicationName, serviceReference)
+ );
}
public static <T> OSGi<?> safeRegisterEndpoint(
- ServiceReference<T> ref, CXFJaxRsServiceRegistrator registrator) {
+ ServiceReference<T> serviceReference,
+ String applicationName,
+ CXFJaxRsServiceRegistrator registrator,
+ AriesJaxRSServiceRuntime runtime) {
return
- bundleContext().flatMap(bundleContext ->
- serviceObjects(ref).flatMap(service ->
- registerEndpoint(ref, registrator, service).
- flatMap(serviceInformation ->
- onClose(() ->
- unregisterEndpoint(registrator, serviceInformation)))));
+ onlyGettables(
+ just(serviceReference),
+ runtime::addNotGettableEndpoint,
+ runtime::removeNotGettableEndpoint
+ ).flatMap(
+ tuple -> serviceObjects(serviceReference).flatMap(
+ serviceObjects -> registerEndpoint(
+ registrator, serviceObjects).flatMap(
+ resourceProvider ->
+ onClose(
+ () -> unregisterEndpoint(
+ registrator, resourceProvider)
+ )
+ )
+ )
+ ).foreach(
+ __ -> runtime.addApplicationEndpoint(
+ applicationName, serviceReference),
+ __ -> runtime.removeApplicationEndpoint(
+ applicationName, serviceReference)
+ );
}
public static <T extends Comparable<? super T>> OSGi<T> repeatInOrder(
@@ -130,14 +165,15 @@ public class Utils {
return program.route(new RepeatInOrderRouter<>());
}
- public static <T> OSGi<MutableTuple<T>> onlyGettables(
- ServiceReference<T> serviceReference,
+ public static <T> OSGi<ServiceTuple<T>> onlyGettables(
+ OSGi<ServiceReference<T>> program,
Consumer<ServiceReference<T>> whenAddedNotGettable,
Consumer<ServiceReference<T>> whenLeavingNotGettable) {
- return bundleContext().flatMap(
- bundleContext -> {
+ return bundleContext().flatMap(bundleContext ->
+ program.flatMap(serviceReference -> {
T service = null;
+
try {
service = bundleContext.getService(serviceReference);
}
@@ -148,7 +184,8 @@ public class Utils {
return
onClose(
- () -> whenLeavingNotGettable.accept(serviceReference)
+ () -> whenLeavingNotGettable.accept(
+ serviceReference)
).then(
nothing()
);
@@ -157,15 +194,14 @@ public class Utils {
onClose(
() -> bundleContext.ungetService(serviceReference)
).then(
- just(new MutableTuple<>(serviceReference, service)))
- ;
+ just(new ServiceTuple<>(serviceReference, service))
+ );
}
- );
+ ));
}
public static <T> OSGi<ResourceProvider>
registerEndpoint(
- ServiceReference<?> serviceReference,
CXFJaxRsServiceRegistrator registrator,
ServiceObjects<T> serviceObjects) {
@@ -353,18 +389,18 @@ public class Utils {
}
- public static class MutableTuple<T> implements Comparable<MutableTuple<T>> {
+ public static class ServiceTuple<T> implements Comparable<ServiceTuple<T>> {
private final ServiceReference<T> _serviceReference;
- private T _service;
+ private final T _service;
- public MutableTuple(ServiceReference<T> a, T service) {
+ public ServiceTuple(ServiceReference<T> a, T service) {
_serviceReference = a;
_service = service;
}
@Override
- public int compareTo(MutableTuple<T> o) {
+ public int compareTo(ServiceTuple<T> o) {
return _serviceReference.compareTo(o._serviceReference);
}
@@ -373,7 +409,7 @@ public class Utils {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
- MutableTuple<?> that = (MutableTuple<?>) o;
+ ServiceTuple<?> that = (ServiceTuple<?>) o;
return _serviceReference.equals(that._serviceReference);
}
@@ -387,13 +423,9 @@ public class Utils {
return _serviceReference;
}
- public T getSecond() {
+ public T getService() {
return _service;
}
-
- public void setService(T service) {
- _service = service;
- }
-
}
+
}
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/216070bb/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 99980bb..cb2c078 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
@@ -17,7 +17,7 @@
package org.apache.aries.jax.rs.whiteboard.internal;
-import org.apache.aries.jax.rs.whiteboard.internal.Utils.MutableTuple;
+import org.apache.aries.jax.rs.whiteboard.internal.Utils.ServiceTuple;
import org.apache.aries.osgi.functional.OSGi;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.extension.ExtensionManagerBus;
@@ -50,12 +50,12 @@ import static org.apache.aries.jax.rs.whiteboard.internal.Utils.onlyGettables;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.safeRegisterEndpoint;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.safeRegisterExtension;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.safeRegisterGeneric;
+import static org.apache.aries.jax.rs.whiteboard.internal.Utils.service;
import static org.apache.aries.osgi.functional.OSGi.all;
import static org.apache.aries.osgi.functional.OSGi.bundleContext;
import static org.apache.aries.osgi.functional.OSGi.just;
import static org.apache.aries.osgi.functional.OSGi.register;
import static org.apache.aries.osgi.functional.OSGi.serviceReferences;
-import static org.apache.aries.osgi.functional.OSGi.services;
import static org.osgi.service.http.runtime.HttpServiceRuntimeConstants.HTTP_SERVICE_ENDPOINT;
import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME;
import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT;
@@ -75,24 +75,25 @@ import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.JAX_RS_
*/
public class Whiteboard {
- public static final Function<MutableTuple<Application>, String> APPLICATION_BASE = ((Function<MutableTuple<Application>, ServiceReference<Application>>) Utils.MutableTuple::getServiceReference).andThen(Whiteboard::getApplicationBase);
+ public static final Function<ServiceTuple<Application>, String> APPLICATION_BASE = ((Function<ServiceTuple<Application>, ServiceReference<Application>>) ServiceTuple::getServiceReference).andThen(Whiteboard::getApplicationBase);
public static OSGi<Void> createWhiteboard(Dictionary<String, ?> configuration) {
+ AriesJaxRSServiceRuntime runtime = new AriesJaxRSServiceRuntime();
+ Map<String, ?> configurationMap = Maps.from(configuration);
return
bundleContext().flatMap(bundleContext ->
- just(new AriesJaxRSServiceRuntime(bundleContext)).flatMap(runtime ->
- registerJaxRSServiceRuntime(runtime, bundleContext, Maps.from(configuration)).flatMap(runtimeResgistration ->
- createDefaultJaxRsServiceRegistrator(Maps.from(configuration)).flatMap(defaultServiceRegistrator ->
+ registerJaxRSServiceRuntime(runtime, bundleContext, configurationMap).flatMap(runtimeResgistration ->
+ createDefaultJaxRsServiceRegistrator(configurationMap).flatMap(defaultServiceRegistrator ->
just(new ServiceRegistrationChangeCounter(runtimeResgistration)).flatMap(counter ->
just(runtimeResgistration.getReference()).flatMap(runtimeRegistration ->
all(
countChanges(whiteboardApplications(runtimeRegistration, runtime, Maps.from(configuration)), counter),
- countChanges(whiteBoardApplicationSingletons(runtimeRegistration), counter),
- countChanges(whiteboardExtensions(runtimeRegistration, defaultServiceRegistrator), counter),
- countChanges(whiteboardSingletons(runtimeRegistration, defaultServiceRegistrator), counter)
- )))))));
+ countChanges(whiteBoardApplicationSingletons(runtimeRegistration, runtime), counter),
+ countChanges(whiteboardExtensions(runtimeRegistration, defaultServiceRegistrator, runtime), counter),
+ countChanges(whiteboardSingletons(runtimeRegistration, defaultServiceRegistrator, runtime), counter)
+ ))))));
}
private static OSGi<Collection<String>> bestEffortCalculationOfEnpoints(Filter filter) {
@@ -125,23 +126,28 @@ public class Whiteboard {
return new String[]{propertyValue.toString()};
}
- private static ExtensionManagerBus createBus(BundleContext bundleContext, Map<String, ?> configuration) {
- BundleWiring wiring = bundleContext.getBundle().adapt(BundleWiring.class);
+ private static ExtensionManagerBus createBus(
+ BundleContext bundleContext, Map<String, ?> configuration) {
+
+ BundleWiring wiring = bundleContext.getBundle().adapt(
+ BundleWiring.class);
@SuppressWarnings("unchecked")
Map<String, Object> properties = (Map<String, Object>)configuration;
- properties.put("org.apache.cxf.bus.id", configuration.get(Constants.SERVICE_PID));
+ properties.put("org.apache.cxf.bus.id", configuration.get(
+ Constants.SERVICE_PID));
- ExtensionManagerBus bus = new ExtensionManagerBus(null, properties, wiring.getClassLoader());
+ ExtensionManagerBus bus = new ExtensionManagerBus(
+ null, properties, wiring.getClassLoader());
bus.initialize();
return bus;
}
- private static OSGi<CXFJaxRsServiceRegistrator> createDefaultJaxRsServiceRegistrator(
- Map<String, ?> configuration) {
+ private static OSGi<CXFJaxRsServiceRegistrator>
+ createDefaultJaxRsServiceRegistrator(Map<String, ?> configuration) {
Map<String, Object> properties = new HashMap<>(configuration);
properties.put(JAX_RS_NAME, ".default");
@@ -177,7 +183,7 @@ public class Whiteboard {
properties.putIfAbsent(
HTTP_WHITEBOARD_TARGET, "(osgi.http.endpoint=*)");
- properties.put(Constants.SERVICE_RANKING, -1);
+ properties.putIfAbsent(Constants.SERVICE_RANKING, -1);
String targetFilter = (String)properties.get(HTTP_WHITEBOARD_TARGET);
@@ -218,17 +224,25 @@ public class Whiteboard {
return program;
}
- private static OSGi<?> whiteBoardApplicationSingletons(ServiceReference<?> jaxRsRuntimeServiceReference) {
+ private static OSGi<?> whiteBoardApplicationSingletons(
+ ServiceReference<?> jaxRsRuntimeServiceReference,
+ AriesJaxRSServiceRuntime runtime) {
return
serviceReferences(format("(%s=*)", JAX_RS_APPLICATION_SELECT)).
filter(new TargetFilter<>(jaxRsRuntimeServiceReference)).
flatMap(ref ->
just(ref.getProperty(JAX_RS_APPLICATION_SELECT).toString()).
flatMap(applicationFilter ->
- services(CXFJaxRsServiceRegistrator.class, applicationFilter).
- flatMap(registrator ->
- safeRegisterGeneric(ref, registrator)
- )));
+ serviceReferences(
+ CXFJaxRsServiceRegistrator.class, applicationFilter).
+ flatMap(registratorReference ->
+ just(
+ AriesJaxRSServiceRuntime.getApplicationName(registratorReference)).
+ flatMap(applicationName ->
+ waitForExtensionDependencies(ref,
+ service(registratorReference).flatMap(registrator ->
+ safeRegisterGeneric(ref, applicationName, registrator, runtime)
+ ))))));
}
private static OSGi<?> whiteboardApplications(
@@ -236,15 +250,14 @@ public class Whiteboard {
AriesJaxRSServiceRuntime runtime,
Map<String, ?> configuration) {
- OSGi<MutableTuple<Application>> gettableAplicationForWhiteboard =
- getApplicationsForWhiteboard(jaxRsRuntimeServiceReference).flatMap(
- sr -> onlyGettables(
- sr, runtime::addNotGettable, runtime::removeNotGettable)
- );
+ OSGi<ServiceTuple<Application>> gettableAplicationForWhiteboard =
+ onlyGettables(
+ getApplicationsForWhiteboard(jaxRsRuntimeServiceReference),
+ runtime::addNotGettableApplication,
+ runtime::removeNotGettableApplication);
- OSGi<MutableTuple<Application>> highestRankedPerPath = highestPer(
- APPLICATION_BASE,
- gettableAplicationForWhiteboard,
+ OSGi<ServiceTuple<Application>> highestRankedPerPath = highestPer(
+ APPLICATION_BASE, gettableAplicationForWhiteboard,
t -> runtime.addShadowedApplication(t.getServiceReference()),
t -> runtime.removeShadowedApplication(t.getServiceReference())
);
@@ -254,7 +267,7 @@ public class Whiteboard {
bundleContext -> highestRankedPerPath.flatMap(
ref -> deployApplication(configuration, bundleContext, ref)
).map(
- MutableTuple::getServiceReference).
+ ServiceTuple::getServiceReference).
foreach(
sr -> runtime.setApplicationForPath(
getApplicationBase(sr), sr),
@@ -264,9 +277,9 @@ public class Whiteboard {
);
}
- private static OSGi<MutableTuple<Application>> deployApplication(
+ private static OSGi<ServiceTuple<Application>> deployApplication(
Map<String, ?> configuration, BundleContext bundleContext,
- MutableTuple<Application> tuple) {
+ ServiceTuple<Application> tuple) {
ExtensionManagerBus bus = createBus(bundleContext, configuration);
@@ -277,12 +290,16 @@ public class Whiteboard {
CXFJaxRsServiceRegistrator.getProperties(
serviceReference, JAX_RS_APPLICATION_BASE);
+ properties.computeIfAbsent(
+ JAX_RS_NAME,
+ (__) -> AriesJaxRSServiceRuntime.generateApplicationName(
+ tuple.getServiceReference()));
+
return
all(
- deployRegistrator(bus, tuple.getSecond(), properties),
+ deployRegistrator(bus, tuple.getService(), properties),
registerCXFServletService(
- bus, getApplicationBase(serviceReference),
- properties)).
+ bus, getApplicationBase(serviceReference), properties)).
then(
just(tuple)
);
@@ -301,20 +318,24 @@ public class Whiteboard {
private static OSGi<?> whiteboardExtensions(
ServiceReference<?> jaxRsRuntimeServiceReference,
- CXFJaxRsServiceRegistrator defaultServiceRegistrator) {
+ CXFJaxRsServiceRegistrator defaultServiceRegistrator,
+ AriesJaxRSServiceRuntime runtime) {
return
serviceReferences(getExtensionFilter()).
filter(new TargetFilter<>(jaxRsRuntimeServiceReference)).
flatMap(ref ->
waitForExtensionDependencies(ref,
- safeRegisterExtension(ref, defaultServiceRegistrator)
+ safeRegisterExtension(
+ ref, ".default", defaultServiceRegistrator, runtime)
)
);
}
private static OSGi<?> whiteboardSingletons(
- ServiceReference<?> jaxRsRuntimeServiceReference, CXFJaxRsServiceRegistrator defaultServiceRegistrator) {
+ ServiceReference<?> jaxRsRuntimeServiceReference,
+ CXFJaxRsServiceRegistrator defaultServiceRegistrator,
+ AriesJaxRSServiceRuntime runtime) {
return
serviceReferences(getSingletonsFilter()).
@@ -322,7 +343,8 @@ public class Whiteboard {
flatMap(serviceReference ->
waitForExtensionDependencies(serviceReference,
safeRegisterEndpoint(
- serviceReference, defaultServiceRegistrator)
+ serviceReference, ".default", defaultServiceRegistrator,
+ runtime)
)
);
}
[2/8] aries-jax-rs-whiteboard git commit: Refactor to include default
application in DTOs
Posted by cs...@apache.org.
Refactor to include default application in DTOs
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/7c1fce86
Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/7c1fce86
Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/7c1fce86
Branch: refs/heads/master
Commit: 7c1fce8609ff45ef80e12b3dcac3ddc38cd284e9
Parents: 216070b
Author: Carlos Sierra <cs...@apache.org>
Authored: Tue Aug 22 18:16:58 2017 +0200
Committer: Carlos Sierra <cs...@apache.org>
Committed: Tue Aug 22 18:16:58 2017 +0200
----------------------------------------------------------------------
jax-rs.itests/src/main/java/test/JaxrsTest.java | 64 +++++++--------
.../internal/AriesJaxRSServiceRuntime.java | 34 ++++----
.../internal/CXFJaxRsServiceRegistrator.java | 20 -----
.../aries/jax/rs/whiteboard/internal/Utils.java | 12 +++
.../jax/rs/whiteboard/internal/Whiteboard.java | 83 +++++++++++---------
5 files changed, 104 insertions(+), 109 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/7c1fce86/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 ecf817a..f089eb6 100644
--- a/jax-rs.itests/src/main/java/test/JaxrsTest.java
+++ b/jax-rs.itests/src/main/java/test/JaxrsTest.java
@@ -69,14 +69,14 @@ public class JaxrsTest extends TestHelper {
try {
JaxRSServiceRuntime runtime = getJaxRSServiceRuntime();
- assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertNotNull(runtime);
serviceRegistration = registerApplication(
new TestApplication());
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(2, runtime.getRuntimeDTO().applicationDTOs.length);
Client client = createClient();
@@ -239,12 +239,12 @@ public class JaxrsTest extends TestHelper {
ServiceRegistration<?> serviceRegistration2;
try {
- assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
serviceRegistration = registerApplication(new TestApplication());
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(2, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
Response response = webTarget.request().get();
@@ -256,7 +256,7 @@ public class JaxrsTest extends TestHelper {
serviceRegistration2 = registerApplication(
new TestApplicationConflict(), "service.ranking", 1);
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(2, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length);
response = webTarget.request().get();
@@ -271,7 +271,7 @@ public class JaxrsTest extends TestHelper {
serviceRegistration2.unregister();
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(2, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
response = webTarget.request().get();
@@ -392,7 +392,7 @@ public class JaxrsTest extends TestHelper {
JaxRSServiceRuntime runtime = getJaxRSServiceRuntime();
Runnable testCase = () -> {
- assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
+ int applications = runtime.getRuntimeDTO().applicationDTOs.length;
assertEquals(404, webTarget.request().get().getStatus());
@@ -408,7 +408,9 @@ public class JaxrsTest extends TestHelper {
get().
readEntity(String.class));
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(
+ applications + 1,
+ runtime.getRuntimeDTO().applicationDTOs.length);
}
finally {
if (serviceRegistration != null) {
@@ -481,13 +483,13 @@ public class JaxrsTest extends TestHelper {
JaxRSServiceRuntime runtime = getJaxRSServiceRuntime();
- assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
ServiceRegistration<Application> serviceRegistration =
registerApplication(new TestApplication());
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(2, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
assertEquals(
@@ -497,7 +499,7 @@ public class JaxrsTest extends TestHelper {
ServiceRegistration<Application> ungettableServiceRegistration =
registerUngettableApplication("service.ranking", 1);
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(2, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length);
assertEquals(
@@ -510,14 +512,14 @@ public class JaxrsTest extends TestHelper {
serviceRegistration.unregister();
- assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length);
assertEquals(404, webTarget.request().get().getStatus());
ungettableServiceRegistration.unregister();
- assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
}
@@ -533,13 +535,13 @@ public class JaxrsTest extends TestHelper {
JaxRSServiceRuntime runtime = getJaxRSServiceRuntime();
- assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
ServiceRegistration<Application> serviceRegistration =
registerApplication(new TestApplication());
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(2, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
assertEquals(
@@ -549,7 +551,7 @@ public class JaxrsTest extends TestHelper {
ServiceRegistration<Application> ungettableServiceRegistration =
registerUngettableApplication("service.ranking", -1);
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(2, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length);
assertEquals(
@@ -562,14 +564,14 @@ public class JaxrsTest extends TestHelper {
serviceRegistration.unregister();
- assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length);
assertEquals(404, webTarget.request().get().getStatus());
ungettableServiceRegistration.unregister();
- assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
}
@@ -577,13 +579,13 @@ public class JaxrsTest extends TestHelper {
public void testNotGettableApplication() throws InterruptedException {
JaxRSServiceRuntime runtime = getJaxRSServiceRuntime();
- assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
ServiceRegistration<Application> serviceRegistration =
registerUngettableApplication();
- assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length);
assertEquals(
@@ -592,21 +594,25 @@ public class JaxrsTest extends TestHelper {
serviceRegistration.unregister();
- assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
}
@Test
- public void testStandaloneEndPoint() {
+ public void testStandaloneEndPoint() throws InterruptedException {
Client client = createClient();
WebTarget webTarget = client.
target("http://localhost:8080").
path("test");
+ JaxRSServiceRuntime runtime = getJaxRSServiceRuntime();
+
ServiceRegistration<?> serviceRegistration = null;
try {
+ runtime.getRuntimeDTO();
+
serviceRegistration = registerAddon(new TestAddon());
Response response = webTarget.request().get();
@@ -964,20 +970,6 @@ public class JaxrsTest extends TestHelper {
Object.class, testFilter, properties);
}
- /*private ServiceRegistration<?> registerFilter(Object... keyValues) {
-
- TestFilter testFilter = new TestFilter();
-
- Dictionary<String, Object> properties = new Hashtable<>();
-
- for (int i = 0; i < keyValues.length; i = i + 2) {
- properties.put(keyValues[i].toString(), keyValues[i + 1]);
- }
-
- return bundleContext.registerService(
- Object.class, testFilter, properties);
- }*/
-
private ServiceRegistration<Application> registerUngettableApplication(
Object... keyValues) {
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/7c1fce86/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 140e82e..da6de71 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
@@ -31,18 +31,20 @@ import org.osgi.service.jaxrs.runtime.dto.RuntimeDTO;
import org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants;
import javax.ws.rs.core.Application;
+import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
+import static org.apache.aries.jax.rs.whiteboard.internal.Utils.getProperties;
import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.JAX_RS_NAME;
public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
private static final long serialVersionUID = 1L;
- private ConcurrentHashMap<String, ServiceReference<Application>>
+ private ConcurrentHashMap<String, Map<String, Object>>
_applications = new ConcurrentHashMap<>();
private ConcurrentHashMap<String, Set<ServiceReference<?>>>
@@ -96,13 +98,13 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
_ungettableExtensions.remove(serviceReference);
}
- public ServiceReference<Application> setApplicationForPath(
- String path, ServiceReference<Application> serviceReference) {
+ public Map<String, Object> setApplicationForPath(
+ String path, Map<String, Object> properties) {
- return _applications.put(path, serviceReference);
+ return _applications.put(path, properties);
}
- public ServiceReference<Application> unsetApplicationForPath(String path) {
+ public Map<String, Object> unsetApplicationForPath(String path) {
return _applications.remove(path);
}
@@ -268,14 +270,13 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
}
private ApplicationDTO buildApplicationDTO(
- ServiceReference<Application> serviceReference) {
+ Map<String, Object> properties) {
ApplicationDTO applicationDTO = new ApplicationDTO(){};
- applicationDTO.name = getApplicationName(serviceReference);
- applicationDTO.base = Whiteboard.getApplicationBase(serviceReference);
- applicationDTO.serviceId = (Long)serviceReference.getProperty(
- "service.id");
+ applicationDTO.name = getApplicationName(properties);
+ applicationDTO.base = Whiteboard.getApplicationBase(properties);
+ applicationDTO.serviceId = (Long)properties.get("service.id");
applicationDTO.resourceDTOs = getApplicationEndpointsStream(
applicationDTO.name).toArray(
@@ -351,7 +352,7 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
JaxRSWhiteboardConstants.JAX_RS_NAME);
failedApplicationDTO.name = nameProperty == null ?
- generateApplicationName(serviceReference) :
+ generateApplicationName(getProperties(serviceReference)) :
nameProperty.toString();
failedApplicationDTO.failureReason = reason;
@@ -359,23 +360,22 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
return failedApplicationDTO;
}
- public static String getApplicationName(
- ServiceReference<?> serviceReference) {
+ public static String getApplicationName(Map<String, Object> properties) {
- Object property = serviceReference.getProperty(JAX_RS_NAME);
+ Object property = properties.get(JAX_RS_NAME);
if (property == null) {
- return generateApplicationName(serviceReference);
+ return generateApplicationName(properties);
}
return property.toString();
}
public static String generateApplicationName(
- ServiceReference<?> serviceReference) {
+ Map<String, Object> properties) {
return ".jax-rs-application-" +
- serviceReference.getProperty("service.id").toString();
+ properties.get("service.id").toString();
}
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/7c1fce86/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 a43abba..50d51a7 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
@@ -19,8 +19,6 @@ package org.apache.aries.jax.rs.whiteboard.internal;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
import javax.ws.rs.core.Application;
@@ -35,7 +33,6 @@ import org.apache.cxf.jaxrs.model.OperationResourceInfo;
import org.apache.cxf.jaxrs.provider.json.JSONProvider;
import org.apache.cxf.jaxrs.utils.ResourceUtils;
import org.apache.cxf.message.Message;
-import org.osgi.framework.ServiceReference;
public class CXFJaxRsServiceRegistrator {
@@ -46,8 +43,6 @@ public class CXFJaxRsServiceRegistrator {
private Server _server;
private final Collection<ResourceProvider> _services = new ArrayList<>();
- private static final String CXF_ENDPOINT_ADDRESS = "CXF_ENDPOINT_ADDRESS";
-
public CXFJaxRsServiceRegistrator(Bus bus, Application application) {
_bus = bus;
_application = application;
@@ -55,21 +50,6 @@ public class CXFJaxRsServiceRegistrator {
rewire();
}
- public static Map<String, Object> getProperties(
- ServiceReference<?> sref, String addressKey) {
-
- String[] propertyKeys = sref.getPropertyKeys();
- Map<String, Object> properties = new HashMap<>(propertyKeys.length);
-
- for (String key : propertyKeys) {
- properties.put(key, sref.getProperty(key));
- }
-
- properties.put(
- CXF_ENDPOINT_ADDRESS, sref.getProperty(addressKey).toString());
- return properties;
- }
-
public void add(ResourceProvider resourceProvider) {
if (_closed) {
return;
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/7c1fce86/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
index 0a343bf..2506be3 100644
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
@@ -28,6 +28,7 @@ import org.osgi.service.jaxrs.runtime.dto.FailedApplicationDTO;
import javax.ws.rs.core.Application;
import java.util.Comparator;
+import java.util.HashMap;
import java.util.Map;
import java.util.NavigableSet;
import java.util.SortedSet;
@@ -47,6 +48,17 @@ import static org.apache.aries.osgi.functional.OSGi.register;
*/
public class Utils {
+ public static Map<String, Object> getProperties(ServiceReference<?> sref) {
+ String[] propertyKeys = sref.getPropertyKeys();
+ Map<String, Object> properties = new HashMap<>(propertyKeys.length);
+
+ for (String key : propertyKeys) {
+ properties.put(key, sref.getProperty(key));
+ }
+
+ return properties;
+ }
+
public static <T> OSGi<T> service(ServiceReference<T> serviceReference) {
return
bundleContext().flatMap(bundleContext ->
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/7c1fce86/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 cb2c078..aaf6220 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
@@ -44,7 +44,9 @@ import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
import static java.lang.String.format;
+import static org.apache.aries.jax.rs.whiteboard.internal.AriesJaxRSServiceRuntime.getApplicationName;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.deployRegistrator;
+import static org.apache.aries.jax.rs.whiteboard.internal.Utils.getProperties;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.highestPer;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.onlyGettables;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.safeRegisterEndpoint;
@@ -75,7 +77,8 @@ import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.JAX_RS_
*/
public class Whiteboard {
- public static final Function<ServiceTuple<Application>, String> APPLICATION_BASE = ((Function<ServiceTuple<Application>, ServiceReference<Application>>) ServiceTuple::getServiceReference).andThen(Whiteboard::getApplicationBase);
+ public static final Function<ServiceTuple<Application>, String> APPLICATION_BASE = ((Function<ServiceTuple<Application>, ServiceReference<Application>>) ServiceTuple::getServiceReference).andThen(Utils::getProperties).andThen(Whiteboard::getApplicationBase);
+ public static final String DEFAULT_NAME = ".default";
public static OSGi<Void> createWhiteboard(Dictionary<String, ?> configuration) {
AriesJaxRSServiceRuntime runtime = new AriesJaxRSServiceRuntime();
@@ -84,15 +87,15 @@ public class Whiteboard {
return
bundleContext().flatMap(bundleContext ->
- registerJaxRSServiceRuntime(runtime, bundleContext, configurationMap).flatMap(runtimeResgistration ->
- createDefaultJaxRsServiceRegistrator(configurationMap).flatMap(defaultServiceRegistrator ->
- just(new ServiceRegistrationChangeCounter(runtimeResgistration)).flatMap(counter ->
- just(runtimeResgistration.getReference()).flatMap(runtimeRegistration ->
+ registerJaxRSServiceRuntime(runtime, bundleContext, configurationMap).flatMap(runtimeRegistration ->
+ createDefaultJaxRsServiceRegistrator(configurationMap, runtime).flatMap(defaultServiceRegistrator ->
+ just(new ServiceRegistrationChangeCounter(runtimeRegistration)).flatMap(counter ->
+ just(runtimeRegistration.getReference()).flatMap(runtimeReference ->
all(
- countChanges(whiteboardApplications(runtimeRegistration, runtime, Maps.from(configuration)), counter),
- countChanges(whiteBoardApplicationSingletons(runtimeRegistration, runtime), counter),
- countChanges(whiteboardExtensions(runtimeRegistration, defaultServiceRegistrator, runtime), counter),
- countChanges(whiteboardSingletons(runtimeRegistration, defaultServiceRegistrator, runtime), counter)
+ countChanges(whiteboardApplications(runtimeReference, runtime, Maps.from(configuration)), counter),
+ countChanges(whiteBoardApplicationSingletons(runtimeReference, runtime), counter),
+ countChanges(whiteboardExtensions(runtimeReference, defaultServiceRegistrator, runtime), counter),
+ countChanges(whiteboardSingletons(runtimeReference, defaultServiceRegistrator, runtime), counter)
))))));
}
@@ -147,18 +150,22 @@ public class Whiteboard {
}
private static OSGi<CXFJaxRsServiceRegistrator>
- createDefaultJaxRsServiceRegistrator(Map<String, ?> configuration) {
+ createDefaultJaxRsServiceRegistrator(
+ Map<String, ?> configuration, AriesJaxRSServiceRuntime runtime) {
Map<String, Object> properties = new HashMap<>(configuration);
properties.put(JAX_RS_NAME, ".default");
+ properties.put(JAX_RS_APPLICATION_BASE, "/");
+ properties.put("service.id", (long)-1);
return
bundleContext().flatMap(bundleContext ->
just(createBus(bundleContext, configuration)).flatMap(bus ->
- registerCXFServletService(bus, "", configuration).then(
- just(
- new CXFJaxRsServiceRegistrator(bus, new DefaultApplication()))
- )));
+ registerCXFServletService(bus, "", configuration).foreach(
+ __ -> runtime.setApplicationForPath("/", properties),
+ __ -> runtime.unsetApplicationForPath("/")).then(
+ just(new CXFJaxRsServiceRegistrator(bus, new DefaultApplication()))
+ )));
}
private static String getApplicationFilter() {
@@ -237,7 +244,7 @@ public class Whiteboard {
CXFJaxRsServiceRegistrator.class, applicationFilter).
flatMap(registratorReference ->
just(
- AriesJaxRSServiceRuntime.getApplicationName(registratorReference)).
+ getApplicationName(getProperties(registratorReference))).
flatMap(applicationName ->
waitForExtensionDependencies(ref,
service(registratorReference).flatMap(registrator ->
@@ -267,12 +274,13 @@ public class Whiteboard {
bundleContext -> highestRankedPerPath.flatMap(
ref -> deployApplication(configuration, bundleContext, ref)
).map(
- ServiceTuple::getServiceReference).
- foreach(
- sr -> runtime.setApplicationForPath(
- getApplicationBase(sr), sr),
- sr -> runtime.unsetApplicationForPath(
- getApplicationBase(sr))
+ ServiceTuple::getServiceReference
+ ).map(
+ Utils::getProperties
+ ).foreach(
+ p -> runtime.setApplicationForPath(
+ getApplicationBase(p), p),
+ p -> runtime.unsetApplicationForPath(getApplicationBase(p))
)
);
}
@@ -286,20 +294,18 @@ public class Whiteboard {
ServiceReference<Application> serviceReference =
tuple.getServiceReference();
- Map<String, Object> properties =
- CXFJaxRsServiceRegistrator.getProperties(
- serviceReference, JAX_RS_APPLICATION_BASE);
+ Map<String, Object> properties = getProperties(serviceReference);
properties.computeIfAbsent(
JAX_RS_NAME,
(__) -> AriesJaxRSServiceRuntime.generateApplicationName(
- tuple.getServiceReference()));
+ properties));
return
all(
deployRegistrator(bus, tuple.getService(), properties),
registerCXFServletService(
- bus, getApplicationBase(serviceReference), properties)).
+ bus, getApplicationBase(properties), properties)).
then(
just(tuple)
);
@@ -310,10 +316,8 @@ public class Whiteboard {
ServiceReference<?> jaxRsRuntimeServiceReference) {
return
- serviceReferences(
- Application.class, getApplicationFilter()).
- filter(
- new TargetFilter<>(jaxRsRuntimeServiceReference));
+ serviceReferences(Application.class, getApplicationFilter()).
+ filter(new TargetFilter<>(jaxRsRuntimeServiceReference));
}
private static OSGi<?> whiteboardExtensions(
@@ -327,7 +331,10 @@ public class Whiteboard {
flatMap(ref ->
waitForExtensionDependencies(ref,
safeRegisterExtension(
- ref, ".default", defaultServiceRegistrator, runtime)
+ ref, DEFAULT_NAME, defaultServiceRegistrator, runtime)
+ ).foreach(
+ __ -> runtime.addApplicationExtension(DEFAULT_NAME, ref),
+ __ -> runtime.removeApplicationExtension(DEFAULT_NAME, ref)
)
);
}
@@ -340,11 +347,15 @@ public class Whiteboard {
return
serviceReferences(getSingletonsFilter()).
filter(new TargetFilter<>(jaxRsRuntimeServiceReference)).
- flatMap(serviceReference ->
- waitForExtensionDependencies(serviceReference,
+ flatMap(ref ->
+ waitForExtensionDependencies(
+ ref,
safeRegisterEndpoint(
- serviceReference, ".default", defaultServiceRegistrator,
+ ref, DEFAULT_NAME, defaultServiceRegistrator,
runtime)
+ ).foreach(
+ __ -> runtime.addApplicationEndpoint(DEFAULT_NAME, ref),
+ __ -> runtime.removeApplicationExtension(DEFAULT_NAME, ref)
)
);
}
@@ -428,9 +439,9 @@ public class Whiteboard {
}
public static String getApplicationBase(
- ServiceReference<Application> serviceReference) {
+ Map<String, Object> properties) {
- return serviceReference.getProperty(JAX_RS_APPLICATION_BASE).toString();
+ return properties.get(JAX_RS_APPLICATION_BASE).toString();
}
[8/8] aries-jax-rs-whiteboard git commit: Register only advertised
interfaces
Posted by cs...@apache.org.
Register only advertised interfaces
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/432486ac
Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/432486ac
Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/432486ac
Branch: refs/heads/master
Commit: 432486ac7993fe0e7605dd265cefcf75e98abfbd
Parents: 08db7b1
Author: Carlos Sierra <cs...@apache.org>
Authored: Fri Aug 25 17:23:12 2017 +0200
Committer: Carlos Sierra <cs...@apache.org>
Committed: Fri Aug 25 17:23:12 2017 +0200
----------------------------------------------------------------------
jax-rs.itests/src/main/java/test/JaxrsTest.java | 51 +++++++++++++++++++
.../types/TestApplicationWithException.java | 39 ++++++++++++++
.../src/main/java/test/types/TestFilter.java | 1 -
.../types/TestFilterAndExceptionMapper.java | 53 ++++++++++++++++++++
.../jax/rs/whiteboard/internal/Whiteboard.java | 2 +-
5 files changed, 144 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/432486ac/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 9857ae4..7f364ce 100644
--- a/jax-rs.itests/src/main/java/test/JaxrsTest.java
+++ b/jax-rs.itests/src/main/java/test/JaxrsTest.java
@@ -41,7 +41,9 @@ import test.types.TestAddonConflict2;
import test.types.TestAddonLifecycle;
import test.types.TestApplication;
import test.types.TestApplicationConflict;
+import test.types.TestApplicationWithException;
import test.types.TestFilter;
+import test.types.TestFilterAndExceptionMapper;
import test.types.TestHelper;
import javax.ws.rs.client.Client;
@@ -49,6 +51,7 @@ import javax.ws.rs.client.WebTarget;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
@@ -982,6 +985,42 @@ public class JaxrsTest extends TestHelper {
}
@Test
+ public void testExtensionRegisterOnlySignalledInterfaces()
+ throws InterruptedException {
+
+ Client client = createClient();
+
+ WebTarget webTarget = client.
+ target("http://localhost:8080").
+ path("test-application");
+
+ ServiceRegistration<?> serviceRegistration = null;
+
+ try {
+ serviceRegistration = registerApplication(
+ new TestApplicationWithException());
+
+ ServiceRegistration<?> filterRegistration =
+ registerMultiExtension(
+ "Filter",
+ ExceptionMapper.class.getName());
+
+ Response response = webTarget.request().get();
+
+ assertEquals(200, response.getStatus());
+
+ assertNull(response.getHeaders().getFirst("Filtered"));
+
+ filterRegistration.unregister();
+ }
+ finally {
+ if (serviceRegistration != null) {
+ serviceRegistration.unregister();
+ }
+ }
+ }
+
+ @Test
public void testUngettableExtension() throws InterruptedException {
Client client = createClient();
@@ -1201,6 +1240,18 @@ public class JaxrsTest extends TestHelper {
ContainerResponseFilter.class, testFilter, properties);
}
+ private ServiceRegistration<?> registerMultiExtension(
+ String name, String... classes) {
+
+ Dictionary<String, Object> properties = new Hashtable<>();
+
+ properties.put(JAX_RS_EXTENSION, true);
+ properties.put(JAX_RS_NAME, name);
+
+ return bundleContext.registerService(
+ classes, new TestFilterAndExceptionMapper(), properties);
+ }
+
private ServiceRegistration<?> registerInvalidExtension(
String name, Object... keyValues) {
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/432486ac/jax-rs.itests/src/main/java/test/types/TestApplicationWithException.java
----------------------------------------------------------------------
diff --git a/jax-rs.itests/src/main/java/test/types/TestApplicationWithException.java b/jax-rs.itests/src/main/java/test/types/TestApplicationWithException.java
new file mode 100644
index 0000000..05eb000
--- /dev/null
+++ b/jax-rs.itests/src/main/java/test/types/TestApplicationWithException.java
@@ -0,0 +1,39 @@
+/*
+ * 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 test.types;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Application;
+import java.util.Collections;
+import java.util.Set;
+
+public class TestApplicationWithException extends Application {
+
+ @Override
+ public Set<Object> getSingletons() {
+ return Collections.<Object>singleton(this);
+ }
+
+ @GET
+ @Produces("text/plain")
+ public String sayHello() {
+ throw new TestFilterAndExceptionMapper.MyException();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/432486ac/jax-rs.itests/src/main/java/test/types/TestFilter.java
----------------------------------------------------------------------
diff --git a/jax-rs.itests/src/main/java/test/types/TestFilter.java b/jax-rs.itests/src/main/java/test/types/TestFilter.java
index 1757ca3..79e4726 100644
--- a/jax-rs.itests/src/main/java/test/types/TestFilter.java
+++ b/jax-rs.itests/src/main/java/test/types/TestFilter.java
@@ -26,7 +26,6 @@ import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;
-@Provider
public class TestFilter implements ContainerResponseFilter {
@Override
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/432486ac/jax-rs.itests/src/main/java/test/types/TestFilterAndExceptionMapper.java
----------------------------------------------------------------------
diff --git a/jax-rs.itests/src/main/java/test/types/TestFilterAndExceptionMapper.java b/jax-rs.itests/src/main/java/test/types/TestFilterAndExceptionMapper.java
new file mode 100644
index 0000000..89cf88c
--- /dev/null
+++ b/jax-rs.itests/src/main/java/test/types/TestFilterAndExceptionMapper.java
@@ -0,0 +1,53 @@
+/*
+ * 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 test.types;
+
+import javax.ws.rs.container.ContainerRequestContext;
+import javax.ws.rs.container.ContainerResponseContext;
+import javax.ws.rs.container.ContainerResponseFilter;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+import java.io.IOException;
+import java.util.Collections;
+
+public class TestFilterAndExceptionMapper implements
+ ContainerResponseFilter,
+ ExceptionMapper<TestFilterAndExceptionMapper.MyException> {
+
+ @Override
+ public void filter(
+ ContainerRequestContext requestContext,
+ ContainerResponseContext responseContext) throws IOException {
+
+ MultivaluedMap<String, Object> headers = responseContext.getHeaders();
+
+ headers.put("Filtered", Collections.singletonList("true"));
+ }
+
+ @Override
+ public Response toResponse(MyException e) {
+ return Response.ok().entity("This is fine").build();
+ }
+
+ public static class MyException extends RuntimeException {
+
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/432486ac/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 02e1b73..a2f5b25 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
@@ -149,7 +149,7 @@ public class Whiteboard {
}
private static OSGi<Void> ignore(OSGi<?> program) {
- return program.map(t -> { return null;});
+ return program.map(t -> null);
}
private static OSGi<Collection<String>> bestEffortCalculationOfEnpoints(Filter filter) {
[5/8] aries-jax-rs-whiteboard git commit: Unify treatment of default
application
Posted by cs...@apache.org.
Unify treatment of default application
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/b05a86af
Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/b05a86af
Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/b05a86af
Branch: refs/heads/master
Commit: b05a86af4ad2d89a8361288ac85fd9ef4fe1e2c9
Parents: 00057c9
Author: Carlos Sierra <cs...@apache.org>
Authored: Thu Aug 24 13:29:30 2017 +0200
Committer: Carlos Sierra <cs...@apache.org>
Committed: Thu Aug 24 13:37:15 2017 +0200
----------------------------------------------------------------------
.../jax/rs/whiteboard/internal/Whiteboard.java | 132 +++++++++----------
1 file changed, 66 insertions(+), 66 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/b05a86af/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 d53985e..b0968cb 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
@@ -52,8 +52,6 @@ import static org.apache.aries.jax.rs.whiteboard.internal.Utils.generateApplicat
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.getProperties;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.highestPer;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.onlyGettables;
-import static org.apache.aries.jax.rs.whiteboard.internal.Utils.safeRegisterEndpoint;
-import static org.apache.aries.jax.rs.whiteboard.internal.Utils.safeRegisterExtension;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.safeRegisterGeneric;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.service;
import static org.apache.aries.osgi.functional.OSGi.all;
@@ -96,14 +94,18 @@ public class Whiteboard {
return
bundleContext().flatMap(bundleContext ->
registerJaxRSServiceRuntime(runtime, bundleContext, configurationMap).flatMap(runtimeRegistration ->
- createDefaultJaxRsServiceRegistrator(configurationMap, runtime).flatMap(defaultServiceRegistrator ->
+ createDefaultJaxRsServiceRegistrator(configurationMap, runtime).flatMap(defaultApplicationReference ->
just(new ServiceRegistrationChangeCounter(runtimeRegistration)).flatMap(counter ->
just(runtimeRegistration.getReference()).flatMap(runtimeReference ->
all(
- countChanges(whiteboardApplications(runtimeReference, runtime, Maps.from(configuration)), counter),
- countChanges(whiteBoardApplicationSingletons(runtimeReference, runtime), counter),
- countChanges(whiteboardExtensions(runtimeReference, defaultServiceRegistrator, runtime), counter),
- countChanges(whiteboardSingletons(runtimeReference, defaultServiceRegistrator, runtime), counter)
+ countChanges(
+ whiteboardApplications(
+ runtimeReference, runtime, Maps.from(configuration)),
+ counter),
+ countChanges(
+ whiteBoardApplicationSingletons(
+ runtimeReference, defaultApplicationReference, runtime),
+ counter)
))))));
}
@@ -161,12 +163,12 @@ public class Whiteboard {
return bus;
}
- private static OSGi<CXFJaxRsServiceRegistrator>
+ private static OSGi<ApplicationReference>
createDefaultJaxRsServiceRegistrator(
Map<String, ?> configuration, AriesJaxRSServiceRuntime runtime) {
Map<String, Object> properties = new HashMap<>(configuration);
- properties.put(JAX_RS_NAME, ".default");
+ properties.put(JAX_RS_NAME, DEFAULT_NAME);
properties.put(JAX_RS_APPLICATION_BASE, "/");
properties.put("service.id", (long)-1);
@@ -176,8 +178,10 @@ public class Whiteboard {
registerCXFServletService(bus, "", configuration).foreach(
__ -> runtime.setApplicationForPath("/", properties),
__ -> runtime.unsetApplicationForPath("/")).then(
- just(new CXFJaxRsServiceRegistrator(bus, new DefaultApplication()))
- )));
+ just(new CXFJaxRsServiceRegistrator(bus, new DefaultApplication())).
+ flatMap(registrator ->
+ just(new ApplicationReference(DEFAULT_NAME, registrator))
+ ))));
}
private static String getApplicationFilter() {
@@ -246,32 +250,47 @@ public class Whiteboard {
private static OSGi<?> whiteBoardApplicationSingletons(
ServiceReference<?> jaxRsRuntimeServiceReference,
+ ApplicationReference defaultApplicationReference,
AriesJaxRSServiceRuntime runtime) {
return
serviceReferences(getApplicationSingletonsFilter()).
filter(new TargetFilter<>(jaxRsRuntimeServiceReference)).
flatMap(endpointReference ->
- just(endpointReference.
- getProperty(JAX_RS_APPLICATION_SELECT).
- toString()).
- flatMap(applicationFilter ->
- serviceReferences(
- CXFJaxRsServiceRegistrator.class, applicationFilter).
- flatMap(registratorReference ->
- just(
- getApplicationName(registratorReference::getProperty)).
- flatMap(applicationName ->
+ chooseApplication(endpointReference, defaultApplicationReference).
+ flatMap(applicationReference ->
waitForExtensionDependencies(endpointReference, runtime,
- service(registratorReference).flatMap(registrator ->
safeRegisterGeneric(
- endpointReference, applicationName, registrator, runtime)
- ))))));
+ endpointReference,
+ applicationReference.getApplicationName(),
+ applicationReference.getRegistrator(), runtime)
+ )));
}
- private static String getApplicationSingletonsFilter() {
- return format(
- "(&(|%s%s)(%s=*))", getExtensionsFilter(), getResourcesFilter(),
+ private static OSGi<ApplicationReference> chooseApplication(
+ ServiceReference<?> serviceReference, ApplicationReference theDefault) {
+
+ Object applicationSelectProperty = serviceReference.getProperty(
JAX_RS_APPLICATION_SELECT);
+
+ if (applicationSelectProperty == null) {
+ return just(theDefault);
+ }
+
+ String applicationName = getApplicationName(
+ serviceReference::getProperty);
+
+ return
+ serviceReferences(
+ CXFJaxRsServiceRegistrator.class,
+ applicationSelectProperty.toString()).
+ flatMap(registratorReference ->
+ service(registratorReference).flatMap(registrator ->
+ just(new ApplicationReference(applicationName, registrator))
+ ));
+ }
+
+ private static String getApplicationSingletonsFilter() {
+ return format("(|%s%s)", getExtensionsFilter(), getResourcesFilter());
}
private static OSGi<?> whiteboardApplications(
@@ -341,45 +360,6 @@ public class Whiteboard {
filter(new TargetFilter<>(jaxRsRuntimeServiceReference));
}
- private static OSGi<?> whiteboardExtensions(
- ServiceReference<?> jaxRsRuntimeServiceReference,
- CXFJaxRsServiceRegistrator defaultServiceRegistrator,
- AriesJaxRSServiceRuntime runtime) {
-
- return
- serviceReferences(getExtensionsFilter()).
- filter(new TargetFilter<>(jaxRsRuntimeServiceReference)).
- flatMap(ref ->
- waitForExtensionDependencies(ref, runtime,
- safeRegisterExtension(
- ref, DEFAULT_NAME, defaultServiceRegistrator, runtime)
- ).foreach(
- __ -> runtime.addApplicationExtension(DEFAULT_NAME, ref),
- __ -> runtime.removeApplicationExtension(DEFAULT_NAME, ref)
- )
- );
- }
-
- private static OSGi<?> whiteboardSingletons(
- ServiceReference<?> jaxRsRuntimeServiceReference,
- CXFJaxRsServiceRegistrator defaultServiceRegistrator,
- AriesJaxRSServiceRuntime runtime) {
-
- return
- serviceReferences(getResourcesFilter()).
- filter(new TargetFilter<>(jaxRsRuntimeServiceReference)).
- flatMap(ref ->
- waitForExtensionDependencies(
- ref, runtime,
- safeRegisterEndpoint(
- ref, DEFAULT_NAME, defaultServiceRegistrator, runtime)
- ).foreach(
- __ -> runtime.addApplicationEndpoint(DEFAULT_NAME, ref),
- __ -> runtime.removeApplicationExtension(DEFAULT_NAME, ref)
- )
- );
- }
-
private static <T> OSGi<T> countChanges(
OSGi<T> program, ChangeCounter counter) {
@@ -462,6 +442,26 @@ public class Whiteboard {
return properties.get(JAX_RS_APPLICATION_BASE).toString();
}
+ private static class ApplicationReference {
+ private final String _applicationName;
+ private final CXFJaxRsServiceRegistrator _registrator;
+
+ public ApplicationReference(
+ String applicationName,
+ CXFJaxRsServiceRegistrator registrator) {
+
+ _applicationName = applicationName;
+ _registrator = registrator;
+ }
+
+ public String getApplicationName() {
+ return _applicationName;
+ }
+ public CXFJaxRsServiceRegistrator getRegistrator() {
+ return _registrator;
+ }
+
+ }
}
[4/8] aries-jax-rs-whiteboard git commit: Calculate needed extensions
per application
Posted by cs...@apache.org.
Calculate needed extensions per application
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/00057c9e
Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/00057c9e
Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/00057c9e
Branch: refs/heads/master
Commit: 00057c9ea2891defbc7adc54bd950b8dce97a6d7
Parents: fe929ae
Author: Carlos Sierra <cs...@apache.org>
Authored: Thu Aug 24 12:58:10 2017 +0200
Committer: Carlos Sierra <cs...@apache.org>
Committed: Thu Aug 24 12:58:10 2017 +0200
----------------------------------------------------------------------
jax-rs.itests/src/main/java/test/JaxrsTest.java | 7 ++-
.../internal/AriesJaxRSServiceRuntime.java | 2 +-
.../aries/jax/rs/whiteboard/internal/Utils.java | 24 +++++++--
.../jax/rs/whiteboard/internal/Whiteboard.java | 53 +++++++++++++-------
4 files changed, 61 insertions(+), 25 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/00057c9e/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 e71f2b4..6491ec3 100644
--- a/jax-rs.itests/src/main/java/test/JaxrsTest.java
+++ b/jax-rs.itests/src/main/java/test/JaxrsTest.java
@@ -25,6 +25,7 @@ import java.util.Hashtable;
import java.util.Set;
import org.junit.After;
+import org.junit.Ignore;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.PrototypeServiceFactory;
@@ -836,8 +837,7 @@ public class JaxrsTest extends TestHelper {
try {
serviceRegistration = registerAddon(new TestAddon());
- filterRegistration = registerExtension(
- "Filter", JAX_RS_EXTENSION, "test-filter");
+ filterRegistration = registerExtension("Filter");
Response response = webTarget.request().get();
@@ -881,8 +881,7 @@ public class JaxrsTest extends TestHelper {
assertNull(response.getHeaders().getFirst("Filtered"));
- filterRegistration = registerExtension(
- "Filter", JAX_RS_EXTENSION, "test-filter");
+ filterRegistration = registerExtension("Filter");
response = webTarget.request().get();
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/00057c9e/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 c556e4f..c680f3c 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
@@ -351,7 +351,7 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
private static ResourceDTO populateResourceDTO(
ResourceDTO resourceDTO, ServiceReference<?> serviceReference) {
- resourceDTO.name = serviceReference.getProperty(JAX_RS_NAME).toString();
+ resourceDTO.name = getApplicationName(serviceReference::getProperty);
resourceDTO.serviceId = (Long)serviceReference.getProperty(
"service.id");
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/00057c9e/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
index 411281f..dd7530b 100644
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
@@ -24,7 +24,7 @@ import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
import org.apache.cxf.message.Message;
import org.osgi.framework.ServiceObjects;
import org.osgi.framework.ServiceReference;
-import org.osgi.service.jaxrs.runtime.dto.FailedApplicationDTO;
+import org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants;
import javax.ws.rs.core.Application;
import java.util.Comparator;
@@ -36,6 +36,7 @@ import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Function;
+import java.util.function.Supplier;
import static org.apache.aries.osgi.functional.OSGi.bundleContext;
import static org.apache.aries.osgi.functional.OSGi.just;
@@ -136,10 +137,21 @@ public class Utils {
}
public static OSGi<?> safeRegisterExtension(
- ServiceReference<?> serviceReference,
- String applicationName, CXFJaxRsServiceRegistrator registrator,
+ ServiceReference<?> serviceReference, String applicationName,
+ CXFJaxRsServiceRegistrator registrator,
AriesJaxRSServiceRuntime runtime) {
+ Map<String, Object> properties = getProperties(serviceReference);
+
+ properties.put(
+ JaxRSWhiteboardConstants.JAX_RS_NAME, applicationName);
+ properties.put(
+ "original.objectClass",
+ serviceReference.getProperty("objectClass"));
+
+ properties.remove(JaxRSWhiteboardConstants.JAX_RS_EXTENSION);
+ properties.remove(JaxRSWhiteboardConstants.JAX_RS_RESOURCE);
+
return
onlyGettables(
just(serviceReference),
@@ -153,6 +165,10 @@ public class Utils {
applicationName, serviceReference),
__ -> runtime.removeApplicationExtension(
applicationName, serviceReference)
+ ).then(
+ register(
+ ApplicationExtensionRegistration.class,
+ new ApplicationExtensionRegistration(){}, properties)
);
}
@@ -459,4 +475,6 @@ public class Utils {
Object get(String propertyName);
}
+ public interface ApplicationExtensionRegistration {}
+
}
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/00057c9e/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 ffb6379..d53985e 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
@@ -17,6 +17,7 @@
package org.apache.aries.jax.rs.whiteboard.internal;
+import org.apache.aries.jax.rs.whiteboard.internal.Utils.ApplicationExtensionRegistration;
import org.apache.aries.jax.rs.whiteboard.internal.Utils.PropertyHolder;
import org.apache.aries.jax.rs.whiteboard.internal.Utils.ServiceTuple;
import org.apache.aries.osgi.functional.OSGi;
@@ -79,7 +80,12 @@ import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.JAX_RS_
*/
public class Whiteboard {
- public static final Function<ServiceTuple<Application>, String> APPLICATION_BASE = ((Function<ServiceTuple<Application>, ServiceReference<Application>>) ServiceTuple::getServiceReference).andThen(sr -> getApplicationBase(sr::getProperty));
+ public static final Function<ServiceTuple<Application>, String>
+ APPLICATION_BASE =
+ ((Function<ServiceTuple<Application>, ServiceReference<Application>>)
+ ServiceTuple::getServiceReference).andThen(
+ sr -> getApplicationBase(sr::getProperty));
+
public static final String DEFAULT_NAME = ".default";
public static OSGi<Void> createWhiteboard(Dictionary<String, ?> configuration) {
@@ -118,7 +124,11 @@ public class Whiteboard {
}
private static String buildExtensionFilter(String filter) {
- return String.format("(&%s%s)", getExtensionFilter(), filter);
+ filter = filter.replace("(objectClass=", "(original.objectClass=");
+
+ return String.format(
+ "(&(objectClass=%s)%s)",
+ ApplicationExtensionRegistration.class.getName(), filter);
}
private static String[] canonicalize(Object propertyValue) {
@@ -174,11 +184,11 @@ public class Whiteboard {
return format("(%s=*)", JAX_RS_APPLICATION_BASE);
}
- private static String getExtensionFilter() {
- return format("(%s=*)", JAX_RS_EXTENSION);
+ private static String getExtensionsFilter() {
+ return format("(%s=true)", JAX_RS_EXTENSION);
}
- private static String getSingletonsFilter() {
+ private static String getResourcesFilter() {
return format("(%s=true)", JAX_RS_RESOURCE);
}
@@ -219,7 +229,8 @@ public class Whiteboard {
}
private static OSGi<?> waitForExtensionDependencies(
- ServiceReference<?> serviceReference, OSGi<?> program) {
+ ServiceReference<?> serviceReference, AriesJaxRSServiceRuntime runtime,
+ OSGi<?> program) {
String[] extensionDependencies = canonicalize(
serviceReference.getProperty(JAX_RS_EXTENSION_SELECT));
@@ -237,10 +248,12 @@ public class Whiteboard {
ServiceReference<?> jaxRsRuntimeServiceReference,
AriesJaxRSServiceRuntime runtime) {
return
- serviceReferences(format("(%s=*)", JAX_RS_APPLICATION_SELECT)).
+ serviceReferences(getApplicationSingletonsFilter()).
filter(new TargetFilter<>(jaxRsRuntimeServiceReference)).
- flatMap(ref ->
- just(ref.getProperty(JAX_RS_APPLICATION_SELECT).toString()).
+ flatMap(endpointReference ->
+ just(endpointReference.
+ getProperty(JAX_RS_APPLICATION_SELECT).
+ toString()).
flatMap(applicationFilter ->
serviceReferences(
CXFJaxRsServiceRegistrator.class, applicationFilter).
@@ -248,12 +261,19 @@ public class Whiteboard {
just(
getApplicationName(registratorReference::getProperty)).
flatMap(applicationName ->
- waitForExtensionDependencies(ref,
+ waitForExtensionDependencies(endpointReference, runtime,
service(registratorReference).flatMap(registrator ->
- safeRegisterGeneric(ref, applicationName, registrator, runtime)
+ safeRegisterGeneric(
+ endpointReference, applicationName, registrator, runtime)
))))));
}
+ private static String getApplicationSingletonsFilter() {
+ return format(
+ "(&(|%s%s)(%s=*))", getExtensionsFilter(), getResourcesFilter(),
+ JAX_RS_APPLICATION_SELECT);
+ }
+
private static OSGi<?> whiteboardApplications(
ServiceReference<?> jaxRsRuntimeServiceReference,
AriesJaxRSServiceRuntime runtime,
@@ -327,10 +347,10 @@ public class Whiteboard {
AriesJaxRSServiceRuntime runtime) {
return
- serviceReferences(getExtensionFilter()).
+ serviceReferences(getExtensionsFilter()).
filter(new TargetFilter<>(jaxRsRuntimeServiceReference)).
flatMap(ref ->
- waitForExtensionDependencies(ref,
+ waitForExtensionDependencies(ref, runtime,
safeRegisterExtension(
ref, DEFAULT_NAME, defaultServiceRegistrator, runtime)
).foreach(
@@ -346,14 +366,13 @@ public class Whiteboard {
AriesJaxRSServiceRuntime runtime) {
return
- serviceReferences(getSingletonsFilter()).
+ serviceReferences(getResourcesFilter()).
filter(new TargetFilter<>(jaxRsRuntimeServiceReference)).
flatMap(ref ->
waitForExtensionDependencies(
- ref,
+ ref, runtime,
safeRegisterEndpoint(
- ref, DEFAULT_NAME, defaultServiceRegistrator,
- runtime)
+ ref, DEFAULT_NAME, defaultServiceRegistrator, runtime)
).foreach(
__ -> runtime.addApplicationEndpoint(DEFAULT_NAME, ref),
__ -> runtime.removeApplicationExtension(DEFAULT_NAME, ref)
[6/8] aries-jax-rs-whiteboard git commit: Signalling of dependent
services
Posted by cs...@apache.org.
Signalling of dependent services
Also proper signalling of default application
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/3b810136
Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/3b810136
Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/3b810136
Branch: refs/heads/master
Commit: 3b810136d4855cad655836e497854c516b74b6c2
Parents: b05a86a
Author: Carlos Sierra <cs...@apache.org>
Authored: Thu Aug 24 18:58:35 2017 +0200
Committer: Carlos Sierra <cs...@apache.org>
Committed: Thu Aug 24 18:58:35 2017 +0200
----------------------------------------------------------------------
jax-rs.itests/src/main/java/test/JaxrsTest.java | 95 +++++++++++++++-----
.../internal/AriesJaxRSServiceRuntime.java | 55 +++++++++---
.../aries/jax/rs/whiteboard/internal/Utils.java | 4 -
.../jax/rs/whiteboard/internal/Whiteboard.java | 68 ++++++++++----
4 files changed, 165 insertions(+), 57 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/3b810136/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 6491ec3..1ed4a6c 100644
--- a/jax-rs.itests/src/main/java/test/JaxrsTest.java
+++ b/jax-rs.itests/src/main/java/test/JaxrsTest.java
@@ -25,7 +25,6 @@ import java.util.Hashtable;
import java.util.Set;
import org.junit.After;
-import org.junit.Ignore;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.PrototypeServiceFactory;
@@ -74,12 +73,12 @@ public class JaxrsTest extends TestHelper {
assertNotNull(runtime);
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
serviceRegistration = registerApplication(
new TestApplication());
- assertEquals(2, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
Client client = createClient();
@@ -107,7 +106,7 @@ public class JaxrsTest extends TestHelper {
RuntimeDTO runtimeDTO = runtime.getRuntimeDTO();
- assertEquals(1, runtimeDTO.applicationDTOs.length);
+ assertEquals(0, runtimeDTO.applicationDTOs.length);
assertEquals(0, runtimeDTO.failedExtensionDTOs.length);
ServiceRegistration<?> serviceRegistration = registerApplication(
@@ -122,7 +121,7 @@ public class JaxrsTest extends TestHelper {
runtimeDTO = runtime.getRuntimeDTO();
- assertEquals(1, runtimeDTO.applicationDTOs.length);
+ assertEquals(0, runtimeDTO.applicationDTOs.length);
assertEquals(1, runtimeDTO.failedApplicationDTOs.length);
assertEquals(
DTOConstants.FAILURE_REASON_UNKNOWN,
@@ -140,7 +139,7 @@ public class JaxrsTest extends TestHelper {
runtimeDTO = runtime.getRuntimeDTO();
- assertEquals(1, runtimeDTO.applicationDTOs.length);
+ assertEquals(0, runtimeDTO.applicationDTOs.length);
assertEquals(0, runtimeDTO.failedApplicationDTOs.length);
}
@@ -287,12 +286,12 @@ public class JaxrsTest extends TestHelper {
ServiceRegistration<?> serviceRegistration2;
try {
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
serviceRegistration = registerApplication(new TestApplication());
- assertEquals(2, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
Response response = webTarget.request().get();
@@ -304,7 +303,7 @@ public class JaxrsTest extends TestHelper {
serviceRegistration2 = registerApplication(
new TestApplicationConflict(), "service.ranking", 1);
- assertEquals(2, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length);
response = webTarget.request().get();
@@ -319,7 +318,7 @@ public class JaxrsTest extends TestHelper {
serviceRegistration2.unregister();
- assertEquals(2, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
response = webTarget.request().get();
@@ -531,13 +530,13 @@ public class JaxrsTest extends TestHelper {
JaxRSServiceRuntime runtime = getJaxRSServiceRuntime();
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
ServiceRegistration<Application> serviceRegistration =
registerApplication(new TestApplication());
- assertEquals(2, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
assertEquals(
@@ -547,7 +546,7 @@ public class JaxrsTest extends TestHelper {
ServiceRegistration<Application> ungettableServiceRegistration =
registerUngettableApplication("service.ranking", 1);
- assertEquals(2, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length);
assertEquals(
@@ -560,14 +559,14 @@ public class JaxrsTest extends TestHelper {
serviceRegistration.unregister();
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length);
assertEquals(404, webTarget.request().get().getStatus());
ungettableServiceRegistration.unregister();
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
}
@@ -583,13 +582,13 @@ public class JaxrsTest extends TestHelper {
JaxRSServiceRuntime runtime = getJaxRSServiceRuntime();
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
ServiceRegistration<Application> serviceRegistration =
registerApplication(new TestApplication());
- assertEquals(2, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
assertEquals(
@@ -599,7 +598,7 @@ public class JaxrsTest extends TestHelper {
ServiceRegistration<Application> ungettableServiceRegistration =
registerUngettableApplication("service.ranking", -1);
- assertEquals(2, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length);
assertEquals(
@@ -612,14 +611,14 @@ public class JaxrsTest extends TestHelper {
serviceRegistration.unregister();
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length);
assertEquals(404, webTarget.request().get().getStatus());
ungettableServiceRegistration.unregister();
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
}
@@ -627,13 +626,13 @@ public class JaxrsTest extends TestHelper {
public void testNotGettableApplication() throws InterruptedException {
JaxRSServiceRuntime runtime = getJaxRSServiceRuntime();
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
ServiceRegistration<Application> serviceRegistration =
registerUngettableApplication();
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(1, runtime.getRuntimeDTO().failedApplicationDTOs.length);
assertEquals(
@@ -642,7 +641,7 @@ public class JaxrsTest extends TestHelper {
serviceRegistration.unregister();
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+ assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);
}
@@ -763,13 +762,17 @@ public class JaxrsTest extends TestHelper {
}
@Test
- public void testStandaloneEndpointWithExtensionsDependencies() {
+ public void testStandaloneEndpointWithExtensionsDependencies()
+ throws InterruptedException {
+
Client client = createClient();
WebTarget webTarget = client.
target("http://localhost:8080").
path("test");
+ JaxRSServiceRuntime runtime = getJaxRSServiceRuntime();
+
ServiceRegistration<?> serviceRegistration = null;
ServiceRegistration<?> extensionRegistration1;
ServiceRegistration<?> extensionRegistration2;
@@ -782,16 +785,36 @@ public class JaxrsTest extends TestHelper {
"(property two=two)",
});
+ RuntimeDTO runtimeDTO = runtime.getRuntimeDTO();
+
+ assertEquals(1, runtimeDTO.failedResourceDTOs.length);
+ assertEquals(
+ (long)serviceRegistration.getReference().getProperty(
+ "service.id"),
+ runtimeDTO.failedResourceDTOs[0].serviceId);
+
assertEquals(404, webTarget.request().get().getStatus());
extensionRegistration1 = registerExtension(
"aExtension", "property one", "one");
+ runtimeDTO = runtime.getRuntimeDTO();
+
+ assertEquals(1, runtimeDTO.failedResourceDTOs.length);
+ assertEquals(
+ (long)serviceRegistration.getReference().getProperty(
+ "service.id"),
+ runtimeDTO.failedResourceDTOs[0].serviceId);
+
assertEquals(404, webTarget.request().get().getStatus());
extensionRegistration2 = registerExtension(
"anotherExtension", "property two", "two");
+ runtimeDTO = runtime.getRuntimeDTO();
+
+ assertEquals(0, runtimeDTO.failedResourceDTOs.length);
+
Response response = webTarget.request().get();
assertEquals(
@@ -800,20 +823,44 @@ public class JaxrsTest extends TestHelper {
extensionRegistration1.unregister();
+ runtimeDTO = runtime.getRuntimeDTO();
+ assertEquals(1, runtimeDTO.failedResourceDTOs.length);
+ assertEquals(
+ (long)serviceRegistration.getReference().getProperty(
+ "service.id"),
+ runtimeDTO.failedResourceDTOs[0].serviceId);
+
assertEquals(404, webTarget.request().get().getStatus());
extensionRegistration1 = registerExtension(
"aExtension", "property one", "one");
+ runtimeDTO = runtime.getRuntimeDTO();
+ assertEquals(0, runtimeDTO.failedResourceDTOs.length);
+
assertEquals(
"This should say hello", "Hello test",
response.readEntity(String.class));
extensionRegistration2.unregister();
+ runtimeDTO = runtime.getRuntimeDTO();
+ assertEquals(1, runtimeDTO.failedResourceDTOs.length);
+ assertEquals(
+ (long)serviceRegistration.getReference().getProperty(
+ "service.id"),
+ runtimeDTO.failedResourceDTOs[0].serviceId);
+
assertEquals(404, webTarget.request().get().getStatus());
extensionRegistration1.unregister();
+
+ runtimeDTO = runtime.getRuntimeDTO();
+ assertEquals(1, runtimeDTO.failedResourceDTOs.length);
+ assertEquals(
+ (long)serviceRegistration.getReference().getProperty(
+ "service.id"),
+ runtimeDTO.failedResourceDTOs[0].serviceId);
}
finally {
if (serviceRegistration != null) {
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/3b810136/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 c680f3c..a098f82 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
@@ -32,6 +32,7 @@ import org.osgi.service.jaxrs.runtime.dto.RuntimeDTO;
import org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants;
import javax.ws.rs.core.Application;
+import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
@@ -39,6 +40,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.generateApplicationName;
+import static org.apache.aries.jax.rs.whiteboard.internal.Whiteboard.getApplicationBase;
import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.JAX_RS_NAME;
public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
@@ -66,6 +68,21 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
private TreeSet<ServiceReference<?>> _ungettableEndpoints = new TreeSet<>();
private TreeSet<ServiceReference<?>> _ungettableExtensions = new TreeSet<>();
+ private TreeSet<ServiceReference<?>> _dependentServices = new TreeSet<>();
+ private Map<String, Object> _defaultApplicationProperties;
+
+ public void addDependentService(ServiceReference<?> serviceReference) {
+ _dependentServices.add(serviceReference);
+ }
+
+ public void clearDefaultApplication() {
+ _defaultApplicationProperties = Collections.emptyMap();
+ }
+
+ public void removeDependentService(ServiceReference<?> serviceReference) {
+ _dependentServices.remove(serviceReference);
+ }
+
public void addErroredApplication(
ServiceReference<Application> serviceReference) {
@@ -120,6 +137,12 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
return _applications.put(path, properties);
}
+ public void setDefaultApplication(Map<String, Object> properties) {
+ _defaultApplicationProperties = properties;
+
+ _applications.put(getApplicationBase(properties::get), properties);
+ }
+
public Map<String, Object> unsetApplicationForPath(String path) {
return _applications.remove(path);
}
@@ -200,6 +223,9 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
public RuntimeDTO getRuntimeDTO() {
RuntimeDTO runtimeDTO = new RuntimeDTO();
+ runtimeDTO.defaultApplication = buildApplicationDTO(
+ _defaultApplicationProperties);
+
runtimeDTO.applicationDTOs = applicationDTOStream().
toArray(
ApplicationDTO[]::new
@@ -214,12 +240,12 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
FailedApplicationDTO[]::new
);
- runtimeDTO.failedResourceDTOs = unreferenciableEndpointsDTOStream().map(
- sr -> buildFailedResourceDTO(
- DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE, sr)
- ).toArray(
- FailedResourceDTO[]::new
- );
+ runtimeDTO.failedResourceDTOs =
+ Stream.concat(
+ unreferenciableEndpointsDTOStream(), dependentServiceStreamDTO()
+ ).toArray(
+ FailedResourceDTO[]::new
+ );
runtimeDTO.failedExtensionDTOs = unreferenciableExtensionsDTOStream().map(
sr -> buildFailedExtensionDTO(
@@ -231,6 +257,12 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
return runtimeDTO;
}
+ private Stream<FailedResourceDTO> dependentServiceStreamDTO() {
+ return _dependentServices.stream().map(
+ sr -> buildFailedResourceDTO(
+ DTOConstants.FAILURE_REASON_REQUIRED_EXTENSIONS_UNAVAILABLE, sr));
+ }
+
private Stream<FailedApplicationDTO> erroredApplicationsDTOStream() {
return _erroredApplications.stream().map(
sr -> buildFailedApplicationDTO(
@@ -266,12 +298,15 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
return failedResourceDTO;
}
- private Stream<ServiceReference<?>> unreferenciableEndpointsDTOStream() {
- return _ungettableEndpoints.stream();
+ private Stream<FailedResourceDTO> unreferenciableEndpointsDTOStream() {
+ return _ungettableEndpoints.stream().map(
+ sr -> buildFailedResourceDTO(
+ DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE, sr));
}
private Stream<ApplicationDTO> applicationDTOStream() {
return _applications.values().stream().
+ filter(p -> !(".default".equals(p.get(JAX_RS_NAME)))).
map(
this::buildApplicationDTO
);
@@ -300,7 +335,7 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
ApplicationDTO applicationDTO = new ApplicationDTO(){};
applicationDTO.name = getApplicationName(properties::get);
- applicationDTO.base = Whiteboard.getApplicationBase(properties::get);
+ applicationDTO.base = getApplicationBase(properties::get);
applicationDTO.serviceId = (Long)properties.get("service.id");
applicationDTO.resourceDTOs = getApplicationEndpointsStream(
@@ -335,7 +370,7 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
private Stream<ExtensionDTO> getApplicationExtensionsStream(String name) {
Set<ServiceReference<?>> applicationExtensions =
- _applicationEndpoints.get(name);
+ _applicationExtensions.get(name);
Stream<ServiceReference<?>> applicationExtensionStream =
applicationExtensions != null ?
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/3b810136/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
index dd7530b..058c1a8 100644
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
@@ -36,7 +36,6 @@ import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Function;
-import java.util.function.Supplier;
import static org.apache.aries.osgi.functional.OSGi.bundleContext;
import static org.apache.aries.osgi.functional.OSGi.just;
@@ -149,9 +148,6 @@ public class Utils {
"original.objectClass",
serviceReference.getProperty("objectClass"));
- properties.remove(JaxRSWhiteboardConstants.JAX_RS_EXTENSION);
- properties.remove(JaxRSWhiteboardConstants.JAX_RS_RESOURCE);
-
return
onlyGettables(
just(serviceReference),
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/3b810136/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 b0968cb..5a586fc 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
@@ -54,6 +54,7 @@ import static org.apache.aries.jax.rs.whiteboard.internal.Utils.highestPer;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.onlyGettables;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.safeRegisterGeneric;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.service;
+import static org.apache.aries.osgi.functional.OSGi.NOOP;
import static org.apache.aries.osgi.functional.OSGi.all;
import static org.apache.aries.osgi.functional.OSGi.bundleContext;
import static org.apache.aries.osgi.functional.OSGi.just;
@@ -100,11 +101,12 @@ public class Whiteboard {
all(
countChanges(
whiteboardApplications(
- runtimeReference, runtime, Maps.from(configuration)),
+ runtimeReference, runtime, configurationMap),
counter),
countChanges(
whiteBoardApplicationSingletons(
- runtimeReference, defaultApplicationReference, runtime),
+ bundleContext, runtimeReference,
+ defaultApplicationReference, runtime),
counter)
))))));
}
@@ -125,14 +127,6 @@ public class Whiteboard {
);
}
- private static String buildExtensionFilter(String filter) {
- filter = filter.replace("(objectClass=", "(original.objectClass=");
-
- return String.format(
- "(&(objectClass=%s)%s)",
- ApplicationExtensionRegistration.class.getName(), filter);
- }
-
private static String[] canonicalize(Object propertyValue) {
if (propertyValue == null) {
return new String[0];
@@ -176,8 +170,8 @@ public class Whiteboard {
bundleContext().flatMap(bundleContext ->
just(createBus(bundleContext, configuration)).flatMap(bus ->
registerCXFServletService(bus, "", configuration).foreach(
- __ -> runtime.setApplicationForPath("/", properties),
- __ -> runtime.unsetApplicationForPath("/")).then(
+ __ -> runtime.setDefaultApplication(properties),
+ __ -> runtime.clearDefaultApplication()).then(
just(new CXFJaxRsServiceRegistrator(bus, new DefaultApplication())).
flatMap(registrator ->
just(new ApplicationReference(DEFAULT_NAME, registrator))
@@ -233,22 +227,54 @@ public class Whiteboard {
}
private static OSGi<?> waitForExtensionDependencies(
- ServiceReference<?> serviceReference, AriesJaxRSServiceRuntime runtime,
- OSGi<?> program) {
+ BundleContext bundleContext,
+ ServiceReference<?> serviceReference,
+ ApplicationReference applicationReference,
+ AriesJaxRSServiceRuntime runtime, OSGi<?> program) {
String[] extensionDependencies = canonicalize(
serviceReference.getProperty(JAX_RS_EXTENSION_SELECT));
+ if (extensionDependencies.length > 0) {
+ runtime.addDependentService(serviceReference);
+ }
+
for (String extensionDependency : extensionDependencies) {
- program =
- serviceReferences(buildExtensionFilter(extensionDependency)).
- then(program);
+ try {
+ extensionDependency = extensionDependency.replace(
+ "(objectClass=", "(original.objectClass=");
+
+ Filter extensionFilter = bundleContext.createFilter(
+ extensionDependency);
+
+ program =
+ serviceReferences(ApplicationExtensionRegistration.class).
+ filter(
+ sr -> getApplicationName(sr::getProperty).equals(
+ applicationReference.getApplicationName())
+ ).
+ filter(
+ extensionFilter::match
+ ).foreach(
+ __ -> {},
+ __ -> runtime.addDependentService(serviceReference)
+ ).
+ then(program);
+ }
+ catch (InvalidSyntaxException e) {
+
+ }
}
+ program = program.foreach(
+ __ -> runtime.removeDependentService(serviceReference)
+ );
+
return program;
}
private static OSGi<?> whiteBoardApplicationSingletons(
+ BundleContext bundleContext,
ServiceReference<?> jaxRsRuntimeServiceReference,
ApplicationReference defaultApplicationReference,
AriesJaxRSServiceRuntime runtime) {
@@ -258,7 +284,8 @@ public class Whiteboard {
flatMap(endpointReference ->
chooseApplication(endpointReference, defaultApplicationReference).
flatMap(applicationReference ->
- waitForExtensionDependencies(endpointReference, runtime,
+ waitForExtensionDependencies(
+ bundleContext, endpointReference, applicationReference, runtime,
safeRegisterGeneric(
endpointReference,
applicationReference.getApplicationName(),
@@ -290,7 +317,10 @@ public class Whiteboard {
}
private static String getApplicationSingletonsFilter() {
- return format("(|%s%s)", getExtensionsFilter(), getResourcesFilter());
+ return format(
+ "(&(!(objectClass=%s))(|%s%s))",
+ ApplicationExtensionRegistration.class.getName(),
+ getExtensionsFilter(), getResourcesFilter());
}
private static OSGi<?> whiteboardApplications(
[3/8] aries-jax-rs-whiteboard git commit: Register errored
applications
Posted by cs...@apache.org.
Register errored applications
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/fe929ae8
Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/fe929ae8
Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/fe929ae8
Branch: refs/heads/master
Commit: fe929ae882a46e17f0608a7baa3d0c313339612e
Parents: 7c1fce8
Author: Carlos Sierra <cs...@apache.org>
Authored: Wed Aug 23 14:24:26 2017 +0200
Committer: Carlos Sierra <cs...@apache.org>
Committed: Wed Aug 23 14:24:26 2017 +0200
----------------------------------------------------------------------
jax-rs.itests/src/main/java/test/JaxrsTest.java | 51 +++++++++++++++++++-
.../internal/AriesJaxRSServiceRuntime.java | 44 ++++++++++++-----
.../aries/jax/rs/whiteboard/internal/Utils.java | 27 +++++++++--
.../jax/rs/whiteboard/internal/Whiteboard.java | 37 +++++++-------
4 files changed, 121 insertions(+), 38 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/fe929ae8/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 f089eb6..e71f2b4 100644
--- a/jax-rs.itests/src/main/java/test/JaxrsTest.java
+++ b/jax-rs.itests/src/main/java/test/JaxrsTest.java
@@ -22,6 +22,7 @@ import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.*;
import java.util.Dictionary;
import java.util.Hashtable;
+import java.util.Set;
import org.junit.After;
import org.junit.Test;
@@ -32,6 +33,7 @@ import org.osgi.framework.ServiceRegistration;
import org.osgi.service.jaxrs.runtime.JaxRSServiceRuntime;
import org.osgi.service.jaxrs.runtime.dto.DTOConstants;
+import org.osgi.service.jaxrs.runtime.dto.RuntimeDTO;
import org.osgi.util.tracker.ServiceTracker;
import test.types.TestAddon;
import test.types.TestAddonConflict;
@@ -69,10 +71,10 @@ public class JaxrsTest extends TestHelper {
try {
JaxRSServiceRuntime runtime = getJaxRSServiceRuntime();
- assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
-
assertNotNull(runtime);
+ assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
+
serviceRegistration = registerApplication(
new TestApplication());
@@ -97,6 +99,51 @@ public class JaxrsTest extends TestHelper {
}
@Test
+ public void testApplicationWithError() throws InterruptedException {
+ JaxRSServiceRuntime runtime = getJaxRSServiceRuntime();
+
+ assertNotNull(runtime);
+
+ RuntimeDTO runtimeDTO = runtime.getRuntimeDTO();
+
+ assertEquals(1, runtimeDTO.applicationDTOs.length);
+ assertEquals(0, runtimeDTO.failedExtensionDTOs.length);
+
+ ServiceRegistration<?> serviceRegistration = registerApplication(
+ new TestApplication() {
+
+ @Override
+ public Set<Object> getSingletons() {
+ throw new RuntimeException();
+ }
+
+ });
+
+ runtimeDTO = runtime.getRuntimeDTO();
+
+ assertEquals(1, runtimeDTO.applicationDTOs.length);
+ assertEquals(1, runtimeDTO.failedApplicationDTOs.length);
+ assertEquals(
+ DTOConstants.FAILURE_REASON_UNKNOWN,
+ runtimeDTO.failedApplicationDTOs[0].failureReason);
+
+ Client client = createClient();
+
+ WebTarget webTarget = client.
+ target("http://localhost:8080").
+ path("/test-application");
+
+ assertEquals(404, webTarget.request().get().getStatus());
+
+ serviceRegistration.unregister();
+
+ runtimeDTO = runtime.getRuntimeDTO();
+
+ assertEquals(1, runtimeDTO.applicationDTOs.length);
+ assertEquals(0, runtimeDTO.failedApplicationDTOs.length);
+ }
+
+ @Test
public void testApplicationConflict() {
Client client = createClient();
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/fe929ae8/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 da6de71..c556e4f 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
@@ -17,6 +17,7 @@
package org.apache.aries.jax.rs.whiteboard.internal;
+import org.apache.aries.jax.rs.whiteboard.internal.Utils.PropertyHolder;
import org.osgi.framework.ServiceReference;
import org.osgi.service.jaxrs.runtime.JaxRSServiceRuntime;
import org.osgi.service.jaxrs.runtime.dto.ApplicationDTO;
@@ -37,7 +38,7 @@ import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
-import static org.apache.aries.jax.rs.whiteboard.internal.Utils.getProperties;
+import static org.apache.aries.jax.rs.whiteboard.internal.Utils.generateApplicationName;
import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.JAX_RS_NAME;
public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
@@ -59,9 +60,18 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
private Set<ServiceReference<Application>> _shadowedApplications =
new TreeSet<>();
+ private Set<ServiceReference<Application>> _erroredApplications =
+ new TreeSet<>();
+
private TreeSet<ServiceReference<?>> _ungettableEndpoints = new TreeSet<>();
private TreeSet<ServiceReference<?>> _ungettableExtensions = new TreeSet<>();
+ public void addErroredApplication(
+ ServiceReference<Application> serviceReference) {
+
+ _erroredApplications.add(serviceReference);
+ }
+
public boolean addNotGettableApplication(
ServiceReference<Application> serviceReference) {
@@ -74,6 +84,12 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
_ungettableExtensions.add(serviceReference);
}
+ public void removeErroredApplication(
+ ServiceReference<Application> serviceReference) {
+
+ _erroredApplications.remove(serviceReference);
+ }
+
public boolean removeNotGettableApplication(
ServiceReference<Application> serviceReference) {
@@ -191,7 +207,9 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
runtimeDTO.failedApplicationDTOs = Stream.concat(
shadowedApplicationsDTOStream(),
- unreferenciableApplicationsDTOStream()
+ Stream.concat(
+ unreferenciableApplicationsDTOStream(),
+ erroredApplicationsDTOStream())
).toArray(
FailedApplicationDTO[]::new
);
@@ -213,6 +231,13 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
return runtimeDTO;
}
+ private Stream<FailedApplicationDTO> erroredApplicationsDTOStream() {
+ return _erroredApplications.stream().map(
+ sr -> buildFailedApplicationDTO(
+ DTOConstants.FAILURE_REASON_UNKNOWN, sr)
+ );
+ }
+
private FailedExtensionDTO buildFailedExtensionDTO(
int reason, ServiceReference<?> serviceReference) {
@@ -274,8 +299,8 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
ApplicationDTO applicationDTO = new ApplicationDTO(){};
- applicationDTO.name = getApplicationName(properties);
- applicationDTO.base = Whiteboard.getApplicationBase(properties);
+ applicationDTO.name = getApplicationName(properties::get);
+ applicationDTO.base = Whiteboard.getApplicationBase(properties::get);
applicationDTO.serviceId = (Long)properties.get("service.id");
applicationDTO.resourceDTOs = getApplicationEndpointsStream(
@@ -352,7 +377,7 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
JaxRSWhiteboardConstants.JAX_RS_NAME);
failedApplicationDTO.name = nameProperty == null ?
- generateApplicationName(getProperties(serviceReference)) :
+ generateApplicationName(serviceReference::getProperty) :
nameProperty.toString();
failedApplicationDTO.failureReason = reason;
@@ -360,7 +385,7 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
return failedApplicationDTO;
}
- public static String getApplicationName(Map<String, Object> properties) {
+ public static String getApplicationName(PropertyHolder properties) {
Object property = properties.get(JAX_RS_NAME);
@@ -371,11 +396,4 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
return property.toString();
}
- public static String generateApplicationName(
- Map<String, Object> properties) {
-
- return ".jax-rs-application-" +
- properties.get("service.id").toString();
- }
-
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/fe929ae8/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
index 2506be3..411281f 100644
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
@@ -48,6 +48,12 @@ import static org.apache.aries.osgi.functional.OSGi.register;
*/
public class Utils {
+ public static String generateApplicationName(
+ PropertyHolder propertyHolder) {
+
+ return ".generated.for." + propertyHolder.get("service.id");
+ }
+
public static Map<String, Object> getProperties(ServiceReference<?> sref) {
String[] propertyKeys = sref.getPropertyKeys();
Map<String, Object> properties = new HashMap<>(propertyKeys.length);
@@ -77,11 +83,12 @@ public class Utils {
}
public static OSGi<?> deployRegistrator(
- Bus bus, Application application, Map<String, Object> props) {
+ Bus bus, ServiceTuple<Application> tuple, Map<String, Object> props,
+ AriesJaxRSServiceRuntime runtime) {
try {
CXFJaxRsServiceRegistrator registrator =
- new CXFJaxRsServiceRegistrator(bus, application);
+ new CXFJaxRsServiceRegistrator(bus, tuple.getService());
return
onClose(registrator::close).then(
@@ -89,8 +96,16 @@ public class Utils {
);
}
catch (Exception e) {
- return register(
- FailedApplicationDTO.class, new FailedApplicationDTO(), props);
+ ServiceReference<Application> serviceReference =
+ tuple.getServiceReference();
+
+ runtime.addErroredApplication(serviceReference);
+
+ return onClose(
+ () -> runtime.removeErroredApplication(serviceReference)
+ ).then(
+ nothing()
+ );
}
}
@@ -440,4 +455,8 @@ public class Utils {
}
}
+ public interface PropertyHolder {
+ Object get(String propertyName);
+ }
+
}
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/fe929ae8/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 aaf6220..ffb6379 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
@@ -17,6 +17,7 @@
package org.apache.aries.jax.rs.whiteboard.internal;
+import org.apache.aries.jax.rs.whiteboard.internal.Utils.PropertyHolder;
import org.apache.aries.jax.rs.whiteboard.internal.Utils.ServiceTuple;
import org.apache.aries.osgi.functional.OSGi;
import org.apache.cxf.Bus;
@@ -46,6 +47,7 @@ import java.util.function.Function;
import static java.lang.String.format;
import static org.apache.aries.jax.rs.whiteboard.internal.AriesJaxRSServiceRuntime.getApplicationName;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.deployRegistrator;
+import static org.apache.aries.jax.rs.whiteboard.internal.Utils.generateApplicationName;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.getProperties;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.highestPer;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.onlyGettables;
@@ -77,7 +79,7 @@ import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.JAX_RS_
*/
public class Whiteboard {
- public static final Function<ServiceTuple<Application>, String> APPLICATION_BASE = ((Function<ServiceTuple<Application>, ServiceReference<Application>>) ServiceTuple::getServiceReference).andThen(Utils::getProperties).andThen(Whiteboard::getApplicationBase);
+ public static final Function<ServiceTuple<Application>, String> APPLICATION_BASE = ((Function<ServiceTuple<Application>, ServiceReference<Application>>) ServiceTuple::getServiceReference).andThen(sr -> getApplicationBase(sr::getProperty));
public static final String DEFAULT_NAME = ".default";
public static OSGi<Void> createWhiteboard(Dictionary<String, ?> configuration) {
@@ -244,7 +246,7 @@ public class Whiteboard {
CXFJaxRsServiceRegistrator.class, applicationFilter).
flatMap(registratorReference ->
just(
- getApplicationName(getProperties(registratorReference))).
+ getApplicationName(registratorReference::getProperty)).
flatMap(applicationName ->
waitForExtensionDependencies(ref,
service(registratorReference).flatMap(registrator ->
@@ -272,22 +274,24 @@ public class Whiteboard {
return
bundleContext().flatMap(
bundleContext -> highestRankedPerPath.flatMap(
- ref -> deployApplication(configuration, bundleContext, ref)
+ tuple -> deployApplication(
+ configuration, bundleContext, tuple, runtime)
).map(
ServiceTuple::getServiceReference
).map(
Utils::getProperties
).foreach(
p -> runtime.setApplicationForPath(
- getApplicationBase(p), p),
- p -> runtime.unsetApplicationForPath(getApplicationBase(p))
+ getApplicationBase(p::get), p),
+ p -> runtime.unsetApplicationForPath(
+ getApplicationBase(p::get))
)
);
}
private static OSGi<ServiceTuple<Application>> deployApplication(
Map<String, ?> configuration, BundleContext bundleContext,
- ServiceTuple<Application> tuple) {
+ ServiceTuple<Application> tuple, AriesJaxRSServiceRuntime runtime) {
ExtensionManagerBus bus = createBus(bundleContext, configuration);
@@ -297,18 +301,15 @@ public class Whiteboard {
Map<String, Object> properties = getProperties(serviceReference);
properties.computeIfAbsent(
- JAX_RS_NAME,
- (__) -> AriesJaxRSServiceRuntime.generateApplicationName(
- properties));
+ JAX_RS_NAME, (__) -> generateApplicationName(
+ serviceReference::getProperty));
return
- all(
- deployRegistrator(bus, tuple.getService(), properties),
- registerCXFServletService(
- bus, getApplicationBase(properties), properties)).
- then(
- just(tuple)
- );
+ deployRegistrator(bus, tuple, properties, runtime).then(
+ registerCXFServletService(
+ bus, getApplicationBase(properties::get), properties)).then(
+ just(tuple)
+ );
}
private static OSGi<ServiceReference<Application>>
@@ -438,9 +439,7 @@ public class Whiteboard {
}
}
- public static String getApplicationBase(
- Map<String, Object> properties) {
-
+ public static String getApplicationBase(PropertyHolder properties) {
return properties.get(JAX_RS_APPLICATION_BASE).toString();
}
[7/8] aries-jax-rs-whiteboard git commit: Handle extension errors
Posted by cs...@apache.org.
Handle extension errors
Also proper signalling of those errors
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/08db7b15
Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/08db7b15
Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/08db7b15
Branch: refs/heads/master
Commit: 08db7b15ce4239e243bac3ed4766f505842d7ac3
Parents: 3b81013
Author: Carlos Sierra <cs...@apache.org>
Authored: Fri Aug 25 14:36:24 2017 +0200
Committer: Carlos Sierra <cs...@apache.org>
Committed: Fri Aug 25 14:36:24 2017 +0200
----------------------------------------------------------------------
jax-rs.itests/src/main/java/test/JaxrsTest.java | 201 ++++++++++++++++++-
.../internal/AriesJaxRSServiceRuntime.java | 37 +++-
.../internal/CXFJaxRsServiceRegistrator.java | 45 ++++-
.../aries/jax/rs/whiteboard/internal/Utils.java | 36 +---
.../jax/rs/whiteboard/internal/Whiteboard.java | 159 ++++++++++++---
5 files changed, 401 insertions(+), 77 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/08db7b15/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 1ed4a6c..9857ae4 100644
--- a/jax-rs.itests/src/main/java/test/JaxrsTest.java
+++ b/jax-rs.itests/src/main/java/test/JaxrsTest.java
@@ -46,6 +46,7 @@ import test.types.TestHelper;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
@@ -870,21 +871,32 @@ public class JaxrsTest extends TestHelper {
}
@Test
- public void testStandaloneFilter() {
+ public void testStandaloneFilter() throws InterruptedException {
Client client = createClient();
WebTarget webTarget = client.
target("http://localhost:8080").
path("test");
- ServiceRegistration<?> filterRegistration = null;
+ JaxRSServiceRuntime runtime = getJaxRSServiceRuntime();
ServiceRegistration<?> serviceRegistration = null;
try {
+ RuntimeDTO runtimeDTO = runtime.getRuntimeDTO();
+
+ assertEquals(
+ 0, runtimeDTO.defaultApplication.extensionDTOs.length);
+
serviceRegistration = registerAddon(new TestAddon());
- filterRegistration = registerExtension("Filter");
+ ServiceRegistration<?> filterRegistration = registerExtension(
+ "Filter");
+
+ runtimeDTO = runtime.getRuntimeDTO();
+
+ assertEquals(
+ 1, runtimeDTO.defaultApplication.extensionDTOs.length);
Response response = webTarget.request().get();
@@ -893,13 +905,139 @@ public class JaxrsTest extends TestHelper {
response.readEntity(String.class));
assertEquals("true", response.getHeaders().getFirst("Filtered"));
+
+ filterRegistration.unregister();
+
+ runtimeDTO = runtime.getRuntimeDTO();
+
+ assertEquals(
+ 0, runtimeDTO.defaultApplication.extensionDTOs.length);
}
finally {
if (serviceRegistration != null) {
serviceRegistration.unregister();
}
- if (filterRegistration != null) {
- filterRegistration.unregister();
+ }
+ }
+
+ @Test
+ public void testInvalidExtension() throws InterruptedException {
+ Client client = createClient();
+
+ WebTarget webTarget = client.
+ target("http://localhost:8080").
+ path("test");
+
+ JaxRSServiceRuntime runtime = getJaxRSServiceRuntime();
+
+ ServiceRegistration<?> serviceRegistration = null;
+
+ try {
+ RuntimeDTO runtimeDTO = runtime.getRuntimeDTO();
+
+ assertEquals(
+ 0, runtimeDTO.defaultApplication.extensionDTOs.length);
+
+ serviceRegistration = registerAddon(new TestAddon());
+
+ ServiceRegistration<?> filterRegistration =
+ registerInvalidExtension("Filter");
+
+ runtimeDTO = runtime.getRuntimeDTO();
+
+ assertEquals(
+ 0, runtimeDTO.defaultApplication.extensionDTOs.length);
+
+ assertEquals(1, runtimeDTO.failedExtensionDTOs.length);
+ assertEquals(
+ (long)filterRegistration.getReference().getProperty(
+ "service.id"),
+ runtimeDTO.failedExtensionDTOs[0].serviceId);
+ assertEquals(
+ DTOConstants.FAILURE_REASON_NOT_AN_EXTENSION_TYPE,
+ runtimeDTO.failedExtensionDTOs[0].failureReason);
+
+ Response response = webTarget.request().get();
+
+ assertEquals(
+ "This should say hello", "Hello test",
+ response.readEntity(String.class));
+
+ assertNull(response.getHeaders().getFirst("Filtered"));
+
+ filterRegistration.unregister();
+
+ runtimeDTO = runtime.getRuntimeDTO();
+
+ assertEquals(
+ 0, runtimeDTO.defaultApplication.extensionDTOs.length);
+
+ assertEquals(0, runtimeDTO.failedExtensionDTOs.length);
+ }
+ finally {
+ if (serviceRegistration != null) {
+ serviceRegistration.unregister();
+ }
+ }
+ }
+
+ @Test
+ public void testUngettableExtension() throws InterruptedException {
+ Client client = createClient();
+
+ WebTarget webTarget = client.
+ target("http://localhost:8080").
+ path("test");
+
+ JaxRSServiceRuntime runtime = getJaxRSServiceRuntime();
+
+ ServiceRegistration<?> serviceRegistration = null;
+
+ try {
+ RuntimeDTO runtimeDTO = runtime.getRuntimeDTO();
+
+ assertEquals(
+ 0, runtimeDTO.defaultApplication.extensionDTOs.length);
+
+ serviceRegistration = registerAddon(new TestAddon());
+
+ ServiceRegistration<?> filterRegistration =
+ registerUngettableExtension("Filter");
+
+ runtimeDTO = runtime.getRuntimeDTO();
+
+ assertEquals(
+ 0, runtimeDTO.defaultApplication.extensionDTOs.length);
+
+ assertEquals(1, runtimeDTO.failedExtensionDTOs.length);
+ assertEquals(
+ (long)filterRegistration.getReference().getProperty(
+ "service.id"),
+ runtimeDTO.failedExtensionDTOs[0].serviceId);
+ assertEquals(
+ DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE,
+ runtimeDTO.failedExtensionDTOs[0].failureReason);
+
+ Response response = webTarget.request().get();
+
+ assertEquals(
+ "This should say hello", "Hello test",
+ response.readEntity(String.class));
+
+ assertNull(response.getHeaders().getFirst("Filtered"));
+
+ filterRegistration.unregister();
+
+ runtimeDTO = runtime.getRuntimeDTO();
+
+ assertEquals(
+ 0, runtimeDTO.defaultApplication.extensionDTOs.length);
+
+ assertEquals(0, runtimeDTO.failedExtensionDTOs.length);
+ }
+ finally {
+ if (serviceRegistration != null) {
+ serviceRegistration.unregister();
}
}
}
@@ -1060,9 +1198,62 @@ public class JaxrsTest extends TestHelper {
}
return bundleContext.registerService(
+ ContainerResponseFilter.class, testFilter, properties);
+ }
+
+ private ServiceRegistration<?> registerInvalidExtension(
+ String name, Object... keyValues) {
+
+ TestFilter testFilter = new TestFilter();
+
+ Dictionary<String, Object> properties = new Hashtable<>();
+
+ properties.put(JAX_RS_EXTENSION, true);
+ properties.put(JAX_RS_NAME, name);
+
+ for (int i = 0; i < keyValues.length; i = i + 2) {
+ properties.put(keyValues[i].toString(), keyValues[i + 1]);
+ }
+
+ return bundleContext.registerService(
Object.class, testFilter, properties);
}
+ private ServiceRegistration<?> registerUngettableExtension(
+ String name, Object... keyValues) {
+
+ Dictionary<String, Object> properties = new Hashtable<>();
+
+ properties.put(JAX_RS_EXTENSION, true);
+ properties.put(JAX_RS_NAME, name);
+
+ for (int i = 0; i < keyValues.length; i = i + 2) {
+ properties.put(keyValues[i].toString(), keyValues[i + 1]);
+ }
+
+ return bundleContext.registerService(
+ ContainerResponseFilter.class,
+ new ServiceFactory<ContainerResponseFilter>() {
+ @Override
+ public ContainerResponseFilter getService(
+ Bundle bundle,
+ ServiceRegistration<ContainerResponseFilter>
+ serviceRegistration) {
+
+ return null;
+ }
+
+ @Override
+ public void ungetService(
+ Bundle bundle,
+ ServiceRegistration<ContainerResponseFilter>
+ serviceRegistration,
+ ContainerResponseFilter containerResponseFilter) {
+
+ }
+ }, properties);
+ }
+
private ServiceRegistration<Application> registerUngettableApplication(
Object... keyValues) {
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/08db7b15/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 a098f82..2d2706e 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
@@ -69,12 +69,19 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
private TreeSet<ServiceReference<?>> _ungettableExtensions = new TreeSet<>();
private TreeSet<ServiceReference<?>> _dependentServices = new TreeSet<>();
+
+ private TreeSet<ServiceReference<?>> _invalidExtensions = new TreeSet<>();
+
private Map<String, Object> _defaultApplicationProperties;
public void addDependentService(ServiceReference<?> serviceReference) {
_dependentServices.add(serviceReference);
}
+ public void addInvalidExtension(ServiceReference<?> serviceReference) {
+ _invalidExtensions.add(serviceReference);
+ }
+
public void clearDefaultApplication() {
_defaultApplicationProperties = Collections.emptyMap();
}
@@ -107,6 +114,10 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
_erroredApplications.remove(serviceReference);
}
+ public void removeInvalidExtension(ServiceReference<?> serviceReference) {
+ _invalidExtensions.remove(serviceReference);
+ }
+
public boolean removeNotGettableApplication(
ServiceReference<Application> serviceReference) {
@@ -247,16 +258,23 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
FailedResourceDTO[]::new
);
- runtimeDTO.failedExtensionDTOs = unreferenciableExtensionsDTOStream().map(
- sr -> buildFailedExtensionDTO(
- DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE, sr)
- ).toArray(
- FailedExtensionDTO[]::new
- );
+ runtimeDTO.failedExtensionDTOs = Stream.concat(
+ unreferenciableExtensionsDTOStream(),
+ invalidExtensionsDTOStream()
+ ).toArray(
+ FailedExtensionDTO[]::new
+ );
return runtimeDTO;
}
+ private Stream<FailedExtensionDTO> invalidExtensionsDTOStream() {
+ return _invalidExtensions.stream().map(
+ sr -> buildFailedExtensionDTO(
+ DTOConstants.FAILURE_REASON_NOT_AN_EXTENSION_TYPE, sr)
+ );
+ }
+
private Stream<FailedResourceDTO> dependentServiceStreamDTO() {
return _dependentServices.stream().map(
sr -> buildFailedResourceDTO(
@@ -282,8 +300,11 @@ public class AriesJaxRSServiceRuntime implements JaxRSServiceRuntime {
return failedExtensionDTO;
}
- private Stream<ServiceReference<?>> unreferenciableExtensionsDTOStream() {
- return _ungettableExtensions.stream();
+ private Stream<FailedExtensionDTO> unreferenciableExtensionsDTOStream() {
+ return _ungettableExtensions.stream().map(
+ sr -> buildFailedExtensionDTO(
+ DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE, sr)
+ );
}
private FailedResourceDTO buildFailedResourceDTO(
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/08db7b15/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 50d51a7..e06a907 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
@@ -18,11 +18,15 @@
package org.apache.aries.jax.rs.whiteboard.internal;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
+import java.util.stream.Stream;
import javax.ws.rs.core.Application;
+import javax.ws.rs.core.Feature;
import org.apache.aries.jax.rs.whiteboard.internal.Utils.ComparableResourceProvider;
+import org.apache.aries.jax.rs.whiteboard.internal.Utils.ServiceTuple;
import org.apache.cxf.Bus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
@@ -33,13 +37,16 @@ import org.apache.cxf.jaxrs.model.OperationResourceInfo;
import org.apache.cxf.jaxrs.provider.json.JSONProvider;
import org.apache.cxf.jaxrs.utils.ResourceUtils;
import org.apache.cxf.message.Message;
+import org.osgi.framework.ServiceReference;
+
+import static org.apache.aries.jax.rs.whiteboard.internal.Utils.canonicalize;
public class CXFJaxRsServiceRegistrator {
private volatile boolean _closed = false;
private final Application _application;
private final Bus _bus;
- private final Collection<Object> _providers = new ArrayList<>();
+ private final Collection<ServiceTuple<?>> _providers = new ArrayList<>();
private Server _server;
private final Collection<ResourceProvider> _services = new ArrayList<>();
@@ -60,12 +67,12 @@ public class CXFJaxRsServiceRegistrator {
rewire();
}
- public void addProvider(Utils.ServiceTuple<?> tuple) {
+ public void addProvider(ServiceTuple<?> tuple) {
if (_closed) {
return;
}
- _providers.add(tuple.getService());
+ _providers.add(tuple);
rewire();
}
@@ -92,12 +99,12 @@ public class CXFJaxRsServiceRegistrator {
rewire();
}
- public void removeProvider(Utils.ServiceTuple<?> tuple) {
+ public void removeProvider(ServiceTuple<?> tuple) {
if (_closed) {
return;
}
- _providers.remove(tuple.getService());
+ _providers.remove(tuple);
rewire();
}
@@ -166,8 +173,32 @@ public class CXFJaxRsServiceRegistrator {
jaxRsServerFactoryBean.setProvider(jsonProvider);
- for (Object provider : _providers) {
- jaxRsServerFactoryBean.setProvider(provider);
+ for (ServiceTuple<?> provider : _providers) {
+ jaxRsServerFactoryBean.setProvider(
+ (Feature) featureContext -> {
+ ServiceReference<?> serviceReference =
+ provider.getServiceReference();
+
+ String[] interfaces = canonicalize(
+ serviceReference.getProperty("objectClass"));
+
+ Class[] classes = Arrays.stream(interfaces).flatMap(
+ className -> {
+ try {
+ return Stream.of(Class.forName(className));
+ }
+ catch (ClassNotFoundException e) {
+ return Stream.empty();
+ }
+ }
+ ).toArray(
+ Class[]::new
+ );
+
+ featureContext.register(provider.getService(), classes);
+
+ return true;
+ });
}
for (ResourceProvider resourceProvider: _services) {
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/08db7b15/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
index 058c1a8..e013c14 100644
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
@@ -48,6 +48,16 @@ import static org.apache.aries.osgi.functional.OSGi.register;
*/
public class Utils {
+ public static String[] canonicalize(Object propertyValue) {
+ if (propertyValue == null) {
+ return new String[0];
+ }
+ if (propertyValue instanceof String[]) {
+ return (String[]) propertyValue;
+ }
+ return new String[]{propertyValue.toString()};
+ }
+
public static String generateApplicationName(
PropertyHolder propertyHolder) {
@@ -109,32 +119,6 @@ public class Utils {
}
}
- public static OSGi<?> safeRegisterGeneric(
- ServiceReference<?> serviceReference,
- String applicationName,
- CXFJaxRsServiceRegistrator registrator,
- AriesJaxRSServiceRuntime runtime) {
-
- if (isExtension(serviceReference)) {
- return safeRegisterExtension(
- serviceReference, applicationName, registrator, runtime);
- }
- else {
- return safeRegisterEndpoint(
- serviceReference, applicationName, registrator, runtime);
- }
- }
-
- private static boolean isExtension(ServiceReference<?> serviceReference) {
- Object extensionProperty = serviceReference.getProperty(
- "osgi.jaxrs.extension");
-
- return
- (extensionProperty != null) &&
- (extensionProperty instanceof Boolean) &&
- ((boolean) extensionProperty);
- }
-
public static OSGi<?> safeRegisterExtension(
ServiceReference<?> serviceReference, String applicationName,
CXFJaxRsServiceRegistrator registrator,
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/08db7b15/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 5a586fc..02e1b73 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
@@ -35,29 +35,46 @@ import org.osgi.service.http.runtime.HttpServiceRuntime;
import org.osgi.service.jaxrs.runtime.JaxRSServiceRuntime;
import javax.servlet.Servlet;
+import javax.ws.rs.container.ContainerRequestFilter;
+import javax.ws.rs.container.ContainerResponseFilter;
+import javax.ws.rs.container.DynamicFeature;
import javax.ws.rs.core.Application;
+import javax.ws.rs.core.Feature;
+import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.ParamConverterProvider;
+import javax.ws.rs.ext.ReaderInterceptor;
+import javax.ws.rs.ext.WriterInterceptor;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Dictionary;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Consumer;
import java.util.function.Function;
import static java.lang.String.format;
import static org.apache.aries.jax.rs.whiteboard.internal.AriesJaxRSServiceRuntime.getApplicationName;
+import static org.apache.aries.jax.rs.whiteboard.internal.Utils.canonicalize;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.deployRegistrator;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.generateApplicationName;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.getProperties;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.highestPer;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.onlyGettables;
-import static org.apache.aries.jax.rs.whiteboard.internal.Utils.safeRegisterGeneric;
+import static org.apache.aries.jax.rs.whiteboard.internal.Utils.safeRegisterEndpoint;
+import static org.apache.aries.jax.rs.whiteboard.internal.Utils.safeRegisterExtension;
import static org.apache.aries.jax.rs.whiteboard.internal.Utils.service;
-import static org.apache.aries.osgi.functional.OSGi.NOOP;
import static org.apache.aries.osgi.functional.OSGi.all;
import static org.apache.aries.osgi.functional.OSGi.bundleContext;
import static org.apache.aries.osgi.functional.OSGi.just;
+import static org.apache.aries.osgi.functional.OSGi.nothing;
+import static org.apache.aries.osgi.functional.OSGi.onClose;
import static org.apache.aries.osgi.functional.OSGi.register;
import static org.apache.aries.osgi.functional.OSGi.serviceReferences;
import static org.osgi.service.http.runtime.HttpServiceRuntimeConstants.HTTP_SERVICE_ENDPOINT;
@@ -85,9 +102,24 @@ public class Whiteboard {
ServiceTuple::getServiceReference).andThen(
sr -> getApplicationBase(sr::getProperty));
+ public static final Collection<String> SUPPORTED_EXTENSION_INTERFACES = new HashSet<>(
+ Arrays.asList(
+ ContainerRequestFilter.class.getName(),
+ ContainerResponseFilter.class.getName(),
+ ReaderInterceptor.class.getName(),
+ WriterInterceptor.class.getName(),
+ MessageBodyReader.class.getName(),
+ MessageBodyWriter.class.getName(),
+ ContextResolver.class.getName(),
+ ExceptionMapper.class.getName(),
+ ParamConverterProvider.class.getName(),
+ Feature.class.getName(),
+ DynamicFeature.class.getName()
+ ));
+
public static final String DEFAULT_NAME = ".default";
- public static OSGi<Void> createWhiteboard(Dictionary<String, ?> configuration) {
+ public static OSGi<?> createWhiteboard(Dictionary<String, ?> configuration) {
AriesJaxRSServiceRuntime runtime = new AriesJaxRSServiceRuntime();
Map<String, ?> configurationMap = Maps.from(configuration);
@@ -99,18 +131,27 @@ public class Whiteboard {
just(new ServiceRegistrationChangeCounter(runtimeRegistration)).flatMap(counter ->
just(runtimeRegistration.getReference()).flatMap(runtimeReference ->
all(
- countChanges(
+ ignore(countChanges(
whiteboardApplications(
runtimeReference, runtime, configurationMap),
- counter),
- countChanges(
- whiteBoardApplicationSingletons(
+ counter)),
+ ignore(countChanges(
+ whiteBoardApplicationResources(
bundleContext, runtimeReference,
defaultApplicationReference, runtime),
- counter)
+ counter)),
+ ignore(countChanges(
+ whiteBoardApplicationExtensions(
+ bundleContext, runtimeReference,
+ defaultApplicationReference, runtime),
+ counter))
))))));
}
+ private static OSGi<Void> ignore(OSGi<?> program) {
+ return program.map(t -> { return null;});
+ }
+
private static OSGi<Collection<String>> bestEffortCalculationOfEnpoints(Filter filter) {
Collection<String> endPoints = new ArrayList<>();
@@ -127,16 +168,6 @@ public class Whiteboard {
);
}
- private static String[] canonicalize(Object propertyValue) {
- if (propertyValue == null) {
- return new String[0];
- }
- if (propertyValue instanceof String[]) {
- return (String[]) propertyValue;
- }
- return new String[]{propertyValue.toString()};
- }
-
private static ExtensionManagerBus createBus(
BundleContext bundleContext, Map<String, ?> configuration) {
@@ -232,7 +263,7 @@ public class Whiteboard {
ApplicationReference applicationReference,
AriesJaxRSServiceRuntime runtime, OSGi<?> program) {
- String[] extensionDependencies = canonicalize(
+ String[] extensionDependencies = Utils.canonicalize(
serviceReference.getProperty(JAX_RS_EXTENSION_SELECT));
if (extensionDependencies.length > 0) {
@@ -273,34 +304,89 @@ public class Whiteboard {
return program;
}
- private static OSGi<?> whiteBoardApplicationSingletons(
+ private static OSGi<?> whiteBoardApplicationResources(
BundleContext bundleContext,
ServiceReference<?> jaxRsRuntimeServiceReference,
ApplicationReference defaultApplicationReference,
AriesJaxRSServiceRuntime runtime) {
return
- serviceReferences(getApplicationSingletonsFilter()).
+ serviceReferences(getResourcesFilter()).
filter(new TargetFilter<>(jaxRsRuntimeServiceReference)).
- flatMap(endpointReference ->
- chooseApplication(endpointReference, defaultApplicationReference).
+ flatMap(resourceReference ->
+ chooseApplication(
+ resourceReference, just(defaultApplicationReference)).
flatMap(applicationReference ->
waitForExtensionDependencies(
- bundleContext, endpointReference, applicationReference, runtime,
- safeRegisterGeneric(
- endpointReference,
+ bundleContext, resourceReference, applicationReference,
+ runtime,
+ safeRegisterEndpoint(
+ resourceReference,
applicationReference.getApplicationName(),
applicationReference.getRegistrator(), runtime)
)));
}
+ private static OSGi<?> whiteBoardApplicationExtensions(
+ BundleContext bundleContext,
+ ServiceReference<?> jaxRsRuntimeServiceReference,
+ ApplicationReference defaultApplicationReference,
+ AriesJaxRSServiceRuntime runtime) {
+ return
+ onlySupportedInterfaces(
+ serviceReferences(getApplicationExtensionsFilter()).
+ filter(new TargetFilter<>(jaxRsRuntimeServiceReference)),
+ runtime::addInvalidExtension, runtime::removeInvalidExtension).
+ flatMap(endpointReference ->
+ chooseApplication(
+ endpointReference,
+ all(
+ just(defaultApplicationReference),
+ allApplicationReferences())).
+ flatMap(applicationReference ->
+ waitForExtensionDependencies(
+ bundleContext, endpointReference, applicationReference, runtime,
+ safeRegisterExtension(
+ endpointReference,
+ applicationReference.getApplicationName(),
+ applicationReference.getRegistrator(), runtime)
+ )));
+ }
+
+ private static OSGi<ServiceReference<Object>> onlySupportedInterfaces(
+ OSGi<ServiceReference<Object>> program,
+ Consumer<ServiceReference<?>> onInvalidAdded,
+ Consumer<ServiceReference<?>> onInvalidRemoved) {
+
+ return program.flatMap(sr -> {
+ if (signalsValidInterface(sr)) {
+ return just(sr);
+ }
+ else {
+ onInvalidAdded.accept(sr);
+ return
+ onClose(() -> onInvalidRemoved.accept(sr)).then(nothing());
+ }
+ });
+ }
+
+ private static boolean signalsValidInterface(
+ ServiceReference<Object> serviceReference) {
+
+ String[] objectClasses = canonicalize(serviceReference.getProperty(
+ "objectClass"));
+
+ return Arrays.stream(objectClasses).
+ anyMatch(SUPPORTED_EXTENSION_INTERFACES::contains);
+ }
+
private static OSGi<ApplicationReference> chooseApplication(
- ServiceReference<?> serviceReference, ApplicationReference theDefault) {
+ ServiceReference<?> serviceReference, OSGi<ApplicationReference> theDefault) {
Object applicationSelectProperty = serviceReference.getProperty(
JAX_RS_APPLICATION_SELECT);
if (applicationSelectProperty == null) {
- return just(theDefault);
+ return theDefault;
}
String applicationName = getApplicationName(
@@ -316,11 +402,22 @@ public class Whiteboard {
));
}
- private static String getApplicationSingletonsFilter() {
+ public static OSGi<ApplicationReference> allApplicationReferences() {
+ return
+ serviceReferences(CXFJaxRsServiceRegistrator.class).
+ flatMap(registratorReference ->
+ just(getApplicationName(registratorReference::getProperty)).
+ flatMap(applicationName ->
+ service(registratorReference).flatMap(registrator ->
+ just(new ApplicationReference(applicationName, registrator))
+ )));
+ }
+
+ private static String getApplicationExtensionsFilter() {
return format(
- "(&(!(objectClass=%s))(|%s%s))",
+ "(&(!(objectClass=%s))(%s=%s)%s)",
ApplicationExtensionRegistration.class.getName(),
- getExtensionsFilter(), getResourcesFilter());
+ JAX_RS_EXTENSION, true, getExtensionsFilter());
}
private static OSGi<?> whiteboardApplications(