]> git.argeo.org Git - lgpl/argeo-commons.git/blob - OsgiRegister.java
5728b90db2609faf8da1266b0ea62306e9e0e3a9
[lgpl/argeo-commons.git] / OsgiRegister.java
1 package org.argeo.osgi.util;
2
3 import java.util.ArrayList;
4 import java.util.Hashtable;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.concurrent.CompletableFuture;
8 import java.util.concurrent.Executor;
9 import java.util.concurrent.ForkJoinPool;
10
11 import org.osgi.framework.BundleContext;
12 import org.osgi.framework.ServiceRegistration;
13
14 public class OsgiRegister {
15 private final BundleContext bundleContext;
16 private Executor executor;
17
18 private CompletableFuture<Void> shutdownStarting = new CompletableFuture<Void>();
19
20 public OsgiRegister(BundleContext bundleContext) {
21 this.bundleContext = bundleContext;
22 // TODO experiment with dedicated executors
23 this.executor = ForkJoinPool.commonPool();
24 }
25
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) {
32 lst.add(c.getName());
33 }
34 ServiceRegistration<?> sr = bundleContext.registerService(lst.toArray(new String[lst.size()]), obj,
35 new Hashtable<String, Object>(attributes));
36 srf.complete(sr);
37 return obj;
38 }, executor);
39 // Singleton<T> singleton = new Singleton<T>(clss, postRegistration);
40
41 // shutdownStarting. //
42 // thenCompose(singleton::prepareUnregistration). //
43 // thenRunAsync(() -> {
44 // try {
45 // srf.get().unregister();
46 // } catch (InterruptedException | ExecutionException e) {
47 // e.printStackTrace();
48 // }
49 // }, executor);
50 // return singleton;
51 }
52
53 public void shutdown() {
54 shutdownStarting.complete(null);
55 }
56 }