@Override public void cleanup() { ThreadLocal threadLocal = getThreadLocal(); if (threadLocal != null) { threadLocal.remove(); } } }
@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()); } }); } }
@Override public void apply(Map<String, Object> storage) { Object value = storage.get(key); ThreadLocal threadLocal = getThreadLocal(); if (threadLocal != null) { threadLocal.set(value); } }
@Override public void collect(Map<String, Object> storage) { ThreadLocal threadLocal = getThreadLocal(); if (threadLocal != null) { Object value = threadLocal.get(); storage.put(key, value); } }