aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From csie...@apache.org
Subject svn commit: r1808318 - /aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/ServiceReferenceOSGi.java
Date Thu, 14 Sep 2017 07:25:02 GMT
Author: csierra
Date: Thu Sep 14 07:25:02 2017
New Revision: 1808318

URL: http://svn.apache.org/viewvc?rev=1808318&view=rev
Log:
Refactor and get Modified to work

Modified:
    aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/ServiceReferenceOSGi.java

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=1808318&r1=1808317&r2=1808318&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
Thu Sep 14 07:25:02 2017
@@ -24,6 +24,7 @@ import org.osgi.framework.ServiceReferen
 import org.osgi.util.tracker.ServiceTracker;
 import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Consumer;
 import java.util.function.Function;
 
@@ -40,26 +41,18 @@ public class ServiceReferenceOSGi<T> ext
 			Pipe<Tuple<ServiceReference<T>>, Tuple<ServiceReference<T>>>
 				added = Pipe.create();
 
-			Consumer<Tuple<ServiceReference<T>>> addedSource =
-				added.getSource();
-
 			Pipe<Tuple<ServiceReference<T>>, Tuple<ServiceReference<T>>>
 				removed = Pipe.create();
 
-			Consumer<Tuple<ServiceReference<T>>> removedSource =
-				removed.getSource();
-
-			ServiceTracker<T, Tuple<ServiceReference<T>>> serviceTracker =
-				new ServiceTracker<>(
+			ServiceTracker<T, AtomicReference<Tuple<ServiceReference<T>>>>
+				serviceTracker = new ServiceTracker<>(
 					bundleContext,
 					buildFilter(bundleContext, filterString, clazz),
 					new DefaultServiceTrackerCustomizer<>(
-						addedSource, removedSource));
+						added.getSource(), removed.getSource()));
 
 			return new OSGiResultImpl<>(
-				added, removed, serviceTracker::open,
-				serviceTracker::close);
-
+				added, removed, serviceTracker::open, serviceTracker::close);
 		});
 
 		_filterString = filterString;
@@ -75,27 +68,22 @@ public class ServiceReferenceOSGi<T> ext
 
 			Pipe<Tuple<S>, Tuple<S>> removed = Pipe.create();
 
-			Consumer<Tuple<S>> addedSource = added.getSource();
-
-			Consumer<Tuple<S>> removedSource = removed.getSource();
-
-			ServiceTracker<T, OSGiResult<S>> serviceTracker =
+			ServiceTracker<T, ?> serviceTracker =
 				new ServiceTracker<>(
 					bundleContext,
 					buildFilter(
 						bundleContext, _filterString, _clazz),
 						new FlatMapServiceTrackerCustomizer<>(
-							fun, bundleContext, addedSource, removedSource));
+							fun, bundleContext, added.getSource(),
+							removed.getSource()));
 
 			return new OSGiResultImpl<>(
-				added, removed, serviceTracker::open,
-				serviceTracker::close);
-
+				added, removed, serviceTracker::open, serviceTracker::close);
 		});
 	}
 
 	private static class DefaultServiceTrackerCustomizer<T>
-		implements ServiceTrackerCustomizer<T, Tuple<ServiceReference<T>>> {
+		implements ServiceTrackerCustomizer<T, AtomicReference<Tuple<ServiceReference<T>>>>
{
 
 		private final Consumer<Tuple<ServiceReference<T>>> _addedSource;
 		private final Consumer<Tuple<ServiceReference<T>>> _removedSource;
@@ -109,32 +97,41 @@ public class ServiceReferenceOSGi<T> ext
 		}
 
 		@Override
-		public Tuple<ServiceReference<T>> addingService(
+		public AtomicReference<Tuple<ServiceReference<T>>> addingService(
 			ServiceReference<T> reference) {
 
 			Tuple<ServiceReference<T>> tuple = Tuple.create(reference);
 
 			_addedSource.accept(tuple);
 
-			return tuple;
+			return new AtomicReference<>(tuple);
 		}
 
 		@Override
 		public void modifiedService(
-			ServiceReference<T> reference, Tuple<ServiceReference<T>> service) {
+			ServiceReference<T> reference,
+			AtomicReference<Tuple<ServiceReference<T>>> tupleReference) {
+
+			_removedSource.accept(tupleReference.get());
+
+			Tuple<ServiceReference<T>> tuple = Tuple.create(reference);
+
+			tupleReference.set(tuple);
 
+			_addedSource.accept(tuple);
 		}
 
 		@Override
 		public void removedService(
-			ServiceReference<T> reference, Tuple<ServiceReference<T>> tuple) {
+			ServiceReference<T> reference,
+			AtomicReference<Tuple<ServiceReference<T>>> tuple) {
 
-			_removedSource.accept(tuple);
+			_removedSource.accept(tuple.get());
 		}
 	}
 
 	private static class FlatMapServiceTrackerCustomizer<T, S>
-		implements ServiceTrackerCustomizer<T, OSGiResult<S>> {
+		implements ServiceTrackerCustomizer<T, AtomicReference<OSGiResult<S>>>
{
 		private final Function<? super ServiceReference<T>, OSGi<? extends S>>
 			_fun;
 		private final BundleContext _bundleContext;
@@ -142,7 +139,7 @@ public class ServiceReferenceOSGi<T> ext
 
 		private final Consumer<Tuple<S>> _removedSource;
 
-		public FlatMapServiceTrackerCustomizer(
+		FlatMapServiceTrackerCustomizer(
 			Function<? super ServiceReference<T>, OSGi<? extends S>> fun,
 			BundleContext bundleContext, Consumer<Tuple<S>> addedSource,
 			Consumer<Tuple<S>> removedSource) {
@@ -154,8 +151,16 @@ public class ServiceReferenceOSGi<T> ext
 		}
 
 		@Override
-        public OSGiResult<S> addingService(ServiceReference<T> reference) {
-            OSGiImpl<S> program = (OSGiImpl<S>) _fun.apply(reference);
+        public AtomicReference<OSGiResult<S>> addingService(
+        	ServiceReference<T> reference) {
+
+			OSGiResultImpl<S> osgiResult = doFlatMap(reference);
+
+			return new AtomicReference<>(osgiResult);
+        }
+
+		private OSGiResultImpl<S> doFlatMap(ServiceReference<T> reference) {
+			OSGiImpl<S> program = (OSGiImpl<S>) _fun.apply(reference);
 
 			OSGiResultImpl<S> osgiResult = program._operation.run(
 				_bundleContext);
@@ -163,22 +168,24 @@ public class ServiceReferenceOSGi<T> ext
 			osgiResult.pipeTo(_addedSource, _removedSource);
 
 			return osgiResult;
-        }
+		}
 
 		@Override
         public void modifiedService(
-        	ServiceReference<T> reference, OSGiResult<S> tracked) {
+        	ServiceReference<T> reference,
+			AtomicReference<OSGiResult<S>> tracked) {
 
-            removedService(reference, tracked);
+			tracked.get().close();
 
-            addingService(reference);
+			tracked.set(doFlatMap(reference));
         }
 
 		@Override
         public void removedService(
-            ServiceReference<T> reference, OSGiResult<S> tracked) {
+            ServiceReference<T> reference,
+			AtomicReference<OSGiResult<S>> tracked) {
 
-            tracked.close();
+            tracked.get().close();
         }
 
 	}



Mime
View raw message