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 2018/02/21 15:45:15 UTC
aries-jax-rs-whiteboard git commit: Source rearrangement
Repository: aries-jax-rs-whiteboard
Updated Branches:
refs/heads/master 92dcd0118 -> 6f25f502b
Source rearrangement
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/6f25f502
Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/6f25f502
Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/6f25f502
Branch: refs/heads/master
Commit: 6f25f502bc1674444d0b39eef4308d57eb3806e5
Parents: 92dcd01
Author: Carlos Sierra <cs...@apache.org>
Authored: Wed Feb 21 16:44:45 2018 +0100
Committer: Carlos Sierra <cs...@apache.org>
Committed: Wed Feb 21 16:44:45 2018 +0100
----------------------------------------------------------------------
.../activator/CxfJaxrsBundleActivator.java | 4 +-
.../internal/AriesJaxrsServiceRuntime.java | 8 +-
.../internal/CxfJaxrsServiceRegistrator.java | 409 -------------------
.../aries/jax/rs/whiteboard/internal/Utils.java | 322 ---------------
.../jax/rs/whiteboard/internal/Whiteboard.java | 24 +-
.../cxf/ComparableResourceComparator.java | 72 ++++
.../cxf/CxfJaxrsServiceRegistrator.java | 302 ++++++++++++++
.../cxf/ServiceReferenceFilterProviderInfo.java | 47 +++
.../ServiceReferenceProviderInfoComparator.java | 64 +++
.../utils/ApplicationExtensionRegistration.java | 20 +
.../internal/utils/PropertyHolder.java | 22 +
.../utils/ServiceReferenceResourceProvider.java | 72 ++++
.../whiteboard/internal/utils/ServiceTuple.java | 70 ++++
.../jax/rs/whiteboard/internal/utils/Utils.java | 216 ++++++++++
14 files changed, 904 insertions(+), 748 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/6f25f502/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CxfJaxrsBundleActivator.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CxfJaxrsBundleActivator.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CxfJaxrsBundleActivator.java
index f7c88f7..d5a8f8b 100644
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CxfJaxrsBundleActivator.java
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/activator/CxfJaxrsBundleActivator.java
@@ -26,7 +26,7 @@ import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.ext.RuntimeDelegate;
import org.apache.aries.jax.rs.whiteboard.internal.client.ClientBuilderFactory;
-import org.apache.aries.jax.rs.whiteboard.internal.Utils.PropertyHolder;
+import org.apache.aries.jax.rs.whiteboard.internal.utils.PropertyHolder;
import org.apache.aries.osgi.functional.OSGi;
import org.apache.aries.osgi.functional.OSGiResult;
import org.apache.cxf.jaxrs.impl.RuntimeDelegateImpl;
@@ -38,7 +38,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.lang.String.format;
-import static org.apache.aries.jax.rs.whiteboard.internal.Utils.canonicalize;
+import static org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.canonicalize;
import static org.apache.aries.jax.rs.whiteboard.internal.Whiteboard.createWhiteboard;
import static org.apache.aries.osgi.functional.OSGi.all;
import static org.apache.aries.osgi.functional.OSGi.configurations;
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/6f25f502/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 aecce6c..6fd30b4 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,8 +17,8 @@
package org.apache.aries.jax.rs.whiteboard.internal;
-import static org.apache.aries.jax.rs.whiteboard.internal.Utils.canonicalize;
-import static org.apache.aries.jax.rs.whiteboard.internal.Utils.generateApplicationName;
+import static org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.canonicalize;
+import static org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.generateApplicationName;
import static org.apache.aries.jax.rs.whiteboard.internal.Whiteboard.DEFAULT_NAME;
import static org.apache.aries.jax.rs.whiteboard.internal.Whiteboard.SUPPORTED_EXTENSION_INTERFACES;
import static org.apache.aries.jax.rs.whiteboard.internal.Whiteboard.getApplicationBase;
@@ -34,7 +34,6 @@ import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.BiFunction;
-import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.ws.rs.Consumes;
@@ -42,7 +41,8 @@ import javax.ws.rs.Produces;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
-import org.apache.aries.jax.rs.whiteboard.internal.Utils.PropertyHolder;
+import org.apache.aries.jax.rs.whiteboard.internal.cxf.CxfJaxrsServiceRegistrator;
+import org.apache.aries.jax.rs.whiteboard.internal.utils.PropertyHolder;
import org.apache.aries.jax.rs.whiteboard.internal.introspection.ClassIntrospector;
import org.apache.aries.osgi.functional.CachingServiceReference;
import org.apache.cxf.Bus;
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/6f25f502/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
deleted file mode 100644
index 2f61c89..0000000
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/CxfJaxrsServiceRegistrator.java
+++ /dev/null
@@ -1,409 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.aries.jax.rs.whiteboard.internal;
-
-import static java.util.stream.Collectors.toMap;
-import static org.apache.aries.jax.rs.whiteboard.internal.Utils.canonicalize;
-import static org.apache.aries.jax.rs.whiteboard.internal.Whiteboard.SUPPORTED_EXTENSION_INTERFACES;
-import static org.apache.cxf.jaxrs.provider.ProviderFactory.DEFAULT_FILTER_NAME_BINDING;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.TreeSet;
-
-import javax.ws.rs.Priorities;
-import javax.ws.rs.RuntimeType;
-import javax.ws.rs.core.Application;
-import javax.ws.rs.core.Feature;
-import javax.ws.rs.core.FeatureContext;
-import javax.ws.rs.ext.RuntimeDelegate;
-
-import org.apache.aries.jax.rs.whiteboard.internal.Utils.ServiceReferenceResourceProvider;
-import org.apache.aries.jax.rs.whiteboard.internal.Utils.ServiceTuple;
-import org.apache.aries.osgi.functional.CachingServiceReference;
-import org.apache.cxf.Bus;
-import org.apache.cxf.common.util.ClassHelper;
-import org.apache.cxf.endpoint.Server;
-import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
-import org.apache.cxf.jaxrs.JAXRSServiceFactoryBean;
-import org.apache.cxf.jaxrs.ext.ResourceComparator;
-import org.apache.cxf.jaxrs.impl.ConfigurableImpl;
-import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
-import org.apache.cxf.jaxrs.model.ApplicationInfo;
-import org.apache.cxf.jaxrs.model.ClassResourceInfo;
-import org.apache.cxf.jaxrs.model.FilterProviderInfo;
-import org.apache.cxf.jaxrs.model.OperationResourceInfo;
-import org.apache.cxf.jaxrs.model.ProviderInfo;
-import org.apache.cxf.jaxrs.provider.ProviderFactory;
-import org.apache.cxf.jaxrs.provider.ProviderFactory.ProviderInfoClassComparator;
-import org.apache.cxf.jaxrs.provider.ServerConfigurableFactory;
-import org.apache.cxf.jaxrs.utils.AnnotationUtils;
-import org.apache.cxf.message.Message;
-import org.osgi.framework.ServiceReference;
-
-public class CxfJaxrsServiceRegistrator {
-
- public CxfJaxrsServiceRegistrator(
- Bus bus, Application application, Map<String, Object> properties) {
- _bus = bus;
- _application = application;
- _properties = Collections.unmodifiableMap(new HashMap<>(properties));
-
- Comparator<ServiceTuple<?>> comparing = Comparator.comparing(
- ServiceTuple::getCachingServiceReference);
-
- _providers = new TreeSet<>(comparing);
-
- rewire();
- }
-
- public void add(ResourceProvider resourceProvider) {
- if (_closed) {
- return;
- }
-
- _services.add(resourceProvider);
-
- try {
- rewire();
- }
- catch (Exception e) {
- remove(resourceProvider);
-
- throw e;
- }
- }
-
- public void addProvider(ServiceTuple<?> tuple) {
- if (_closed) {
- return;
- }
-
- _providers.add(tuple);
-
- try {
- rewire();
- }
- catch (Exception e) {
- removeProvider(tuple);
-
- throw e;
- }
-
- }
-
- public void close() {
- if (_closed) {
- return;
- }
-
- if (_server != null) {
- _server.destroy();
- }
-
- _closed = true;
- }
-
- public <T> T createEndpoint(Application app, Class<T> endpointType) {
- JAXRSServerFactoryBean bean =
- RuntimeDelegate.getInstance().createEndpoint(
- app, JAXRSServerFactoryBean.class);
-
- if (JAXRSServerFactoryBean.class.isAssignableFrom(endpointType)) {
- return endpointType.cast(bean);
- }
- bean.setStart(false);
- Server server = bean.create();
- return endpointType.cast(server);
- }
-
- public Bus getBus() {
- return _bus;
- }
-
- public void remove(ResourceProvider resourceProvider) {
- if (_closed) {
- return;
- }
-
- _services.remove(resourceProvider);
-
- rewire();
- }
-
- public void removeProvider(ServiceTuple<?> tuple) {
- if (_closed) {
- return;
- }
-
- _providers.remove(tuple);
-
- rewire();
- }
-
- protected static Set<String> getFilterNameBindings(Bus bus, Object provider) {
- Class<?> pClass = ClassHelper.getRealClass(bus, provider);
- Set<String> names = AnnotationUtils.getNameBindings(pClass.getAnnotations());
- if (names.isEmpty()) {
- names = Collections.singleton(DEFAULT_FILTER_NAME_BINDING);
- }
- return names;
- }
- private final Application _application;
- private final Bus _bus;
- private final Collection<ServiceTuple<?>> _providers;
- private final Collection<ResourceProvider> _services = new ArrayList<>();
- private volatile boolean _closed = false;
- private JAXRSServerFactoryBean _jaxRsServerFactoryBean;
- private Map<String, Object> _properties;
- private Server _server;
-
- private static class ComparableResourceComparator
- implements ResourceComparator {
-
- static {
- comparator = Comparator.comparing(
- srrp -> srrp.getImmutableServiceReference());
- }
-
- @Override
- public int compare(
- ClassResourceInfo cri1, ClassResourceInfo cri2, Message message) {
-
- ResourceProvider rp1 = cri1.getResourceProvider();
- ResourceProvider rp2 = cri2.getResourceProvider();
-
- if (rp1 instanceof ServiceReferenceResourceProvider &&
- rp2 instanceof ServiceReferenceResourceProvider) {
-
- return comparator.compare(
- (ServiceReferenceResourceProvider)rp2,
- (ServiceReferenceResourceProvider)rp1);
- }
-
- if (rp1 instanceof ServiceReferenceResourceProvider) {
- return -1;
- }
-
- if (rp2 instanceof ServiceReferenceResourceProvider) {
- return 1;
- }
-
- return 0;
- }
-
- @Override
- public int compare(
- OperationResourceInfo oper1, OperationResourceInfo oper2,
- Message message) {
-
- return 0;
- }
- private static Comparator<ServiceReferenceResourceProvider> comparator;
-
- }
-
- private static class ProviderInfoComparator implements Comparator<ProviderInfo<?>> {
- public ProviderInfoComparator(
- ProviderInfoClassComparator providerInfoClassComparator) {
-
- _providerInfoClassComparator = providerInfoClassComparator;
- }
-
- @Override
- public int compare(ProviderInfo<?> pi1, ProviderInfo<?> pi2) {
- if (pi1 instanceof ServiceReferenceFilterProviderInfo<?>) {
- if (pi2 instanceof ServiceReferenceFilterProviderInfo<?>) {
- ServiceReference serviceReference1 =
- ((ServiceReferenceFilterProviderInfo) pi1).
- getServiceReference();
- ServiceReference serviceReference2 =
- ((ServiceReferenceFilterProviderInfo) pi2).
- getServiceReference();
-
- return serviceReference1.compareTo(serviceReference2);
- }
- else {
- return -1;
- }
- }
- else {
- if (pi2 instanceof ServiceReferenceFilterProviderInfo<?>) {
- return 1;
- }
- }
-
- return _providerInfoClassComparator.compare(pi1, pi2);
- }
-
- private final ProviderInfoClassComparator
- _providerInfoClassComparator;
- }
-
- private static class ServiceReferenceFilterProviderInfo<T>
- extends FilterProviderInfo<T> {
-
- public ServiceReferenceFilterProviderInfo(
- ServiceReference<?> serviceReference,
- Class<?> resourceClass, Class<?> serviceClass, T provider, Bus bus,
- Set<String> nameBindings, boolean dynamic, Map<Class<?>, Integer>
- supportedContracts) {
-
- super(resourceClass, serviceClass, provider, bus, nameBindings,
- dynamic, supportedContracts);
-
- _serviceReference = serviceReference;
- }
-
- public ServiceReference<?> getServiceReference() {
- return _serviceReference;
- }
-
- private ServiceReference<?> _serviceReference;
- }
-
- protected synchronized void rewire() {
- if (_server != null) {
- _server.destroy();
- }
-
- if (_services.isEmpty() &&
- _application.getSingletons().isEmpty() &&
- _application.getClasses().isEmpty()) {
-
- return;
- }
-
- _jaxRsServerFactoryBean = createEndpoint(
- _application, JAXRSServerFactoryBean.class);
-
- _jaxRsServerFactoryBean.setBus(_bus);
-
- _bus.setExtension(
- context -> {
- ConfigurableImpl<FeatureContext> configurable =
- new ConfigurableImpl<>(
- context, RuntimeType.SERVER,
- ServerConfigurableFactory.
- SERVER_FILTER_INTERCEPTOR_CLASSES);
-
- configurable.property(
- "osgi.jaxrs.application.serviceProperties", _properties);
-
- return configurable;
- },
- ServerConfigurableFactory.class);
-
- _jaxRsServerFactoryBean.setStart(false);
-
- for (ServiceTuple<?> provider : _providers) {
- CachingServiceReference<?> cachingServiceReference =
- provider.getCachingServiceReference();
-
- ServiceReference<?> serviceReference =
- cachingServiceReference.getServiceReference();
-
- Object service = provider.getService();
-
- if (service instanceof Feature) {
- _jaxRsServerFactoryBean.setProvider(service);
-
- continue;
- }
-
- Class<?> realClass = ClassHelper.getRealClass(getBus(), service);
-
- Class<?>[] interfaces = Arrays.stream(canonicalize(
- serviceReference.getProperty("objectClass")))
- .filter(SUPPORTED_EXTENSION_INTERFACES::containsKey)
- .map(SUPPORTED_EXTENSION_INTERFACES::get)
- .toArray(Class[]::new);
-
- Map<Class<?>, Integer> classesWithPriorities=
- Arrays.stream(interfaces).collect(
- toMap(
- c -> c,
- __ -> AnnotationUtils.getBindingPriority(
- realClass))
- );
-
- _jaxRsServerFactoryBean.setProvider(
- new ServiceReferenceFilterProviderInfo<>(
- serviceReference, realClass, realClass, service, getBus(),
- getFilterNameBindings(getBus(), service), false,
- classesWithPriorities));
- }
-
- for (ResourceProvider resourceProvider: _services) {
- _jaxRsServerFactoryBean.setResourceProvider(resourceProvider);
- }
-
- if (_jaxRsServerFactoryBean.getResourceClasses().isEmpty()) {
- return;
- }
-
- _jaxRsServerFactoryBean.setResourceComparator(
- new ComparableResourceComparator());
-
- ProviderInfoClassComparator providerInfoClassComparator =
- new ProviderInfoClassComparator(Object.class);
-
- _jaxRsServerFactoryBean.setProviderComparator(
- new ProviderInfoComparator(providerInfoClassComparator)
- );
-
- _server = _jaxRsServerFactoryBean.create();
-
- ApplicationInfo applicationInfo = (ApplicationInfo)
- _server.getEndpoint().get(Application.class.getName());
-
- applicationInfo.setOverridingProps(new HashMap<String, Object>() {{
- put("osgi.jaxrs.application.serviceProperties", _properties);
- }});
-
- _server.start();
- }
-
- protected Iterable<Class<?>> getStaticResourceClasses() {
- JAXRSServiceFactoryBean serviceFactory =
- _jaxRsServerFactoryBean.getServiceFactory();
-
- List<ClassResourceInfo> classResourceInfo =
- serviceFactory.getClassResourceInfo();
-
- ArrayList<Class<?>> classes = new ArrayList<>();
-
- for (ClassResourceInfo resourceInfo : classResourceInfo) {
- if (!ServiceReferenceResourceProvider.class.isAssignableFrom(
- resourceInfo.getResourceProvider().getClass())) {
-
- classes.add(resourceInfo.getResourceClass());
- }
- }
-
- return classes;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/6f25f502/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
deleted file mode 100644
index 0007673..0000000
--- a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/Utils.java
+++ /dev/null
@@ -1,322 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.aries.jax.rs.whiteboard.internal;
-
-import org.apache.aries.osgi.functional.CachingServiceReference;
-import org.apache.aries.osgi.functional.OSGi;
-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.framework.ServiceRegistration;
-
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.Dictionary;
-import java.util.HashMap;
-import java.util.Hashtable;
-import java.util.Map;
-import java.util.function.Consumer;
-import java.util.function.Function;
-
-import static org.apache.aries.osgi.functional.OSGi.bundleContext;
-import static org.apache.aries.osgi.functional.OSGi.effects;
-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.Utils.highest;
-
-/**
- * @author Carlos Sierra Andrés
- */
-public class Utils {
-
- public static String[] canonicalize(Object propertyValue) {
- if (propertyValue == null) {
- return new String[0];
- }
- if (propertyValue instanceof String[]) {
- return (String[]) propertyValue;
- }
- if (propertyValue instanceof Collection) {
- return
- ((Collection<?>)propertyValue).stream().
- map(
- Object::toString
- ).toArray(
- String[]::new
- );
- }
-
- return new String[]{propertyValue.toString()};
- }
-
- public static String generateApplicationName(
- PropertyHolder propertyHolder) {
-
- return ".generated.for." + propertyHolder.get("service.id");
- }
-
- public static Map<String, Object> getProperties(CachingServiceReference<?> 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 int getRanking(
- CachingServiceReference<?> cachingServiceReference) {
-
- Object property = cachingServiceReference.getProperty(
- "service.ranking");
-
- if (property == null) {
- return 0;
- }
-
- if (property instanceof Number) {
- return ((Number)property).intValue();
- }
- else {
- return 0;
- }
- }
-
- public static <T> ServiceReferenceResourceProvider getResourceProvider(
- ServiceObjects<T> serviceObjects) {
-
- CachingServiceReference<T> serviceReference =
- new CachingServiceReference<>(
- serviceObjects.getServiceReference());
-
- return new ServiceReferenceResourceProvider(
- serviceReference, serviceObjects);
- }
-
- public static <K, T extends Comparable<? super T>> OSGi<T> highestPer(
- Function<T, K> keySupplier, OSGi<T> program,
- Consumer<? super T> onAddingShadowed,
- Consumer<? super T> onRemovedShadowed) {
-
- return program.splitBy(
- keySupplier,
- p -> highest(
- p, Comparator.naturalOrder(),
- discards -> discards.
- effects(onAddingShadowed, onRemovedShadowed).
- then(nothing())
- )
- );
- }
-
- public static <T> OSGi<ServiceTuple<T>> onlyGettables(
- OSGi<CachingServiceReference<T>> program,
- Consumer<CachingServiceReference<T>> whenAddedNotGettable,
- Consumer<CachingServiceReference<T>> whenLeavingNotGettable) {
-
- return bundleContext().flatMap(bundleContext ->
- program.recoverWith(
- (serviceReference, e) ->
- notGettableResult(
- whenAddedNotGettable, whenLeavingNotGettable,
- serviceReference)
- ).flatMap(serviceReference -> {
- ServiceObjects<T> serviceObjects =
- bundleContext.getServiceObjects(
- serviceReference.getServiceReference());
- T service = serviceObjects.getService();
-
- if (service == null) {
- return notGettableResult(
- whenAddedNotGettable, whenLeavingNotGettable,
- serviceReference);
- }
-
- return
- onClose(
- () -> serviceObjects.ungetService(service)
- ).then(
- just(new ServiceTuple<>(
- serviceReference, serviceObjects, service))
- );
- }));
- }
-
- private static <T, S> OSGi<S> notGettableResult(
- Consumer<CachingServiceReference<T>> whenAddedNotGettable,
- Consumer<CachingServiceReference<T>> whenLeavingNotGettable,
- CachingServiceReference<T> immutable) {
-
- return effects(
- () -> whenAddedNotGettable.accept(immutable),
- () -> whenLeavingNotGettable.accept(immutable)
- ).then(
- nothing()
- );
- }
-
- public static <T> OSGi<T> service(
- CachingServiceReference<T> immutableServiceReference) {
-
- ServiceReference<T> serviceReference =
- immutableServiceReference.getServiceReference();
-
- return
- bundleContext().flatMap(bundleContext ->
- onClose(() -> bundleContext.ungetService(serviceReference)).then(
- just(bundleContext.getService(serviceReference))
- ));
- }
-
- public static <T> OSGi<ServiceObjects<T>> serviceObjects(
- CachingServiceReference<T> immutableServiceReference) {
-
- return
- bundleContext().flatMap(bundleContext ->
- just(bundleContext.getServiceObjects(
- immutableServiceReference.getServiceReference()))
- );
- }
-
- public static void updateProperty(
- ServiceRegistration<?> serviceRegistration, String key, Object value) {
-
- CachingServiceReference<?> serviceReference =
- new CachingServiceReference<>(serviceRegistration.getReference());
-
- Dictionary<String, Object> properties = new Hashtable<>();
-
- for (String propertyKey : serviceReference.getPropertyKeys()) {
- properties.put(
- propertyKey, serviceReference.getProperty(propertyKey));
- }
-
- properties.put(key, value);
-
- serviceRegistration.setProperties(properties);
- }
-
- public interface PropertyHolder {
- Object get(String propertyName);
- }
-
- public interface ApplicationExtensionRegistration {}
-
- public static class ServiceReferenceResourceProvider
- implements ResourceProvider {
-
- private final ServiceObjects<?> _serviceObjects;
- private CachingServiceReference<?> _serviceReference;
-
- ServiceReferenceResourceProvider(
- CachingServiceReference<?> serviceReference,
- ServiceObjects<?> serviceObjects) {
- _serviceReference = serviceReference;
-
- _serviceObjects = serviceObjects;
- }
-
- @Override
- public Object getInstance(Message m) {
- return _serviceObjects.getService();
- }
-
- @Override
- @SuppressWarnings({ "unchecked", "rawtypes" })
- public void releaseInstance(Message m, Object o) {
- ((ServiceObjects)_serviceObjects).ungetService(o);
- }
-
- @SuppressWarnings({ "rawtypes", "unchecked" })
- @Override
- public Class<?> getResourceClass() {
- Object service = _serviceObjects.getService();
-
- try {
- return service.getClass();
- }
- finally {
- ((ServiceObjects)_serviceObjects).ungetService(service);
- }
- }
-
- @Override
- public boolean isSingleton() {
- return false;
- }
-
- CachingServiceReference<?> getImmutableServiceReference() {
- return _serviceReference;
- }
-
- }
-
- public static class ServiceTuple<T> implements Comparable<ServiceTuple<T>> {
-
- private final CachingServiceReference<T> _serviceReference;
- private ServiceObjects<T> _serviceObjects;
- private final T _service;
-
- ServiceTuple(
- CachingServiceReference<T> cachingServiceReference,
- ServiceObjects<T> serviceObjects, T service) {
-
- _serviceReference = cachingServiceReference;
- _serviceObjects = serviceObjects;
- _service = service;
- }
-
- @Override
- public int compareTo(ServiceTuple<T> o) {
- return _serviceReference.compareTo(o._serviceReference);
- }
-
- public T getService() {
- return _service;
- }
-
- public ServiceObjects<T> getServiceObjects() {
- return _serviceObjects;
- }
-
- @Override
- public int hashCode() {
- return _serviceReference.hashCode();
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- ServiceTuple<?> that = (ServiceTuple<?>) o;
-
- return _serviceReference.equals(that._serviceReference);
- }
-
- CachingServiceReference<T> getCachingServiceReference() {
- return _serviceReference;
- }
-
- }
-
-}
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/6f25f502/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 4510075..7155e5e 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,9 +17,11 @@
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.jax.rs.whiteboard.internal.cxf.CxfJaxrsServiceRegistrator;
+import org.apache.aries.jax.rs.whiteboard.internal.utils.Utils;
+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.CachingServiceReference;
import org.apache.aries.osgi.functional.OSGi;
import org.apache.aries.osgi.functional.OSGiResult;
@@ -68,13 +70,13 @@ import java.util.stream.Stream;
import static java.lang.String.format;
import static java.util.stream.Collectors.toMap;
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.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.service;
-import static org.apache.aries.jax.rs.whiteboard.internal.Utils.updateProperty;
+import static org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.canonicalize;
+import static org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.generateApplicationName;
+import static org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.getProperties;
+import static org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.highestPer;
+import static org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.onlyGettables;
+import static org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.service;
+import static org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.updateProperty;
import static org.apache.aries.osgi.functional.OSGi.all;
import static org.apache.aries.osgi.functional.OSGi.effects;
import static org.apache.aries.osgi.functional.OSGi.ignore;
@@ -106,7 +108,7 @@ import static org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants.JAX_RS_
*/
public class Whiteboard {
- static final Map<String, Class<?>> SUPPORTED_EXTENSION_INTERFACES =
+ public static final Map<String, Class<?>> SUPPORTED_EXTENSION_INTERFACES =
Collections.unmodifiableMap(
Stream.of(ContainerRequestFilter.class,
ContainerResponseFilter.class,
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/6f25f502/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/cxf/ComparableResourceComparator.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/cxf/ComparableResourceComparator.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/cxf/ComparableResourceComparator.java
new file mode 100644
index 0000000..f68588c
--- /dev/null
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/cxf/ComparableResourceComparator.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.aries.jax.rs.whiteboard.internal.cxf;
+
+import org.apache.aries.jax.rs.whiteboard.internal.utils.ServiceReferenceResourceProvider;
+import org.apache.cxf.jaxrs.ext.ResourceComparator;
+import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
+import org.apache.cxf.jaxrs.model.ClassResourceInfo;
+import org.apache.cxf.jaxrs.model.OperationResourceInfo;
+import org.apache.cxf.message.Message;
+
+import java.util.Comparator;
+
+public class ComparableResourceComparator
+ implements ResourceComparator {
+
+ static {
+ comparator = Comparator.comparing(
+ srrp -> srrp.getImmutableServiceReference());
+ }
+
+ @Override
+ public int compare(
+ ClassResourceInfo cri1, ClassResourceInfo cri2, Message message) {
+
+ ResourceProvider rp1 = cri1.getResourceProvider();
+ ResourceProvider rp2 = cri2.getResourceProvider();
+
+ if (rp1 instanceof ServiceReferenceResourceProvider &&
+ rp2 instanceof ServiceReferenceResourceProvider) {
+
+ return comparator.compare(
+ (ServiceReferenceResourceProvider)rp2,
+ (ServiceReferenceResourceProvider)rp1);
+ }
+
+ if (rp1 instanceof ServiceReferenceResourceProvider) {
+ return -1;
+ }
+
+ if (rp2 instanceof ServiceReferenceResourceProvider) {
+ return 1;
+ }
+
+ return 0;
+ }
+
+ @Override
+ public int compare(
+ OperationResourceInfo oper1, OperationResourceInfo oper2,
+ Message message) {
+
+ return 0;
+ }
+ private static Comparator<ServiceReferenceResourceProvider> comparator;
+
+}
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/6f25f502/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/cxf/CxfJaxrsServiceRegistrator.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/cxf/CxfJaxrsServiceRegistrator.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/cxf/CxfJaxrsServiceRegistrator.java
new file mode 100644
index 0000000..bd37d98
--- /dev/null
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/cxf/CxfJaxrsServiceRegistrator.java
@@ -0,0 +1,302 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.aries.jax.rs.whiteboard.internal.cxf;
+
+import static java.util.stream.Collectors.toMap;
+import static org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.canonicalize;
+import static org.apache.aries.jax.rs.whiteboard.internal.Whiteboard.SUPPORTED_EXTENSION_INTERFACES;
+import static org.apache.cxf.jaxrs.provider.ProviderFactory.DEFAULT_FILTER_NAME_BINDING;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+
+import javax.ws.rs.RuntimeType;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.core.Feature;
+import javax.ws.rs.core.FeatureContext;
+import javax.ws.rs.ext.RuntimeDelegate;
+
+import org.apache.aries.jax.rs.whiteboard.internal.utils.ServiceReferenceResourceProvider;
+import org.apache.aries.jax.rs.whiteboard.internal.utils.ServiceTuple;
+import org.apache.aries.osgi.functional.CachingServiceReference;
+import org.apache.cxf.Bus;
+import org.apache.cxf.common.util.ClassHelper;
+import org.apache.cxf.endpoint.Server;
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.JAXRSServiceFactoryBean;
+import org.apache.cxf.jaxrs.impl.ConfigurableImpl;
+import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
+import org.apache.cxf.jaxrs.model.ApplicationInfo;
+import org.apache.cxf.jaxrs.model.ClassResourceInfo;
+import org.apache.cxf.jaxrs.provider.ProviderFactory.ProviderInfoClassComparator;
+import org.apache.cxf.jaxrs.provider.ServerConfigurableFactory;
+import org.apache.cxf.jaxrs.utils.AnnotationUtils;
+import org.osgi.framework.ServiceReference;
+
+public class CxfJaxrsServiceRegistrator {
+
+ public CxfJaxrsServiceRegistrator(
+ Bus bus, Application application, Map<String, Object> properties) {
+ _bus = bus;
+ _application = application;
+ _properties = Collections.unmodifiableMap(new HashMap<>(properties));
+
+ Comparator<ServiceTuple<?>> comparing = Comparator.comparing(
+ ServiceTuple::getCachingServiceReference);
+
+ _providers = new TreeSet<>(comparing);
+
+ rewire();
+ }
+
+ public void add(ResourceProvider resourceProvider) {
+ if (_closed) {
+ return;
+ }
+
+ _services.add(resourceProvider);
+
+ try {
+ rewire();
+ }
+ catch (Exception e) {
+ remove(resourceProvider);
+
+ throw e;
+ }
+ }
+
+ public void addProvider(ServiceTuple<?> tuple) {
+ if (_closed) {
+ return;
+ }
+
+ _providers.add(tuple);
+
+ try {
+ rewire();
+ }
+ catch (Exception e) {
+ removeProvider(tuple);
+
+ throw e;
+ }
+
+ }
+
+ public void close() {
+ if (_closed) {
+ return;
+ }
+
+ if (_server != null) {
+ _server.destroy();
+ }
+
+ _closed = true;
+ }
+
+ public <T> T createEndpoint(Application app, Class<T> endpointType) {
+ JAXRSServerFactoryBean bean =
+ RuntimeDelegate.getInstance().createEndpoint(
+ app, JAXRSServerFactoryBean.class);
+
+ if (JAXRSServerFactoryBean.class.isAssignableFrom(endpointType)) {
+ return endpointType.cast(bean);
+ }
+ bean.setStart(false);
+ Server server = bean.create();
+ return endpointType.cast(server);
+ }
+
+ public Bus getBus() {
+ return _bus;
+ }
+
+ public Iterable<Class<?>> getStaticResourceClasses() {
+ JAXRSServiceFactoryBean serviceFactory =
+ _jaxRsServerFactoryBean.getServiceFactory();
+
+ List<ClassResourceInfo> classResourceInfo =
+ serviceFactory.getClassResourceInfo();
+
+ ArrayList<Class<?>> classes = new ArrayList<>();
+
+ for (ClassResourceInfo resourceInfo : classResourceInfo) {
+ if (!ServiceReferenceResourceProvider.class.isAssignableFrom(
+ resourceInfo.getResourceProvider().getClass())) {
+
+ classes.add(resourceInfo.getResourceClass());
+ }
+ }
+
+ return classes;
+ }
+
+ public void remove(ResourceProvider resourceProvider) {
+ if (_closed) {
+ return;
+ }
+
+ _services.remove(resourceProvider);
+
+ rewire();
+ }
+
+ public void removeProvider(ServiceTuple<?> tuple) {
+ if (_closed) {
+ return;
+ }
+
+ _providers.remove(tuple);
+
+ rewire();
+ }
+
+ protected synchronized void rewire() {
+ if (_server != null) {
+ _server.destroy();
+ }
+
+ if (_services.isEmpty() &&
+ _application.getSingletons().isEmpty() &&
+ _application.getClasses().isEmpty()) {
+
+ return;
+ }
+
+ _jaxRsServerFactoryBean = createEndpoint(
+ _application, JAXRSServerFactoryBean.class);
+
+ _jaxRsServerFactoryBean.setBus(_bus);
+
+ _bus.setExtension(
+ context -> {
+ ConfigurableImpl<FeatureContext> configurable =
+ new ConfigurableImpl<>(
+ context, RuntimeType.SERVER,
+ ServerConfigurableFactory.
+ SERVER_FILTER_INTERCEPTOR_CLASSES);
+
+ configurable.property(
+ "osgi.jaxrs.application.serviceProperties", _properties);
+
+ return configurable;
+ },
+ ServerConfigurableFactory.class);
+
+ _jaxRsServerFactoryBean.setStart(false);
+
+ for (ServiceTuple<?> provider : _providers) {
+ CachingServiceReference<?> cachingServiceReference =
+ provider.getCachingServiceReference();
+
+ ServiceReference<?> serviceReference =
+ cachingServiceReference.getServiceReference();
+
+ Object service = provider.getService();
+
+ if (service instanceof Feature) {
+ _jaxRsServerFactoryBean.setProvider(service);
+
+ continue;
+ }
+
+ Class<?> realClass = ClassHelper.getRealClass(getBus(), service);
+
+ Class<?>[] interfaces = Arrays.stream(canonicalize(
+ serviceReference.getProperty("objectClass")))
+ .filter(SUPPORTED_EXTENSION_INTERFACES::containsKey)
+ .map(SUPPORTED_EXTENSION_INTERFACES::get)
+ .toArray(Class[]::new);
+
+ Map<Class<?>, Integer> classesWithPriorities=
+ Arrays.stream(interfaces).collect(
+ toMap(
+ c -> c,
+ __ -> AnnotationUtils.getBindingPriority(
+ realClass))
+ );
+
+ _jaxRsServerFactoryBean.setProvider(
+ new ServiceReferenceFilterProviderInfo<>(
+ serviceReference, realClass, realClass, service, getBus(),
+ getFilterNameBindings(getBus(), service), false,
+ classesWithPriorities));
+ }
+
+ for (ResourceProvider resourceProvider: _services) {
+ _jaxRsServerFactoryBean.setResourceProvider(resourceProvider);
+ }
+
+ if (_jaxRsServerFactoryBean.getResourceClasses().isEmpty()) {
+ return;
+ }
+
+ _jaxRsServerFactoryBean.setResourceComparator(
+ new ComparableResourceComparator());
+
+ ProviderInfoClassComparator providerInfoClassComparator =
+ new ProviderInfoClassComparator(Object.class);
+
+ _jaxRsServerFactoryBean.setProviderComparator(
+ new ServiceReferenceProviderInfoComparator(
+ providerInfoClassComparator)
+ );
+
+ _server = _jaxRsServerFactoryBean.create();
+
+ ApplicationInfo applicationInfo = (ApplicationInfo)
+ _server.getEndpoint().get(Application.class.getName());
+
+ applicationInfo.setOverridingProps(new HashMap<String, Object>() {{
+ put("osgi.jaxrs.application.serviceProperties", _properties);
+ }});
+
+ _server.start();
+ }
+
+ private final Application _application;
+ private final Bus _bus;
+ private final Collection<ServiceTuple<?>> _providers;
+ private final Collection<ResourceProvider> _services = new ArrayList<>();
+ private volatile boolean _closed = false;
+ private JAXRSServerFactoryBean _jaxRsServerFactoryBean;
+ private Map<String, Object> _properties;
+ private Server _server;
+
+ private static Set<String> getFilterNameBindings(
+ Bus bus, Object provider) {
+ Class<?> pClass = ClassHelper.getRealClass(bus, provider);
+ Set<String> names = AnnotationUtils.getNameBindings(
+ pClass.getAnnotations());
+ if (names.isEmpty()) {
+ names = Collections.singleton(DEFAULT_FILTER_NAME_BINDING);
+ }
+ return names;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/6f25f502/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/cxf/ServiceReferenceFilterProviderInfo.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/cxf/ServiceReferenceFilterProviderInfo.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/cxf/ServiceReferenceFilterProviderInfo.java
new file mode 100644
index 0000000..50cbb82
--- /dev/null
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/cxf/ServiceReferenceFilterProviderInfo.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.aries.jax.rs.whiteboard.internal.cxf;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.jaxrs.model.FilterProviderInfo;
+import org.osgi.framework.ServiceReference;
+
+import java.util.Map;
+import java.util.Set;
+
+public class ServiceReferenceFilterProviderInfo<T>
+ extends FilterProviderInfo<T> {
+
+ public ServiceReferenceFilterProviderInfo(
+ ServiceReference<?> serviceReference,
+ Class<?> resourceClass, Class<?> serviceClass, T provider, Bus bus,
+ Set<String> nameBindings, boolean dynamic, Map<Class<?>, Integer>
+ supportedContracts) {
+
+ super(resourceClass, serviceClass, provider, bus, nameBindings,
+ dynamic, supportedContracts);
+
+ _serviceReference = serviceReference;
+ }
+
+ public ServiceReference<?> getServiceReference() {
+ return _serviceReference;
+ }
+
+ private ServiceReference<?> _serviceReference;
+}
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/6f25f502/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/cxf/ServiceReferenceProviderInfoComparator.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/cxf/ServiceReferenceProviderInfoComparator.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/cxf/ServiceReferenceProviderInfoComparator.java
new file mode 100644
index 0000000..9490b43
--- /dev/null
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/cxf/ServiceReferenceProviderInfoComparator.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.aries.jax.rs.whiteboard.internal.cxf;
+
+import org.apache.cxf.jaxrs.model.ProviderInfo;
+import org.apache.cxf.jaxrs.provider.ProviderFactory;
+import org.apache.cxf.jaxrs.provider.ProviderFactory.ProviderInfoClassComparator;
+import org.osgi.framework.ServiceReference;
+
+import java.util.Comparator;
+
+public class ServiceReferenceProviderInfoComparator implements
+ Comparator<ProviderInfo<?>> {
+
+ public ServiceReferenceProviderInfoComparator(
+ ProviderInfoClassComparator providerInfoClassComparator) {
+
+ _providerInfoClassComparator = providerInfoClassComparator;
+ }
+
+ @Override
+ public int compare(ProviderInfo<?> pi1, ProviderInfo<?> pi2) {
+ if (pi1 instanceof ServiceReferenceFilterProviderInfo<?>) {
+ if (pi2 instanceof ServiceReferenceFilterProviderInfo<?>) {
+ ServiceReference serviceReference1 =
+ ((ServiceReferenceFilterProviderInfo) pi1).
+ getServiceReference();
+ ServiceReference serviceReference2 =
+ ((ServiceReferenceFilterProviderInfo) pi2).
+ getServiceReference();
+
+ return serviceReference1.compareTo(serviceReference2);
+ }
+ else {
+ return -1;
+ }
+ }
+ else {
+ if (pi2 instanceof ServiceReferenceFilterProviderInfo<?>) {
+ return 1;
+ }
+ }
+
+ return _providerInfoClassComparator.compare(pi1, pi2);
+ }
+
+ private final ProviderInfoClassComparator
+ _providerInfoClassComparator;
+}
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/6f25f502/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/ApplicationExtensionRegistration.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/ApplicationExtensionRegistration.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/ApplicationExtensionRegistration.java
new file mode 100644
index 0000000..3a5cec5
--- /dev/null
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/ApplicationExtensionRegistration.java
@@ -0,0 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.aries.jax.rs.whiteboard.internal.utils;
+
+public interface ApplicationExtensionRegistration {}
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/6f25f502/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/PropertyHolder.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/PropertyHolder.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/PropertyHolder.java
new file mode 100644
index 0000000..14c1bba
--- /dev/null
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/PropertyHolder.java
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.aries.jax.rs.whiteboard.internal.utils;
+
+public interface PropertyHolder {
+ Object get(String propertyName);
+}
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/6f25f502/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/ServiceReferenceResourceProvider.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/ServiceReferenceResourceProvider.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/ServiceReferenceResourceProvider.java
new file mode 100644
index 0000000..6483242
--- /dev/null
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/ServiceReferenceResourceProvider.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.aries.jax.rs.whiteboard.internal.utils;
+
+import org.apache.aries.osgi.functional.CachingServiceReference;
+import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
+import org.apache.cxf.message.Message;
+import org.osgi.framework.ServiceObjects;
+
+public class ServiceReferenceResourceProvider
+ implements ResourceProvider {
+
+ private final ServiceObjects<?> _serviceObjects;
+ private CachingServiceReference<?> _serviceReference;
+
+ ServiceReferenceResourceProvider(
+ CachingServiceReference<?> serviceReference,
+ ServiceObjects<?> serviceObjects) {
+ _serviceReference = serviceReference;
+
+ _serviceObjects = serviceObjects;
+ }
+
+ @Override
+ public Object getInstance(Message m) {
+ return _serviceObjects.getService();
+ }
+
+ @Override
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ public void releaseInstance(Message m, Object o) {
+ ((ServiceObjects)_serviceObjects).ungetService(o);
+ }
+
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ @Override
+ public Class<?> getResourceClass() {
+ Object service = _serviceObjects.getService();
+
+ try {
+ return service.getClass();
+ }
+ finally {
+ ((ServiceObjects)_serviceObjects).ungetService(service);
+ }
+ }
+
+ @Override
+ public boolean isSingleton() {
+ return false;
+ }
+
+ public CachingServiceReference<?> getImmutableServiceReference() {
+ return _serviceReference;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/6f25f502/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/ServiceTuple.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/ServiceTuple.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/ServiceTuple.java
new file mode 100644
index 0000000..5504a86
--- /dev/null
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/ServiceTuple.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.aries.jax.rs.whiteboard.internal.utils;
+
+import org.apache.aries.osgi.functional.CachingServiceReference;
+import org.osgi.framework.ServiceObjects;
+
+public class ServiceTuple<T> implements Comparable<ServiceTuple<T>> {
+
+ private final CachingServiceReference<T> _serviceReference;
+ private ServiceObjects<T> _serviceObjects;
+ private final T _service;
+
+ ServiceTuple(
+ CachingServiceReference<T> cachingServiceReference,
+ ServiceObjects<T> serviceObjects, T service) {
+
+ _serviceReference = cachingServiceReference;
+ _serviceObjects = serviceObjects;
+ _service = service;
+ }
+
+ @Override
+ public int compareTo(ServiceTuple<T> o) {
+ return _serviceReference.compareTo(o._serviceReference);
+ }
+
+ public T getService() {
+ return _service;
+ }
+
+ public ServiceObjects<T> getServiceObjects() {
+ return _serviceObjects;
+ }
+
+ @Override
+ public int hashCode() {
+ return _serviceReference.hashCode();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ ServiceTuple<?> that = (ServiceTuple<?>) o;
+
+ return _serviceReference.equals(that._serviceReference);
+ }
+
+ public CachingServiceReference<T> getCachingServiceReference() {
+ return _serviceReference;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/6f25f502/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/Utils.java
----------------------------------------------------------------------
diff --git a/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/Utils.java b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/Utils.java
new file mode 100644
index 0000000..eaf56e0
--- /dev/null
+++ b/jax-rs.whiteboard/src/main/java/org/apache/aries/jax/rs/whiteboard/internal/utils/Utils.java
@@ -0,0 +1,216 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.aries.jax.rs.whiteboard.internal.utils;
+
+import org.apache.aries.osgi.functional.CachingServiceReference;
+import org.apache.aries.osgi.functional.OSGi;
+import org.osgi.framework.ServiceObjects;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.function.Consumer;
+import java.util.function.Function;
+
+import static org.apache.aries.osgi.functional.OSGi.bundleContext;
+import static org.apache.aries.osgi.functional.OSGi.effects;
+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.Utils.highest;
+
+/**
+ * @author Carlos Sierra Andrés
+ */
+public class Utils {
+
+ public static String[] canonicalize(Object propertyValue) {
+ if (propertyValue == null) {
+ return new String[0];
+ }
+ if (propertyValue instanceof String[]) {
+ return (String[]) propertyValue;
+ }
+ if (propertyValue instanceof Collection) {
+ return
+ ((Collection<?>)propertyValue).stream().
+ map(
+ Object::toString
+ ).toArray(
+ String[]::new
+ );
+ }
+
+ return new String[]{propertyValue.toString()};
+ }
+
+ public static String generateApplicationName(
+ PropertyHolder propertyHolder) {
+
+ return ".generated.for." + propertyHolder.get("service.id");
+ }
+
+ public static Map<String, Object> getProperties(CachingServiceReference<?> 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 int getRanking(
+ CachingServiceReference<?> cachingServiceReference) {
+
+ Object property = cachingServiceReference.getProperty(
+ "service.ranking");
+
+ if (property == null) {
+ return 0;
+ }
+
+ if (property instanceof Number) {
+ return ((Number)property).intValue();
+ }
+ else {
+ return 0;
+ }
+ }
+
+ public static <T> ServiceReferenceResourceProvider getResourceProvider(
+ ServiceObjects<T> serviceObjects) {
+
+ CachingServiceReference<T> serviceReference =
+ new CachingServiceReference<>(
+ serviceObjects.getServiceReference());
+
+ return new ServiceReferenceResourceProvider(
+ serviceReference, serviceObjects);
+ }
+
+ public static <K, T extends Comparable<? super T>> OSGi<T> highestPer(
+ Function<T, K> keySupplier, OSGi<T> program,
+ Consumer<? super T> onAddingShadowed,
+ Consumer<? super T> onRemovedShadowed) {
+
+ return program.splitBy(
+ keySupplier,
+ p -> highest(
+ p, Comparator.naturalOrder(),
+ discards -> discards.
+ effects(onAddingShadowed, onRemovedShadowed).
+ then(nothing())
+ )
+ );
+ }
+
+ public static <T> OSGi<ServiceTuple<T>> onlyGettables(
+ OSGi<CachingServiceReference<T>> program,
+ Consumer<CachingServiceReference<T>> whenAddedNotGettable,
+ Consumer<CachingServiceReference<T>> whenLeavingNotGettable) {
+
+ return bundleContext().flatMap(bundleContext ->
+ program.recoverWith(
+ (serviceReference, e) ->
+ notGettableResult(
+ whenAddedNotGettable, whenLeavingNotGettable,
+ serviceReference)
+ ).flatMap(serviceReference -> {
+ ServiceObjects<T> serviceObjects =
+ bundleContext.getServiceObjects(
+ serviceReference.getServiceReference());
+ T service = serviceObjects.getService();
+
+ if (service == null) {
+ return notGettableResult(
+ whenAddedNotGettable, whenLeavingNotGettable,
+ serviceReference);
+ }
+
+ return
+ onClose(
+ () -> serviceObjects.ungetService(service)
+ ).then(
+ just(new ServiceTuple<>(
+ serviceReference, serviceObjects, service))
+ );
+ }));
+ }
+
+ private static <T, S> OSGi<S> notGettableResult(
+ Consumer<CachingServiceReference<T>> whenAddedNotGettable,
+ Consumer<CachingServiceReference<T>> whenLeavingNotGettable,
+ CachingServiceReference<T> immutable) {
+
+ return effects(
+ () -> whenAddedNotGettable.accept(immutable),
+ () -> whenLeavingNotGettable.accept(immutable)
+ ).then(
+ nothing()
+ );
+ }
+
+ public static <T> OSGi<T> service(
+ CachingServiceReference<T> immutableServiceReference) {
+
+ ServiceReference<T> serviceReference =
+ immutableServiceReference.getServiceReference();
+
+ return
+ bundleContext().flatMap(bundleContext ->
+ onClose(() -> bundleContext.ungetService(serviceReference)).then(
+ just(bundleContext.getService(serviceReference))
+ ));
+ }
+
+ public static <T> OSGi<ServiceObjects<T>> serviceObjects(
+ CachingServiceReference<T> immutableServiceReference) {
+
+ return
+ bundleContext().flatMap(bundleContext ->
+ just(bundleContext.getServiceObjects(
+ immutableServiceReference.getServiceReference()))
+ );
+ }
+
+ public static void updateProperty(
+ ServiceRegistration<?> serviceRegistration, String key, Object value) {
+
+ CachingServiceReference<?> serviceReference =
+ new CachingServiceReference<>(serviceRegistration.getReference());
+
+ Dictionary<String, Object> properties = new Hashtable<>();
+
+ for (String propertyKey : serviceReference.getPropertyKeys()) {
+ properties.put(
+ propertyKey, serviceReference.getProperty(propertyKey));
+ }
+
+ properties.put(key, value);
+
+ serviceRegistration.setProperties(properties);
+ }
+
+}