/** * Factory method for creating a {@link DelegatingSecurityContextRunnable}. * * @param delegate the original {@link Runnable} that will be delegated to after * establishing a {@link SecurityContext} on the {@link SecurityContextHolder}. Cannot * have null. * @param securityContext the {@link SecurityContext} to establish before invoking the * delegate {@link Runnable}. If null, the current {@link SecurityContext} from the * {@link SecurityContextHolder} will be used. * @return */ public static Runnable create(Runnable delegate, SecurityContext securityContext) { Assert.notNull(delegate, "delegate cannot be null"); return securityContext == null ? new DelegatingSecurityContextRunnable(delegate) : new DelegatingSecurityContextRunnable(delegate, securityContext); } }
protected final Runnable wrap(Runnable delegate) { return DelegatingSecurityContextRunnable.create(delegate, securityContext); }
/** * Factory method for creating a {@link DelegatingSecurityContextRunnable}. * * @param delegate the original {@link Runnable} that will be delegated to after * establishing a {@link SecurityContext} on the {@link SecurityContextHolder}. Cannot * have null. * @param securityContext the {@link SecurityContext} to establish before invoking the * delegate {@link Runnable}. If null, the current {@link SecurityContext} from the * {@link SecurityContextHolder} will be used. * @return */ public static Runnable create(Runnable delegate, SecurityContext securityContext) { Assert.notNull(delegate, "delegate cannot be null"); return securityContext == null ? new DelegatingSecurityContextRunnable(delegate) : new DelegatingSecurityContextRunnable(delegate, securityContext); } }
protected final Runnable wrap(Runnable delegate) { return DelegatingSecurityContextRunnable.create(delegate, securityContext); }
@Test(expected = IllegalArgumentException.class) public void constructorNullDelegate() { new DelegatingSecurityContextRunnable(null); }
@Test(expected = IllegalArgumentException.class) public void createNullDelegateAndSecurityContext() { DelegatingSecurityContextRunnable.create(null, null); }
@Test public void toStringDelegates() { runnable = new DelegatingSecurityContextRunnable(delegate, securityContext); assertThat(runnable.toString()).isEqualTo(delegate.toString()); }
@Test(expected = IllegalArgumentException.class) public void createNullDelegate() { DelegatingSecurityContextRunnable.create(null, securityContext); }
@Test(expected = IllegalArgumentException.class) public void constructorNullDelegateNonNullSecurityContext() { new DelegatingSecurityContextRunnable(null, securityContext); }
@Test(expected = IllegalArgumentException.class) public void createNullDelegateAndSecurityContext() { DelegatingSecurityContextRunnable.create(null, null); }
@Test(expected = IllegalArgumentException.class) public void constructorNullSecurityContext() { new DelegatingSecurityContextRunnable(delegate, null); }
@Test public void create() throws Exception { runnable = DelegatingSecurityContextRunnable.create(delegate, securityContext); assertWrapped(runnable); }
@Test(expected = IllegalArgumentException.class) public void constructorNullDelegateAndSecurityContext() { new DelegatingSecurityContextRunnable(null, null); }
@Test public void createNullSecurityContext() throws Exception { SecurityContextHolder.setContext(securityContext); runnable = DelegatingSecurityContextRunnable.create(delegate, null); SecurityContextHolder.clearContext(); // ensure runnable is what sets up the // SecurityContextHolder assertWrapped(runnable); }
@Test public void call() throws Exception { runnable = new DelegatingSecurityContextRunnable(delegate, securityContext); assertWrapped(runnable); }
protected final Runnable wrap(Runnable delegate) { return DelegatingSecurityContextRunnable.create(delegate, securityContext); }
@Test public void callDefaultSecurityContext() throws Exception { SecurityContextHolder.setContext(securityContext); runnable = new DelegatingSecurityContextRunnable(delegate); SecurityContextHolder.clearContext(); // ensure runnable is what sets up the // SecurityContextHolder assertWrapped(runnable); }
@Test public void callOnSameThread() throws Exception { originalSecurityContext = securityContext; SecurityContextHolder.setContext(originalSecurityContext); executor = synchronousExecutor(); runnable = new DelegatingSecurityContextRunnable(delegate, securityContext); assertWrapped(runnable); }
/** * Factory method for creating a {@link DelegatingSecurityContextRunnable}. * * @param delegate the original {@link Runnable} that will be delegated to after * establishing a {@link SecurityContext} on the {@link SecurityContextHolder}. Cannot * have null. * @param securityContext the {@link SecurityContext} to establish before invoking the * delegate {@link Runnable}. If null, the current {@link SecurityContext} from the * {@link SecurityContextHolder} will be used. * @return */ public static Runnable create(Runnable delegate, SecurityContext securityContext) { Assert.notNull(delegate, "delegate cannot be null"); return securityContext == null ? new DelegatingSecurityContextRunnable(delegate) : new DelegatingSecurityContextRunnable(delegate, securityContext); } }
public void authorizedExecute(Runnable runnable) { new Thread(new DelegatingSecurityContextRunnable(runnable)).start(); }