@Test public void testCallable() throws Exception { assertEquals(5, context.callWithContext(new Callable<Integer>() { @Override public Integer call() throws Exception { return 5; } }).intValue()); }
@SuppressWarnings("unchecked") @Override public <T> T callWithContext(Callable<T> callable) throws Exception { Object owner = new Object(); Stack<ListenerInvocation> invocations = new Stack<ListenerInvocation>(); boolean reentry = !ManagedContextUtils.setAndCheckOwner(owner); Throwable preError = null; Throwable logicError = null; try { for (ManagedContextListener<?> listener : listeners) { Object data = null; try { data = listener.onEnterContext(reentry); } catch (Throwable t) { if (preError == null) { preError = t; } log.error("Failed onEnterContext for listener [{}]", listener, t); } invocations.push(new ListenerInvocation((ManagedContextListener<Object>) listener, data)); } try { if (preError == null) { return callable.call(); } else { throwException(preError); return null; } } catch (Throwable t) { if (preError != t) { logicError = t; } throwException(t); return null; } finally { Throwable postError = null; while (!invocations.isEmpty()) { ListenerInvocation invocation = invocations.pop(); try { invocation.listener.onLeaveContext(invocation.data, reentry, logicError == null ? preError : logicError); } catch (Throwable t) { postError = t; log.error("Failed onLeaveContext for listener [{}]", invocation.listener, t); } } if (preError == null && postError != null) { throwException(postError); } } } finally { if (ManagedContextUtils.clearOwner(owner)) ManagedThreadLocal.reset(); } }
DefaultManagedContext implements ManagedContext { @SuppressWarnings("unchecked") @Override public <T> T callWithContext(Callable<T> callable) throws Exception { Object owner = new Object(); Stack<ListenerInvocation> invocations = new Stack<ListenerInvocation>(); boolean reentry = !ManagedContextUtils.setAndCheckOwner(owner); Throwable preError = null; Throwable logicError = null; try { for (ManagedContextListener<?> listener : listeners) { Object data = null; try { data = listener.onEnterContext(reentry); } catch (Throwable t) { if (preError == null) { preError = t; } log.error("Failed onEnterContext for listener [{}]", listener, t); } invocations.push(new ListenerInvocation((ManagedContextListener<Object>) listener, data)); } try { if (preError == null) { return callable.call(); } else { throwException(preError); return null; } } catch (Throwable t) { if (preError != t) { logicError = t; } throwException(t); return null; } finally { Throwable postError = null; while (!invocations.isEmpty()) { ListenerInvocation invocation = invocations.pop(); try { invocation.listener.onLeaveContext(invocation.data, reentry, logicError == null ? preError : logicError); } catch (Throwable t) { postError = t; log.error("Failed onLeaveContext for listener [{}]", invocation.listener, t); } } if (preError == null && postError != null) { throwException(postError); } } } finally { if (ManagedContextUtils.clearOwner(owner)) ManagedThreadLocal.reset(); } } }
DefaultManagedContext implements ManagedContext { @SuppressWarnings("unchecked") @Override public <T> T callWithContext(Callable<T> callable) throws Exception { Object owner = new Object(); Stack<ListenerInvocation> invocations = new Stack<ListenerInvocation>(); boolean reentry = !ManagedContextUtils.setAndCheckOwner(owner); Throwable preError = null; Throwable logicError = null; try { for (ManagedContextListener<?> listener : listeners) { Object data = null; try { data = listener.onEnterContext(reentry); } catch (Throwable t) { if (preError == null) { preError = t; } log.error("Failed onEnterContext for listener [{}]", listener, t); } invocations.push(new ListenerInvocation((ManagedContextListener<Object>) listener, data)); } try { if (preError == null) { return callable.call(); } else { throwException(preError); return null; } } catch (Throwable t) { if (preError != t) { logicError = t; } throwException(t); return null; } finally { Throwable postError = null; while (!invocations.isEmpty()) { ListenerInvocation invocation = invocations.pop(); try { invocation.listener.onLeaveContext(invocation.data, reentry, logicError == null ? preError : logicError); } catch (Throwable t) { postError = t; log.error("Failed onLeaveContext for listener [{}]", invocation.listener, t); } } if (preError == null && postError != null) { throwException(postError); } } } finally { if (ManagedContextUtils.clearOwner(owner)) ManagedThreadLocal.reset(); } }  }
DefaultManagedContext implements ManagedContext { @SuppressWarnings("unchecked") @Override public <T> T callWithContext(Callable<T> callable) throws Exception { Object owner = new Object(); Stack<ListenerInvocation> invocations = new Stack<ListenerInvocation>(); boolean reentry = !ManagedContextUtils.setAndCheckOwner(owner); Throwable preError = null; Throwable logicError = null; try { for (ManagedContextListener<?> listener : listeners) { Object data = null; try { data = listener.onEnterContext(reentry); } catch (Throwable t) { if (preError == null) { preError = t; } log.error("Failed onEnterContext for listener [{}]", listener, t); } invocations.push(new ListenerInvocation((ManagedContextListener<Object>) listener, data)); } try { if (preError == null) { return callable.call(); } else { throwException(preError); return null; } } catch (Throwable t) { if (preError != t) { logicError = t; } throwException(t); return null; } finally { Throwable postError = null; while (!invocations.isEmpty()) { ListenerInvocation invocation = invocations.pop(); try { invocation.listener.onLeaveContext(invocation.data, reentry, logicError == null ? preError : logicError); } catch (Throwable t) { postError = t; log.error("Failed onLeaveContext for listener [{}]", invocation.listener, t); } } if (preError == null && postError != null) { throwException(postError); } } } finally { if (ManagedContextUtils.clearOwner(owner)) ManagedThreadLocal.reset(); } }  @Override void registerListener(ManagedContextListener<?> listener); @Override void unregisterListener(ManagedContextListener<?> listener); @Override void runWithContext(final Runnable run); @SuppressWarnings("unchecked") @Override T callWithContext(Callable<T> callable); List<ManagedContextListener<?>> getListeners(); void setListeners(List<ManagedContextListener<?>> listeners); }
DefaultManagedContext implements ManagedContext { @SuppressWarnings("unchecked") @Override public <T> T callWithContext(Callable<T> callable) throws Exception { Object owner = new Object(); Stack<ListenerInvocation> invocations = new Stack<ListenerInvocation>(); boolean reentry = !ManagedContextUtils.setAndCheckOwner(owner); Throwable preError = null; Throwable logicError = null; try { for (ManagedContextListener<?> listener : listeners) { Object data = null; try { data = listener.onEnterContext(reentry); } catch (Throwable t) { if (preError == null) { preError = t; } log.error("Failed onEnterContext for listener [{}]", listener, t); } invocations.push(new ListenerInvocation((ManagedContextListener<Object>) listener, data)); } try { if (preError == null) { return callable.call(); } else { throwException(preError); return null; } } catch (Throwable t) { if (preError != t) { logicError = t; } throwException(t); return null; } finally { Throwable postError = null; while (!invocations.isEmpty()) { ListenerInvocation invocation = invocations.pop(); try { invocation.listener.onLeaveContext(invocation.data, reentry, logicError == null ? preError : logicError); } catch (Throwable t) { postError = t; log.error("Failed onLeaveContext for listener [{}]", invocation.listener, t); } } if (preError == null && postError != null) { throwException(postError); } } } finally { if (ManagedContextUtils.clearOwner(owner)) ManagedThreadLocal.reset(); } }  @Override void registerListener(ManagedContextListener<?> listener); @Override void unregisterListener(ManagedContextListener<?> listener); @Override void runWithContext(final Runnable run); @SuppressWarnings("unchecked") @Override T callWithContext(Callable<T> callable); List<ManagedContextListener<?>> getListeners(); void setListeners(List<ManagedContextListener<?>> listeners);  }