feat(jdk8): move files to new folder to avoid resources compiled.

This commit is contained in:
2025-09-07 15:25:52 +08:00
parent 3f0047bf6f
commit 8c35cfb1c0
17415 changed files with 217 additions and 213 deletions

View File

@@ -0,0 +1,160 @@
/*
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jndi.ldap.pool;
/**
* Represents a description of PooledConnection in Connections.
* Contains a PooledConnection, its state (busy, idle, expired), and idle time.
*
* Any access or update to a descriptor's state is synchronized.
*
* @author Rosanna Lee
*/
final class ConnectionDesc {
private final static boolean debug = Pool.debug;
// Package private because used by Pool.showStats()
static final byte BUSY = (byte)0;
static final byte IDLE = (byte)1;
static final byte EXPIRED = (byte)2;
final private PooledConnection conn;
private byte state = IDLE; // initial state
private long idleSince;
private long useCount = 0; // for stats & debugging only
ConnectionDesc(PooledConnection conn) {
this.conn = conn;
}
ConnectionDesc(PooledConnection conn, boolean use) {
this.conn = conn;
if (use) {
state = BUSY;
++useCount;
}
}
/**
* Two desc are equal if their PooledConnections are the same.
* This is useful when searching for a ConnectionDesc using only its
* PooledConnection.
*/
public boolean equals(Object obj) {
return obj != null
&& obj instanceof ConnectionDesc
&& ((ConnectionDesc)obj).conn == conn;
}
/**
* Hashcode is that of PooledConnection to facilitate
* searching for a ConnectionDesc using only its PooledConnection.
*/
public int hashCode() {
return conn.hashCode();
}
/**
* Changes the state of a ConnectionDesc from BUSY to IDLE and
* records the current time so that we will know how long it has been idle.
* @return true if state change occurred.
*/
synchronized boolean release() {
d("release()");
if (state == BUSY) {
state = IDLE;
idleSince = System.currentTimeMillis();
return true; // Connection released, ready for reuse
} else {
return false; // Connection wasn't busy to begin with
}
}
/**
* If ConnectionDesc is IDLE, change its state to BUSY and return
* its connection.
*
* @return ConnectionDesc's PooledConnection if it was idle; null otherwise.
*/
synchronized PooledConnection tryUse() {
d("tryUse()");
if (state == IDLE) {
state = BUSY;
++useCount;
return conn;
}
return null;
}
/**
* If ConnectionDesc is IDLE and has expired, close the corresponding
* PooledConnection.
*
* @param threshold a connection that has been idle before this time
* have expired.
*
* @return true if entry is idle and has expired; false otherwise.
*/
synchronized boolean expire(long threshold) {
if (state == IDLE && idleSince < threshold) {
d("expire(): expired");
state = EXPIRED;
conn.closeConnection(); // Close real connection
return true; // Expiration successful
} else {
d("expire(): not expired");
return false; // Expiration did not occur
}
}
public String toString() {
return conn.toString() + " " +
(state == BUSY ? "busy" : (state == IDLE ? "idle" : "expired"));
}
// Used by Pool.showStats()
int getState() {
return state;
}
// Used by Pool.showStats()
long getUseCount() {
return useCount;
}
private void d(String msg) {
if (debug) {
System.err.println("ConnectionDesc." + msg + " " + toString());
}
}
}

View File

@@ -0,0 +1,391 @@
/*
* Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jndi.ldap.pool;
import java.util.ArrayList; // JDK 1.2
import java.util.List;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import javax.naming.NamingException;
import javax.naming.InterruptedNamingException;
import javax.naming.CommunicationException;
/**
* Represents a list of PooledConnections (actually, ConnectionDescs) with the
* same pool id.
* The list starts out with an initial number of connections.
* Additional PooledConnections are created lazily upon demand.
* The list has a maximum size. When the number of connections
* reaches the maximum size, a request for a PooledConnection blocks until
* a connection is returned to the list. A maximum size of zero means that
* there is no maximum: connection creation will be attempted when
* no idle connection is available.
*
* The list may also have a preferred size. If the current list size
* is less than the preferred size, a request for a connection will result in
* a PooledConnection being created (even if an idle connection is available).
* If the current list size is greater than the preferred size,
* a connection being returned to the list will be closed and removed from
* the list. A preferred size of zero means that there is no preferred size:
* connections are created only when no idle connection is available and
* a connection being returned to the list is not closed. Regardless of the
* preferred size, connection creation always observes the maximum size:
* a connection won't be created if the list size is at or exceeds the
* maximum size.
*
* @author Rosanna Lee
*/
// Package private: accessed only by Pool
final class Connections implements PoolCallback {
private static final boolean debug = Pool.debug;
private static final boolean trace =
com.sun.jndi.ldap.LdapPoolManager.trace;
private static final int DEFAULT_SIZE = 10;
final private int maxSize;
final private int prefSize;
final private List<ConnectionDesc> conns;
private boolean closed = false; // Closed for business
private Reference<Object> ref; // maintains reference to id to prevent premature GC
/**
* @param id the identity (connection request) of the connections in the list
* @param initSize the number of connections to create initially
* @param prefSize the preferred size of the pool. The pool will try
* to maintain a pool of this size by creating and closing connections
* as needed.
* @param maxSize the maximum size of the pool. The pool will not exceed
* this size. If the pool is at this size, a request for a connection
* will block until an idle connection is released to the pool or
* when one is removed.
* @param factory The factory responsible for creating a connection
*/
Connections(Object id, int initSize, int prefSize, int maxSize,
PooledConnectionFactory factory) throws NamingException {
this.maxSize = maxSize;
if (maxSize > 0) {
// prefSize and initSize cannot exceed specified maxSize
this.prefSize = Math.min(prefSize, maxSize);
initSize = Math.min(initSize, maxSize);
} else {
this.prefSize = prefSize;
}
conns = new ArrayList<>(maxSize > 0 ? maxSize : DEFAULT_SIZE);
// Maintain soft ref to id so that this Connections' entry in
// Pool doesn't get GC'ed prematurely
ref = new SoftReference<>(id);
d("init size=", initSize);
d("max size=", maxSize);
d("preferred size=", prefSize);
// Create initial connections
PooledConnection conn;
for (int i = 0; i < initSize; i++) {
conn = factory.createPooledConnection(this);
td("Create ", conn ,factory);
conns.add(new ConnectionDesc(conn)); // Add new idle conn to pool
}
}
/**
* Retrieves a PooledConnection from this list of connections.
* Use an existing one if one is idle, or create one if the list's
* max size hasn't been reached. If max size has been reached, wait
* for a PooledConnection to be returned, or one to be removed (thus
* not reaching the max size any longer).
*
* @param timeout if > 0, msec to wait until connection is available
* @param factory creates the PooledConnection if one needs to be created
*
* @return A non-null PooledConnection
* @throws NamingException PooledConnection cannot be created, because this
* thread was interrupted while it waited for an available connection,
* or if it timed out while waiting, or the creation of a connection
* resulted in an error.
*/
synchronized PooledConnection get(long timeout,
PooledConnectionFactory factory) throws NamingException {
PooledConnection conn;
long start = (timeout > 0 ? System.currentTimeMillis() : 0);
long waittime = timeout;
d("get(): before");
while ((conn = getOrCreateConnection(factory)) == null) {
if (timeout > 0 && waittime <= 0) {
throw new CommunicationException(
"Timeout exceeded while waiting for a connection: " +
timeout + "ms");
}
try {
d("get(): waiting");
if (waittime > 0) {
wait(waittime); // Wait until one is released or removed
} else {
wait();
}
} catch (InterruptedException e) {
throw new InterruptedNamingException(
"Interrupted while waiting for a connection");
}
// Check whether we timed out
if (timeout > 0) {
long now = System.currentTimeMillis();
waittime = timeout - (now - start);
}
}
d("get(): after");
return conn;
}
/**
* Retrieves an idle connection from this list if one is available.
* If none is available, create a new one if maxSize hasn't been reached.
* If maxSize has been reached, return null.
* Always called from a synchronized method.
*/
private PooledConnection getOrCreateConnection(
PooledConnectionFactory factory) throws NamingException {
int size = conns.size(); // Current number of idle/nonidle conns
PooledConnection conn = null;
if (prefSize <= 0 || size >= prefSize) {
// If no prefSize specified, or list size already meets or
// exceeds prefSize, then first look for an idle connection
ConnectionDesc entry;
for (int i = 0; i < size; i++) {
entry = conns.get(i);
if ((conn = entry.tryUse()) != null) {
d("get(): use ", conn);
td("Use ", conn);
return conn;
}
}
}
// Check if list size already at maxSize specified
if (maxSize > 0 && size >= maxSize) {
return null; // List size is at limit; cannot create any more
}
conn = factory.createPooledConnection(this);
td("Create and use ", conn, factory);
conns.add(new ConnectionDesc(conn, true)); // Add new conn to pool
return conn;
}
/**
* Releases connection back into list.
* If the list size is below prefSize, the connection may be reused.
* If the list size exceeds prefSize, then the connection is closed
* and removed from the list.
*
* public because implemented as part of PoolCallback.
*/
public synchronized boolean releasePooledConnection(PooledConnection conn) {
ConnectionDesc entry;
int loc = conns.indexOf(entry=new ConnectionDesc(conn));
d("release(): ", conn);
if (loc >= 0) {
// Found entry
if (closed || (prefSize > 0 && conns.size() > prefSize)) {
// If list size exceeds prefSize, close connection
d("release(): closing ", conn);
td("Close ", conn);
// size must be >= 2 so don't worry about empty list
conns.remove(entry);
conn.closeConnection();
} else {
d("release(): release ", conn);
td("Release ", conn);
// Get ConnectionDesc from list to get correct state info
entry = conns.get(loc);
// Return connection to list, ready for reuse
entry.release();
}
notifyAll();
d("release(): notify");
return true;
} else {
return false;
}
}
/**
* Removes PooledConnection from list of connections.
* The closing of the connection is separate from this method.
* This method is called usually when the caller encouters an error
* when using the connection and wants it removed from the pool.
*
* @return true if conn removed; false if it was not in pool
*
* public because implemented as part of PoolCallback.
*/
public synchronized boolean removePooledConnection(PooledConnection conn) {
if (conns.remove(new ConnectionDesc(conn))) {
d("remove(): ", conn);
notifyAll();
d("remove(): notify");
td("Remove ", conn);
if (conns.isEmpty()) {
// Remove softref to make pool entry eligible for GC.
// Once ref has been removed, it cannot be reinstated.
ref = null;
}
return true;
} else {
d("remove(): not found ", conn);
return false;
}
}
/**
* Goes through all entries in list, removes and closes ones that have been
* idle before threshold.
*
* @param threshold an entry idle since this time has expired.
* @return true if no more connections in list
*/
boolean expire(long threshold) {
List<ConnectionDesc> clonedConns;
synchronized(this) {
clonedConns = new ArrayList<>(conns);
}
List<ConnectionDesc> expired = new ArrayList<>();
for (ConnectionDesc entry : clonedConns) {
d("expire(): ", entry);
if (entry.expire(threshold)) {
expired.add(entry);
td("expire(): Expired ", entry);
}
}
synchronized (this) {
conns.removeAll(expired);
// Don't need to call notify() because we're
// removing only idle connections. If there were
// idle connections, then there should be no waiters.
return conns.isEmpty(); // whether whole list has 'expired'
}
}
/**
* Called when this instance of Connections has been removed from Pool.
* This means that no one can get any pooled connections from this
* Connections any longer. Expire all idle connections as of 'now'
* and leave indicator so that any in-use connections will be closed upon
* their return.
*/
synchronized void close() {
expire(System.currentTimeMillis()); // Expire idle connections
closed = true; // Close in-use connections when they are returned
}
String getStats() {
int idle = 0;
int busy = 0;
int expired = 0;
long use = 0;
int len;
synchronized (this) {
len = conns.size();
ConnectionDesc entry;
for (int i = 0; i < len; i++) {
entry = conns.get(i);
use += entry.getUseCount();
switch (entry.getState()) {
case ConnectionDesc.BUSY:
++busy;
break;
case ConnectionDesc.IDLE:
++idle;
break;
case ConnectionDesc.EXPIRED:
++expired;
}
}
}
return "size=" + len + "; use=" + use + "; busy=" + busy
+ "; idle=" + idle + "; expired=" + expired;
}
private void d(String msg, Object o1) {
if (debug) {
d(msg + o1);
}
}
private void d(String msg, int i) {
if (debug) {
d(msg + i);
}
}
private void d(String msg) {
if (debug) {
System.err.println(this + "." + msg + "; size: " + conns.size());
}
}
private void td(String msg, Object o1, Object o2) {
if (trace) { // redo test to avoid object creation
td(msg + o1 + "[" + o2 + "]");
}
}
private void td(String msg, Object o1) {
if (trace) { // redo test to avoid object creation
td(msg + o1);
}
}
private void td(String msg) {
if (trace) {
System.err.println(msg);
}
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jndi.ldap.pool;
/**
* Is a reference to Connections that is stored in Pool.
* This is an intermediate object that is outside of the circular
* reference loop of
* com.sun.jndi.ldap.Connection <-> com.sun.jndi.ldap.LdapClient
* <-> com.sun.jndi.ldap.pool.Connections
*
* Because Connection is a daemon thread, it will keep LdapClient
* alive until LdapClient closes Connection. This will in turn
* keep Connections alive. So even when Connections is removed
* from (the WeakHashMap of) Pool, it won't be finalized.
* ConnectionsRef acts as Connections's finalizer.
*
* Without connection pooling, com.sun.jndi.ldap.LdapCtx's finalize()
* closes LdapClient, which in turn closes Connection.
* With connection pooling, ConnectionsRef's finalize() calls
* Connections.close(), which in turn will close all idle connections
* and mark Connections such that in-use connections will be closed
* when they are returned to the pool.
*/
final class ConnectionsRef {
final private Connections conns;
ConnectionsRef(Connections conns) {
this.conns = conns;
}
Connections getConnections() {
return conns;
}
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jndi.ldap.pool;
import java.lang.ref.WeakReference;
import java.lang.ref.ReferenceQueue;
/*
* This class defines a WeakReference to the ConnectionRef (the referent).
*
* The ConnectionRef enables to break the reference
* cycle between Connection, LdapClient, Connections and ConnectionDesc,
* shown in the figure below.
*
* -------> Connections -----> ConnectionDesc
* | ^ |
* | | |
* | | |
* ConnectionsRef LdapClient <------------
* ^ | ^
* : | |
* : v |
* ConnectionsWeakRef Connection
*
* The ConnectionsRef is for cleaning up the resources held by the
* Connection thread by making them available to the GC. The pool
* uses ConnectionRef to hold the pooled resources.
*
* This class in turn holds a WeakReference with a ReferenceQueue to the
* ConnectionRef to track when the ConnectionRef becomes ready
* for getting GC'ed. It extends from WeakReference in order to hold a
* reference to Connections used for closing (which in turn terminates
* the Connection thread) it by monitoring the ReferenceQueue.
*/
class ConnectionsWeakRef extends WeakReference<ConnectionsRef> {
private final Connections conns;
ConnectionsWeakRef (ConnectionsRef connsRef,
ReferenceQueue<? super ConnectionsRef> queue) {
super(connsRef, queue);
this.conns = connsRef.getConnections();
}
Connections getConnections() {
return conns;
}
}

View File

@@ -0,0 +1,254 @@
/*
* Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jndi.ldap.pool;
import java.util.ArrayList;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.io.PrintStream;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import javax.naming.NamingException;
/**
* A map of pool ids to Connections.
* Key is an object that uniquely identifies a PooledConnection request
* (typically information needed to create the connection).
* The definitions of the key's equals() and hashCode() methods are
* vital to its unique identification in a Pool.
*
* Value is a ConnectionsRef, which is a reference to Connections,
* a list of equivalent connections.
*
* Supports methods that
* - retrieves (or creates as necessary) a connection from the pool
* - removes expired connections from the pool
*
* Connections cleanup:
* A WeakHashMap is used for mapping the pool ids and Connections.
* A SoftReference from the value to the key is kept to hold the map
* entry as long as possible. This allows the GC to remove Connections
* from the Pool under situations of VM running out of resources.
* To take an appropriate action of 'closing the connections' before the GC
* reclaims the ConnectionsRef objects, the ConnectionsRef objects are made
* weakly reachable through a list of weak references registered with
* a reference queue.
* Upon an entry gets removed from the WeakHashMap, the ConnectionsRef (value
* in the map) object is weakly reachable. When another sweep of
* clearing the weak references is made by the GC it puts the corresponding
* ConnectionsWeakRef object into the reference queue.
* The reference queue is monitored lazily for reclaimable Connections
* whenever a pooled connection is requested or a call to remove the expired
* connections is made. The monitoring is done regularly when idle connection
* timeout is set as the PoolCleaner removes expired connections periodically.
* As determined by the experiements, cleanup of resources using the
* ReferenceQueue mechanism is reliable and has immidiate effect than the
* finalizer approach.
*
* @author Rosanna Lee
*/
final public class Pool {
static final boolean debug = com.sun.jndi.ldap.LdapPoolManager.debug;
/*
* Used for connections cleanup
*/
private static final ReferenceQueue<ConnectionsRef> queue =
new ReferenceQueue<>();
private static final Collection<Reference<ConnectionsRef>> weakRefs =
Collections.synchronizedList(new LinkedList<Reference<ConnectionsRef>>());
final private int maxSize; // max num of identical conn per pool
final private int prefSize; // preferred num of identical conn per pool
final private int initSize; // initial number of identical conn to create
final private Map<Object, ConnectionsRef> map;
public Pool(int initSize, int prefSize, int maxSize) {
map = new WeakHashMap<>();
this.prefSize = prefSize;
this.maxSize = maxSize;
this.initSize = initSize;
}
/**
* Gets a pooled connection for id. The pooled connection might be
* newly created, as governed by the maxSize and prefSize settings.
* If a pooled connection is unavailable and cannot be created due
* to the maxSize constraint, this call blocks until the constraint
* is removed or until 'timeout' ms has elapsed.
*
* @param id identity of the connection to get
* @param timeout the number of milliseconds to wait before giving up
* @param factory the factory to use for creating the connection if
* creation is necessary
* @return a pooled connection
* @throws NamingException the connection could not be created due to
* an error.
*/
public PooledConnection getPooledConnection(Object id, long timeout,
PooledConnectionFactory factory) throws NamingException {
d("get(): ", id);
if (debug) {
synchronized (map) {
d("size: ", map.size());
}
}
expungeStaleConnections();
Connections conns;
synchronized (map) {
conns = getConnections(id);
if (conns == null) {
d("get(): creating new connections list for ", id);
// No connections for this id so create a new list
conns = new Connections(id, initSize, prefSize, maxSize,
factory);
ConnectionsRef connsRef = new ConnectionsRef(conns);
map.put(id, connsRef);
// Create a weak reference to ConnectionsRef
Reference<ConnectionsRef> weakRef =
new ConnectionsWeakRef(connsRef, queue);
// Keep the weak reference through the element of a linked list
weakRefs.add(weakRef);
}
d("get(): size after: ", map.size());
}
return conns.get(timeout, factory); // get one connection from list
}
private Connections getConnections(Object id) {
ConnectionsRef ref = map.get(id);
return (ref != null) ? ref.getConnections() : null;
}
/**
* Goes through the connections in this Pool and expires ones that
* have been idle before 'threshold'. An expired connection is closed
* and then removed from the pool (removePooledConnection() will eventually
* be called, and the list of pools itself removed if it becomes empty).
*
* @param threshold connections idle before 'threshold' should be closed
* and removed.
*/
public void expire(long threshold) {
Collection<ConnectionsRef> copy;
synchronized (map) {
copy = new ArrayList<>(map.values());
}
ArrayList<ConnectionsRef> removed = new ArrayList<>();
Connections conns;
for (ConnectionsRef ref : copy) {
conns = ref.getConnections();
if (conns.expire(threshold)) {
d("expire(): removing ", conns);
removed.add(ref);
}
}
synchronized (map) {
map.values().removeAll(removed);
}
expungeStaleConnections();
}
/*
* Closes the connections contained in the ConnectionsRef object that
* is going to be reclaimed by the GC. Called by getPooledConnection()
* and expire() methods of this class.
*/
private static void expungeStaleConnections() {
ConnectionsWeakRef releaseRef = null;
while ((releaseRef = (ConnectionsWeakRef) queue.poll())
!= null) {
Connections conns = releaseRef.getConnections();
if (debug) {
System.err.println(
"weak reference cleanup: Closing Connections:" + conns);
}
// cleanup
conns.close();
weakRefs.remove(releaseRef);
releaseRef.clear();
}
}
public void showStats(PrintStream out) {
Object id;
Connections conns;
out.println("===== Pool start ======================");
out.println("maximum pool size: " + maxSize);
out.println("preferred pool size: " + prefSize);
out.println("initial pool size: " + initSize);
synchronized (map) {
out.println("current pool size: " + map.size());
for (Map.Entry<Object, ConnectionsRef> entry : map.entrySet()) {
id = entry.getKey();
conns = entry.getValue().getConnections();
out.println(" " + id + ":" + conns.getStats());
}
}
out.println("====== Pool end =====================");
}
public String toString() {
synchronized (map) {
return super.toString() + " " + map.toString();
}
}
private void d(String msg, int i) {
if (debug) {
System.err.println(this + "." + msg + i);
}
}
private void d(String msg, Object obj) {
if (debug) {
System.err.println(this + "." + msg + obj);
}
}
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jndi.ldap.pool;
/**
* Represents a callback used to release or remove a PooledConnection back
* into the pool.
*
* A pooled connection typically has a close method that its clients
* use to indicate that they no longer need the connection. This close
* method should use the methods defined in this interface to
* interact with the connection pool to return the connection
* to the pool.
*
* The methods in this interface are typically invoked by a PooledConnection.
* The methods in this interface are typically implemented by the connection
* pool manager.
*
* @author Rosanna Lee
*/
public interface PoolCallback {
/**
* Releases a useable connection back to the pool.
*
* @param conn The connection to release.
* @return true if the connection released; false if the connection
* is no longer in the pool.
*/
public abstract boolean releasePooledConnection(PooledConnection conn);
/**
* Removes a connection from the pool. The connection should not be reused.
* The physical connection should have already been closed.
*
* @param conn The connection to return.
* @return true if the connection was removed; false if the connection
* is no longer in the pool prior to removal.
*/
public abstract boolean removePooledConnection(PooledConnection conn);
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jndi.ldap.pool;
/**
* Thread that wakes up periodically and closes expired, unused connections.
*
* @author Rosanna Lee
*/
final public class PoolCleaner implements Runnable {
final private Pool[] pools;
final private long period;
/**
* @param period ms to wait between cleaning
* @param pools non-null array of Pools to clean
*/
public PoolCleaner(long period, Pool[] pools) {
super();
this.period = period;
this.pools = pools.clone();
}
@Override
public void run() {
long threshold;
while (true) {
synchronized (this) {
// Wait for duration of period ms
try {
wait(period);
} catch (InterruptedException ignore) {
}
// Connections idle since threshold have expired
threshold = System.currentTimeMillis() - period;
for (int i = 0; i < pools.length; i++) {
if (pools[i] != null) {
pools[i].expire(threshold);
}
}
}
}
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jndi.ldap.pool;
/**
* Represents a connection that is managed in a pool. The connection
* may be reused by multiple clients.
*
* A pooled connection typically has a close method that its clients
* use to indicate that they no longer need the connection. This close
* method would interact with the connection pool to return the connection
* to the pool (see PoolCallback).
*<p>
* The pooled connection also needs to provide a close method that the
* connection pool can use to physically close the connection.
* The pool might need to physically close the connection as determined
* by the pool's policy (for example, to manage the pool size or idle
* connections). This second close method should *not* use PoolCallback
* methods. It should only do what is required to close the physical
* connection.
*
* @author Rosanna Lee
*/
public interface PooledConnection {
/**
* Closes the physical connection.
*/
public abstract void closeConnection();
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jndi.ldap.pool;
import javax.naming.NamingException;
/**
* Represents a factory that creates PooledConnection.
*
* The user of the connection pool should provide an implementation of this
* interface and pass it to the Pool.getPooledConnection() method.
* The implementation of the factory should contain all the information
* necessary to create a PooledConnection.
*
* @author Rosanna Lee
*/
public interface PooledConnectionFactory {
/**
* Creates a pooled connection.
* @param pcb callback responsible for removing and releasing the pooled
* connection from the pool.
*/
public abstract PooledConnection createPooledConnection(PoolCallback pcb)
throws NamingException;
};