You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ge...@apache.org on 2003/08/10 22:43:58 UTC
cvs commit: incubator-geronimo/modules/core/src/java/org/apache/geronimo/cache InstanceCache.java InstanceFactory.java InstancePool.java LRUInstanceCache.java LRURunner.java SimpleInstanceCache.java SimpleInstancePool.java
geirm 2003/08/10 13:43:58
Added: modules/core/src/java/org/apache/geronimo/cache
InstanceCache.java InstanceFactory.java
InstancePool.java LRUInstanceCache.java
LRURunner.java SimpleInstanceCache.java
SimpleInstancePool.java
Log:
initial import
Revision Changes Path
1.1 incubator-geronimo/modules/core/src/java/org/apache/geronimo/cache/InstanceCache.java
Index: InstanceCache.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Geronimo" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Geronimo", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
package org.apache.geronimo.cache;
/**
*
* @version $Revision: 1.1 $ $Date: 2003/08/10 20:43:58 $
*/
public interface InstanceCache {
Object get(Object key) throws Exception;
Object peek(Object key);
void putActive(Object key, Object value);
void putInactive(Object key, Object value);
Object remove(Object key);
boolean isActive(Object key);
}
1.1 incubator-geronimo/modules/core/src/java/org/apache/geronimo/cache/InstanceFactory.java
Index: InstanceFactory.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Geronimo" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Geronimo", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
package org.apache.geronimo.cache;
/**
*
*
* @version $Revision: 1.1 $ $Date: 2003/08/10 20:43:58 $
*/
public interface InstanceFactory {
Object createInstance() throws Exception;
}
1.1 incubator-geronimo/modules/core/src/java/org/apache/geronimo/cache/InstancePool.java
Index: InstancePool.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Geronimo" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Geronimo", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
package org.apache.geronimo.cache;
/**
* Instance pool holds a number if instances that can be borrowed from the pool.
* Once an instance has been acquired it must be returned to the pool by calling
* release, which puts the instance back into the pool, or by calling remove,
* which marks the instance as no longer used but does not put it back in the pool.
* An implementation will have to refill the pool as entries are removed.
*
*
* @version $Revision: 1.1 $ $Date: 2003/08/10 20:43:58 $
*/
public interface InstancePool {
/**
* Get an instance from the pool. This method may block indefinately if the pool has a
* strict limit.
*
* @return an instance
* @throws InterruptedException if pool is using hard limits and thread was interrupted
* while waiting for an instance to become available
* @throws Exception if object demand object construction was required and
* an error occured during construction
*/
Object acquire() throws InterruptedException, Exception;
/**
* Releases the hold on the instance. This method may or may not reinsert the instance
* into the pool. This method can not block.
*
* @param instance the instance to return to the pool
* @return true is the instance was reinserted into the pool.
*/
boolean release(Object instance);
/**
* Drop an instance permanently from the pool. The instance will never be used again.
* This method can not block.
*
* @param instance the instance to discard
*/
void remove(Object instance);
}
1.1 incubator-geronimo/modules/core/src/java/org/apache/geronimo/cache/LRUInstanceCache.java
Index: LRUInstanceCache.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Geronimo" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Geronimo", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
package org.apache.geronimo.cache;
import java.util.HashMap;
/**
* This is a very simple implementation of InstanceCache designed for raw flat
* out speed. It does not directly support passivation or have any storage
* limits.
*
*
* @version $Revision: 1.1 $ $Date: 2003/08/10 20:43:58 $
*/
public final class LRUInstanceCache implements InstanceCache {
private final HashMap active = new HashMap();
private final HashMap inactive = new HashMap();
private final Entry header = new Entry();
public synchronized int size() {
return active.size() + inactive.size();
}
public synchronized void putActive(Object key, Object value) {
//assert (key != null);
//assert (value != null);
// if it is in the inactive list remove it and add it to the active list
Entry entry = (Entry) inactive.remove(key);
if (entry != null) {
// if we have an entry we remove it from the linked list
entry.remove();
}
// now stick it in the active map
active.put(key, value);
}
public synchronized void putInactive(Object key, Object value) {
//assert (key != null);
//assert (value != null);
// remove it from the active list if it was there
active.remove(key);
// create a new link entry, put it in the hashTable and add it to the linked list
Entry entry = new Entry(key, value);
inactive.put(key, entry);
header.addAfter(entry);
}
public synchronized Object get(Object key) {
//assert (key != null);
Object value = active.get(key);
if (value != null) {
return value;
}
// if it is in the inactive list remove it and add it to the active list
Entry entry = (Entry) inactive.remove(key);
if (entry != null) {
// if we have an entry we need to unwrap it
value = entry.getValue();
// remove the entry from the list
entry.remove();
// now put it in the active map
active.put(key, value);
}
return value;
}
public synchronized Object remove(Object key) {
//assert (key != null);
// first check the active map
Object value = active.remove(key);
// also check for an entry in the inactive map
Entry entry = (Entry) inactive.remove(key);
if (entry != null) {
// this should never happen because we don't let a key be in both maps
//assert (value == null);
// unwrap the entry and remove it from thhe linked list
value = entry.getValue();
entry.remove();
}
return value;
}
public synchronized Object peek(Object key) {
//assert (key != null);
// first check the active map
Object value = active.get(key);
if (value != null) {
return value;
}
// wasn't there - check for an entry in the inactive map
Entry entry = (Entry) inactive.get(key);
if (entry != null) {
// unwrap the entry
return entry.getValue();
}
return null;
}
public synchronized boolean isActive(Object key) {
//assert (key != null);
return active.containsKey(key);
}
public void run(LRURunner runner) {
Entry entry = header;
Object key;
Object value;
while (runner.shouldContinue()) {
synchronized (this) {
do {
// even though this entry may have been removed from the list in
// the previous iteration, it maintains a link to the previous node
// in the hierarchy, so it can alyways find its way back
entry = entry.getPrevious();
// we only allow the runner to walk the list once
if (entry == header) {
return;
}
key = entry.getKey();
value = entry.getValue();
} while (entry.isRemoved() || !runner.shouldRemove(key, value));
// remove the entry from the map
inactive.remove(key);
// remove the entry from the list
entry.remove();
}
runner.remove(key, value);
}
}
private static final class Entry {
private Entry next;
private Entry previous;
private Object key;
private Object value;
private boolean removed = false;
public Entry() {
next = this;
previous = this;
}
public Entry(Object key, Object value) {
//assert (key!=null);
//assert (value!=null);
this.key = key;
this.value = value;
}
public Entry getPrevious() {
return previous;
}
public Entry getNext() {
return next;
}
public Object getKey() {
return key;
}
public Object getValue() {
return value;
}
public boolean isRemoved() {
return removed;
}
public void addAfter(Entry entry) {
//assert (!removed);
//assert (!entry.removed);
//assert (entry.next == null);
//assert (entry.previous == null);
// update the entry's pointers
entry.previous = this;
entry.next = next;
// update my next's pointers
next.previous = entry;
// update my pointers
next = entry;
}
public void addBefore(Entry entry) {
//assert (!removed);
//assert (!entry.removed);
//assert (entry.next == null);
//assert (entry.previous == null);
// update the entry's pointers
entry.next = this;
entry.previous = previous;
// update my previous's pointer
previous.next = entry;
// update my pointer
previous = entry;
}
public void remove() {
if (!removed) {
previous.next = next;
next.previous = previous;
next = null;
// leave previous pointer so we can find our way back into the list
key = null;
value = null;
removed = true;
}
}
}
}
1.1 incubator-geronimo/modules/core/src/java/org/apache/geronimo/cache/LRURunner.java
Index: LRURunner.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Geronimo" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Geronimo", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
package org.apache.geronimo.cache;
/**
*
*
* @version $Revision: 1.1 $ $Date: 2003/08/10 20:43:58 $
*/
public interface LRURunner {
boolean shouldContinue();
boolean shouldRemove(Object key, Object value);
void remove(Object key, Object value);
}
1.1 incubator-geronimo/modules/core/src/java/org/apache/geronimo/cache/SimpleInstanceCache.java
Index: SimpleInstanceCache.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Geronimo" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Geronimo", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
package org.apache.geronimo.cache;
import java.util.HashMap;
/**
* This is a very simple implementation of InstanceCache designed for raw flat
* out speed. It does not directly support passivation or have any storage
* limits.
*
*
* @version $Revision: 1.1 $ $Date: 2003/08/10 20:43:58 $
*/
public final class SimpleInstanceCache implements InstanceCache {
private HashMap active = new HashMap();
private HashMap inactive = new HashMap();
public synchronized void putActive(Object key, Object value) {
inactive.remove(key);
active.put(key, value);
}
public synchronized void putInactive(Object key, Object value) {
active.remove(key);
inactive.put(key, value);
}
public synchronized Object get(Object key) {
Object value = active.get(key);
if (value != null) {
return value;
}
// if it is in the inactive list remove it and add it to the active list
value = inactive.remove(key);
if (value != null) {
active.put(key, value);
}
return value;
}
public synchronized Object remove(Object key) {
// first check the active map
Object value = active.remove(key);
// also check for an entry in the inactive map
if (value == null) {
value = inactive.remove(key);
} else {
// this should never happen because we don't let a key be in both maps
// assert (inactive.remove(key) == null);
}
return value;
}
public synchronized Object peek(Object key) {
Object value = active.get(key);
if (value != null) {
return value;
}
return inactive.get(key);
}
public synchronized boolean isActive(Object key) {
return active.containsKey(key);
}
}
1.1 incubator-geronimo/modules/core/src/java/org/apache/geronimo/cache/SimpleInstancePool.java
Index: SimpleInstancePool.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Geronimo" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Geronimo", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
package org.apache.geronimo.cache;
import java.util.LinkedList;
import java.util.List;
import EDU.oswego.cs.dl.util.concurrent.Semaphore;
import EDU.oswego.cs.dl.util.concurrent.WaiterPreferenceSemaphore;
/**
*
*
* @version $Revision: 1.1 $ $Date: 2003/08/10 20:43:58 $
*/
public final class SimpleInstancePool implements InstancePool {
private LinkedList pool;
private int allocated;
private Semaphore semaphore;
private InstanceFactory factory;
private int maxSize;
private boolean hardLimit;
public SimpleInstancePool(final InstanceFactory factory, final int maxSize, final boolean hardLimit) {
this.factory = factory;
this.maxSize = maxSize;
this.hardLimit = hardLimit;
pool = new LinkedList();
if (hardLimit) {
semaphore = new WaiterPreferenceSemaphore(maxSize);
}
}
public void fill() throws Exception {
synchronized (this) {
while (pool != null && allocated + pool.size() < maxSize) {
Object instance = factory.createInstance();
pool.addFirst(instance);
}
}
}
public Object acquire() throws Exception {
// if we are using hard limits we need to acquire a permit
if (hardLimit) {
semaphore.acquire();
}
// get the instance from the pool is possible
Object instance = null;
synchronized (this) {
allocated++;
// if we have not stopped pooling and there is one in the pool, use it
if (pool != null && !pool.isEmpty()) {
instance = pool.removeFirst();
}
}
// didn't get an instance? create a new one
if (instance == null) {
instance = factory.createInstance();
}
return instance;
}
public boolean release(Object instance) {
boolean reinserted = false;
synchronized (this) {
// if we have not stopped pooling and we are under the limit put it back in the pool
if (pool != null && allocated + pool.size() < maxSize) {
pool.addFirst(instance);
reinserted = true;
}
allocated--;
}
// if we are using hard limits we need to release our permit
if (hardLimit) {
semaphore.release();
}
return reinserted;
}
public void remove(Object instance) {
instance = null;
// Create a new one... You have done nothing good for the pool, so at least try to
// create a replacement instance for the one you broke
// Do this outside the synchronized block because the factory can take a long time.
try {
instance = factory.createInstance();
} catch (Exception ignored) {
// well that didn't work either
}
synchronized (this) {
// Always add... if we have a hard limit, we will be down one, and if we have a soft
// limit, an extra one is no big deal. If we have stopped pooling, then it is a
// wasted creation.
if (pool != null) {
pool.addFirst(instance);
}
allocated--;
}
// if we are using hard limits we need to release our permit
if (hardLimit) {
semaphore.release();
}
}
public List stopPooling() {
synchronized (this) {
List temp = pool;
pool = null;
return temp;
}
}
public void startPooling() {
synchronized (this) {
if (pool == null) {
pool = new LinkedList();
}
}
}
/**
* Return the size of the pool.
*
* @return the size of the pool
*/
public int getSize() {
synchronized (this) {
return allocated + pool.size();
}
}
/**
* Gets the number of allocated instances. This may be larger then the max if the pools
* is using a soft limit.
*/
public int getAllocatedSize() {
synchronized (this) {
return allocated;
}
}
/**
* Get the maximum size of the pool.
*
* @return the size of the pool
*/
public int getMaxSize() {
return maxSize;
}
/**
* Determines if this pool has a hard limit.
*
* @return true if this pool is using a hard limit
*/
public boolean isHardLimit() {
return hardLimit;
}
}