Author: csierra
Date: Wed Jul 19 10:48:53 2017
New Revision: 1802372

URL: http://svn.apache.org/viewvc?rev=1802372&view=rev
Log:
Suuport the varied ways of register services in OSGi

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/ServiceRegistrationOSGiImpl.java

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=1802372&r1=1802371&r2=1802372&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
 Wed Jul 19 10:48:53 2017
@@ -37,11 +37,11 @@ import org.apache.aries.osgi.functional.
 import org.apache.aries.osgi.functional.internal.ServicesOSGi;
 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.ArrayList;
 import java.util.Dictionary;
 import java.util.Map;
 import java.util.function.Consumer;
@@ -114,11 +114,22 @@ public interface OSGi<T> extends OSGiRun
                return new PrototypesOSGi<>(clazz, filterString);
        }
 
-       static <T, S extends T> OSGi<ServiceRegistration<T>> register(
-               Class<T> clazz, S service, Map<String, Object> properties) {
+       static <T> OSGi<ServiceRegistration<T>> register(
+               Class<T> clazz, T service, Map<String, Object> properties) {
 
-               return new ServiceRegistrationOSGiImpl<>(
-                       clazz, service, properties);
+               return new ServiceRegistrationOSGiImpl<>(clazz, service, 
properties);
+       }
+
+       static <T> OSGi<ServiceRegistration<T>> register(
+               Class<T> clazz, ServiceFactory<T> service, Map<String, Object> 
properties) {
+
+               return new ServiceRegistrationOSGiImpl<>(clazz, service, 
properties);
+       }
+
+       static OSGi<ServiceRegistration<?>> register(
+               String[] classes, Object service, Map<String, ?> properties) {
+
+               return new ServiceRegistrationOSGiImpl(classes, service, 
properties);
        }
 
        static <T> OSGi<T> services(Class<T> clazz) {

Modified: 
aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/ServiceRegistrationOSGiImpl.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/ServiceRegistrationOSGiImpl.java?rev=1802372&r1=1802371&r2=1802372&view=diff
==============================================================================
--- 
aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/ServiceRegistrationOSGiImpl.java
 (original)
+++ 
aries/trunk/component-dsl/component-dsl/src/main/java/org/apache/aries/osgi/functional/internal/ServiceRegistrationOSGiImpl.java
 Wed Jul 19 10:48:53 2017
@@ -17,6 +17,7 @@
 
 package org.apache.aries.osgi.functional.internal;
 
+import org.osgi.framework.ServiceFactory;
 import org.osgi.framework.ServiceRegistration;
 
 import java.util.Hashtable;
@@ -26,47 +27,89 @@ import java.util.function.Consumer;
 /**
  * @author Carlos Sierra Andrés
  */
-public class ServiceRegistrationOSGiImpl<T, S extends T>
+public class ServiceRegistrationOSGiImpl<T>
        extends OSGiImpl<ServiceRegistration<T>> {
 
        public ServiceRegistrationOSGiImpl(
-               Class<T> clazz, S service, Map<String, Object> properties) {
+               Class<T> clazz, T service, Map<String, Object> properties) {
 
                super(bundleContext -> {
                        ServiceRegistration<T> serviceRegistration =
                                bundleContext.registerService(
+                                       clazz, service, 
getProperties(properties));
+
+                       return 
getServiceRegistrationOSGiResult(serviceRegistration);
+               });
+       }
+
+       public ServiceRegistrationOSGiImpl(
+               Class<T> clazz, ServiceFactory<T> serviceFactory,
+               Map<String, Object> properties) {
+
+               super(bundleContext -> {
+                       ServiceRegistration<T> serviceRegistration =
+                               bundleContext.registerService(
+                                       clazz, serviceFactory, 
getProperties(properties));
+
+                       return 
getServiceRegistrationOSGiResult(serviceRegistration);
+               });
+       }
+
+       public ServiceRegistrationOSGiImpl(
+               String[] clazz, Object service, Map<String, ?> properties) {
+
+               super(bundleContext -> {
+                       ServiceRegistration<?> serviceRegistration =
+                               bundleContext.registerService(
                                        clazz, service, new 
Hashtable<>(properties));
 
-                       Pipe<Tuple
-                               <ServiceRegistration<T>>, 
Tuple<ServiceRegistration<T>>>
-                               added = Pipe.create();
-
-                       Consumer<Tuple<ServiceRegistration<T>>> addedSource =
-                               added.getSource();
-
-                       Tuple<ServiceRegistration<T>> tuple = Tuple.create(
-                               serviceRegistration);
-
-                       Pipe<Tuple<ServiceRegistration<T>>, 
Tuple<ServiceRegistration<T>>>
-                               removed = Pipe.create();
-
-                       Consumer<Tuple<ServiceRegistration<T>>> removedSource =
-                               removed.getSource();
-
-                       return new OSGiResultImpl<>(
-                               added, removed,
-                               () -> addedSource.accept(tuple),
-                               () -> {
-                                       try {
-                                               
serviceRegistration.unregister();
-                                       }
-                                       catch (Exception e) {
-                                       }
-                                       finally {
-                                               removedSource.accept(tuple);
-                                       }
-                               });
+                       return getServiceRegistrationOSGiResult(
+                               (ServiceRegistration)serviceRegistration);
                });
        }
 
+       private static Hashtable<String, Object> getProperties(
+               Map<String, Object> properties) {
+
+               if (properties == null) {
+                       return new Hashtable<>();
+               }
+
+               return new Hashtable<>(properties);
+       }
+
+       private static <T> OSGiResultImpl<ServiceRegistration<T>>
+               getServiceRegistrationOSGiResult(
+                       ServiceRegistration<T> serviceRegistration) {
+
+               Pipe<Tuple<ServiceRegistration<T>>, 
Tuple<ServiceRegistration<T>>>
+            added = Pipe.create();
+
+               Consumer<Tuple<ServiceRegistration<T>>> addedSource =
+            added.getSource();
+
+               Tuple<ServiceRegistration<T>> tuple = Tuple.create(
+            serviceRegistration);
+
+               Pipe<Tuple<ServiceRegistration<T>>, 
Tuple<ServiceRegistration<T>>>
+            removed = Pipe.create();
+
+               Consumer<Tuple<ServiceRegistration<T>>> removedSource =
+            removed.getSource();
+
+               return new OSGiResultImpl<>(
+            added, removed,
+            () -> addedSource.accept(tuple),
+            () -> {
+                try {
+                    serviceRegistration.unregister();
+                }
+                catch (Exception e) {
+                }
+                finally {
+                    removedSource.accept(tuple);
+                }
+            });
+       }
+
 }


Reply via email to