/Users/lyon/j4p/src/classUtils/pack/util/pool2/DefaultPassivationManager.java
|
1 package classUtils.pack.util.pool2;
2
3 import java.util.HashMap;
4 import java.util.LinkedList;
5 import java.util.List;
6 import java.util.Map;
7
8 /**
9 * The default passivation manager.
10 * <p>
11 * This passivation manager:
12 * <ul>
13 * <li> requires that the object be <tt>Serializable</tt> for considering them
14 * passivable.
15 * <li> uses the {@link Configuration Configuration} {@link Configuration#getStorageDirectory()
16 * storage directory} to store passivated instances.
17 * <li> passivates instances when they have been inactive for over one minute.
18 * </ul>
19 * <p>
20 *
21 * @author Cristiano Sadun
22 */
23 public class DefaultPassivationManager extends BasePassivationManager {
24
25 private static DefaultPassivationManager instance;
26 private static long defaultPassivationThreshold=36000L;
27
28 private class PooledObjectState {
29 private long inserted;
30 private long lastAccessed;
31
32 public PooledObjectState(boolean acquired, long inserted) {
33 this.inserted=inserted;
34 this.lastAccessed=inserted;
35 }
36 }
37
38 /**
39 * A Map to Maps of PooledObjects to States.
40 * There exists a map for each monitored pool, mapping PooledObjects to PooledObjectStates.
41 */
42 private Map statesMap = new HashMap();
43
44 private long passivationThreshold;
45
46 /**
47 * Constructor for DefaultPassivationManager.
48 * @param name
49 */
50 public DefaultPassivationManager() {
51 super("Default passivation thread");
52 commonInit();
53 }
54
55
56 /**
57 * Constructor for DefaultPassivationManager.
58 * @param name
59 */
60 public DefaultPassivationManager(String name) {
61 super(name);
62 commonInit();
63 }
64
65 /**
66 * Constructor for DefaultPassivationManager.
67 * @param name
68 */
69 public DefaultPassivationManager(String name, int sleepTime) {
70 super(name, sleepTime);
71 commonInit();
72 }
73
74 /**
75 * Constructor for DefaultPassivationManager.
76 * @param pool
77 * @param name
78 * @param sleepTime
79 */
80 public DefaultPassivationManager(
81 ObjectPool pool,
82 String name,
83 int sleepTime) {
84 super(pool, name, sleepTime);
85 commonInit();
86 }
87
88 /**
89 * Method commonInit.
90 */
91 private void commonInit() {
92 this.passivationThreshold=getDefaultPassivationThreshold();
93 }
94
95 protected void createState(ObjectPool pool, PooledObject obj) {
96 Map poolStatesMap = (Map)statesMap.get(pool);
97 if (poolStatesMap == null) {
98 poolStatesMap =new HashMap();
99 statesMap.put(pool, poolStatesMap);
100 }
101 poolStatesMap.put(obj, new PooledObjectState(pool.isAcquired(obj), System.currentTimeMillis()));
102 }
103
104 /**
105 * @see classUtils.pack.util.pool2.BasePassivationManager#removeState(ObjectPool, PooledObject)
106 */
107 protected void removeState(ObjectPool pool, PooledObject obj) {
108 Map poolStatesMap = (Map)statesMap.get(pool);
109 if (poolStatesMap == null) return;
110 poolStatesMap.remove(obj);
111 }
112
113
114 /**
115 * @see classUtils.pack.util.pool2.BasePassivationManager#handleInvoked(classUtils.pack.util.pool2.ObjectPool, classUtils.pack.util.pool2.PooledObject)
116 */
117 protected void handleInvoked(ObjectPool pool, PooledObject obj) {
118 // Reset the lastAccessed field object of the invoked object
119 PooledObjectState state = (PooledObjectState)((Map)statesMap.get(pool)).get(obj);
120 state.lastAccessed=System.currentTimeMillis();
121 }
122
123 /**
124 * @see classUtils.pack.util.pool2.BasePassivationManager#isToPassivate(classUtils.pack.util.pool2.ObjectPool, classUtils.pack.util.pool2.PooledObject, java.lang.Object)
125 */
126 protected boolean isToPassivate(ObjectPool pool, PooledObject obj, Object preparationResult) {
127 PooledObjectState state = (PooledObjectState)((Map)statesMap.get(pool)).get(obj);
128 long currentTime=System.currentTimeMillis();
129 if (currentTime - state.lastAccessed > passivationThreshold) return true;
130 if (pool.getLogStream()!=null)
131 pool.getLogStream().println(obj+" accessed "+(currentTime - state.lastAccessed)+"ms ago, not to passivate");
132 return false;
133 }
134
135 /**
136 * @see classUtils.pack.util.pool2.BasePassivationManager#handleAcquired(ObjectPool, PooledObject)
137 */
138 protected void handleAcquired(ObjectPool pool, PooledObject obj) {
139 }
140
141 /**
142 * @see classUtils.pack.util.pool2.BasePassivationManager#handleReleased(ObjectPool, PooledObject)
143 */
144 protected void handleReleased(ObjectPool pool, PooledObject obj) {
145 }
146
147 /**
148 * @see classUtils.pack.util.pool2.BasePassivationManager#prepareForPassivationCheck(ObjectPool)
149 */
150 protected Object prepareForPassivationCheck(ObjectPool pool) {
151 return null;
152 }
153
154 /**
155 * Returns the instance.
156 * @return DefaultPassivationManager
157 */
158 public static DefaultPassivationManager getInstance() {
159 if (instance==null) instance=new DefaultPassivationManager();
160 return instance;
161 }
162
163 /**
164 * Returns the passivation threshold.
165 * @return long
166 */
167 public long getPassivationThreshold() {
168 return passivationThreshold;
169 }
170
171 /**
172 * Sets the passivation threshold.
173 * @param passivationThreshold The passivationThreshold to set
174 */
175 public void setPassivationThreshold(long passivationThreshold) {
176 this.passivationThreshold = passivationThreshold;
177 }
178
179 /**
180 * Returns the defaultPassivationThreshold.
181 * @return long
182 */
183 public static long getDefaultPassivationThreshold() {
184 return defaultPassivationThreshold;
185 }
186
187 /**
188 * Sets the defaultPassivationThreshold.
189 * @param defaultPassivationThreshold The defaultPassivationThreshold to set
190 */
191 public static void setDefaultPassivationThreshold(long defaultPassivationThreshold) {
192 DefaultPassivationManager.defaultPassivationThreshold =
193 defaultPassivationThreshold;
194 }
195
196 }
197