/* * 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: * * http://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.Collection; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.stream.Collectors; /** * A delegating {@link ExecutorService} that makes sure all submitted tasks are * executed within the {@link RequestContext}. */ class RequestContextAwareExecutorService implements ExecutorService { private final RequestContext context; private final ExecutorService delegate; RequestContextAwareExecutorService(RequestContext context, ExecutorService delegate) { this.context = context; this.delegate = delegate; } final RequestContext context() { return context; } ExecutorService delegate() { return delegate; } @Override public final void shutdown() { delegate.shutdown(); } @Override public final List<Runnable> shutdownNow() { return delegate.shutdownNow(); } @Override public Future<?> submit(Runnable task) { return delegate.submit(context.makeContextAware(task)); } @Override public <T> Future<T> submit(Runnable task, T result) { return delegate.submit(context.makeContextAware(task), result); } @Override public <T> Future<T> submit(Callable<T> task) { return delegate.submit(context.makeContextAware(task)); } @Override public final boolean isShutdown() { return delegate.isShutdown(); } @Override public final boolean isTerminated() { return delegate.isTerminated(); } @Override public final boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { return delegate.awaitTermination(timeout, unit); } @Override public final <T> List<Future<T>> invokeAll( Collection<? extends Callable<T>> tasks) throws InterruptedException { return delegate.invokeAll(makeContextAware(tasks)); } @Override public final <T> List<Future<T>> invokeAll( Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException { return delegate.invokeAll(makeContextAware(tasks), timeout, unit); } @Override public final <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException { return delegate.invokeAny(makeContextAware(tasks)); } @Override public final <T> T invokeAny( Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return delegate.invokeAny(makeContextAware(tasks), timeout, unit); } @Override public final void execute(Runnable command) { delegate.execute(context.makeContextAware(command)); } private <T> Collection<? extends Callable<T>> makeContextAware( Collection<? extends Callable<T>> tasks) { return tasks.stream().map(context::makeContextAware).collect(Collectors.toList()); } }