All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.linecorp.armeria.common.AbstractRequestContext Maven / Gradle / Ivy

/*
 * Copyright 2016 LINE Corporation
 *
 * LINE Corporation licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.linecorp.armeria.common;

import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

import com.linecorp.armeria.common.util.Exceptions;
import com.linecorp.armeria.common.util.SafeCloseable;

import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoop;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import io.netty.util.concurrent.GenericFutureListener;
import io.netty.util.concurrent.Promise;

/**
 * A skeletal {@link RequestContext} implementation.
 */
public abstract class AbstractRequestContext implements RequestContext {

    private static final CancellationException CANCELLATION_EXCEPTION =
            Exceptions.clearTrace(new CancellationException());

    private boolean timedOut;

    @Override
    public final EventLoop contextAwareEventLoop() {
        return RequestContext.super.contextAwareEventLoop();
    }

    @Override
    public final Executor makeContextAware(Executor executor) {
        return RequestContext.super.makeContextAware(executor);
    }

    @Override
    public final  Callable makeContextAware(Callable callable) {
        return () -> {
            try (SafeCloseable ignored = pushIfAbsent()) {
                return callable.call();
            }
        };
    }

    @Override
    public final Runnable makeContextAware(Runnable runnable) {
        return () -> {
            try (SafeCloseable ignored = pushIfAbsent()) {
                runnable.run();
            }
        };
    }

    @Override
    public final  Function makeContextAware(Function function) {
        return t -> {
            try (SafeCloseable ignored = pushIfAbsent()) {
                return function.apply(t);
            }
        };
    }

    @Override
    public final  BiFunction makeContextAware(BiFunction function) {
        return (t, u) -> {
            try (SafeCloseable ignored = pushIfAbsent()) {
                return function.apply(t, u);
            }
        };
    }

    @Override
    public final  Consumer makeContextAware(Consumer action) {
        return t -> {
            try (SafeCloseable ignored = pushIfAbsent()) {
                action.accept(t);
            }
        };
    }

    @Override
    public final  BiConsumer makeContextAware(BiConsumer action) {
        return (t, u) -> {
            try (SafeCloseable ignored = pushIfAbsent()) {
                action.accept(t, u);
            }
        };
    }

    @Override
    public final  FutureListener makeContextAware(FutureListener listener) {
        return future -> invokeOperationComplete(listener, future);
    }

    @Override
    public final ChannelFutureListener makeContextAware(ChannelFutureListener listener) {
        return future -> invokeOperationComplete(listener, future);
    }

    @Override
    public final > GenericFutureListener
    makeContextAware(GenericFutureListener listener) {
        return future -> invokeOperationComplete(listener, future);
    }

    @Override
    public final  CompletionStage makeContextAware(CompletionStage stage) {
        final CompletableFuture future = new RequestContextAwareCompletableFuture<>(this);
        stage.whenComplete((result, cause) -> {
            try (SafeCloseable ignored = pushIfAbsent()) {
                if (cause != null) {
                    future.completeExceptionally(cause);
                } else {
                    future.complete(result);
                }
            }
        });
        return future;
    }

    @Override
    public final  CompletableFuture makeContextAware(CompletableFuture future) {
        return RequestContext.super.makeContextAware(future);
    }

    @Override
    public boolean isTimedOut() {
        return timedOut;
    }

    /**
     * Marks this {@link RequestContext} as having been timed out. Any callbacks created with
     * {@code makeContextAware} that are run after this will be failed with {@link CancellationException}.
     */
    public void setTimedOut() {
        timedOut = true;
    }

    private > void invokeOperationComplete(
            GenericFutureListener listener, T future) throws Exception {
        try (SafeCloseable ignored = pushIfAbsent()) {
            listener.operationComplete(future);
        }
    }

    @Override
    public final void onEnter(Runnable callback) {
        RequestContext.super.onEnter(callback);
    }

    @Override
    public final void onExit(Runnable callback) {
        RequestContext.super.onExit(callback);
    }

    @Override
    public final void resolvePromise(Promise promise, Object result) {
        RequestContext.super.resolvePromise(promise, result);
    }

    @Override
    public final void rejectPromise(Promise promise, Throwable cause) {
        RequestContext.super.rejectPromise(promise, cause);
    }

    @Override
    public final int hashCode() {
        return super.hashCode();
    }

    @Override
    public final boolean equals(Object obj) {
        return super.equals(obj);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy