Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to generalize context propagation pattern #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.Assert;
import org.springframework.util.concurrent.DelegatingContextCallable;

/**
* <p>
Expand All @@ -35,22 +35,7 @@
* @author Rob Winch
* @since 3.2
*/
public final class DelegatingSecurityContextCallable<V> implements Callable<V> {

private final Callable<V> delegate;


/**
* The {@link SecurityContext} that the delegate {@link Callable} will be
* ran as.
*/
private final SecurityContext delegateSecurityContext;

/**
* The {@link SecurityContext} that was on the {@link SecurityContextHolder}
* prior to being set to the delegateSecurityContext.
*/
private SecurityContext originalSecurityContext;
public final class DelegatingSecurityContextCallable<V> extends DelegatingContextCallable<SecurityContext, V> {

/**
* Creates a new {@link DelegatingSecurityContextCallable} with a specific
Expand All @@ -62,10 +47,7 @@ public final class DelegatingSecurityContextCallable<V> implements Callable<V> {
*/
public DelegatingSecurityContextCallable(Callable<V> delegate,
SecurityContext securityContext) {
Assert.notNull(delegate, "delegate cannot be null");
Assert.notNull(securityContext, "securityContext cannot be null");
this.delegate = delegate;
this.delegateSecurityContext = securityContext;
super(delegate, securityContext, SecurityContextOps.INSTANCE);
}

/**
Expand All @@ -75,31 +57,7 @@ public DelegatingSecurityContextCallable(Callable<V> delegate,
* {@link SecurityContext}. Cannot be null.
*/
public DelegatingSecurityContextCallable(Callable<V> delegate) {
this(delegate, SecurityContextHolder.getContext());
}

@Override
public V call() throws Exception {
this.originalSecurityContext = SecurityContextHolder.getContext();

try {
SecurityContextHolder.setContext(delegateSecurityContext);
return delegate.call();
}
finally {
SecurityContext emptyContext = SecurityContextHolder.createEmptyContext();
if (emptyContext.equals(originalSecurityContext)) {
SecurityContextHolder.clearContext();
} else {
SecurityContextHolder.setContext(originalSecurityContext);
}
this.originalSecurityContext = null;
}
}

@Override
public String toString() {
return delegate.toString();
super(delegate, SecurityContextHolder.getContext(), SecurityContextOps.INSTANCE);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,26 @@
*/
package org.springframework.security.concurrent;

import java.util.concurrent.Executor;

import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.Assert;
import org.springframework.util.concurrent.DelegatingContextExecutor;

import java.util.concurrent.Executor;

/**
* An {@link Executor} which wraps each {@link Runnable} in a
* {@link DelegatingSecurityContextRunnable}.
* {@link org.springframework.util.concurrent.DelegatingContextRunnable}
*
* @author Rob Winch
* @since 3.2
*/
public class DelegatingSecurityContextExecutor extends
AbstractDelegatingSecurityContextSupport implements Executor {
private final Executor delegate;
DelegatingContextExecutor<SecurityContext> {

/**
* Creates a new {@link DelegatingSecurityContextExecutor} that uses the specified
* {@link SecurityContext}.
*
* @param delegateExecutor the {@link Executor} to delegate to. Cannot be null.
* @param securityContext the {@link SecurityContext} to use for each
* {@link DelegatingSecurityContextRunnable} or null to default to the current
* {@link SecurityContext}
*/
public DelegatingSecurityContextExecutor(Executor delegateExecutor,
SecurityContext securityContext) {
super(securityContext);
Assert.notNull(delegateExecutor, "delegateExecutor cannot be null");
this.delegate = delegateExecutor;
public DelegatingSecurityContextExecutor(Executor delegateExecutor, SecurityContext context) {
super(delegateExecutor, context, SecurityContextOps.INSTANCE);
}

/**
* Creates a new {@link DelegatingSecurityContextExecutor} that uses the current
* {@link SecurityContext} from the {@link SecurityContextHolder} at the time the task
* is submitted.
*
* @param delegate the {@link Executor} to delegate to. Cannot be null.
*/
public DelegatingSecurityContextExecutor(Executor delegate) {
this(delegate, null);
}

public final void execute(Runnable task) {
task = wrap(task);
delegate.execute(task);
}

protected final Executor getDelegateExecutor() {
return delegate;
super(delegate, SecurityContextOps.INSTANCE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,130 +15,28 @@
*/
package org.springframework.security.concurrent;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.util.concurrent.DelegatingContextExecutorService;

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 org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

/**
* An {@link ExecutorService} which wraps each {@link Runnable} in a
* {@link DelegatingSecurityContextRunnable} and each {@link Callable} in a
* {@link DelegatingSecurityContextCallable}.
* {@link org.springframework.util.concurrent.DelegatingContextRunnable} and each {@link Callable} in a
* {@link org.springframework.util.concurrent.DelegatingContextCallable}.
*
* @author Rob Winch
* @since 3.2
*/
public class DelegatingSecurityContextExecutorService extends
DelegatingSecurityContextExecutor implements ExecutorService {
/**
* Creates a new {@link DelegatingSecurityContextExecutorService} that uses the
* specified {@link SecurityContext}.
*
* @param delegateExecutorService the {@link ExecutorService} to delegate to. Cannot
* be null.
* @param securityContext the {@link SecurityContext} to use for each
* {@link DelegatingSecurityContextRunnable} and each
* {@link DelegatingSecurityContextCallable}.
*/
public DelegatingSecurityContextExecutorService(
ExecutorService delegateExecutorService, SecurityContext securityContext) {
super(delegateExecutorService, securityContext);
}
DelegatingContextExecutorService<SecurityContext> {

/**
* Creates a new {@link DelegatingSecurityContextExecutorService} that uses the
* current {@link SecurityContext} from the {@link SecurityContextHolder}.
*
* @param delegate the {@link ExecutorService} to delegate to. Cannot be
* null.
*/
public DelegatingSecurityContextExecutorService(ExecutorService delegate) {
this(delegate, null);
}

public final void shutdown() {
getDelegate().shutdown();
public DelegatingSecurityContextExecutorService(ExecutorService delegateExecutorService, SecurityContext context) {
super(delegateExecutorService, context, SecurityContextOps.INSTANCE);
}

public final List<Runnable> shutdownNow() {
return getDelegate().shutdownNow();
}

public final boolean isShutdown() {
return getDelegate().isShutdown();
}

public final boolean isTerminated() {
return getDelegate().isTerminated();
}

public final boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException {
return getDelegate().awaitTermination(timeout, unit);
}

public final <T> Future<T> submit(Callable<T> task) {
task = wrap(task);
return getDelegate().submit(task);
}

public final <T> Future<T> submit(Runnable task, T result) {
task = wrap(task);
return getDelegate().submit(task, result);
}

public final Future<?> submit(Runnable task) {
task = wrap(task);
return getDelegate().submit(task);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public final List invokeAll(Collection tasks) throws InterruptedException {
tasks = createTasks(tasks);
return getDelegate().invokeAll(tasks);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public final List invokeAll(Collection tasks, long timeout, TimeUnit unit)
throws InterruptedException {
tasks = createTasks(tasks);
return getDelegate().invokeAll(tasks, timeout, unit);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public final Object invokeAny(Collection tasks) throws InterruptedException,
ExecutionException {
tasks = createTasks(tasks);
return getDelegate().invokeAny(tasks);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public final Object invokeAny(Collection tasks, long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
tasks = createTasks(tasks);
return getDelegate().invokeAny(tasks, timeout, unit);
}

private <T> Collection<Callable<T>> createTasks(Collection<Callable<T>> tasks) {
if (tasks == null) {
return null;
}
List<Callable<T>> results = new ArrayList<>(tasks.size());
for (Callable<T> task : tasks) {
results.add(wrap(task));
}
return results;
}

private ExecutorService getDelegate() {
return (ExecutorService) getDelegateExecutor();
public DelegatingSecurityContextExecutorService(ExecutorService delegate) {
super(delegate, SecurityContextOps.INSTANCE);
}
}
Loading