1 9 package javolution.context; 10 11 import j2me.io.Serializable; 12 import j2me.lang.IllegalStateException; 13 import j2me.lang.ThreadLocal; 14 15 69 public abstract class Context extends RealtimeObject implements Serializable { 70 71 74 public static final HeapContext ROOT = new HeapContext(); 75 76 79 private static final ThreadLocal CURRENT = new ThreadLocal () { 80 protected Object initialValue() { 81 return ROOT; 82 } 83 }; 84 85 88 transient Thread _owner; 89 90 93 transient Context _outer; 94 95 98 protected Context() { 99 } 100 101 108 public static Context current() { 109 return (Context) Context.CURRENT.get(); 110 } 111 112 121 public final Thread getOwner() { 122 return _owner; 123 } 124 125 131 public final Context getOuter() { 132 return _outer; 133 } 134 135 139 protected abstract void enterAction(); 140 141 145 protected abstract void exitAction(); 146 147 153 public static void enter(Context context) { 154 if (context._owner != null) 155 throw new IllegalStateException ("Context is currently in use"); 156 Context current = Context.current(); 157 context._outer = current; 158 context._owner = Thread.currentThread(); 159 Context.CURRENT.set(context); 160 context.enterAction(); 161 } 162 163 172 public static void exit(Context context) { 173 if (context != Context.current()) 174 throw new IllegalStateException ("The Specified context is not the current context"); 175 if (context._owner != Thread.currentThread()) throw new IllegalStateException ("Cannot exit context belonging to another thread"); 177 exitNoCheck(context); 178 } 179 static void exitNoCheck(Context context) { 180 try { 181 context.exitAction(); 182 } finally { 183 Context.CURRENT.set(context._outer); 184 context._outer = null; 185 context._owner = null; 186 } 187 } 188 189 193 static void setCurrent(ConcurrentContext context) { 194 Context.CURRENT.set(context); 195 } 196 197 203 void setPoolsActive(boolean value) { 204 _outer.setPoolsActive(value); 205 } 206 207 212 LocalPools getLocalPools() { 213 return _outer.getLocalPools(); 214 } 215 216 } | Popular Tags |