commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jcar...@apache.org
Subject svn commit: r240200 - in /jakarta/commons/sandbox/proxy/trunk: ./ src/java/org/apache/commons/proxy/provider/ src/java/org/apache/commons/proxy/provider/cache/ src/test/org/apache/commons/proxy/provider/
Date Fri, 26 Aug 2005 11:05:14 GMT
Author: jcarman
Date: Fri Aug 26 04:05:04 2005
New Revision: 240200

URL: http://svn.apache.org/viewcvs?rev=240200&view=rev
Log:
Added cached object providers (threaded/pooled)

Added:
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/CachedProvider.java
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/PooledProvider.java
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/AbstractCache.java
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/Cache.java
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/CacheEvictionEvent.java
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/CacheEvictionListener.java
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/CachedObject.java
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/SimpleCache.java
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/ThreadLocalCache.java
    jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/CountingProvider.java
    jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestCachedProvider.java
    jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestPooledProvider.java
Modified:
    jakarta/commons/sandbox/proxy/trunk/build.xml
    jakarta/commons/sandbox/proxy/trunk/commons-proxy.iml

Modified: jakarta/commons/sandbox/proxy/trunk/build.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/build.xml?rev=240200&r1=240199&r2=240200&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/build.xml (original)
+++ jakarta/commons/sandbox/proxy/trunk/build.xml Fri Aug 26 04:05:04 2005
@@ -58,6 +58,8 @@
         <runtime-dependency groupId="aopalliance" version="1.0"/>
         <runtime-dependency groupId="cglib" artifactId="cglib-full" version="2.0.2"/>
         <runtime-dependency groupId="commons-logging" version="1.0.4" />
+        <runtime-dependency groupId="commons-pool" version="1.2" />
+        <runtime-dependency groupId="commons-collections" version="3.1" />
         <runtime-dependency groupId="burlap" version="2.1.7"/>
         <runtime-dependency groupId="hessian" version="3.0.1"/>
         <runtime-dependency groupId="axis" version="1.2.1" />

Modified: jakarta/commons/sandbox/proxy/trunk/commons-proxy.iml
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/commons-proxy.iml?rev=240200&r1=240199&r2=240200&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/commons-proxy.iml (original)
+++ jakarta/commons/sandbox/proxy/trunk/commons-proxy.iml Fri Aug 26 04:05:04 2005
@@ -129,6 +129,24 @@
         <SOURCES />
       </library>
     </orderEntry>
+    <orderEntry type="module-library">
+      <library>
+        <CLASSES>
+          <root url="jar://$MODULE_DIR$/lib/runtime/commons-pool/commons-pool.jar!/" />
+        </CLASSES>
+        <JAVADOC />
+        <SOURCES />
+      </library>
+    </orderEntry>
+    <orderEntry type="module-library">
+      <library>
+        <CLASSES>
+          <root url="jar://$MODULE_DIR$/lib/runtime/commons-collections/commons-collections.jar!/"
/>
+        </CLASSES>
+        <JAVADOC />
+        <SOURCES />
+      </library>
+    </orderEntry>
     <orderEntryProperties />
   </component>
 </module>

Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/CachedProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/CachedProvider.java?rev=240200&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/CachedProvider.java
(added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/CachedProvider.java
Fri Aug 26 04:05:04 2005
@@ -0,0 +1,51 @@
+/*
+ *  Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.commons.proxy.provider;
+
+import org.apache.commons.proxy.ObjectProvider;
+import org.apache.commons.proxy.provider.cache.Cache;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class CachedProvider<T> extends ProviderDecorator<T>
+{
+    private final Object cacheKey = new Object();
+
+    private Cache cache;
+
+    public CachedProvider( ObjectProvider<? extends T> inner )
+    {
+        super( inner );
+    }
+
+    public void setCache( Cache cache )
+    {
+        this.cache = cache;
+    }
+
+    public T getObject()
+    {
+        T object = ( T )cache.retrieveObject( cacheKey );
+        if( object == null )
+        {
+            object = super.getObject();
+            cache.storeObject( cacheKey, object );
+        }
+        return object;
+    }
+}

Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/PooledProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/PooledProvider.java?rev=240200&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/PooledProvider.java
(added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/PooledProvider.java
Fri Aug 26 04:05:04 2005
@@ -0,0 +1,143 @@
+/*
+ *  Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.commons.proxy.provider;
+
+import org.apache.commons.pool.BasePoolableObjectFactory;
+import org.apache.commons.pool.impl.GenericObjectPool;
+import org.apache.commons.proxy.ObjectProvider;
+import org.apache.commons.proxy.exception.ObjectProviderException;
+import org.apache.commons.proxy.provider.cache.Cache;
+import org.apache.commons.proxy.provider.cache.CacheEvictionEvent;
+import org.apache.commons.proxy.provider.cache.CacheEvictionListener;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class PooledProvider<T> extends ProviderDecorator<T> implements CacheEvictionListener
+{
+    private final Object cacheKey = new Object();
+    private final GenericObjectPool pool;
+    private Cache cache;
+
+    public PooledProvider( ObjectProvider<? extends T> inner )
+    {
+        super( inner );
+        pool = new GenericObjectPool( new Factory() );
+    }
+
+    public void setCache( Cache cache )
+    {
+        this.cache = cache;
+    }
+
+    public void objectEvicted( CacheEvictionEvent e )
+    {
+        try
+        {
+            log.debug( "Returning object to pool in thread " + Thread.currentThread().getName()
+ "..." );
+            pool.returnObject( e.getEvictedObject() );
+        }
+        catch( Exception e1 )
+        {
+            // Do nothing.
+        }
+    }
+
+    public T getObject()
+    {
+        try
+        {
+            log.debug( "Checking for object in cache in thread " + Thread.currentThread().getName()
+ "..." );
+            T object = ( T ) cache.retrieveObject( cacheKey );
+            if( object == null )
+            {
+                log.debug( "Did not object in cache; borrowing from pool in thread " + Thread.currentThread().getName()
+ "..." );
+                object = ( T ) pool.borrowObject();
+                cache.storeObject( cacheKey, object, this );
+            }
+            return object;
+        }
+        catch( Exception e )
+        {
+            throw new ObjectProviderException( "Unable to borrow object from pool.", e );
+        }
+    }
+
+    public void setMaxActive( int i )
+    {
+        pool.setMaxActive( i );
+    }
+
+    public void setWhenExhaustedAction( byte b )
+    {
+        pool.setWhenExhaustedAction( b );
+    }
+
+    public void setMaxWait( long l )
+    {
+        pool.setMaxWait( l );
+    }
+
+    public void setMaxIdle( int i )
+    {
+        pool.setMaxIdle( i );
+    }
+
+    public void setTestOnReturn( boolean b )
+    {
+        pool.setTestOnReturn( b );
+    }
+
+    public void setTestOnBorrow( boolean b )
+    {
+        pool.setTestOnBorrow( b );
+    }
+
+    public void setMinIdle( int i )
+    {
+        pool.setMinIdle( i );
+    }
+
+    public void setTimeBetweenEvictionRunsMillis( long l )
+    {
+        pool.setTimeBetweenEvictionRunsMillis( l );
+    }
+
+    public void setNumTestsPerEvictionRun( int i )
+    {
+        pool.setNumTestsPerEvictionRun( i );
+    }
+
+    public void setMinEvictableIdleTimeMillis( long l )
+    {
+        pool.setMinEvictableIdleTimeMillis( l );
+    }
+
+    public void setTestWhileIdle( boolean b )
+    {
+        pool.setTestWhileIdle( b );
+    }
+
+    private class Factory extends BasePoolableObjectFactory
+    {
+        public T makeObject() throws Exception
+        {
+            log.debug( "Creating new object for pool in thread " + Thread.currentThread().getName()
+ "..." );
+            return inner.getObject();
+        }
+    }
+}

Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/AbstractCache.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/AbstractCache.java?rev=240200&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/AbstractCache.java
(added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/AbstractCache.java
Fri Aug 26 04:05:04 2005
@@ -0,0 +1,62 @@
+/*
+ *  Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.commons.proxy.provider.cache;
+
+import java.util.LinkedList;
+import java.util.Map;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public abstract class AbstractCache implements Cache
+{
+    protected abstract Map<Object,CachedObject> getCachedObjectMap();
+
+    public void storeObject( Object key, Object value )
+    {
+        getCachedObjectMap().put( key, new CachedObject( value ) );
+    }
+
+    public void storeObject( Object key, Object value, CacheEvictionListener listener )
+    {
+        getCachedObjectMap().put( key, new CachedObject( value, listener ) );
+    }
+
+    public Object retrieveObject( Object key )
+    {
+        CachedObject cachedObject = getCachedObjectMap().get( key );
+        return cachedObject == null ? null : cachedObject.getObject();
+    }
+
+    public void clearCache()
+    {
+        for( Object cacheKey: new LinkedList<Object>( getCachedObjectMap().keySet()
) )
+        {
+            final CachedObject cachedObject = getCachedObjectMap().get( cacheKey );
+            if( cachedObject != null )
+            {
+                getCachedObjectMap().remove( cacheKey );
+                if( cachedObject.getListener() != null )
+                {
+                    cachedObject.getListener().objectEvicted( new CacheEvictionEvent( cacheKey,
cachedObject.getObject() ) );
+                }
+            }
+        }
+    }
+
+
+}

Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/Cache.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/Cache.java?rev=240200&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/Cache.java
(added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/Cache.java
Fri Aug 26 04:05:04 2005
@@ -0,0 +1,28 @@
+/*
+ *  Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.commons.proxy.provider.cache;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public interface Cache
+{
+    public void storeObject( Object key, Object value );
+    public void storeObject( Object key, Object value, CacheEvictionListener listener );
+    public Object retrieveObject( Object key );
+    public void clearCache();
+}

Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/CacheEvictionEvent.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/CacheEvictionEvent.java?rev=240200&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/CacheEvictionEvent.java
(added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/CacheEvictionEvent.java
Fri Aug 26 04:05:04 2005
@@ -0,0 +1,47 @@
+/*
+ *  Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.commons.proxy.provider.cache;
+
+import java.util.EventObject;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class CacheEvictionEvent extends EventObject
+{
+    private final Object cacheKey;
+    private final Object evictedObject;
+
+    public CacheEvictionEvent( Object cacheKey, Object evictedObject )
+    {
+        super( evictedObject );
+        this.cacheKey = cacheKey;
+        this.evictedObject = evictedObject;
+    }
+
+    public Object getCacheKey()
+    {
+        return cacheKey;
+    }
+
+    public Object getEvictedObject()
+    {
+        return evictedObject;
+    }
+
+
+}

Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/CacheEvictionListener.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/CacheEvictionListener.java?rev=240200&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/CacheEvictionListener.java
(added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/CacheEvictionListener.java
Fri Aug 26 04:05:04 2005
@@ -0,0 +1,27 @@
+/*
+ *  Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.commons.proxy.provider.cache;
+
+import java.util.EventListener;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public interface CacheEvictionListener extends EventListener
+{
+    public void objectEvicted( CacheEvictionEvent e );
+}

Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/CachedObject.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/CachedObject.java?rev=240200&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/CachedObject.java
(added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/CachedObject.java
Fri Aug 26 04:05:04 2005
@@ -0,0 +1,48 @@
+/*
+ *  Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.commons.proxy.provider.cache;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class CachedObject
+{
+    private final Object object;
+    private final CacheEvictionListener listener;
+
+    public CachedObject( Object object )
+    {
+        this.object = object;
+        this.listener = null;
+    }
+
+    public CachedObject( Object object, CacheEvictionListener listener )
+    {
+        this.object = object;
+        this.listener = listener;
+    }
+
+    public Object getObject()
+    {
+        return object;
+    }
+
+    public CacheEvictionListener getListener()
+    {
+        return listener;
+    }
+}

Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/SimpleCache.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/SimpleCache.java?rev=240200&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/SimpleCache.java
(added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/SimpleCache.java
Fri Aug 26 04:05:04 2005
@@ -0,0 +1,34 @@
+/*
+ *  Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.commons.proxy.provider.cache;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class SimpleCache extends AbstractCache
+{
+    private final Map<Object, CachedObject> map = Collections.synchronizedMap( new
HashMap<Object, CachedObject>() );
+
+    protected Map<Object, CachedObject> getCachedObjectMap()
+    {
+        return map;
+    }
+}

Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/ThreadLocalCache.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/ThreadLocalCache.java?rev=240200&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/ThreadLocalCache.java
(added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/cache/ThreadLocalCache.java
Fri Aug 26 04:05:04 2005
@@ -0,0 +1,39 @@
+/*
+ *  Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.commons.proxy.provider.cache;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class ThreadLocalCache extends AbstractCache
+{
+    private ThreadLocal<Map<Object, CachedObject>> threadLocalMap = new ThreadLocal<Map<Object,
CachedObject>>();
+
+    public Map<Object, CachedObject> getCachedObjectMap()
+    {
+        Map<Object, CachedObject> map = threadLocalMap.get();
+        if( map == null )
+        {
+            map = new HashMap<Object, CachedObject>();
+            threadLocalMap.set( map );
+        }
+        return map;
+    }
+}

Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/CountingProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/CountingProvider.java?rev=240200&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/CountingProvider.java
(added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/CountingProvider.java
Fri Aug 26 04:05:04 2005
@@ -0,0 +1,43 @@
+/*
+ *  Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.commons.proxy.provider;
+
+import org.apache.commons.proxy.ObjectProvider;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class CountingProvider<T> extends ProviderDecorator<T>
+{
+    private int count = 0;
+
+    public CountingProvider( ObjectProvider<T> inner )
+    {
+        super( inner );
+    }
+
+    public synchronized T getObject()
+    {
+        count++;
+        return super.getObject();
+    }
+
+    public synchronized int getCount()
+    {
+        return count;
+    }
+}

Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestCachedProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestCachedProvider.java?rev=240200&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestCachedProvider.java
(added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestCachedProvider.java
Fri Aug 26 04:05:04 2005
@@ -0,0 +1,60 @@
+/*
+ *  Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.commons.proxy.provider;
+
+import junit.framework.TestCase;
+import org.apache.commons.proxy.provider.cache.SimpleCache;
+import org.apache.commons.proxy.provider.cache.ThreadLocalCache;
+import org.apache.commons.proxy.util.Echo;
+import org.apache.commons.proxy.util.EchoImpl;
+
+import java.util.concurrent.CountDownLatch;
+
+public class TestCachedProvider extends TestCase
+{
+    public void testWithSimpleCache()
+    {
+        final CountingProvider<Echo> counter = new CountingProvider<Echo>( new
ConstantProvider<Echo>( new EchoImpl() ) );
+        final CachedProvider<Echo> provider = new CachedProvider<Echo>( counter
);
+        provider.setCache( new SimpleCache() );
+        for( int i = 0; i < 10; ++i )
+        {
+            provider.getObject().echoBack( "Hello, World" );
+        }
+        assertEquals( 1, counter.getCount() );
+    }
+
+    public void testWithThreadLocalCache() throws Exception
+    {
+        final CountingProvider<Echo> counter = new CountingProvider<Echo>( new
ConstantProvider<Echo>( new EchoImpl() ) );
+        final CachedProvider<Echo> provider = new CachedProvider<Echo>( counter
);
+        provider.setCache( new ThreadLocalCache() );
+        final CountDownLatch latch = new CountDownLatch( 10 );
+        for( int i = 0; i < 10; ++i )
+        {
+            new Thread( new Runnable()
+            {
+                public void run()
+                {
+                    provider.getObject().echoBack( "Hello, World" );
+                    latch.countDown();
+                }
+            }).start();
+        }
+        latch.await();
+        assertEquals( 10, counter.getCount() );
+    }
+}
\ No newline at end of file

Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestPooledProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestPooledProvider.java?rev=240200&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestPooledProvider.java
(added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestPooledProvider.java
Fri Aug 26 04:05:04 2005
@@ -0,0 +1,86 @@
+/*
+ *  Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.commons.proxy.provider;
+
+import junit.framework.TestCase;
+import org.apache.commons.pool.impl.GenericObjectPool;
+import org.apache.commons.proxy.provider.cache.SimpleCache;
+import org.apache.commons.proxy.provider.cache.ThreadLocalCache;
+import org.apache.commons.proxy.util.Echo;
+import org.apache.commons.proxy.util.EchoImpl;
+
+import java.util.concurrent.CountDownLatch;
+
+public class TestPooledProvider extends TestCase
+{
+    public void testWithSimpleCache()
+    {
+        final CountingProvider<Echo> counter = new CountingProvider<Echo>( new
ConstantProvider<Echo>( new EchoImpl() ) );
+        final PooledProvider<Echo> provider = new PooledProvider<Echo>( counter
);
+        final SimpleCache cache = new SimpleCache();
+        provider.setCache( cache );
+        for( int i = 0; i < 10; ++i )
+        {
+            provider.getObject().echoBack( "Hello, World" );
+            cache.clearCache();
+        }
+        assertEquals( 1, counter.getCount() );
+
+    }
+
+    public void testWithThreadLocalCache() throws Exception
+    {
+        final CountingProvider<Echo> counter = new CountingProvider<Echo>( new
ConstantProvider<Echo>( new EchoImpl() ) );
+        final PooledProvider<Echo> provider = new PooledProvider<Echo>( counter
);
+        provider.setMaxActive( 10 );
+        provider.setMinIdle( 5 );
+        provider.setWhenExhaustedAction( GenericObjectPool.WHEN_EXHAUSTED_GROW );
+        final ThreadLocalCache cache = new ThreadLocalCache();
+        provider.setCache( cache );
+        final CountDownLatch goLatch = new CountDownLatch( 1 );
+        final CountDownLatch borrowedLatch = new CountDownLatch( 10 );
+        final CountDownLatch finished = new CountDownLatch( 10 );
+        for( int i = 0; i < 10; ++i )
+        {
+            new Thread( new Runnable()
+            {
+                public void run()
+                {
+                    try
+                    {
+                        provider.getObject().echoBack( "Hello, World" );
+                        borrowedLatch.countDown();
+                        goLatch.await();
+                        for( int i = 0; i < 10; ++i )
+                        {
+                            provider.getObject().echoBack( "Hello, World" );
+
+                        }
+                        cache.clearCache();
+                        finished.countDown();
+                    }
+                    catch( InterruptedException e )
+                    {
+                    }
+                }
+            } ).start();
+        }
+        borrowedLatch.await();
+        goLatch.countDown();
+        finished.await();
+        assertEquals( 10, counter.getCount() );
+    }
+}
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message