-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RC3 check-in. This includes improvements to connection pooling and a …
…few more functors to make writing configurations easier.
- Loading branch information
Showing
559 changed files
with
4,241 additions
and
9,872 deletions.
There are no files selected for viewing
160 changes: 87 additions & 73 deletions
160
...curity-common/ncsa-security-core/src/main/java/edu/uiuc/ncsa/security/core/util/Pool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,128 @@ | ||
package edu.uiuc.ncsa.security.core.util; | ||
|
||
|
||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* A pool of items, that is to say, a managed list that keeps valid items | ||
* on it and can create or destroy them as needed. when pushing and popping off a stack. | ||
* <p>Supremely useful with SQL connections. | ||
* <p>Supremely useful with SQL connections. This does check for validity as a matter | ||
* of course. | ||
* <p>Created by Jeff Gaynor<br> | ||
* on Mar 12, 2010 at 3:48:20 PM | ||
*/ | ||
public abstract class Pool<T> { | ||
int INFINITY = -1; | ||
int MAX_SIZE = 1000; | ||
public static final int INFINITE = -1; | ||
int maxSize = INFINITE; | ||
protected int inUse = 0; | ||
List<T> stack = new LinkedList<T>(); | ||
static int inUse = 0; | ||
|
||
/** | ||
* Create a new one. | ||
* | ||
* @return | ||
* Create a new, ready-to-use object for the pool | ||
* @return the object | ||
*/ | ||
public abstract T create(); | ||
public abstract T create() throws PoolException; | ||
|
||
/** | ||
* Destroy the given element. | ||
* | ||
* @param t | ||
* Destroy an object that is no longer needed. | ||
* @param object the object | ||
*/ | ||
public abstract void destroy(T t); | ||
|
||
public Pool(int maximumSize) { | ||
this.MAX_SIZE = maximumSize; | ||
} | ||
|
||
public Pool() { | ||
} | ||
|
||
static int maxStackSize = 0; | ||
public abstract void destroy(T object) throws PoolException; | ||
|
||
protected synchronized int maxStackSize() { | ||
if (maxStackSize < stack.size()) { | ||
maxStackSize = stack.size(); | ||
} | ||
return maxStackSize; | ||
/** | ||
* Is this item still good? If not it will be removed from the | ||
* pool so a new one can be created. Default is to return true. | ||
*/ | ||
public boolean isValid(T object) throws PoolException { | ||
return true; | ||
} | ||
|
||
static int stopValue = 0; | ||
|
||
public synchronized T pop() { | ||
T t = null; | ||
/** | ||
* Pop an object off the stack if ther eis one, otherwise, create one. | ||
* @return the object | ||
*/ | ||
public synchronized T pop() throws PoolException { | ||
try { | ||
if (!stack.isEmpty()) { | ||
t = stack.remove(0); | ||
T item = stack.remove(0); | ||
if(!isValid(item)){ | ||
return doCreate(); | ||
} | ||
} catch (IndexOutOfBoundsException | NoSuchElementException ix) { | ||
// do nothing. | ||
inUse++; | ||
return item; | ||
} catch(IndexOutOfBoundsException x) { // pool is empty | ||
return doCreate(); | ||
} catch(NoSuchElementException x) { // pool is empty | ||
return doCreate(); | ||
} | ||
if (t == null) { | ||
t = realCreate(); | ||
} | ||
inUse++; | ||
return t; | ||
} | ||
|
||
protected synchronized T realCreate() { | ||
if (MAX_SIZE == INFINITY || inUse < MAX_SIZE) { | ||
T t = create(); | ||
maxStackSize(); | ||
return t; | ||
} | ||
throw new PoolException("Error: Maximum capacity of " + MAX_SIZE + " elements has been exceeded"); | ||
public synchronized T doCreate() throws PoolException { | ||
// this is only called if stack.size()==0 | ||
if(maxSize == INFINITE || inUse < maxSize) { | ||
T item = create(); | ||
inUse++; | ||
return item; | ||
} else { | ||
throw new PoolException("pool at capacity: "+inUse+" item(s) checked out. " + stack.size() + " items in stack."); | ||
} | ||
} | ||
|
||
public synchronized void realDestroy(T t) { | ||
destroy(t); | ||
inUse--; | ||
|
||
/** | ||
* Destroying an item reduces the number of in-use items. | ||
* Do not call this on an item that hasn't been checked out. | ||
* @param item the item | ||
* @throws PoolException | ||
*/ | ||
public synchronized void doDestroy(T item) throws PoolException { | ||
destroy(item); | ||
inUse--; | ||
} | ||
|
||
public synchronized void push(T t) { | ||
if (MAX_SIZE <= stack.size()) { | ||
realDestroy(t); | ||
return; | ||
} | ||
if (stack.contains(t)) { | ||
throw new PoolException("Error: This element has already been checked into the pool"); | ||
} | ||
stack.add(0, t); | ||
inUse--; | ||
|
||
/** | ||
* Set the maximum number of items. | ||
* @param c | ||
*/ | ||
public void setMaxSize(int c) { | ||
maxSize = c; | ||
} | ||
/** | ||
* Get the maximum number of items | ||
* @return capacity | ||
*/ | ||
public int getMaxSize() { | ||
return maxSize; | ||
} | ||
|
||
|
||
/** | ||
* Remove and destroy all elements on stack. | ||
* Check an object into the pool. If the pool is at capacity, | ||
* destroy the object. | ||
* @param object the object | ||
*/ | ||
public synchronized boolean clear() { | ||
boolean ok = true; | ||
while (!stack.isEmpty()) { | ||
public synchronized void push(T object) throws PoolException { | ||
if((maxSize != INFINITE && stack.size() >= maxSize) || !isValid(object)) { | ||
doDestroy(object); | ||
} else if(stack.contains(object)) { | ||
throw new PoolException("can't check in object more than once: " + object); | ||
} else { | ||
stack.add(0, object); | ||
inUse--; | ||
} | ||
} | ||
|
||
public synchronized boolean destroyAll() { | ||
boolean success = true; | ||
Iterator<T> it = stack.iterator(); | ||
while(it.hasNext()) { | ||
try { | ||
destroy(pop()); | ||
inUse--; | ||
} catch (Exception x) { | ||
ok = false; | ||
// rock on. | ||
doDestroy(it.next()); | ||
} catch(PoolException x) { | ||
success = false; | ||
} | ||
it.remove(); | ||
} | ||
return ok; | ||
return success; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.