KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > util > InstancePool


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.util;
19
20
21
22
23 public class InstancePool {
24     // -----------------------------------------------------------------------
25
// inner classes
26
// -----------------------------------------------------------------------
27

28     private static class Entry {
29         Object JavaDoc object;
30         long timeout;
31         Entry next;
32     }
33
34     // -----------------------------------------------------------------------
35
// private data
36
// -----------------------------------------------------------------------
37

38     private Entry _stack = null;
39
40     private Entry _freeList = null;
41
42     private long _idleTimeout;
43
44     // -----------------------------------------------------------------------
45
// public methods
46
// -----------------------------------------------------------------------
47

48     public InstancePool(String JavaDoc name) {
49         init(name, 0); //, null);
50
}
51
52     public InstancePool(String JavaDoc name, long idleTimeout) // , TimeoutObject timeoutObject)
53
{
54         init(name, idleTimeout); //, timeoutObject);
55
}
56
57     public Object JavaDoc get() {
58         synchronized (this) {
59             Entry top = _stack;
60             if (top != null) {
61                 _stack = top.next;
62                 //_size.decrement();
63
Object JavaDoc object = top.object;
64                 top.object = null;
65                 top.next = _freeList;
66                 _freeList = top;
67                 return object;
68             } else {
69                 return null;
70             }
71         }
72     }
73
74     public void put(Object JavaDoc object) {
75         synchronized (this) {
76             //_size.increment();
77
Entry top = _freeList;
78             if (top != null) {
79                 _freeList = top.next;
80             } else {
81                 top = new Entry();
82             }
83             top.object = object;
84             if (_idleTimeout > 0) {
85                 top.timeout = System.currentTimeMillis() + _idleTimeout;
86             }
87             top.next = _stack;
88             _stack = top;
89         }
90     }
91
92     // -----------------------------------------------------------------------
93
// private methods
94
// -----------------------------------------------------------------------
95

96     private void init(final String JavaDoc name, long idleTimeout) //, final TimeoutObject timeoutObject)
97
{
98         //_size = sizeStatistic.getInstance(name);
99
_idleTimeout = idleTimeout;
100
101         if (_idleTimeout > 0) {
102             /*
103             long now = SystemClock.getLastSampleTime();
104             final long checkInterval = _idleTimeout > 10 ? (_idleTimeout / 10) : _idleTimeout;
105             Task timeoutTask = new Task()
106             {
107                 public long run(long time)
108                 {
109                     Entry restoreStack = null;
110                     Entry timeoutChain = null;
111                     synchronized (InstancePool.this)
112                     {
113                         while (_stack != null)
114                         {
115                             Entry entry = _stack;
116                             _stack = entry.next;
117                             if (entry.timeout > time)
118                             {
119                                 entry.next = restoreStack;
120                                 restoreStack = entry;
121                             }
122                             else
123                             {
124                                 entry.next = timeoutChain;
125                                 timeoutChain = entry;
126                                 _size.decrement();
127                             }
128                         }
129
130                         // Restore still-active entries to the stack in
131                         // their original order. This ensures that less
132                         // frequently used entries stay at the bottom of
133                         // the stack, becoming elegible for timeout.
134                         while (restoreStack != null)
135                         {
136                             Entry entry = restoreStack;
137                             restoreStack = entry.next;
138                             entry.next = _stack;
139                             _stack = entry;
140                         }
141                     }
142                     while (timeoutChain != null)
143                     {
144                         Entry entry = timeoutChain;
145                         timeoutChain = entry.next;
146                         try
147                         {
148                             timeoutObject.onTimeout(entry.object);
149                         }
150                         catch (Throwable ex)
151                         {
152                             ExceptionLog.getInstance().log(ex, InstancePool.class.getName() + ".onTimeout(" + name + ")");
153                         }
154                         entry.object = null;
155                         entry.next = null;
156                     }
157                     return time + checkInterval;
158                 }
159             }
160             ;
161             TaskScheduler.getInstance().start(timeoutTask, now);
162             */

163         }
164     }
165 }
166
Popular Tags