Support default whiteboard configuration
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/8aa660c8 Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/8aa660c8 Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/8aa660c8 Branch: refs/heads/master Commit: 8aa660c8552738e5d7097ab6f3dc3d6c0839ad69 Parents: 06e797a Author: Carlos Sierra <[email protected]> Authored: Mon Apr 30 16:50:52 2018 +0200 Committer: Carlos Sierra <[email protected]> Committed: Tue May 8 08:39:45 2018 +0200 ---------------------------------------------------------------------- jax-rs.itests/src/main/java/test/JaxrsTest.java | 42 +++++++ .../src/main/java/test/types/TestHelper.java | 23 ++++ .../activator/CxfJaxrsBundleActivator.java | 122 +++++++++++++------ 3 files changed, 149 insertions(+), 38 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/8aa660c8/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 752c923..35f7aa4 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 static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHIT import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT; import static org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants.*; +import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; import java.util.ArrayList; @@ -49,6 +50,8 @@ import org.apache.cxf.message.Message; import org.junit.Test; import org.osgi.framework.ServiceRegistration; +import org.osgi.service.cm.Configuration; +import org.osgi.service.cm.ConfigurationAdmin; import org.osgi.service.http.context.ServletContextHelper; import org.osgi.service.jaxrs.client.PromiseRxInvoker; import org.osgi.service.jaxrs.client.SseEventSourceFactory; @@ -1168,6 +1171,45 @@ public class JaxrsTest extends TestHelper { } @Test + public void testDefaultWeb() throws IOException, InterruptedException { + WebTarget defaultTarget = createDefaultTarget(); + + assertFalse( + defaultTarget.request().get(String.class). + contains("No services have been found")); + + ConfigurationAdmin configurationAdmin = getConfigurationAdmin(); + + Configuration configuration = configurationAdmin.getConfiguration( + "org.apache.aries.jax.rs.whiteboard.default", "?"); + + try { + Hashtable<String, Object> properties = new Hashtable<>(); + + properties.put( + "org.apache.aries.jax.rs.whiteboard.default.application", + "false"); + + configuration.update(properties); + + Thread.sleep(3000); + + assertTrue( + defaultTarget.request().get(String.class). + contains("No services have been found")); + } + finally { + configuration.delete(); + + Thread.sleep(3000); + } + + assertFalse( + defaultTarget.request().get(String.class). + contains("No services have been found")); + } + + @Test public void testEndpointsOverride() { WebTarget webTarget = createDefaultTarget().path("conflict"); http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/8aa660c8/jax-rs.itests/src/main/java/test/types/TestHelper.java ---------------------------------------------------------------------- diff --git a/jax-rs.itests/src/main/java/test/types/TestHelper.java b/jax-rs.itests/src/main/java/test/types/TestHelper.java index d5c8014..f452faf 100644 --- a/jax-rs.itests/src/main/java/test/types/TestHelper.java +++ b/jax-rs.itests/src/main/java/test/types/TestHelper.java @@ -32,6 +32,7 @@ import org.osgi.framework.PrototypeServiceFactory; import org.osgi.framework.ServiceFactory; import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; +import org.osgi.service.cm.ConfigurationAdmin; import org.osgi.service.jaxrs.client.SseEventSourceFactory; import org.osgi.service.jaxrs.runtime.JaxrsServiceRuntime; import org.osgi.service.jaxrs.runtime.dto.RuntimeDTO; @@ -90,6 +91,10 @@ public class TestHelper { } _clientBuilderTracker.close(); + + _configurationAdminTracker.close(); + + _sseEventSourceFactoryTracker.close(); } @Before @@ -99,6 +104,11 @@ public class TestHelper { _clientBuilderTracker.open(); + _configurationAdminTracker = new ServiceTracker<>( + bundleContext, ConfigurationAdmin.class, null); + + _configurationAdminTracker.open(); + _sseEventSourceFactoryTracker = new ServiceTracker<>( bundleContext, SseEventSourceFactory.class, null); @@ -119,6 +129,9 @@ public class TestHelper { _runtimeServiceReference = _runtimeTracker.getServiceReference(); } + private ServiceTracker<ConfigurationAdmin, ConfigurationAdmin> + _configurationAdminTracker; + private ServiceTracker<SseEventSourceFactory, SseEventSourceFactory> _sseEventSourceFactoryTracker; @@ -137,6 +150,16 @@ public class TestHelper { return new String[]{propertyValue.toString()}; } + public ConfigurationAdmin getConfigurationAdmin() { + + try { + return _configurationAdminTracker.waitForService(5000); + } + catch (InterruptedException e) { + throw new IllegalStateException(e); + } + } + protected Client createClient() { ClientBuilder clientBuilder; http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/8aa660c8/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 647b304..98d699d 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 @@ -28,6 +28,8 @@ import javax.ws.rs.client.WebTarget; import javax.ws.rs.ext.RuntimeDelegate; import javax.ws.rs.sse.SseEventSource; +import org.apache.aries.component.dsl.Transformer; +import org.apache.aries.component.dsl.internal.OnlyLastPublisher; import org.apache.aries.jax.rs.whiteboard.internal.client.ClientBuilderFactory; import org.apache.aries.jax.rs.whiteboard.internal.utils.PropertyHolder; import org.apache.aries.component.dsl.OSGi; @@ -37,12 +39,16 @@ import org.apache.cxf.jaxrs.sse.client.SseEventSourceBuilderImpl; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; +import org.osgi.service.cm.Configuration; +import org.osgi.service.cm.ConfigurationAdmin; import org.osgi.service.http.runtime.HttpServiceRuntime; import org.osgi.service.jaxrs.client.SseEventSourceFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import static java.lang.String.format; +import static org.apache.aries.component.dsl.OSGi.configuration; +import static org.apache.aries.component.dsl.OSGi.services; import static org.apache.aries.jax.rs.whiteboard.internal.utils.LogUtils.ifInfoEnabled; import static org.apache.aries.jax.rs.whiteboard.internal.utils.LogUtils.debugTracking; import static org.apache.aries.jax.rs.whiteboard.internal.utils.Utils.canonicalize; @@ -83,17 +89,15 @@ public class CxfJaxrsBundleActivator implements BundleActivator { _whiteboardsResult = whiteboards.run(bundleContext); - Dictionary<String, Object> defaultConfiguration = new Hashtable<>(); - - defaultConfiguration.put( - Constants.SERVICE_PID, - "org.apache.aries.jax.rs.whiteboard.default"); - _defaultOSGiResult = all( ignore(registerClient()), ignore(registerSseEventSourceFactory()), - ignore(runWhiteboard(bundleContext, defaultConfiguration)) + ignore( + defaultWhiteboardConfiguration().flatMap( + c -> runWhiteboard(bundleContext, c) + ) + ) ) .run(bundleContext); @@ -116,9 +120,51 @@ public class CxfJaxrsBundleActivator implements BundleActivator { _log.debug("Stopped whiteboard factory"); } } - private OSGiResult _defaultOSGiResult; private OSGiResult _whiteboardsResult; + + private static OSGi<Dictionary<String, ?>> + defaultWhiteboardConfiguration() { + + return once(services(ConfigurationAdmin.class)).flatMap(cm -> { + Configuration[] configurations; + + try { + configurations = cm.listConfigurations( + "(service.pid=org.apache.aries.jax.rs.whiteboard.default)"); + } + catch (Exception e) { + e.printStackTrace(); + + throw new RuntimeException(e); + } + + Dictionary<String, Object> properties; + + if (configurations == null || configurations.length == 0) { + properties = new Hashtable<>(); + + properties.put( + Constants.SERVICE_PID, + "org.apache.aries.jax.rs.whiteboard.default"); + } + else { + properties = configurations[0].getProperties(); + } + + Transformer<Dictionary<String, ?>, Dictionary<String, ?>> onlyLast = + op -> new OnlyLastPublisher<>(op, () -> properties); + + return + all( + just(properties), + configuration("org.apache.aries.jax.rs.whiteboard.default"). + filter(c -> !properties.equals(c)) + ). + transform(onlyLast); + }); + } + private static String endpointFilter(PropertyHolder configuration ) { Object whiteBoardTargetProperty = configuration.get( @@ -135,6 +181,36 @@ public class CxfJaxrsBundleActivator implements BundleActivator { targetFilter); } + private static OSGi<?> registerClient() { + return register( + ClientBuilder.class, new ClientBuilderFactory(), + (Map<String, Object>) null). + effects( + ifInfoEnabled(_log, () -> "Registered ClientBuilder"), + ifInfoEnabled(_log, () -> "Unregistered ClientBuilder") + ); + } + + private static OSGi<?> registerSseEventSourceFactory() { + return register( + SseEventSourceFactory.class, new SseEventSourceFactory() { + @Override + public SseEventSource.Builder newBuilder(WebTarget target) { + return new SseEventSourceBuilderImpl(){{target(target);}}; + } + + @Override + public SseEventSource newSource(WebTarget target) { + return newBuilder(target).build(); + } + }, + new Hashtable<>()). + effects( + ifInfoEnabled(_log, () -> "Registered SseEventSourceFactory"), + ifInfoEnabled(_log, () -> "Unregistered SseEventSourceFactory") + ); + } + private static OSGi<?> runWhiteboard( BundleContext bundleContext, Dictionary<String, ?> configuration) { @@ -182,34 +258,4 @@ public class CxfJaxrsBundleActivator implements BundleActivator { ); } - private static OSGi<?> registerClient() { - return register( - ClientBuilder.class, new ClientBuilderFactory(), - (Map<String, Object>) null). - effects( - ifInfoEnabled(_log, () -> "Registered ClientBuilder"), - ifInfoEnabled(_log, () -> "Unregistered ClientBuilder") - ); - } - - private static OSGi<?> registerSseEventSourceFactory() { - return register( - SseEventSourceFactory.class, new SseEventSourceFactory() { - @Override - public SseEventSource.Builder newBuilder(WebTarget target) { - return new SseEventSourceBuilderImpl(){{target(target);}}; - } - - @Override - public SseEventSource newSource(WebTarget target) { - return newBuilder(target).build(); - } - }, - new Hashtable<>()). - effects( - ifInfoEnabled(_log, () -> "Registered SseEventSourceFactory"), - ifInfoEnabled(_log, () -> "Unregistered SseEventSourceFactory") - ); - } - } \ No newline at end of file
