commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1531278 - in /commons/proper/pool/trunk/src: changes/ main/java/org/apache/commons/pool2/proxy/ test/java/org/apache/commons/pool2/proxy/
Date Fri, 11 Oct 2013 12:56:40 GMT
Author: markt
Date: Fri Oct 11 12:56:40 2013
New Revision: 1531278

URL: http://svn.apache.org/r1531278
Log:
POOL-211
Initial implementation of the Proxy wrappers. This is based on Brad Koehn's proposed patch
but has been refactored for Pool2 and to extend Proxy support to KeyedObjectPools. The cglib
proxy support and last use tracking have not yet been implemented.

Added:
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/BaseProxyHandler.java
  (with props)
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/JdkProxyHandler.java
  (with props)
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/JdkProxySource.java
  (with props)
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxiedKeyedObjectPool.java
  (with props)
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxiedObjectPool.java
  (with props)
    commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxySource.java
  (with props)
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/
    commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedObjectPoolWithJdkProxy.java
  (with props)
Modified:
    commons/proper/pool/trunk/src/changes/changes.xml

Modified: commons/proper/pool/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/changes/changes.xml?rev=1531278&r1=1531277&r2=1531278&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/changes/changes.xml (original)
+++ commons/proper/pool/trunk/src/changes/changes.xml Fri Oct 11 12:56:40 2013
@@ -160,6 +160,11 @@ clarify behaviour and improve consistenc
     <action issue="POOL-100" dev="markt" type="fix">
       Allow custom eviction policies to be defined.
     </action>
+    <action issue="POOL-211" dev="markt" type="add" due-to="Brad Koehn">
+      Add support for proxy wrappers for ObjectPool and KeyedObjectPool. The
+      primary advantage of using these wrappers is that use of pooled objects
+      is prevented after they have been returned to the pool.  
+    </action>
   </release>
   <release version="1.5.6" date="2011-04-03" description="This is a patch release, including
bugfixes only.">
     <action dev="markt" type="fix" issue="POOL-179" due-to="Axel Grossmann">

Added: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/BaseProxyHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/BaseProxyHandler.java?rev=1531278&view=auto
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/BaseProxyHandler.java
(added)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/BaseProxyHandler.java
Fri Oct 11 12:56:40 2013
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.pool2.proxy;
+
+class BaseProxyHandler<T> {
+
+    private T pooledObject;
+
+
+    BaseProxyHandler(T pooledObject) {
+        this.pooledObject = pooledObject;
+    }
+
+
+    T getPooledObject() {
+        return pooledObject;
+    }
+
+
+    T disableProxy() {
+        T result = pooledObject;
+        pooledObject = null;
+        return result;
+    }
+
+
+    void validateProxiedObject() {
+        if (pooledObject == null) {
+            throw new IllegalStateException("This object may no longer be " +
+            		"used as it has been returned to the Object Pool.");
+        }
+    }
+}

Propchange: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/BaseProxyHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/JdkProxyHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/JdkProxyHandler.java?rev=1531278&view=auto
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/JdkProxyHandler.java
(added)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/JdkProxyHandler.java
Fri Oct 11 12:56:40 2013
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.pool2.proxy;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+
+class JdkProxyHandler<T> extends BaseProxyHandler<T>
+        implements InvocationHandler {
+
+
+    JdkProxyHandler(T pooledObject) {
+        super(pooledObject);
+    }
+
+
+    @Override
+    public Object invoke(Object proxy, Method method, Object[] args)
+            throws Throwable {
+        validateProxiedObject();
+        return method.invoke(getPooledObject(), args);
+    }
+}
+

Propchange: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/JdkProxyHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/JdkProxySource.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/JdkProxySource.java?rev=1531278&view=auto
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/JdkProxySource.java
(added)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/JdkProxySource.java
Fri Oct 11 12:56:40 2013
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.pool2.proxy;
+
+import java.lang.reflect.Proxy;
+
+class JdkProxySource<T> implements ProxySource<T> {
+
+    private final ClassLoader classLoader;
+    private final Class<?>[] interfaces;
+
+
+    JdkProxySource(ClassLoader classLoader, Class<?>[] interfaces) {
+        this.classLoader = classLoader;
+        this.interfaces = interfaces;
+    }
+
+
+    @Override
+    public T createProxy(T pooledObject) {
+        @SuppressWarnings("unchecked")
+        T proxy = (T) Proxy.newProxyInstance(
+                classLoader, interfaces, new JdkProxyHandler<T>(pooledObject));
+        return proxy;
+    }
+
+
+    @Override
+    public T resolveProxy(T proxy) {
+        @SuppressWarnings("unchecked")
+        JdkProxyHandler<T> jdkProxyHandler =
+                (JdkProxyHandler<T>) Proxy.getInvocationHandler(proxy);
+        T pooledObject = jdkProxyHandler.disableProxy();
+        return pooledObject;
+    }
+}

Propchange: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/JdkProxySource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxiedKeyedObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxiedKeyedObjectPool.java?rev=1531278&view=auto
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxiedKeyedObjectPool.java
(added)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxiedKeyedObjectPool.java
Fri Oct 11 12:56:40 2013
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.pool2.proxy;
+
+import java.util.NoSuchElementException;
+
+import org.apache.commons.pool2.KeyedObjectPool;
+
+public class ProxiedKeyedObjectPool<K,V> implements KeyedObjectPool<K,V> {
+
+    private final KeyedObjectPool<K,V> pool;
+    private final ProxySource<V> proxySource;
+
+
+    public ProxiedKeyedObjectPool(KeyedObjectPool<K,V> pool,
+            ProxySource<V> proxySource) {
+        this.pool = pool;
+        this.proxySource = proxySource;
+    }
+
+
+    @Override
+    public V borrowObject(K key) throws Exception, NoSuchElementException,
+            IllegalStateException {
+        V pooledObject = pool.borrowObject(key);
+        V proxy = proxySource.createProxy(pooledObject);
+        return proxy;
+    }
+
+    @Override
+    public void returnObject(K key, V proxy) throws Exception {
+        V pooledObject = proxySource.resolveProxy(proxy);
+        pool.returnObject(key, pooledObject);
+    }
+
+    @Override
+    public void invalidateObject(K key, V proxy) throws Exception {
+        V pooledObject = proxySource.resolveProxy(proxy);
+        pool.invalidateObject(key, pooledObject);
+    }
+
+    @Override
+    public void addObject(K key) throws Exception, IllegalStateException,
+            UnsupportedOperationException {
+        pool.addObject(key);
+    }
+
+    @Override
+    public int getNumIdle(K key) {
+        return pool.getNumIdle(key);
+    }
+
+    @Override
+    public int getNumActive(K key) {
+        return pool.getNumActive(key);
+    }
+
+    @Override
+    public int getNumIdle() {
+        return pool.getNumIdle();
+    }
+
+    @Override
+    public int getNumActive() {
+        return pool.getNumActive();
+    }
+
+    @Override
+    public void clear() throws Exception, UnsupportedOperationException {
+        pool.clear();
+    }
+
+    @Override
+    public void clear(K key) throws Exception, UnsupportedOperationException {
+        pool.clear(key);
+    }
+
+    @Override
+    public void close() {
+        pool.close();
+    }
+}

Propchange: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxiedKeyedObjectPool.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxiedObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxiedObjectPool.java?rev=1531278&view=auto
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxiedObjectPool.java
(added)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxiedObjectPool.java
Fri Oct 11 12:56:40 2013
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.pool2.proxy;
+
+import java.util.NoSuchElementException;
+
+import org.apache.commons.pool2.ObjectPool;
+
+public class ProxiedObjectPool<T> implements ObjectPool<T> {
+
+    private final ObjectPool<T> pool;
+    private final ProxySource<T> proxySource;
+
+
+    public ProxiedObjectPool(ObjectPool<T> pool, ProxySource<T> proxySource)
{
+        this.pool = pool;
+        this.proxySource = proxySource;
+    }
+
+
+    // --------------------------------------------------- ObjectPool<T> methods
+
+    @Override
+    public T borrowObject() throws Exception, NoSuchElementException,
+            IllegalStateException {
+        T pooledObject = pool.borrowObject();
+        T proxy = proxySource.createProxy(pooledObject);
+        return proxy;
+    }
+
+
+    @Override
+    public void returnObject(T proxy) throws Exception {
+        T pooledObject = proxySource.resolveProxy(proxy);
+        pool.returnObject(pooledObject);
+    }
+
+
+    @Override
+    public void invalidateObject(T proxy) throws Exception {
+        T pooledObject = proxySource.resolveProxy(proxy);
+        pool.invalidateObject(pooledObject);
+    }
+
+
+    @Override
+    public void addObject() throws Exception, IllegalStateException,
+            UnsupportedOperationException {
+        pool.addObject();
+    }
+
+
+    @Override
+    public int getNumIdle() {
+        return pool.getNumIdle();
+    }
+
+
+    @Override
+    public int getNumActive() {
+        return pool.getNumActive();
+    }
+
+
+    @Override
+    public void clear() throws Exception, UnsupportedOperationException {
+        pool.clear();
+    }
+
+
+    @Override
+    public void close() {
+        pool.close();
+    }
+}

Propchange: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxiedObjectPool.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxySource.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxySource.java?rev=1531278&view=auto
==============================================================================
--- commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxySource.java
(added)
+++ commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxySource.java
Fri Oct 11 12:56:40 2013
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.pool2.proxy;
+
+interface ProxySource<T> {
+    T createProxy(T pooledObject);
+    T resolveProxy(T proxy);
+}

Propchange: commons/proper/pool/trunk/src/main/java/org/apache/commons/pool2/proxy/ProxySource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedObjectPoolWithJdkProxy.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedObjectPoolWithJdkProxy.java?rev=1531278&view=auto
==============================================================================
--- commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedObjectPoolWithJdkProxy.java
(added)
+++ commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedObjectPoolWithJdkProxy.java
Fri Oct 11 12:56:40 2013
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.pool2.proxy;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import org.apache.commons.pool2.BasePooledObjectFactory;
+import org.apache.commons.pool2.ObjectPool;
+import org.apache.commons.pool2.PooledObjectFactory;
+import org.apache.commons.pool2.impl.GenericObjectPool;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class TestProxiedObjectPoolWithJdkProxy {
+
+    private static final String DATA1 = "data1";
+
+    private ObjectPool<TestObject> pool;
+
+    @Before
+    public void setup() {
+        PooledObjectFactory<TestObject> factory = new TestObjectFactory();
+        ObjectPool<TestObject> innerPool = new GenericObjectPool<TestObject>(factory);
+
+        ProxySource<TestObject> proxySource = new JdkProxySource<TestObject>(
+                this.getClass().getClassLoader(), new Class<?>[] { TestObject.class
});
+        pool = new ProxiedObjectPool<TestObject>(innerPool, proxySource);
+    }
+
+
+    @Test
+    public void testBorrowObject() throws Exception {
+        TestObject obj = pool.borrowObject();
+        assertNotNull(obj);
+
+        // Make sure proxied methods are working
+        obj.setData(DATA1);
+        assertEquals(DATA1, obj.getData());
+
+        pool.returnObject(obj);
+    }
+
+
+    @Test(expected=IllegalStateException.class)
+    public void testAccessAfterReturn() throws Exception {
+        TestObject obj = pool.borrowObject();
+        assertNotNull(obj);
+
+        // Make sure proxied methods are working
+        obj.setData(DATA1);
+        assertEquals(DATA1, obj.getData());
+
+        pool.returnObject(obj);
+
+        assertNotNull(obj);
+
+        obj.getData();
+    }
+
+
+    private static class TestObjectFactory extends
+            BasePooledObjectFactory<TestObject> {
+
+        @Override
+        public TestObject create() throws Exception {
+            return new TestObjectImpl();
+        }
+    }
+
+
+    private static interface TestObject {
+        String getData();
+        void setData(String data);
+    }
+
+
+    private static class TestObjectImpl implements TestObject {
+
+        private String data;
+
+        @Override
+        public String getData() {
+            return data;
+        }
+
+        @Override
+        public void setData(String data) {
+            this.data = data;
+        }
+    }
+}

Propchange: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedObjectPoolWithJdkProxy.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message