blob: 97b4b0937cc83a397617d590778594bab74f5685 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package com.sun.jndi.ldap.pool;
27
28/**
29 * Represents a description of PooledConnection in Connections.
30 * Contains a PooledConnection, its state (busy, idle, expired), and idle time.
31 *
32 * Any access or update to a descriptor's state is synchronized.
33 *
34 * @author Rosanna Lee
35 */
36final class ConnectionDesc {
37 private final static boolean debug = Pool.debug;
38
39 // Package private because used by Pool.showStats()
40 static final byte BUSY = (byte)0;
41 static final byte IDLE = (byte)1;
42 static final byte EXPIRED = (byte)2;
43
44 final private PooledConnection conn;
45
46 private byte state = IDLE; // initial state
47 private long idleSince;
48 private long useCount = 0; // for stats & debugging only
49
50 ConnectionDesc(PooledConnection conn) {
51 this.conn = conn;
52 }
53
54 ConnectionDesc(PooledConnection conn, boolean use) {
55 this.conn = conn;
56 if (use) {
57 state = BUSY;
58 ++useCount;
59 }
60 }
61
62 /**
63 * Two desc are equal if their PooledConnections are the same.
64 * This is useful when searching for a ConnectionDesc using only its
65 * PooledConnection.
66 */
67 public boolean equals(Object obj) {
68 return obj != null
69 && obj instanceof ConnectionDesc
70 && ((ConnectionDesc)obj).conn == conn;
71 }
72
73 /**
74 * Hashcode is that of PooledConnection to facilitate
75 * searching for a ConnectionDesc using only its PooledConnection.
76 */
77 public int hashCode() {
78 return conn.hashCode();
79 }
80
81 /**
82 * Changes the state of a ConnectionDesc from BUSY to IDLE and
83 * records the current time so that we will know how long it has been idle.
84 * @return true if state change occurred.
85 */
86 synchronized boolean release() {
87 d("release()");
88 if (state == BUSY) {
89 state = IDLE;
90
91 idleSince = System.currentTimeMillis();
92 return true; // Connection released, ready for reuse
93 } else {
94 return false; // Connection wasn't busy to begin with
95 }
96 }
97
98 /**
99 * If ConnectionDesc is IDLE, change its state to BUSY and return
100 * its connection.
101 *
102 * @return ConnectionDesc's PooledConnection if it was idle; null otherwise.
103 */
104 synchronized PooledConnection tryUse() {
105 d("tryUse()");
106
107 if (state == IDLE) {
108 state = BUSY;
109 ++useCount;
110 return conn;
111 }
112
113 return null;
114 }
115
116 /**
117 * If ConnectionDesc is IDLE and has expired, close the corresponding
118 * PooledConnection.
119 *
120 * @param threshold a connection that has been idle before this time
121 * have expired.
122 *
123 * @return true if entry is idle and has expired; false otherwise.
124 */
125 synchronized boolean expire(long threshold) {
126 if (state == IDLE && idleSince < threshold) {
127
128 d("expire(): expired");
129
130 state = EXPIRED;
131 conn.closeConnection(); // Close real connection
132
133 return true; // Expiration successful
134 } else {
135 d("expire(): not expired");
136 return false; // Expiration did not occur
137 }
138 }
139
140 public String toString() {
141 return conn.toString() + " " +
142 (state == BUSY ? "busy" : (state == IDLE ? "idle" : "expired"));
143 }
144
145 // Used by Pool.showStats()
146 int getState() {
147 return state;
148 }
149
150 // Used by Pool.showStats()
151 long getUseCount() {
152 return useCount;
153 }
154
155 private void d(String msg) {
156 if (debug) {
157 System.err.println("ConnectionDesc." + msg + " " + toString());
158 }
159 }
160}