KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > context > LocalPools


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2006 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

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 /**
18  * <p> This class represents the thread-local pools.</p>
19  *
20  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
21  * @version 4.2, December 14, 2006
22  */

23 final class LocalPools {
24
25     /**
26      * Holds the XML format for pools (mapping between the factory class and
27      * the size of the pool).
28      */

29     final static XMLFormat XML = new XMLFormat(
30             Javolution.j2meGetClass("javolution.context.LocalPools")) {
31         public Object JavaDoc newInstance(Class JavaDoc cls, InputElement xml) throws XMLStreamException {
32             return new LocalPools(xml.getAttribute("isStack", false));
33         }
34
35         public void read(InputElement xml, Object JavaDoc obj) throws XMLStreamException {
36             LocalPools localPools = (LocalPools)obj;
37             while (xml.hasNext()) {
38                 Class JavaDoc factoryClass = (Class JavaDoc) xml.get("Factory", CLASS_CLASS);
39                 int size = ((Integer JavaDoc)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 JavaDoc 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 JavaDoc(pool.getSize()), "PoolSize", INTEGER_CLASS);
56                 }
57             }
58         }
59     };
60     private static final Class JavaDoc INTEGER_CLASS = new Integer JavaDoc(0).getClass();
61     private static final Class JavaDoc CLASS_CLASS = "".getClass().getClass();
62     
63     /**
64      * Holds direct mapping from factory index to pool.
65      */

66     private final ObjectPool[] _pools = new ObjectPool[ObjectFactory._Instances.length];
67
68     /**
69      * Holds the pools currently in use (might not be the current pools if owner
70      * has entered an inner pool/heap context).
71      */

72     private final FastTable _inUsePools = new FastTable();
73
74     /**
75      * Holds the owner of this pool (unique) <code>null</code> if no owner yet
76      * (e.g. deserialized).
77      */

78     Thread JavaDoc _owner;
79
80     /**
81      * Indicates if stack or heap pool.
82      */

83     private final boolean _isStack;
84
85     /**
86      * Creates a new pool.
87      *
88      * @param isStack <code>true</code> if stack pool; <code>false</code>
89      * if heap pool.
90      */

91     LocalPools( boolean isStack) {
92         _isStack = isStack;
93     }
94
95     /**
96      * Returns the pool form the specified factory.
97      *
98      * @param factory the factory of the pool to return.
99      * @param activate <code>true</code> if this pool is the current pool
100      * for the thread owner; <code>false</code> otherwise.
101      * @return the pool for the specified factory.
102      */

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 JavaDoc() {
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) { // Marks it in use.
116
pool._inUse = true;
117             _inUsePools.add(pool);
118         }
119         if (activate) {
120             pool._user = _owner;
121         }
122         return pool;
123     }
124
125     /**
126      * Clears all pools.
127      */

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     /**
141      * Sets the current user of this pool.
142      */

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     /**
151      * Sets the current user of this pool.
152      */

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     /**
161      * Recycles all the pools which have been used and deactivates them.
162      */

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     /**
174      * Returns the string representation of the pools (e.g. size) for
175      * debugging purpose.
176      *
177      * @return the size of all pools.
178      */

179     public String JavaDoc toString() { // For debugging.
180
String JavaDoc 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