Repository: aries-jax-rs-whiteboard Updated Branches: refs/heads/master 4b959b585 -> e123aac77
Make serviceProperties available in all scopes 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/e123aac7 Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/e123aac7 Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/e123aac7 Branch: refs/heads/master Commit: e123aac774aa074e5865084c3e5f360b2df38489 Parents: 4b959b5 Author: Carlos Sierra <[email protected]> Authored: Tue Dec 12 18:16:38 2017 +0100 Committer: Carlos Sierra <[email protected]> Committed: Tue Dec 12 18:17:17 2017 +0100 ---------------------------------------------------------------------- jax-rs.itests/src/main/java/test/JaxrsTest.java | 55 +++++++++++++++++++- .../test/types/ConfigurationAwareResource.java | 39 ++++++++++++++ .../internal/CXFJaxRsServiceRegistrator.java | 36 ++++++++++--- 3 files changed, 123 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/e123aac7/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 763ac47..360d76c 100644 --- a/jax-rs.itests/src/main/java/test/JaxrsTest.java +++ b/jax-rs.itests/src/main/java/test/JaxrsTest.java @@ -40,7 +40,6 @@ import java.util.function.Function; import java.util.function.Predicate; import org.junit.After; -import org.junit.Ignore; import org.junit.Test; import org.osgi.framework.Bundle; import org.osgi.framework.PrototypeServiceFactory; @@ -52,6 +51,7 @@ import org.osgi.service.jaxrs.runtime.dto.DTOConstants; import org.osgi.service.jaxrs.runtime.dto.FailedApplicationDTO; import org.osgi.service.jaxrs.runtime.dto.RuntimeDTO; import org.osgi.util.tracker.ServiceTracker; +import test.types.ConfigurationAwareResource; import test.types.TestAddon; import test.types.TestAddonConflict; import test.types.TestAddonConflict2; @@ -1139,6 +1139,23 @@ public class JaxrsTest extends TestHelper { } @Test + public void testServiceReferencePropertiesAreAvailableInConfigurationInjection() { + registerApplication( + new Application() { + @Override + public Set<Object> getSingletons() { + return new HashSet<>(Collections.singleton( + new ConfigurationAwareResource() + )); + } + }, JAX_RS_NAME, "test", "property", "aValue"); + + WebTarget webTarget = createDefaultTarget().path("test-application"); + + assertEquals("aValue", webTarget.request().get(String.class)); + } + + @Test public void testServiceReferencePropertiesAreAvailableInFeatures() { AtomicBoolean executed = new AtomicBoolean(); AtomicReference<Object> propertyvalue = new AtomicReference<>(); @@ -1176,6 +1193,42 @@ public class JaxrsTest extends TestHelper { } @Test + public void testServiceReferencePropertiesAreAvailableInStaticFeatures() { + AtomicBoolean executed = new AtomicBoolean(); + AtomicReference<Object> propertyvalue = new AtomicReference<>(); + + registerApplication( + new Application() { + @Override + public Set<Object> getSingletons() { + return new HashSet<>(Arrays.asList( + (Feature)featureContext -> { + executed.set(true); + + Map<String, Object> properties = + (Map<String, Object>) + featureContext.getConfiguration().getProperty( + "osgi.jaxrs.application.serviceProperties"); + propertyvalue.set(properties.get("property")); + + return false; + }, + new Object() { + + @GET + public String hello() { + return "hello"; + } + } + )); + } + }, JAX_RS_NAME, "test", "property", true); + + assertTrue(executed.get()); + assertEquals(true, propertyvalue.get()); + } + + @Test public void testStandaloneEndPoint() throws InterruptedException { WebTarget webTarget = createDefaultTarget().path("test"); http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/e123aac7/jax-rs.itests/src/main/java/test/types/ConfigurationAwareResource.java ---------------------------------------------------------------------- diff --git a/jax-rs.itests/src/main/java/test/types/ConfigurationAwareResource.java b/jax-rs.itests/src/main/java/test/types/ConfigurationAwareResource.java new file mode 100644 index 0000000..8fbb83a --- /dev/null +++ b/jax-rs.itests/src/main/java/test/types/ConfigurationAwareResource.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.core.Configuration; +import javax.ws.rs.core.Context; +import java.util.Map; + +public class ConfigurationAwareResource { + + @GET + public String hello() { + Map<String, Object> properties = + (Map<String, Object>) + configuration.getProperty( + "osgi.jaxrs.application.serviceProperties"); + + return properties.get("property").toString(); + } + + @Context + Configuration configuration; +} http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/e123aac7/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 5789637..e09c01b 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 @@ -28,8 +28,10 @@ import java.util.TreeSet; import java.util.stream.Collectors; import java.util.stream.Stream; +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; @@ -39,10 +41,12 @@ import org.apache.cxf.Bus; import org.apache.cxf.endpoint.Server; import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; 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.OperationResourceInfo; -import org.apache.cxf.jaxrs.provider.json.JSONProvider; +import org.apache.cxf.jaxrs.provider.ServerConfigurableFactory; import org.apache.cxf.message.Message; import org.osgi.framework.ServiceReference; @@ -146,10 +150,10 @@ public class CXFJaxRsServiceRegistrator { } private final Application _application; - private Map<String, Object> _properties; private final Bus _bus; private final Collection<ServiceTuple<?>> _providers; private final Collection<ResourceProvider> _services = new ArrayList<>(); + private Map<String, Object> _properties; private volatile boolean _closed = false; private Server _server; @@ -215,12 +219,25 @@ public class CXFJaxRsServiceRegistrator { 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); + jaxRsServerFactoryBean.setProvider( (Feature) featureContext -> { - featureContext.property( - "osgi.jaxrs.application.serviceProperties", - _properties); - for (ServiceTuple<?> provider : _providers) { CachingServiceReference<?> cachingServiceReference = provider.getCachingServiceReference(); @@ -283,6 +300,13 @@ public class CXFJaxRsServiceRegistrator { _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(); }
