aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From csie...@apache.org
Subject aries-jax-rs-whiteboard git commit: First implementation of count change
Date Thu, 03 Aug 2017 16:50:31 GMT
Repository: aries-jax-rs-whiteboard
Updated Branches:
  refs/heads/master ee69fc90d -> 14019e2d5


First implementation of count change

To account for all the modification in each Runtime and update the
SERVICE_CHANGECOUNT property of the Runtime.


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/14019e2d
Tree: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/tree/14019e2d
Diff: http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/diff/14019e2d

Branch: refs/heads/master
Commit: 14019e2d5adb7a61785215e1487c2e404e1d21d1
Parents: ee69fc9
Author: Carlos Sierra <csierra@apache.org>
Authored: Thu Aug 3 18:44:13 2017 +0200
Committer: Carlos Sierra <csierra@apache.org>
Committed: Thu Aug 3 18:50:06 2017 +0200

----------------------------------------------------------------------
 .../main/java/test/WhiteboardFactoryTest.java   | 49 ++++++++++++
 .../jax/rs/whiteboard/internal/Whiteboard.java  | 80 ++++++++++++++++++--
 2 files changed, 121 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/14019e2d/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 810dcc2..b945eff 100644
--- a/jax-rs.itests/src/main/java/test/WhiteboardFactoryTest.java
+++ b/jax-rs.itests/src/main/java/test/WhiteboardFactoryTest.java
@@ -19,17 +19,26 @@ package test;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+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;
 
 import org.junit.Test;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
 import org.osgi.service.jaxrs.runtime.JaxRSServiceRuntime;
 import org.osgi.util.tracker.ServiceTracker;
+import test.types.TestApplication;
+
+import javax.ws.rs.core.Application;
 
 public class WhiteboardFactoryTest {
 
@@ -112,6 +121,46 @@ public class WhiteboardFactoryTest {
         }
     }
 
+    @Test
+    public void testChangeCount() throws Exception {
+        ServiceTracker<JaxRSServiceRuntime, JaxRSServiceRuntime> runtimeTracker =
+            new ServiceTracker<>(
+                bundleContext, JaxRSServiceRuntime.class, null);
+
+        try {
+            runtimeTracker.open();
+
+            JaxRSServiceRuntime runtime = runtimeTracker.waitForService(5000);
+
+            assertNotNull(runtime);
+
+            ServiceReference<JaxRSServiceRuntime> serviceReference = runtimeTracker.getServiceReference();
+
+            Long changeCount = (Long)serviceReference.getProperty("service.changecount");
+
+            Dictionary<String, Object> properties = new Hashtable<>();
+
+            properties.put(JAX_RS_APPLICATION_BASE, "/test-counter");
+
+            ServiceRegistration<?> serviceRegistration =
+                bundleContext.registerService(
+                    Application.class, new TestApplication(), properties);
+
+            assertTrue(
+                changeCount < (Long)runtimeTracker.getServiceReference().getProperty("service.changecount"));
+
+            changeCount = (Long)serviceReference.getProperty("service.changecount");
+
+            serviceRegistration.unregister();
+
+            assertTrue(
+                changeCount < (Long)runtimeTracker.getServiceReference().getProperty("service.changecount"));
+        }
+        finally {
+            runtimeTracker.close();
+        }
+    }
+
     private BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
 
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/aries-jax-rs-whiteboard/blob/14019e2d/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 0f71906..e498e54 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
@@ -25,6 +25,7 @@ import org.osgi.framework.Constants;
 import org.osgi.framework.Filter;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.framework.wiring.BundleWiring;
 import org.osgi.service.http.runtime.HttpServiceRuntime;
 import org.osgi.service.jaxrs.runtime.JaxRSServiceRuntime;
@@ -35,7 +36,9 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Dictionary;
 import java.util.HashMap;
+import java.util.Hashtable;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
 
 import static java.lang.String.format;
 import static org.apache.aries.jax.rs.whiteboard.internal.Utils.cxfRegistrator;
@@ -69,17 +72,20 @@ import static org.osgi.service.jaxrs.whiteboard.JaxRSWhiteboardConstants.JAX_RS_
  */
 public class Whiteboard {
     public static OSGi<Void> createWhiteboard(Dictionary<String, ?> configuration)
{
+        AtomicLong changeCount = new AtomicLong();
+
         return
             bundleContext().flatMap(bundleContext ->
             just(createBus(bundleContext, configuration)).flatMap(bus ->
             just(createDefaultJaxRsServiceRegistrator(bus)).flatMap(defaultServiceRegistrator
->
+            registerJaxRSServiceRuntime(bundleContext, bus, Maps.from(configuration)).flatMap(registratorRegistration
->
+            just(new ServiceRegistrationChangeCounter(changeCount, "service.changecount",
registratorRegistration)).flatMap(counter ->
                 all(
-                    registerJaxRSServiceRuntime(bundleContext, bus, Maps.from(configuration)),
-                    whiteboardApplications(bus),
-                    whiteBoardApplicationSingletons(),
-                    whiteboardExtensions(defaultServiceRegistrator),
-                    whiteboardSingletons(defaultServiceRegistrator)
-            ))));
+                    countChanges(whiteboardApplications(bus), counter),
+                    countChanges(whiteBoardApplicationSingletons(), counter),
+                    countChanges(whiteboardExtensions(defaultServiceRegistrator), counter),
+                    countChanges(whiteboardSingletons(defaultServiceRegistrator), counter)
+            ))))));
     }
 
     private static OSGi<Collection<String>> bestEffortCalculationOfEnpoints(Filter
filter) {
@@ -150,8 +156,9 @@ public class Whiteboard {
         return format("(%s=true)", JAX_RS_RESOURCE);
     }
 
-    private static OSGi<?> registerJaxRSServiceRuntime(
-        BundleContext bundleContext, Bus bus, Map<String, ?> configuration) {
+    private static OSGi<ServiceRegistration<?>>
+        registerJaxRSServiceRuntime(
+            BundleContext bundleContext, Bus bus, Map<String, ?> configuration) {
 
         Map<String, Object> properties = new HashMap<>(configuration);
 
@@ -257,4 +264,61 @@ public class Whiteboard {
             )
         );
     }
+
+    private static <T> OSGi<T> countChanges(
+        OSGi<T> program, ChangeCounter counter) {
+
+        return program.map(t -> {counter.inc(); return t;});
+    }
+
+    private static interface ChangeCounter {
+
+        public void inc();
+
+    }
+
+    private static class ServiceRegistrationChangeCounter
+        implements ChangeCounter{
+
+        private final AtomicLong _atomicLong;
+        private String _property;
+        private ServiceRegistration<?> _serviceRegistration;
+        private final Hashtable<String, Object> _properties;
+
+        public ServiceRegistrationChangeCounter(
+            AtomicLong atomicLong, String property,
+            ServiceRegistration<?> serviceRegistration) {
+
+            _atomicLong = atomicLong;
+            _property = property;
+            _serviceRegistration = serviceRegistration;
+
+            ServiceReference<?> serviceReference =
+                _serviceRegistration.getReference();
+
+            String[] propertyKeys = serviceReference.getPropertyKeys();
+
+            _properties = new Hashtable<>();
+
+            for (int i = 0; i < propertyKeys.length; i++) {
+                String propertyKey = propertyKeys[i];
+
+                _properties.put(
+                    propertyKey, serviceReference.getProperty(propertyKey));
+            }
+        }
+
+        @Override
+        public void inc() {
+            long l = _atomicLong.incrementAndGet();
+
+            Hashtable<String, Object> properties =
+                (Hashtable<String, Object>)_properties.clone();
+
+            properties.put(_property, l);
+
+            _serviceRegistration.setProperties(properties);
+        }
+    }
+
 }


Mime
View raw message