001: package com.quadcap.sql.lock;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.util.Iterator;
042:
043: import com.quadcap.util.Debug;
044:
045: //#set defs(NOPOOLxx) 1
046: //#autogen begin
047: //#autogen end
048:
049: /**
050: *
051: *
052: * @author Stan Bailes
053: */
054: public class ObjectPool {
055: PooledObject proto;
056:
057: public ObjectPool(PooledObject proto) {
058: this .proto = proto;
059: }
060:
061: //#ifdef NOPOOL
062: //- public PooledObject get() {
063: //- PooledObject p = proto.create();
064: //- p.live = true;
065: //- return p;
066: //- }
067: //- public void release(PooledObject p) {
068: //- p.live = false;
069: //- }
070: //- public Iterator iterator() { return null; }
071: //#else
072: PooledObject[] pool;
073: int next = 0;
074:
075: Object lock = new Object();
076:
077: final void resizePool(int lim) {
078: PooledObject[] old = pool;
079: if (old == null || old.length != lim) {
080: pool = new PooledObject[lim];
081: int f = 0;
082: if (old != null) {
083: System.arraycopy(old, 0, pool, 0, Math.min(old.length,
084: lim));
085: f = old.length;
086: }
087: for (int i = f; i < lim; i++) {
088: pool[i] = proto.create();
089: pool[i].poolIndex = i;
090: }
091: }
092: }
093:
094: public PooledObject get() {
095: synchronized (lock) {
096: if (pool == null || next >= pool.length) {
097: resizePool(pool == null ? 8
098: : (pool.length + (pool.length >> 2)));
099: }
100: PooledObject obj = pool[next++];
101: if (obj.live)
102: throw new RuntimeException("Already live: " + obj);
103: obj.live = true;
104: return obj;
105: }
106: }
107:
108: public void release(PooledObject obj) {
109: synchronized (lock) {
110: if (!obj.live)
111: throw new RuntimeException("Not live: " + obj);
112: obj.live = false;
113: final int idx = obj.poolIndex;
114: if (pool[idx] != obj)
115: throw new RuntimeException("Pool check: " + obj);
116: final int last = --next;
117: if (idx != last) {
118: pool[idx] = pool[last];
119: pool[idx].poolIndex = idx;
120: pool[last] = obj;
121: obj.poolIndex = last;
122: }
123: }
124: }
125:
126: /**
127: * Return an Iterator that can be used to access all live objects
128: * in the pool.
129: */
130: public Iterator iterator() {
131: return new Iterator() {
132: int idx = 0;
133:
134: public boolean hasNext() {
135: return idx < next;
136: }
137:
138: public Object next() {
139: return idx < next ? pool[idx++] : null;
140: }
141:
142: public void remove() {
143: }
144: };
145: }
146:
147: //#ifdef DEBUG
148: public String toString() {
149: String s = getClass().getName();
150: int idx = s.lastIndexOf('.');
151: if (idx >= 0)
152: s = s.substring(idx + 1);
153: StringBuffer sb = new StringBuffer(s);
154: sb.append("(");
155: int len = s.length() + 1; // s + '['
156: for (int i = 0; i < next; i++) {
157: if (i > 0)
158: sb.append(',');
159: String p = pool[i].toString();
160: final int plen = p.length();
161: len += plen + 1;
162: if (len > 80) {
163: sb.append('\n');
164: len = plen;
165: }
166: sb.append(p);
167: }
168: sb.append(')');
169: return sb.toString();
170: }
171: //#endif
172: //#endif
173: }
|