/** Set the thread local values in the current thread */ public void apply() { for (ThreadLocalTransfer transfer : transfers) { transfer.apply(storage); } }
/** Clean up the thread locals in the current thread */ public void cleanup() { for (ThreadLocalTransfer transfer : transfers) { transfer.cleanup(); } } }
@Override public void cleanup() { ThreadLocal threadLocal = getThreadLocal(); if (threadLocal != null) { threadLocal.remove(); } } }
@Override public Void call() throws Exception { // this is the the main thread, we are actually running inside the thread pool assertNotEquals(originalThread, Thread.currentThread()); // apply the thread local, check it has been applied correctly transfer.apply(storage); assertThreadLocalApplied(); // clean up, check the therad local is now empty transfer.cleanup(); assertThreadLocalCleaned(); return null; }
@Override public Void call() throws Exception { // this is the the main thread, we are actually running inside the thread pool assertNotEquals(originalThread, Thread.currentThread()); // apply the thread local, check it has been applied correctly transfer.apply(storage); assertThreadLocalApplied(); // clean up, check the therad local is now empty transfer.cleanup(); assertThreadLocalCleaned(); return null; }
final Authentication auth = new UsernamePasswordAuthenticationToken("user", "password"); SecurityContextHolder.getContext().setAuthentication(auth); final ThreadLocalsTransfer transfer = new ThreadLocalsTransfer(); Future<Void> future = executor.submit(
/** * Starts and runs all registered {@link ThreadLocalsTransfer} objects and collects the * associated thread locals in the storage object */ public ThreadLocalsTransfer() { transfers = GeoServerExtensions.extensions(ThreadLocalTransfer.class); for (ThreadLocalTransfer transfer : transfers) { transfer.collect(storage); } }
private void testCleanup() { transfer.cleanup(); // check all thread locals have been cleaned up from the current // thread assertNull(Dispatcher.REQUEST.get()); assertNull(AdminRequest.get()); assertNull(LocalPublished.get()); assertNull(LocalWorkspace.get()); assertNull(SecurityContextHolder.getContext().getAuthentication()); } });
private void testApply() { transfer.apply(); // check all thread locals have been applied to the current thread assertSame(request, Dispatcher.REQUEST.get()); assertSame(myState, AdminRequest.get()); assertSame(layer, LocalPublished.get()); assertSame(ws, LocalWorkspace.get()); assertSame( auth, SecurityContextHolder.getContext().getAuthentication()); }
@Override public Void call() throws Exception { testApply(); testCleanup(); return null; }
@Test public void testAdminRequest() throws InterruptedException, ExecutionException { // setup the state final Object myState = new Object(); AdminRequest.start(myState); // test it's transferred properly using the base class machinery testThreadLocalTransfer( new ThreadLocalTransferCallable(new AdminRequestThreadLocalTransfer()) { @Override void assertThreadLocalCleaned() { assertNull(AdminRequest.get()); } @Override void assertThreadLocalApplied() { assertSame(myState, AdminRequest.get()); } }); } }
@Test public void testRequest() throws InterruptedException, ExecutionException { // setup the state final Authentication auth = new UsernamePasswordAuthenticationToken("user", "password"); SecurityContextHolder.getContext().setAuthentication(auth); // test it's transferred properly using the base class machinery testThreadLocalTransfer( new ThreadLocalTransferCallable(new AuthenticationThreadLocalTransfer()) { @Override void assertThreadLocalCleaned() { assertNull(SecurityContextHolder.getContext().getAuthentication()); } @Override void assertThreadLocalApplied() { assertSame(auth, SecurityContextHolder.getContext().getAuthentication()); } }); } }
@Test public void testRequest() throws InterruptedException, ExecutionException, SecurityException, NoSuchFieldException { // setup the state final Request request = new Request(); Dispatcher.REQUEST.set(request); // test it's transferred properly using the base class machinery testThreadLocalTransfer( new ThreadLocalTransferCallable( new PublicThreadLocalTransfer(Dispatcher.class, "REQUEST")) { @Override void assertThreadLocalCleaned() { assertNull(Dispatcher.REQUEST.get()); } @Override void assertThreadLocalApplied() { assertSame(request, Dispatcher.REQUEST.get()); } }); } }
@Test public void testRequest() throws InterruptedException, ExecutionException { // setup the state final LayerInfo layer = new LayerInfoImpl(); LocalPublished.set(layer); // test it's transferred properly using the base class machinery testThreadLocalTransfer( new ThreadLocalTransferCallable(new LocalPublishedThreadLocalTransfer()) { @Override void assertThreadLocalCleaned() { assertNull(LocalPublished.get()); } @Override void assertThreadLocalApplied() { assertSame(layer, LocalPublished.get()); } }); } }
@Test public void testRequest() throws InterruptedException, ExecutionException { // setup the state final WorkspaceInfo ws = new WorkspaceInfoImpl(); LocalWorkspace.set(ws); // test it's transferred properly using the base class machinery testThreadLocalTransfer( new ThreadLocalTransferCallable(new LocalWorkspaceThreadLocalTransfer()) { @Override void assertThreadLocalCleaned() { assertNull(LocalWorkspace.get()); } @Override void assertThreadLocalApplied() { assertSame(ws, LocalWorkspace.get()); } }); } }
public ProcessCallable( Name processName, Map<String, Object> inputs, ProgressListener listener) { this.processName = processName; this.inputs = inputs; this.listener = listener; this.threadLocalTransfer = new ThreadLocalsTransfer(); }
public ThreadLocalTransferCallable(ThreadLocalTransfer transfer) { this.originalThread = Thread.currentThread(); this.transfer = transfer; this.transfer.collect(storage); }
@Override public void apply(Map<String, Object> storage) { Object value = storage.get(key); ThreadLocal threadLocal = getThreadLocal(); if (threadLocal != null) { threadLocal.set(value); } }
public ThreadLocalTransferCallable(ThreadLocalTransfer transfer) { this.originalThread = Thread.currentThread(); this.transfer = transfer; this.transfer.collect(storage); }
@Override public void collect(Map<String, Object> storage) { ThreadLocal threadLocal = getThreadLocal(); if (threadLocal != null) { Object value = threadLocal.get(); storage.put(key, value); } }