Search in sources :

Example 1 with Promise

use of ratpack.exec.Promise in project ratpack by ratpack.

the class Pac4jAuthenticator method createClients.

private Promise<Clients> createClients(Context ctx, PathBinding pathBinding) throws Exception {
    String boundTo = pathBinding.getBoundTo();
    PublicAddress publicAddress = ctx.get(PublicAddress.class);
    String absoluteCallbackUrl = publicAddress.get(b -> b.maybeEncodedPath(boundTo).maybeEncodedPath(path)).toASCIIString();
    Iterable<? extends Client<?, ?>> result = clientsProvider.get(ctx);
    @SuppressWarnings("rawtypes") List<Client> clients;
    if (result instanceof List) {
        clients = Types.cast(result);
    } else {
        clients = ImmutableList.copyOf(result);
    }
    return Promise.value(new Clients(absoluteCallbackUrl, clients));
}
Also used : Types(ratpack.util.Types) Context(ratpack.handling.Context) RatpackPac4j(ratpack.pac4j.RatpackPac4j) Exceptions.uncheck(ratpack.util.Exceptions.uncheck) Promise(ratpack.exec.Promise) PublicAddress(ratpack.server.PublicAddress) Blocking(ratpack.exec.Blocking) RequiresHttpAction(org.pac4j.core.exception.RequiresHttpAction) WebContext(org.pac4j.core.context.WebContext) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Clients(org.pac4j.core.client.Clients) Client(org.pac4j.core.client.Client) Handler(ratpack.handling.Handler) Registry(ratpack.registry.Registry) Optional(java.util.Optional) PathBinding(ratpack.path.PathBinding) TechnicalException(org.pac4j.core.exception.TechnicalException) UserProfile(org.pac4j.core.profile.UserProfile) SessionData(ratpack.session.SessionData) Credentials(org.pac4j.core.credentials.Credentials) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) PublicAddress(ratpack.server.PublicAddress) Client(org.pac4j.core.client.Client) Clients(org.pac4j.core.client.Clients)

Example 2 with Promise

use of ratpack.exec.Promise in project ratpack by ratpack.

the class DefaultParallelBatch method publisher.

@Override
public TransformablePublisher<T> publisher() {
    Iterator<? extends Promise<T>> iterator = promises.iterator();
    return new BufferingPublisher<>(Action.noop(), write -> {
        return new Subscription() {

            volatile boolean cancelled;

            volatile boolean complete;

            final AtomicLong finished = new AtomicLong();

            volatile long started;

            @Override
            public void request(long n) {
                while (n-- > 0 && !cancelled) {
                    if (iterator.hasNext()) {
                        ++started;
                        Promise<T> promise = iterator.next();
                        if (!iterator.hasNext()) {
                            complete = true;
                        }
                        Execution.fork().onStart(execInit).onComplete(e -> {
                            long finished = this.finished.incrementAndGet();
                            if (finished == started && complete && !cancelled) {
                                write.complete();
                            }
                        }).start(e -> promise.onError(write::error).then(write::item));
                    } else {
                        return;
                    }
                }
            }

            @Override
            public void cancel() {
                cancelled = true;
            }
        };
    });
}
Also used : Types(ratpack.util.Types) Iterator(java.util.Iterator) Execution(ratpack.exec.Execution) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Operation(ratpack.exec.Operation) Promise(ratpack.exec.Promise) AtomicReference(java.util.concurrent.atomic.AtomicReference) BiAction(ratpack.func.BiAction) ParallelBatch(ratpack.exec.util.ParallelBatch) BufferingPublisher(ratpack.stream.internal.BufferingPublisher) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Lists(com.google.common.collect.Lists) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Action(ratpack.func.Action) Subscription(org.reactivestreams.Subscription) TransformablePublisher(ratpack.stream.TransformablePublisher) ExecResult(ratpack.exec.ExecResult) BufferingPublisher(ratpack.stream.internal.BufferingPublisher) AtomicLong(java.util.concurrent.atomic.AtomicLong) Subscription(org.reactivestreams.Subscription)

Example 3 with Promise

use of ratpack.exec.Promise in project ratpack by ratpack.

the class TimedMethodInterceptor method invoke.

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    String timerTag = buildTimerTag(invocation.getMethod().getAnnotation(Timed.class), invocation.getMethod());
    final Timer timer = metricRegistry.timer(timerTag);
    final Timer.Context timerContext = timer.time();
    Object result;
    try {
        result = invocation.proceed();
        if (result instanceof Promise<?>) {
            result = ((Promise<?>) result).time(duration -> timer.update(duration.getNano(), TimeUnit.NANOSECONDS));
        } else {
            timerContext.stop();
        }
    } catch (Exception e) {
        timerContext.stop();
        throw e;
    }
    return result;
}
Also used : Inject(javax.inject.Inject) TimeUnit(java.util.concurrent.TimeUnit) Timed(com.codahale.metrics.annotation.Timed) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) MetricRegistry(com.codahale.metrics.MetricRegistry) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Timer(com.codahale.metrics.Timer) Promise(ratpack.exec.Promise) Method(java.lang.reflect.Method) Promise(ratpack.exec.Promise) Timer(com.codahale.metrics.Timer) Timed(com.codahale.metrics.annotation.Timed)

Example 4 with Promise

use of ratpack.exec.Promise in project ratpack by ratpack.

the class DefaultParallelBatch method yieldAll.

@Override
public Promise<List<? extends ExecResult<T>>> yieldAll() {
    List<Promise<T>> promises = Lists.newArrayList(this.promises);
    List<ExecResult<T>> results = Types.cast(promises);
    AtomicInteger counter = new AtomicInteger(promises.size());
    return Promise.async(d -> {
        for (int i = 0; i < promises.size(); ++i) {
            final int finalI = i;
            Execution.fork().onStart(execInit).onComplete(e -> {
                if (counter.decrementAndGet() == 0) {
                    d.success(results);
                }
            }).start(e -> promises.get(finalI).result(t -> {
                results.set(finalI, t);
            }));
        }
    });
}
Also used : Types(ratpack.util.Types) Iterator(java.util.Iterator) Execution(ratpack.exec.Execution) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Operation(ratpack.exec.Operation) Promise(ratpack.exec.Promise) AtomicReference(java.util.concurrent.atomic.AtomicReference) BiAction(ratpack.func.BiAction) ParallelBatch(ratpack.exec.util.ParallelBatch) BufferingPublisher(ratpack.stream.internal.BufferingPublisher) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Lists(com.google.common.collect.Lists) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Action(ratpack.func.Action) Subscription(org.reactivestreams.Subscription) TransformablePublisher(ratpack.stream.TransformablePublisher) ExecResult(ratpack.exec.ExecResult) Promise(ratpack.exec.Promise) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecResult(ratpack.exec.ExecResult)

Example 5 with Promise

use of ratpack.exec.Promise in project ratpack by ratpack.

the class DefaultParallelBatch method forEach.

@Override
public Operation forEach(BiAction<? super Integer, ? super T> consumer) {
    AtomicReference<Throwable> error = new AtomicReference<>();
    AtomicBoolean done = new AtomicBoolean();
    AtomicInteger wip = new AtomicInteger();
    return Promise.async(d -> {
        int i = 0;
        Iterator<? extends Promise<T>> iterator = promises.iterator();
        while (iterator.hasNext()) {
            Promise<T> promise = iterator.next();
            final int finalI = i++;
            wip.incrementAndGet();
            if (!iterator.hasNext()) {
                done.set(true);
            }
            Execution.fork().onStart(execInit).onComplete(e -> {
                if (wip.decrementAndGet() == 0 && done.get()) {
                    Throwable t = error.get();
                    if (t == null) {
                        d.success(null);
                    } else {
                        d.error(t);
                    }
                }
            }).start(e -> {
                if (error.get() == null) {
                    promise.result(t -> {
                        if (t.isError()) {
                            Throwable thisError = t.getThrowable();
                            if (!error.compareAndSet(null, thisError)) {
                                Throwable firstError = error.get();
                                if (firstError != thisError) {
                                    firstError.addSuppressed(thisError);
                                }
                            }
                        } else {
                            consumer.execute(finalI, t.getValue());
                        }
                    });
                }
            });
        }
        if (i == 0) {
            d.success(null);
        }
    }).operation();
}
Also used : Types(ratpack.util.Types) Iterator(java.util.Iterator) Execution(ratpack.exec.Execution) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Operation(ratpack.exec.Operation) Promise(ratpack.exec.Promise) AtomicReference(java.util.concurrent.atomic.AtomicReference) BiAction(ratpack.func.BiAction) ParallelBatch(ratpack.exec.util.ParallelBatch) BufferingPublisher(ratpack.stream.internal.BufferingPublisher) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Lists(com.google.common.collect.Lists) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Action(ratpack.func.Action) Subscription(org.reactivestreams.Subscription) TransformablePublisher(ratpack.stream.TransformablePublisher) ExecResult(ratpack.exec.ExecResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Promise(ratpack.exec.Promise) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(java.util.Iterator) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Aggregations

Promise (ratpack.exec.Promise)8 TransformablePublisher (ratpack.stream.TransformablePublisher)6 List (java.util.List)5 Subscription (org.reactivestreams.Subscription)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Operation (ratpack.exec.Operation)4 Action (ratpack.func.Action)4 BufferingPublisher (ratpack.stream.internal.BufferingPublisher)4 Types (ratpack.util.Types)4 Lists (com.google.common.collect.Lists)3 Iterator (java.util.Iterator)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 ExecResult (ratpack.exec.ExecResult)3 Execution (ratpack.exec.Execution)3 ParallelBatch (ratpack.exec.util.ParallelBatch)3 BiAction (ratpack.func.BiAction)3 ByteBuf (io.netty.buffer.ByteBuf)2 Block (ratpack.func.Block)2