use of org.osgi.util.promise.Failure in project aries by apache.
the class ChainTest method testThenFail.
@Test
public void testThenFail() throws Exception {
Deferred<String> def = new Deferred<String>();
final Promise<String> promise = def.getPromise();
Promise<String> chain = promise.then(null, new Failure() {
@Override
public void fail(Promise<?> resolved) throws Exception {
}
});
assertFalse("chain not resolved", chain.isDone());
Throwable failure = new Throwable("fail!");
def.fail(failure);
assertTrue("chain resolved", chain.isDone());
assertEquals("chain failure matches", failure, chain.getFailure());
}
use of org.osgi.util.promise.Failure in project bnd by bndtools.
the class IndexFile method sync.
private void sync() throws Exception {
List<Promise<Void>> sync = new ArrayList<>(promises.size());
for (Iterator<Entry<Archive, Promise<File>>> i = promises.entrySet().iterator(); i.hasNext(); ) {
Entry<Archive, Promise<File>> entry = i.next();
final Archive archive = entry.getKey();
Promise<File> promise = entry.getValue();
i.remove();
sync.add(promise.<Void>then(null, new Failure() {
@Override
public void fail(Promise<?> resolved) throws Exception {
reporter.exception(resolved.getFailure(), "Failed to sync %s", archive);
}
}));
}
// block until all promises resolved
Promises.all(sync).getFailure();
}
use of org.osgi.util.promise.Failure in project aries by apache.
the class MethodCall method fireAndForget.
public Promise<Void> fireAndForget(Bundle clientBundle, ExecutorService executor, ScheduledExecutorService ses) {
PromiseImpl<Void> started = new PromiseImpl<Void>(executor, ses);
Object svc;
try {
svc = getService();
} catch (Exception e) {
logError("Unable to obtain the service object", e);
started.fail(e);
return started;
}
if (svc instanceof AsyncDelegate) {
try {
if (((AsyncDelegate) svc).execute(method, arguments)) {
releaseService();
started.resolve(null);
return started;
}
} catch (Exception e) {
releaseService();
logError("The AsyncDelegate rejected the fire-and-forget invocation with an exception", e);
started.fail(e);
return started;
}
}
//If we get here then svc is either not an async delegate, or it rejected the call
PromiseImpl<Void> cleanup = new PromiseImpl<Void>();
try {
executor.execute(new FireAndForgetWork(this, cleanup, started));
cleanup.onResolve(new Runnable() {
public void run() {
releaseService();
}
}).then(null, new Failure() {
public void fail(Promise<?> resolved) throws Exception {
logError("The fire-and-forget invocation failed", resolved.getFailure());
}
});
} catch (RejectedExecutionException ree) {
logError("The Async Service threadpool rejected the fire-and-forget invocation", ree);
started.fail(new ServiceException("Unable to enqueue the fire-and forget task", 7, ree));
}
return started;
}
use of org.osgi.util.promise.Failure in project aries by apache.
the class PromiseImpl method recover.
@Override
public Promise<T> recover(final Function<Promise<?>, ? extends T> recovery) {
if (recovery == null)
throw new NullPointerException();
final PromiseImpl<T> result = new PromiseImpl<T>(exec, ses);
then(new Success<T, T>() {
@Override
public Promise<T> call(Promise<T> resolved) throws Exception {
result.resolve(resolved.getValue());
return null;
}
}, new Failure() {
@Override
public void fail(Promise<?> resolved) throws Exception {
try {
T recover = recovery.apply(resolved);
if (recover != null) {
result.resolve(recover);
} else {
result.fail(resolved.getFailure());
}
} catch (Throwable t) {
result.fail(t);
}
}
});
return result;
}
use of org.osgi.util.promise.Failure in project aries by apache.
the class PromiseImpl method resolveWith.
public Promise<Void> resolveWith(final Promise<? extends T> with) {
if (with == null)
throw new NullPointerException();
final PromiseImpl<Void> result = new PromiseImpl<Void>(exec, ses);
with.then(new Success<T, T>() {
@Override
public Promise<T> call(Promise<T> resolved) throws Exception {
if (isDone()) {
result.fail(new IllegalStateException("associated Promise already resolved"));
}
PromiseImpl.this.resolve(resolved.getValue());
result.resolve(null);
return null;
}
}, new Failure() {
@Override
public void fail(Promise<?> resolved) throws Exception {
if (isDone()) {
result.fail(new IllegalStateException("associated Promise already resolved"));
}
PromiseImpl.this.fail(resolved.getFailure());
result.resolve(null);
}
});
return result;
}
Aggregations