You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by cs...@apache.org on 2017/11/03 15:22:34 UTC

svn commit: r1814211 - in /aries/trunk/component-dsl: component-dsl/src/main/java/org/apache/aries/osgi/functional/ component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/ itests/src/main/java/org/apache/aries/osgi/functional/test/

Author: csierra
Date: Fri Nov  3 15:22:33 2017
New Revision: 1814211

URL: http://svn.apache.org/viewvc?rev=1814211&view=rev
Log:
[Component-DSL] Add CachingServiceReference

Added:
    aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/CachingServiceReference.java
    aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/CachingServiceReferenceTests.java
Modified:
    aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/OSGi.java
    aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/ServiceReferenceOSGi.java
    aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/ComponentTest.java
    aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/DSLTest.java
    aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/HighestRankingRouter.java

Added: aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/CachingServiceReference.java
URL: http://svn.apache.org/viewvc/aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/CachingServiceReference.java?rev=1814211&view=auto
==============================================================================
--- aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/CachingServiceReference.java (added)
+++ aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/CachingServiceReference.java Fri Nov  3 15:22:33 2017
@@ -0,0 +1,200 @@
+package org.apache.aries.osgi.functional;
+
+import org.osgi.framework.ServiceReference;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
+
+/**
+ * This class is an explicit wrapper around {@link ServiceReference} (it DOES NOT
+ * implement {@link ServiceReference}) and provides methods to access underlying
+ * {@link ServiceReference} properties and caches them so future access to the same
+ * properties return the same values.
+ *
+ * Property values are cached <i>on demand</i>. Values that have never been
+ * queried through the method are not cached.
+ *
+ * Properties that did not exist when queried will no longer exist even though
+ * they were available at a later time in the underlying {@link ServiceReference}.
+ *
+ * Method {@link CachingServiceReference#isDirty()} will return t
+ *
+ * @author Carlos Sierra Andrés
+ */
+public class CachingServiceReference<T>
+    implements Comparable<CachingServiceReference<T>> {
+
+    private final ConcurrentHashMap<String, Object> _properties;
+    private final ServiceReference<T> _serviceReference;
+
+    public CachingServiceReference(ServiceReference<T> serviceReference) {
+        _properties = new ConcurrentHashMap<>();
+        _serviceReference = serviceReference;
+    }
+
+    @Override
+    public int compareTo(CachingServiceReference<T> o) {
+        Object myServiceRankingObject = getProperty("service.ranking");
+        Object otherRankingObject = o.getProperty("service.ranking");
+
+        if (myServiceRankingObject == null ||
+            !(myServiceRankingObject instanceof Integer)) {
+                myServiceRankingObject = 0;
+        }
+        if (otherRankingObject == null ||
+            !(otherRankingObject instanceof Integer)) {
+                otherRankingObject = 0;
+        }
+        int compare = Integer.compare(
+            (Integer)myServiceRankingObject, (Integer)otherRankingObject);
+
+        if (compare != 0) {
+            return compare;
+        }
+        else {
+            return Long.compare(
+                (Long)o.getProperty("service.id"),
+                (Long)_serviceReference.getProperty("service.id")
+            );
+        }
+    }
+
+    /**
+     * Returns the value associated with a key from the underlying {@link ServiceReference}
+     * The returned value is then cached and the {@link ServiceReference} is
+     * never queried again for the same value.
+     *
+     * Values that are not present in the {@link ServiceReference} return null.
+     *
+     * Values that were present in the moment they were first queried will return
+     * the same value even if they disappear from they underlying {@link ServiceReference}.
+     *
+     * Values that were not present when queried the first time will continue to return
+     * null even though they exist in future queries.
+     * @param key the key of the property to be returned
+     * @return the value associated with that key
+     */
+    public Object getProperty(String key) {
+        Object propertyValue = _properties.compute(
+            key,
+            (__, value) -> {
+                if (value == null) {
+                    Object realValue = _serviceReference.getProperty(key);
+
+                    if (realValue == null) {
+                        return NULL.INSTANCE;
+                    }
+
+                    return realValue;
+                } else {
+                    return value;
+                }
+            }
+        );
+
+        if (propertyValue == NULL.INSTANCE) {
+            return null;
+        }
+
+        return propertyValue;
+    }
+
+    /**
+     * @return a union of the properties keys already cached by the instance
+     * and the property keys available in the underlying {@link ServiceReference}.
+     *
+     * Cached property keys that returned null are not returned here.
+     */
+    public String[] getPropertyKeys() {
+        Set<String> set = new HashSet<>();
+
+        set.addAll(Arrays.asList(_serviceReference.getPropertyKeys()));
+        set.addAll(_properties.keySet());
+
+        List<String> nullProperties = _properties.entrySet().stream().filter(
+            e -> e.getValue().equals(null)
+        ).map(
+            Map.Entry::getKey
+        ).collect(
+            Collectors.toList()
+        );
+
+        set.removeAll(nullProperties);
+
+        return set.toArray(new String[]{});
+    }
+
+    public String[] getCachedPropertyKeys() {
+        return _properties.keySet().toArray(new String[0]);
+    }
+
+    /**
+     * @return The underlying {@link ServiceReference}
+     */
+    public ServiceReference<T> getServiceReference() {
+        return _serviceReference;
+    }
+
+    @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;
+
+        CachingServiceReference<?> that = (CachingServiceReference<?>) o;
+
+        return _serviceReference.equals(that._serviceReference);
+    }
+
+    /**
+     * Checks if any of the cached properties has a different value in the
+     * underlying {@link ServiceReference}. Only properties that have been
+     * accessed through {@link CachingServiceReference#getProperty(String)} are
+     * checked, so this method can't be used to know whether the underlying
+     * {@link ServiceReference} han been altered since the creation of the
+     * instance.
+     *
+     * @return true if any of the cached properties has a different value than
+     * the ones held by the underlying {@link ServiceReference}
+     */
+    public boolean isDirty() {
+        return _properties.entrySet().stream().anyMatch(
+            e -> !e.getValue().equals(_serviceReference.getProperty(e.getKey()))
+        );
+    }
+
+    /**
+     * Checks if the property is dirty in this instance without caching the
+     * value. Trying to do the same using getProperty would cache the property
+     * which might not be desirable everytime.
+     * @param key
+     * @return
+     */
+    public boolean isDirty(String key) {
+        Object value = _properties.get(key);
+
+        return value != null &&
+            !value.equals(_serviceReference.getProperty(key));
+    }
+
+    private static class NULL {
+        private static NULL INSTANCE = new NULL();
+
+        @Override
+        public boolean equals(Object obj) {
+            return ((this == obj) || (obj == null));
+        }
+    }
+
+}

Modified: aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/OSGi.java
URL: http://svn.apache.org/viewvc/aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/OSGi.java?rev=1814211&r1=1814210&r2=1814211&view=diff
==============================================================================
--- aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/OSGi.java (original)
+++ aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/OSGi.java Fri Nov  3 15:22:33 2017
@@ -39,7 +39,6 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceFactory;
 import org.osgi.framework.ServiceObjects;
-import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 
 import java.util.Collection;
@@ -140,7 +139,10 @@ public interface OSGi<T> extends OSGiRun
 		bundleContext ->
 
 		serviceReferences(clazz, filterString).map(
-			bundleContext::getServiceObjects));
+			CachingServiceReference::getServiceReference
+		).map(
+			bundleContext::getServiceObjects)
+		);
 	}
 
 	static <T> OSGi<ServiceRegistration<T>> register(
@@ -174,14 +176,16 @@ public interface OSGi<T> extends OSGiRun
 			bundleContext().flatMap(
 			bundleContext ->
 
-			serviceReferences(clazz, filterString).flatMap(
-			sr -> {
-				T service = bundleContext.getService(sr);
-
-				return
-					onClose(() -> bundleContext.ungetService(sr)).then(
-					just(service)
-				);
+			serviceReferences(clazz, filterString).map(
+				CachingServiceReference::getServiceReference
+			).flatMap(
+				sr -> {
+					T service = bundleContext.getService(sr);
+
+					return
+						onClose(() -> bundleContext.ungetService(sr)).then(
+						just(service)
+					);
 			}
 		));
 	}
@@ -212,19 +216,19 @@ public interface OSGi<T> extends OSGiRun
 		});
 	}
 
-	static <T> OSGi<ServiceReference<T>> serviceReferences(
+	static <T> OSGi<CachingServiceReference<T>> serviceReferences(
 		Class<T> clazz) {
 
 		return new ServiceReferenceOSGi<>(null, clazz);
 	}
 
-	static OSGi<ServiceReference<Object>> serviceReferences(
+	static OSGi<CachingServiceReference<Object>> serviceReferences(
 		String filterString) {
 
 		return new ServiceReferenceOSGi<>(filterString, null);
 	}
 
-	static <T> OSGi<ServiceReference<T>> serviceReferences(
+	static <T> OSGi<CachingServiceReference<T>> serviceReferences(
 		Class<T> clazz, String filterString) {
 
 		return new ServiceReferenceOSGi<>(filterString, clazz);

Modified: aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/ServiceReferenceOSGi.java
URL: http://svn.apache.org/viewvc/aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/ServiceReferenceOSGi.java?rev=1814211&r1=1814210&r2=1814211&view=diff
==============================================================================
--- aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/ServiceReferenceOSGi.java (original)
+++ aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/ServiceReferenceOSGi.java Fri Nov  3 15:22:33 2017
@@ -17,6 +17,7 @@
 
 package org.apache.aries.osgi.functional.internal;
 
+import org.apache.aries.osgi.functional.CachingServiceReference;
 import org.apache.aries.osgi.functional.OSGi;
 import org.apache.aries.osgi.functional.OSGiResult;
 import org.osgi.framework.BundleContext;
@@ -25,13 +26,13 @@ import org.osgi.util.tracker.ServiceTrac
 import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
 import java.util.concurrent.atomic.AtomicReference;
-import java.util.function.Consumer;
 import java.util.function.Function;
 
 /**
  * @author Carlos Sierra Andrés
  */
-public class ServiceReferenceOSGi<T> extends OSGiImpl<ServiceReference<T>> {
+public class ServiceReferenceOSGi<T>
+	extends OSGiImpl<CachingServiceReference<T>> {
 
 	private String _filterString;
 	private Class<T> _clazz;
@@ -54,7 +55,7 @@ public class ServiceReferenceOSGi<T> ext
 
 	@Override
 	public <S> OSGiImpl<S> flatMap(
-		Function<? super ServiceReference<T>, OSGi<? extends S>> fun) {
+		Function<? super CachingServiceReference<T>, OSGi<? extends S>> fun) {
 
 		return new OSGiImpl<>((bundleContext, op) -> {
 			ServiceTracker<T, ?> serviceTracker =
@@ -73,10 +74,11 @@ public class ServiceReferenceOSGi<T> ext
 	private static class DefaultServiceTrackerCustomizer<T>
 		implements ServiceTrackerCustomizer<T, AtomicReference<Runnable>> {
 
-		private final Function<ServiceReference<T>, Runnable> _addedSource;
+		private final Function<CachingServiceReference<T>, Runnable>
+			_addedSource;
 
 		public DefaultServiceTrackerCustomizer(
-			Function<ServiceReference<T>, Runnable> addedSource) {
+			Function<CachingServiceReference<T>, Runnable> addedSource) {
 
 			_addedSource = addedSource;
 		}
@@ -85,7 +87,8 @@ public class ServiceReferenceOSGi<T> ext
 		public AtomicReference<Runnable> addingService(
 			ServiceReference<T> reference) {
 
-			return new AtomicReference<>(_addedSource.apply(reference));
+			return new AtomicReference<>(
+				_addedSource.apply(new CachingServiceReference<>(reference)));
 		}
 
 		@Override
@@ -95,7 +98,8 @@ public class ServiceReferenceOSGi<T> ext
 
 			tupleReference.get().run();
 
-			tupleReference.set(_addedSource.apply(reference));
+			tupleReference.set(
+				_addedSource.apply(new CachingServiceReference<>(reference)));
 		}
 
 		@Override
@@ -109,13 +113,13 @@ public class ServiceReferenceOSGi<T> ext
 
 	private static class FlatMapServiceTrackerCustomizer<T, S>
 		implements ServiceTrackerCustomizer<T, AtomicReference<OSGiResult>> {
-		private final Function<? super ServiceReference<T>, OSGi<? extends S>>
+		private final Function<? super CachingServiceReference<T>, OSGi<? extends S>>
 			_fun;
 		private final BundleContext _bundleContext;
 		private final Function<S, Runnable> _op;
 
 		FlatMapServiceTrackerCustomizer(
-			Function<? super ServiceReference<T>, OSGi<? extends S>> fun,
+			Function<? super CachingServiceReference<T>, OSGi<? extends S>> fun,
 			BundleContext bundleContext, Function<S, Runnable> op) {
 
 			_fun = fun;
@@ -127,12 +131,13 @@ public class ServiceReferenceOSGi<T> ext
         public AtomicReference<OSGiResult> addingService(
         	ServiceReference<T> reference) {
 
-			OSGiResultImpl osgiResult = doFlatMap(reference);
+			OSGiResultImpl osgiResult = doFlatMap(
+				new CachingServiceReference<>(reference));
 
 			return new AtomicReference<>(osgiResult);
         }
 
-		private OSGiResultImpl doFlatMap(ServiceReference<T> reference) {
+		private OSGiResultImpl doFlatMap(CachingServiceReference<T> reference) {
 			OSGiImpl<S> program = (OSGiImpl<S>) _fun.apply(reference);
 
 			OSGiResultImpl result = program._operation.run(_bundleContext, _op);
@@ -149,7 +154,7 @@ public class ServiceReferenceOSGi<T> ext
 
 			tracked.get().close();
 
-			tracked.set(doFlatMap(reference));
+			tracked.set(doFlatMap(new CachingServiceReference<>(reference)));
         }
 
 		@Override

Added: aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/CachingServiceReferenceTests.java
URL: http://svn.apache.org/viewvc/aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/CachingServiceReferenceTests.java?rev=1814211&view=auto
==============================================================================
--- aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/CachingServiceReferenceTests.java (added)
+++ aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/CachingServiceReferenceTests.java Fri Nov  3 15:22:33 2017
@@ -0,0 +1,421 @@
+/*
+ * 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.osgi.functional.test;
+
+import org.apache.aries.osgi.functional.CachingServiceReference;
+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 java.util.Arrays;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.TreeSet;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * @author Carlos Sierra Andrés
+ */
+public class CachingServiceReferenceTests {
+    private static BundleContext bundleContext = FrameworkUtil.getBundle(
+        CachingServiceReferenceTests.class).getBundleContext();
+
+    @Test
+    public void testComparable() {
+        ServiceRegistration<Service> serviceRegistrationNoRanking =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                }});
+
+        ServiceRegistration<Service> serviceRegistrationOne =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                    put("service.ranking", 1);
+                }});
+
+        ServiceRegistration<Service> serviceRegistrationMinusOne =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                    put("service.ranking", -1);
+                }});
+
+        try {
+            TreeSet<CachingServiceReference<?>> set = new TreeSet<>();
+
+            CachingServiceReference<Service> noRanking =
+                new CachingServiceReference<>(
+                    serviceRegistrationNoRanking.getReference());
+
+            set.add(noRanking);
+
+            CachingServiceReference<Service> rankingOne =
+                new CachingServiceReference<>(
+                    serviceRegistrationOne.getReference());
+
+            set.add(rankingOne);
+
+            CachingServiceReference<Service> minusOne =
+                new CachingServiceReference<>(
+                    serviceRegistrationMinusOne.getReference());
+
+            set.add(minusOne);
+
+            CachingServiceReference<?> first = set.first();
+
+            assertTrue(first.equals(minusOne));
+            CachingServiceReference<?> second = set.higher(first);
+            assertTrue(second.equals(noRanking));
+            CachingServiceReference<?> third = set.higher(second);
+            assertTrue(third.equals(rankingOne));
+        }
+        finally {
+            serviceRegistrationMinusOne.unregister();
+            serviceRegistrationNoRanking.unregister();
+            serviceRegistrationOne.unregister();
+        }
+    }
+
+    @Test
+    public void testCachingNullProperties() {
+        ServiceRegistration<Service> serviceRegistration =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                }});
+
+        try {
+            ServiceReference<Service> serviceReference =
+                serviceRegistration.getReference();
+
+            CachingServiceReference<Service> cachingServiceReference =
+                new CachingServiceReference<>(serviceReference);
+
+            assertEquals(
+                serviceReference.getProperty("property"),
+                cachingServiceReference.getProperty("property"));
+
+            serviceRegistration.setProperties(
+                new Hashtable<String, Object>() {{
+                    put("property", "value2");
+                }}
+            );
+
+            assertEquals(serviceReference.getProperty("property"), "value2");
+
+            assertNull(cachingServiceReference.getProperty("property"));
+
+            assertTrue(cachingServiceReference.isDirty());
+        }
+        finally {
+            serviceRegistration.unregister();
+        }
+    }
+
+    @Test
+    public void testNullPropertiesKeysAreNotReturned() {
+        ServiceRegistration<Service> serviceRegistration =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                }});
+
+        try {
+            ServiceReference<Service> serviceReference =
+                serviceRegistration.getReference();
+
+            CachingServiceReference<Service> cachingServiceReference =
+                new CachingServiceReference<>(serviceReference);
+
+            assertNull(serviceReference.getProperty("property"));
+
+            assertEquals(
+                serviceReference.getProperty("property"),
+                cachingServiceReference.getProperty("property"));
+
+            List<String> properties = Arrays.asList(
+                cachingServiceReference.getPropertyKeys());
+
+            assertFalse(properties.contains("property"));
+
+            serviceRegistration.setProperties(
+                new Hashtable<String, Object>() {{
+                    put("property", "value2");
+                }}
+            );
+
+            assertEquals(serviceReference.getProperty("property"), "value2");
+
+            properties = Arrays.asList(cachingServiceReference.getPropertyKeys());
+
+            assertFalse(properties.contains("property"));
+        }
+        finally {
+            serviceRegistration.unregister();
+        }
+    }
+
+    @Test
+    public void testCachingProperties() {
+        ServiceRegistration<Service> serviceRegistration =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                    put("property", "value");
+                }});
+
+        try {
+            ServiceReference<Service> serviceReference =
+                serviceRegistration.getReference();
+
+            CachingServiceReference<Service> cachingServiceReference =
+                new CachingServiceReference<>(serviceReference);
+
+            assertEquals(
+                serviceReference.getProperty("property"),
+                cachingServiceReference.getProperty("property"));
+
+            serviceRegistration.setProperties(
+                new Hashtable<String, Object>() {{
+                    put("property", "value2");
+                }}
+            );
+
+            assertEquals(serviceReference.getProperty("property"), "value2");
+
+            assertEquals(cachingServiceReference.getProperty("property"), "value");
+        }
+        finally {
+            serviceRegistration.unregister();
+        }
+
+    }
+
+    @Test
+    public void testCachingPropertiesBecomingNull() {
+        ServiceRegistration<Service> serviceRegistration =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                    put("property", "value");
+                }});
+
+        try {
+            ServiceReference<Service> serviceReference =
+                serviceRegistration.getReference();
+
+            String[] propertyKeys = serviceReference.getPropertyKeys();
+
+            Arrays.sort(propertyKeys);
+
+            CachingServiceReference<Service> cachingServiceReference =
+                new CachingServiceReference<>(serviceReference);
+
+            assertEquals(
+                serviceReference.getProperty("property"),
+                cachingServiceReference.getProperty("property"));
+
+            serviceRegistration.setProperties(
+                new Hashtable<String, Object>() {{
+                }}
+            );
+
+            assertNull(serviceReference.getProperty("property"));
+
+            assertEquals("value", cachingServiceReference.getProperty("property"));
+
+            assertTrue(cachingServiceReference.isDirty());
+
+            String[] cachedPropertyKeys = cachingServiceReference.getPropertyKeys();
+
+            Arrays.sort(cachedPropertyKeys);
+
+            assertTrue(Arrays.equals(propertyKeys, cachedPropertyKeys));
+        }
+        finally {
+            serviceRegistration.unregister();
+        }
+    }
+
+    @Test
+    public void testIsDirty() {
+        ServiceRegistration<Service> serviceRegistration =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                    put("property", "value");
+                }});
+
+        try {
+            ServiceReference<Service> serviceReference =
+                serviceRegistration.getReference();
+
+            CachingServiceReference<Service> cachingServiceReference =
+                new CachingServiceReference<>(serviceReference);
+
+            assertEquals(
+                serviceReference.getProperty("property"),
+                cachingServiceReference.getProperty("property"));
+
+            serviceRegistration.setProperties(
+                new Hashtable<String, Object>() {{
+                    put("property", "value2");
+                }}
+            );
+
+            assertTrue(cachingServiceReference.isDirty());
+        }
+        finally {
+            serviceRegistration.unregister();
+        }
+    }
+
+    @Test
+    public void testIsNotDirty() {
+        ServiceRegistration<Service> serviceRegistration =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                    put("property", "value");
+                }});
+        try {
+            ServiceReference<Service> serviceReference =
+                serviceRegistration.getReference();
+
+            CachingServiceReference<Service> cachingServiceReference =
+                new CachingServiceReference<>(serviceReference);
+
+            serviceRegistration.setProperties(
+                new Hashtable<String, Object>() {{
+                    put("property", "value2");
+                }}
+            );
+
+            assertFalse(cachingServiceReference.isDirty());
+        }
+        finally {
+            serviceRegistration.unregister();
+        }
+    }
+
+    @Test
+    public void testIsDirtyDoesNotCache() {
+        ServiceRegistration<Service> serviceRegistration =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                    put("property", "value");
+                }});
+        try {
+            ServiceReference<Service> serviceReference =
+                serviceRegistration.getReference();
+
+            CachingServiceReference<Service> cachingServiceReference =
+                new CachingServiceReference<>(serviceReference);
+
+            serviceRegistration.setProperties(
+                new Hashtable<String, Object>() {{
+                    put("property", "value2");
+                }}
+            );
+
+            assertFalse(cachingServiceReference.isDirty("property"));
+
+            assertEquals(
+                "value2", cachingServiceReference.getProperty("property"));
+
+            assertFalse(cachingServiceReference.isDirty("property"));
+
+            serviceRegistration.setProperties(
+                new Hashtable<String, Object>() {{
+                    put("property", "value3");
+                }}
+            );
+
+            assertTrue(cachingServiceReference.isDirty("property"));
+        }
+        finally {
+            serviceRegistration.unregister();
+        }
+    }
+
+    @Test
+    public void testIsDirtyNullProperty() {
+        ServiceRegistration<Service> serviceRegistration =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                }});
+        try {
+            ServiceReference<Service> serviceReference =
+                serviceRegistration.getReference();
+
+            CachingServiceReference<Service> cachingServiceReference =
+                new CachingServiceReference<>(serviceReference);
+
+            assertFalse(cachingServiceReference.isDirty("nonExisting"));
+        }
+        finally {
+            serviceRegistration.unregister();
+        }
+    }
+
+
+    @Test
+    public void testPropertiesAreCachedWhenQueried() {
+        ServiceRegistration<Service> serviceRegistration =
+            bundleContext.registerService(
+                Service.class, new Service(),
+                new Hashtable<String, Object>() {{
+                    put("property", "value");
+                }});
+        try {
+            ServiceReference<Service> serviceReference =
+                serviceRegistration.getReference();
+
+            CachingServiceReference<Service> cachingServiceReference =
+                new CachingServiceReference<>(serviceReference);
+
+            serviceRegistration.setProperties(
+                new Hashtable<String, Object>() {{
+                    put("property", "value2");
+                }}
+            );
+
+            assertEquals(
+                serviceReference.getProperty("property"),
+                cachingServiceReference.getProperty("property"));
+        }
+        finally {
+            serviceRegistration.unregister();
+        }
+    }
+
+
+
+    private class Service {}
+
+}

Modified: aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/ComponentTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/ComponentTest.java?rev=1814211&r1=1814210&r2=1814211&view=diff
==============================================================================
--- aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/ComponentTest.java (original)
+++ aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/ComponentTest.java Fri Nov  3 15:22:33 2017
@@ -17,6 +17,7 @@
 
 package org.apache.aries.osgi.functional.test;
 
+import org.apache.aries.osgi.functional.CachingServiceReference;
 import org.apache.aries.osgi.functional.OSGi;
 import org.apache.aries.osgi.functional.OSGiResult;
 import org.junit.AfterClass;
@@ -336,7 +337,7 @@ public class ComponentTest {
 
 
     private static <T> OSGi<T> highestService(Class<T> clazz) {
-        return highest(clazz).flatMap(sr ->
+        return highest(clazz).map(CachingServiceReference::getServiceReference).flatMap(sr ->
         bundleContext().flatMap(bc ->
         onClose(() -> bc.ungetService(sr)).then(
         just(bc.getService(sr))

Modified: aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/DSLTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/DSLTest.java?rev=1814211&r1=1814210&r2=1814211&view=diff
==============================================================================
--- aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/DSLTest.java (original)
+++ aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/DSLTest.java Fri Nov  3 15:22:33 2017
@@ -17,12 +17,11 @@
 
 package org.apache.aries.osgi.functional.test;
 
+import org.apache.aries.osgi.functional.CachingServiceReference;
 import org.apache.aries.osgi.functional.OSGi;
 import org.apache.aries.osgi.functional.OSGiResult;
 import org.apache.aries.osgi.functional.SentEvent;
 import org.apache.aries.osgi.functional.internal.ProbeImpl;
-import org.junit.Assert;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
@@ -125,7 +124,7 @@ public class DSLTest {
 
     @Test
     public void testServiceReferences() {
-        AtomicReference<ServiceReference<Service>> atomicReference =
+        AtomicReference<CachingServiceReference<Service>> atomicReference =
             new AtomicReference<>();
 
         ServiceRegistration<Service> serviceRegistration = null;
@@ -141,7 +140,8 @@ public class DSLTest {
                 Service.class, new Service(), new Hashtable<>());
 
             assertEquals(
-                serviceRegistration.getReference(), atomicReference.get());
+                serviceRegistration.getReference(),
+                atomicReference.get().getServiceReference());
         }
         finally {
             if (serviceRegistration != null) {
@@ -152,10 +152,10 @@ public class DSLTest {
 
     @Test
     public void testServiceReferencesAndClose() {
-        AtomicReference<ServiceReference<Service>> atomicReference =
+        AtomicReference<CachingServiceReference<Service>> atomicReference =
             new AtomicReference<>();
 
-        OSGi<ServiceReference<Service>> program =
+        OSGi<CachingServiceReference<Service>> program =
             serviceReferences(Service.class).flatMap(ref ->
             onClose(() -> atomicReference.set(null)).
             then(just(ref))
@@ -173,7 +173,8 @@ public class DSLTest {
                 Service.class, new Service(), new Hashtable<>());
 
             assertEquals(
-                serviceRegistration.getReference(), atomicReference.get());
+                serviceRegistration.getReference(),
+                atomicReference.get().getServiceReference());
         }
         finally {
             if (serviceRegistration != null) {
@@ -505,7 +506,7 @@ public class DSLTest {
 
     @Test
     public void testHighestRankingOnly() {
-        AtomicReference<ServiceReference<Service>> current =
+        AtomicReference<CachingServiceReference<Service>> current =
             new AtomicReference<>();
 
         OSGi<Void> program =
@@ -522,7 +523,9 @@ public class DSLTest {
                         put("service.ranking", 0);
                     }});
 
-            assertEquals(serviceRegistrationOne.getReference(), current.get());
+            assertEquals(
+                serviceRegistrationOne.getReference(),
+                current.get().getServiceReference());
 
             ServiceRegistration<Service> serviceRegistrationTwo =
                 bundleContext.registerService(
@@ -531,7 +534,9 @@ public class DSLTest {
                         put("service.ranking", 1);
                     }});
 
-            assertEquals(serviceRegistrationTwo.getReference(), current.get());
+            assertEquals(
+                serviceRegistrationTwo.getReference(),
+                current.get().getServiceReference());
 
             ServiceRegistration<Service> serviceRegistrationMinusOne =
                 bundleContext.registerService(
@@ -540,16 +545,21 @@ public class DSLTest {
                         put("service.ranking", -1);
                     }});
 
-            assertEquals(serviceRegistrationTwo.getReference(), current.get());
+            assertEquals(
+                serviceRegistrationTwo.getReference(),
+                current.get().getServiceReference());
 
             serviceRegistrationTwo.unregister();
 
-            assertEquals(serviceRegistrationOne.getReference(), current.get());
+            assertEquals(
+                serviceRegistrationOne.getReference(),
+                current.get().getServiceReference());
 
             serviceRegistrationOne.unregister();
 
             assertEquals(
-                serviceRegistrationMinusOne.getReference(), current.get());
+                serviceRegistrationMinusOne.getReference(),
+                current.get().getServiceReference());
 
             serviceRegistrationOne =
                 bundleContext.registerService(
@@ -558,7 +568,9 @@ public class DSLTest {
                         put("service.ranking", 0);
                     }});
 
-            assertEquals(serviceRegistrationOne.getReference(), current.get());
+            assertEquals(
+                serviceRegistrationOne.getReference(),
+                current.get().getServiceReference());
 
             serviceRegistrationMinusOne.unregister();
         }

Modified: aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/HighestRankingRouter.java
URL: http://svn.apache.org/viewvc/aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/HighestRankingRouter.java?rev=1814211&r1=1814210&r2=1814211&view=diff
==============================================================================
--- aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/HighestRankingRouter.java (original)
+++ aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/HighestRankingRouter.java Fri Nov  3 15:22:33 2017
@@ -17,9 +17,9 @@
 package org.apache.aries.osgi.functional.test;
 
 import org.apache.aries.osgi.functional.Event;
+import org.apache.aries.osgi.functional.CachingServiceReference;
 import org.apache.aries.osgi.functional.OSGi;
 import org.apache.aries.osgi.functional.SentEvent;
-import org.osgi.framework.ServiceReference;
 
 import java.util.Comparator;
 import java.util.PriorityQueue;
@@ -36,7 +36,7 @@ public class HighestRankingRouter<T exte
     private PriorityQueue<Event<T>> _instances;
     private SentEvent sent;
 
-    public static <T> OSGi<ServiceReference<T>> highest(Class<T> clazz) {
+    public static <T> OSGi<CachingServiceReference<T>> highest(Class<T> clazz) {
         return serviceReferences(clazz).route(new HighestRankingRouter<>());
     }