org.osgi.util.promise.Promise Java Examples

The following examples show how to use org.osgi.util.promise.Promise. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PromiseAwareJAXRSInvoker.java    From aries-jax-rs-whiteboard with Apache License 2.0 6 votes vote down vote up
/**
 * OSGi promises are a great way to do asynchronous work, and should be handled
 * natively just like a CompletionStage
 */
protected AsyncResponseImpl checkFutureResponse(Message inMessage, Object result) {
    if (inMessage == null || result == null) {
        return null;
    }

    // Fast path - do they share our view of the Promise
    if (result instanceof Promise) {
        return handlePromise(inMessage, (Promise<?>) result);
    } 
    
    // Slower check, is it a Promise?
    Class<?> clazz = result.getClass();
    if(Arrays.stream(clazz.getInterfaces())
        .map(Class::getName)
        .anyMatch(n -> "org.osgi.util.promise.Promise".equals(n))) {
        
        return handlePromiseFromAnotherClassSpace(inMessage, result, clazz);
    }
    
    return super.checkFutureResponse(inMessage, result);
}
 
Example #2
Source File: TestAsyncResource.java    From aries-jax-rs-whiteboard with Apache License 2.0 6 votes vote down vote up
@GET
@Path("promise/{name}")
@Produces(MediaType.TEXT_PLAIN)
public Promise<String> echoPromise(@PathParam("name") String value) {
    
    Deferred<String> d = new Deferred<>();
    
    new Thread(() -> {
        try {
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                preResume.run();
                d.fail(e);
                return;
            }
            preResume.run();
            d.resolve(value);
        } finally {
            postResume.run();
        }
    }).start();
    
    return d.getPromise();
}
 
Example #3
Source File: JaxrsTest.java    From aries-jax-rs-whiteboard with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsyncResourceClientWithPromises()
    throws ExecutionException, InterruptedException,
    InvocationTargetException {

    WebTarget webTarget =
        createDefaultTarget().
            path("whiteboard").
            path("async").
            path("promise").
            path("HelloAsync");

    AtomicBoolean pre = new AtomicBoolean();
    AtomicBoolean post = new AtomicBoolean();

    CountDownLatch countDownLatch = new CountDownLatch(1);

    registerAddon(
        new TestAsyncResource(
            () -> pre.set(true),
            () -> {
                post.set(true);

                countDownLatch.countDown();
            }));

    Promise<String> promise =
        webTarget.
            request().
            rx(PromiseRxInvoker.class).
            get(String.class);

    String result = promise.getValue();

    countDownLatch.await(1, TimeUnit.MINUTES);

    assertTrue(post.get());

    assertEquals("This should say HelloAsync", "HelloAsync", result);
}
 
Example #4
Source File: RepositoryImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Promise<Collection<Resource>> findProviders(RequirementExpression expression) {
  Deferred<Collection<Resource>> d = new Deferred<Collection<Resource>>();
  try {
    new ExpressionResolver(this, expression, d).start();
  } catch (Exception e) {
    d.fail(e);
  }
  return d.getPromise();
}
 
Example #5
Source File: ServiceComponentRuntimeImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  {@inheritDoc}
 */
public Promise<Void> enableComponent(ComponentDescriptionDTO description) {
  Component c = getComponent(description);
  Deferred<Void> d = new Deferred<Void>();
  if (c != null) {
    c.enable(d);
  } else {
    d.fail(new IllegalArgumentException("Unknown component"));
  }
  return d.getPromise();
}
 
Example #6
Source File: ServiceComponentRuntimeImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  {@inheritDoc}
 */
public Promise<Void> disableComponent(ComponentDescriptionDTO description) {
  Component c = getComponent(description);
  Deferred<Void> d = new Deferred<Void>();
  if (c != null) {
    c.disable(d);
  } else {
    d.fail(new IllegalArgumentException("Unknown component"));
  }
  return d.getPromise();
}
 
Example #7
Source File: PromiseRxInvokerImpl.java    From aries-jax-rs-whiteboard with Apache License 2.0 5 votes vote down vote up
@Override
public <R> Promise<R> method(
    String s, Entity<?> entity, GenericType<R> genericType) {

        Deferred<R> deferred = _promiseFactory.deferred();
    
    _webClient.doInvokeAsync(s, entity, null, null, genericType.getRawType(), genericType.getType(), 
            new DeferredHandler<R>(deferred));

    return deferred.getPromise();
}
 
Example #8
Source File: PromiseRxInvokerImpl.java    From aries-jax-rs-whiteboard with Apache License 2.0 5 votes vote down vote up
@Override
public <R> Promise<R> method(String s, GenericType<R> genericType) {
    Deferred<R> deferred = _promiseFactory.deferred();
    
    _webClient.doInvokeAsync(s, null, null, null, genericType.getRawType(), genericType.getType(), 
            new DeferredHandler<R>(deferred));

    return deferred.getPromise();
}
 
Example #9
Source File: RFIDSegmentImpl.java    From osgi.iot.contest.sdk with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized Promise<String> nextRFID() {
	return nextRFID.getPromise();
}
 
Example #10
Source File: PromiseRxInvokerImpl.java    From aries-jax-rs-whiteboard with Apache License 2.0 4 votes vote down vote up
@Override
public <R> Promise<R> trace(GenericType<R> genericType) {
    return method("TRACE", genericType);
}
 
Example #11
Source File: TrainLocationClient.java    From osgi.iot.contest.sdk with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized Promise<String> nextLocation() {
    return nextLocation.getPromise();
}
 
Example #12
Source File: EmulatorFactory.java    From osgi.iot.contest.sdk with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized Promise<String> nextRFID() {
	return nextRFID.getPromise();
}
 
Example #13
Source File: TrainControllerImpl.java    From osgi.iot.contest.sdk with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized Promise<String> nextLocation() {
    return nextLocation.getPromise();
}
 
Example #14
Source File: PromiseRxInvokerImpl.java    From aries-jax-rs-whiteboard with Apache License 2.0 4 votes vote down vote up
@Override
public <R> Promise<R> trace(Class<R> aClass) {
    return method("TRACE", aClass);
}
 
Example #15
Source File: MicroSwitchSegmentImpl.java    From osgi.iot.contest.sdk with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized Promise<String> nextRFID() {
	return nextRFID.getPromise();
}
 
Example #16
Source File: Closer.java    From osgi.enroute.examples with Apache License 2.0 4 votes vote down vote up
public Promise<T> onResolve(Runnable callback) {
	return deferred.getPromise().onResolve(callback);
}
 
Example #17
Source File: Closer.java    From osgi.enroute.examples with Apache License 2.0 4 votes vote down vote up
public <R> Promise<R> then(Success<? super T, ? extends R> success,
		Failure failure) {
	return deferred.getPromise().then(success, failure);
}
 
Example #18
Source File: Closer.java    From osgi.enroute.examples with Apache License 2.0 4 votes vote down vote up
public <R> Promise<R> then(Success<? super T, ? extends R> success) {
	return deferred.getPromise().then(success);
}
 
Example #19
Source File: Closer.java    From osgi.enroute.examples with Apache License 2.0 4 votes vote down vote up
public Promise<T> filter(Predicate<? super T> predicate) {
	return deferred.getPromise().filter(predicate);
}
 
Example #20
Source File: Closer.java    From osgi.enroute.examples with Apache License 2.0 4 votes vote down vote up
public <R> Promise<R> map(Function<? super T, ? extends R> mapper) {
	return deferred.getPromise().map(mapper);
}
 
Example #21
Source File: Closer.java    From osgi.enroute.examples with Apache License 2.0 4 votes vote down vote up
public <R> Promise<R> flatMap(
		Function<? super T, Promise<? extends R>> mapper) {
	return deferred.getPromise().flatMap(mapper);
}
 
Example #22
Source File: Closer.java    From osgi.enroute.examples with Apache License 2.0 4 votes vote down vote up
public Promise<T> recover(Function<Promise<?>, ? extends T> recovery) {
	return deferred.getPromise().recover(recovery);
}
 
Example #23
Source File: Closer.java    From osgi.enroute.examples with Apache License 2.0 4 votes vote down vote up
public Promise<T> recoverWith(
		Function<Promise<?>, Promise<? extends T>> recovery) {
	return deferred.getPromise().recoverWith(recovery);
}
 
Example #24
Source File: Closer.java    From osgi.enroute.examples with Apache License 2.0 4 votes vote down vote up
public Promise<T> fallbackTo(Promise<? extends T> fallback) {
	return deferred.getPromise().fallbackTo(fallback);
}
 
Example #25
Source File: PromiseRxInvokerImpl.java    From aries-jax-rs-whiteboard with Apache License 2.0 4 votes vote down vote up
@Override
public Promise<Response> method(String s) {
    return method(s, Response.class);
}
 
Example #26
Source File: PromiseAwareJAXRSInvoker.java    From aries-jax-rs-whiteboard with Apache License 2.0 4 votes vote down vote up
private AsyncResponseImpl handlePromise(Message inMessage, final Promise<?> promise) {
    final AsyncResponseImpl asyncResponse = new AsyncResponseImpl(inMessage);
    promise.onSuccess(asyncResponse::resume)
           .onFailure(asyncResponse::resume);
    return asyncResponse;
}
 
Example #27
Source File: PromiseRxInvokerImpl.java    From aries-jax-rs-whiteboard with Apache License 2.0 4 votes vote down vote up
@Override
public Promise<Response> delete() {
    return method(HttpMethod.DELETE);
}
 
Example #28
Source File: PromiseRxInvokerImpl.java    From aries-jax-rs-whiteboard with Apache License 2.0 4 votes vote down vote up
@Override
public <R> Promise<R> delete(Class<R> aClass) {
    return method(HttpMethod.DELETE, aClass);
}
 
Example #29
Source File: PromiseRxInvokerImpl.java    From aries-jax-rs-whiteboard with Apache License 2.0 4 votes vote down vote up
@Override
public <R> Promise<R> delete(GenericType<R> genericType) {
    return method(HttpMethod.DELETE, genericType);
}
 
Example #30
Source File: PromiseRxInvokerImpl.java    From aries-jax-rs-whiteboard with Apache License 2.0 4 votes vote down vote up
@Override
public Promise<Response> get() {
    return method(HttpMethod.GET);
}