1 package org
.argeo
.osgi
.util
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Hashtable
;
7 import java
.util
.concurrent
.CompletableFuture
;
8 import java
.util
.concurrent
.Executor
;
9 import java
.util
.concurrent
.ForkJoinPool
;
11 import org
.osgi
.framework
.BundleContext
;
12 import org
.osgi
.framework
.ServiceRegistration
;
14 public class OsgiRegister
{
15 private final BundleContext bundleContext
;
16 private Executor executor
;
18 private CompletableFuture
<Void
> shutdownStarting
= new CompletableFuture
<Void
>();
20 public OsgiRegister(BundleContext bundleContext
) {
21 this.bundleContext
= bundleContext
;
22 // TODO experiment with dedicated executors
23 this.executor
= ForkJoinPool
.commonPool();
26 public <T
> void set(T obj
, Class
<T
> clss
, Map
<String
, Object
> attributes
, Class
<?
>... classes
) {
27 CompletableFuture
<ServiceRegistration
<?
>> srf
= new CompletableFuture
<ServiceRegistration
<?
>>();
28 CompletableFuture
<T
> postRegistration
= CompletableFuture
.supplyAsync(() -> {
29 List
<String
> lst
= new ArrayList
<>();
30 lst
.add(clss
.getName());
31 for (Class
<?
> c
: classes
) {
34 ServiceRegistration
<?
> sr
= bundleContext
.registerService(lst
.toArray(new String
[lst
.size()]), obj
,
35 new Hashtable
<String
, Object
>(attributes
));
39 // Singleton<T> singleton = new Singleton<T>(clss, postRegistration);
41 // shutdownStarting. //
42 // thenCompose(singleton::prepareUnregistration). //
43 // thenRunAsync(() -> {
45 // srf.get().unregister();
46 // } catch (InterruptedException | ExecutionException e) {
47 // e.printStackTrace();
53 public void shutdown() {
54 shutdownStarting
.complete(null);