1 9 package javolution.context; 10 11 import j2me.lang.UnsupportedOperationException; 12 import j2me.lang.ThreadLocal; 13 import j2mex.realtime.MemoryArea; 14 import javolution.Javolution; 15 import javolution.JavolutionError; 16 import javolution.text.Text; 17 import javolution.util.FastTable; 18 import javolution.xml.XMLFormat; 19 import javolution.xml.stream.XMLStreamException; 20 21 70 public class PoolContext extends Context { 71 72 75 private static Factory FACTORY = new Factory() { 76 protected Object create() { 77 return new PoolContext(); 78 } 79 }; 80 81 84 private static final LocalContext.Reference ENABLED 85 = new LocalContext.Reference(new Boolean (true)); 86 87 90 final static XMLFormat XML = new XMLFormat( 91 Javolution.j2meGetClass("javolution.context.PoolContext")) { 92 93 public void read(InputElement xml, Object obj) throws XMLStreamException { 94 PoolContext ctx = (PoolContext)obj; 95 Class cls = ctx._allPools.getClass(); 96 ctx._allPools.addAll((FastTable)xml.get("Pools", cls)); 97 } 98 99 public void write(Object obj, OutputElement xml) throws XMLStreamException { 100 PoolContext ctx = (PoolContext)obj; 101 Class cls = ctx._allPools.getClass(); 102 xml.add(ctx._allPools, "Pools", cls); 103 } 104 }; 105 106 111 private final FastTable _allPools = new FastTable(); 112 113 116 private boolean _isEnabled = true; 117 118 121 private final ThreadLocal _localPools = new ThreadLocal () { 122 private Runnable _createLocalPools = new Runnable () { 123 public void run() { 124 LocalPools pools = new LocalPools(true); 125 pools._owner = Thread.currentThread(); 126 _allPools.add(pools); 127 } 128 }; 129 130 protected synchronized Object initialValue() { 131 for (int i=0; i < _allPools.size(); i++) { 133 LocalPools pools = (LocalPools) _allPools.get(i); 134 if (pools._owner == null) { 135 pools._owner = Thread.currentThread(); 136 return pools; 137 } 138 } 139 MemoryArea.getMemoryArea(PoolContext.this).executeInArea( 141 _createLocalPools); 142 return _allPools.getLast(); 143 } 144 }; 145 146 149 public PoolContext() { 150 } 151 152 158 public staticContext current() { 159 for (Context ctx = Context.current(); ctx != null; ctx = ctx.getOuter()) { 160 if (ctx instanceof PoolContext) 161 return (PoolContext) ctx; 162 if (ctx instanceof HeapContext) 163 return null; 164 } 165 throw new JavolutionError("No heap context or pool context"); 166 } 167 168 175 public static void setEnabled(boolean enabled) { 176 ENABLED.set(enabled ? TRUE : FALSE); 177 } 178 179 private static final Boolean TRUE = new Boolean (true); 181 private static final Boolean FALSE = new Boolean (false); 183 191 public static boolean isEnabled() { 192 return ((Boolean ) ENABLED.get()).booleanValue(); 193 } 194 195 200 public static void enter() { 201 PoolContext ctx = (PoolContext) FACTORY.object(); 202 ctx._isInternal = true; 203 ctx._isEnabled = PoolContext.isEnabled(); 204 Context.enter(ctx); 205 } 206 private transient boolean _isInternal; 207 208 214 public static void exit() { 215 PoolContext ctx = (PoolContext) Context.current(); 216 if (!ctx._isInternal) throw new UnsupportedOperationException 217 ("The context to exit must be specified"); 218 ctx._isInternal = false; 219 Context.exitNoCheck(ctx); 220 FACTORY.recycle(ctx); 221 } 222 223 226 public void clear() { 227 for (int i=0; i < _allPools.size(); i++) { 228 ((LocalPools) _allPools.get(i)).clear(); 229 } 230 } 231 232 236 public Text toText() { 237 return _allPools.toText(); 238 } 239 240 protected void enterAction() { 242 Context outer = this.getOuter(); 243 outer.getLocalPools().deactivatePools(); 244 this.getLocalPools().activatePools(); 245 } 246 247 protected void exitAction() { 249 if (_isEnabled) { 251 for (int i=0; i < _allPools.size(); i++) { 252 ((LocalPools) _allPools.get(i)).reset(); 253 } 254 } 255 this.getLocalPools().deactivatePools(); 256 Context outer = this.getOuter(); 257 outer.getLocalPools().activatePools(); 258 } 259 260 final LocalPools getLocalPools() { 262 return _isEnabled ? (LocalPools) _localPools.get() : 263 Context.ROOT.getLocalPools(); 264 } 265 266 294 public static final class Referenceextends RealtimeObject implements 295 javolution.lang.Reference{ 296 297 300 private static final Factory FACTORY = new Factory() { 301 protected Object create() { 302 return new Reference(); 303 } 304 305 protected void cleanup(Object obj) { 306 ((Reference) obj)._value = null; 307 } 308 }; 309 310 313 private Object _value; 314 315 319 private Reference() { 320 } 321 322 328 public staticReference newInstance() { 329 return (Reference) FACTORY.object(); 330 } 331 332 337 public void recycle() { 338 FACTORY.recycle(this); 339 } 340 341 347 public Text toText() { 348 return Text.valueOf(this.get()); 349 } 350 351 public Object get() { 353 return _value; 354 } 355 356 public void set(Object value) { 358 _value = value; 359 } 360 } 361 362 } | Popular Tags |