001package systems.dmx.core.util; 002 003import java.util.concurrent.Callable; 004 005 006 007public class ContextTracker { 008 009 // ---------------------------------------------------------------------------------------------- Instance Variables 010 011 private ThreadLocal<Integer> trackingLevel = new ThreadLocal() { 012 @Override 013 protected Integer initialValue() { 014 return 0; 015 } 016 }; 017 018 // -------------------------------------------------------------------------------------------------- Public Methods 019 020 public <V> V run(Callable<V> callable) throws Exception { 021 int level = trackingLevel.get(); 022 try { 023 trackingLevel.set(level + 1); 024 return callable.call(); // throws exception 025 } finally { 026 trackingLevel.set(level); 027 } 028 } 029 030 public boolean runsInTrackedContext() { 031 return trackingLevel.get() > 0; 032 } 033}