1 9 package javolution.context; 10 11 import javolution.Javolution; 12 import javolution.util.FastTable; 13 import javolution.xml.XMLFormat; 14 import javolution.xml.stream.XMLStreamException; 15 import j2mex.realtime.MemoryArea; 16 17 23 final class LocalPools { 24 25 29 final static XMLFormat XML = new XMLFormat( 30 Javolution.j2meGetClass("javolution.context.LocalPools")) { 31 public Object newInstance(Class cls, InputElement xml) throws XMLStreamException { 32 return new LocalPools(xml.getAttribute("isStack", false)); 33 } 34 35 public void read(InputElement xml, Object obj) throws XMLStreamException { 36 LocalPools localPools = (LocalPools)obj; 37 while (xml.hasNext()) { 38 Class factoryClass = (Class ) xml.get("Factory", CLASS_CLASS); 39 int size = ((Integer )xml.get("PoolSize", INTEGER_CLASS)).intValue(); 40 ObjectFactory factory = ObjectFactory.getInstance(factoryClass); 41 ObjectPool pool = localPools._isStack ? 42 factory.newStackPool() : factory.newHeapPool(); 43 localPools._pools[factory._index] = pool; 44 pool.setSize(size); 45 } 46 } 47 48 public void write(Object obj, OutputElement xml) throws XMLStreamException { 49 LocalPools localPools = (LocalPools)obj; 50 xml.setAttribute("isStack", localPools._isStack); 51 for (int i =0, n = ObjectFactory._Count; i < n; i++) { 52 ObjectPool pool = localPools._pools[i]; 53 if (pool != null) { 54 xml.add(ObjectFactory._Instances[i].getClass(), "Factory", CLASS_CLASS); 55 xml.add(new Integer (pool.getSize()), "PoolSize", INTEGER_CLASS); 56 } 57 } 58 } 59 }; 60 private static final Class INTEGER_CLASS = new Integer (0).getClass(); 61 private static final Class CLASS_CLASS = "".getClass().getClass(); 62 63 66 private final ObjectPool[] _pools = new ObjectPool[ObjectFactory._Instances.length]; 67 68 72 private final FastTable _inUsePools = new FastTable(); 73 74 78 Thread _owner; 79 80 83 private final boolean _isStack; 84 85 91 LocalPools( boolean isStack) { 92 _isStack = isStack; 93 } 94 95 103 ObjectPool getPool(final ObjectFactory factory, boolean activate) { 104 final int index = factory._index; 105 ObjectPool pool = _pools[index]; 106 if (pool == null) { 107 MemoryArea.getMemoryArea(this).executeInArea(new Runnable () { 108 public void run() { 109 _pools[factory._index] = _isStack ? factory.newStackPool() 110 : factory.newHeapPool(); 111 } 112 }); 113 pool = _pools[index]; 114 } 115 if (!pool._inUse) { pool._inUse = true; 117 _inUsePools.add(pool); 118 } 119 if (activate) { 120 pool._user = _owner; 121 } 122 return pool; 123 } 124 125 128 void clear() { 129 for (int i =0, n = ObjectFactory._Count; i < n; i++) { 130 ObjectPool pool = _pools[i]; 131 if (pool != null) { 132 pool._user = null; 133 pool._inUse = false; 134 pool.clearAll(); 135 } 136 } 137 _inUsePools.clear(); 138 } 139 140 143 void deactivatePools() { 144 for (int i =0, n = _inUsePools.size(); i < n;) { 145 ObjectPool pool = (ObjectPool) _inUsePools.get(i++); 146 pool._user = null; 147 } 148 } 149 150 153 void activatePools() { 154 for (int i =0, n = _inUsePools.size(); i < n;) { 155 ObjectPool pool = (ObjectPool) _inUsePools.get(i++); 156 pool._user = _owner; 157 } 158 } 159 160 163 public void reset() { 164 for (int i =0, n = _inUsePools.size(); i < n;) { 165 ObjectPool pool = (ObjectPool) _inUsePools.get(i++); 166 pool.recycleAll(); 167 pool._user = null; 168 pool._inUse = false; 169 } 170 _inUsePools.clear(); 171 } 172 173 179 public String toString() { String str = (_isStack ? "StackPool@" : "HeapPool@") + hashCode() + ": "; 181 for (int i =0, n = ObjectFactory._Count; i < n; i++) { 182 ObjectPool pool = _pools[i]; 183 if (pool != null) { 184 str += ObjectFactory._Instances[i].getClass().getName() + 185 "(" + _pools[i].getSize() + ") "; 186 } 187 } 188 return str; 189 } 190 } 191 | Popular Tags |