X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.enterprise%2Fsrc%2Forg%2Fargeo%2Fosgi%2Futil%2FOnServiceRegistration.java;fp=org.argeo.enterprise%2Fsrc%2Forg%2Fargeo%2Fosgi%2Futil%2FOnServiceRegistration.java;h=0000000000000000000000000000000000000000;hb=9f729eeb8255a9d800ad2506735dda8cc215a135;hp=5a6760e0f64c24bf3ec2468f8e6204ba9e1d7bdd;hpb=f9efbe5228615951dd8482a4582aa24e00c10ce5;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.enterprise/src/org/argeo/osgi/util/OnServiceRegistration.java b/org.argeo.enterprise/src/org/argeo/osgi/util/OnServiceRegistration.java deleted file mode 100644 index 5a6760e0f..000000000 --- a/org.argeo.enterprise/src/org/argeo/osgi/util/OnServiceRegistration.java +++ /dev/null @@ -1,98 +0,0 @@ -package org.argeo.osgi.util; - -import java.util.concurrent.CancellationException; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; -import java.util.function.Function; - -import org.osgi.framework.BundleContext; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.ServiceReference; -import org.osgi.util.tracker.ServiceTracker; - -public class OnServiceRegistration implements Future { - private BundleContext ownBundleContext = FrameworkUtil.getBundle(OnServiceRegistration.class).getBundleContext(); - - private ServiceTracker st; - - private R result; - private boolean cancelled = false; - private Throwable exception; - - public OnServiceRegistration(Class clss, Function function) { - this(null, clss, function); - } - - public OnServiceRegistration(BundleContext bundleContext, Class clss, Function function) { - st = new ServiceTracker(bundleContext != null ? bundleContext : ownBundleContext, clss, null) { - - @Override - public T addingService(ServiceReference reference) { - T service = super.addingService(reference); - try { - if (result != null)// we only want the first one - return service; - result = function.apply(service); - return service; - } catch (Exception e) { - exception = e; - return service; - } finally { - close(); - } - } - }; - st.open(bundleContext == null); - } - - @Override - public boolean cancel(boolean mayInterruptIfRunning) { - if (result != null || exception != null || cancelled) - return false; - st.close(); - cancelled = true; - return true; - } - - @Override - public boolean isCancelled() { - return cancelled; - } - - @Override - public boolean isDone() { - return result != null || cancelled; - } - - @Override - public R get() throws InterruptedException, ExecutionException { - st.waitForService(0); - return tryGetResult(); - } - - @Override - public R get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { - st.waitForService(TimeUnit.MILLISECONDS.convert(timeout, unit)); - if (result == null) - throw new TimeoutException("No result after " + timeout + " " + unit); - return tryGetResult(); - } - - protected R tryGetResult() throws ExecutionException, CancellationException { - if (cancelled) - throw new CancellationException(); - if (exception != null) - throw new ExecutionException(exception); - if (result == null)// this should not happen - try { - throw new IllegalStateException("No result available"); - } catch (Exception e) { - exception = e; - throw new ExecutionException(e); - } - return result; - } - -}