commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r1550982 - in /commons/proper/pool/trunk: doc/ReaderUtil.java doc/ReaderUtilClient.java doc/StringBufferFactory.java src/site/xdoc/examples.xml
Date Sat, 14 Dec 2013 22:57:24 GMT
Author: psteitz
Date: Sat Dec 14 22:57:24 2013
New Revision: 1550982

URL: http://svn.apache.org/r1550982
Log:
Updated examples to work with pool2 APIs. Added example sources.

Added:
    commons/proper/pool/trunk/doc/ReaderUtil.java   (with props)
    commons/proper/pool/trunk/doc/ReaderUtilClient.java   (with props)
    commons/proper/pool/trunk/doc/StringBufferFactory.java   (with props)
Modified:
    commons/proper/pool/trunk/src/site/xdoc/examples.xml

Added: commons/proper/pool/trunk/doc/ReaderUtil.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/doc/ReaderUtil.java?rev=1550982&view=auto
==============================================================================
--- commons/proper/pool/trunk/doc/ReaderUtil.java (added)
+++ commons/proper/pool/trunk/doc/ReaderUtil.java Sat Dec 14 22:57:24 2013
@@ -0,0 +1,64 @@
+/*
+ * 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.
+ */
+import java.io.IOException;
+import java.io.Reader;
+
+import org.apache.commons.pool2.ObjectPool;
+
+/**
+ * Maintains a pool of StringBuffers used to dump contents of Readers.
+ */
+public class ReaderUtil {
+
+    private ObjectPool<StringBuffer> pool;
+
+    public ReaderUtil(ObjectPool<StringBuffer> pool) {
+        this.pool = pool;
+    }
+
+    /**
+     * Dumps the contents of the {@link Reader} to a String, closing the {@link Reader} when
done.
+     */
+    public String readToString(Reader in)
+        throws IOException {
+        StringBuffer buf = null;
+        try {
+            buf = pool.borrowObject();
+            for (int c = in.read(); c != -1; c = in.read()) {
+                buf.append((char) c);
+            }
+            return buf.toString();
+        } catch (IOException e) {
+            throw e;
+        } catch (Exception e) {
+            throw new RuntimeException("Unable to borrow buffer from pool" + e.toString());
+        } finally {
+            try {
+                in.close();
+            } catch (Exception e) {
+                // ignored
+            }
+            try {
+                if (null != buf) {
+                    pool.returnObject(buf);
+                }
+            } catch (Exception e) {
+                // ignored
+            }
+        }
+    }
+}

Propchange: commons/proper/pool/trunk/doc/ReaderUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/pool/trunk/doc/ReaderUtil.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: commons/proper/pool/trunk/doc/ReaderUtilClient.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/doc/ReaderUtilClient.java?rev=1550982&view=auto
==============================================================================
--- commons/proper/pool/trunk/doc/ReaderUtilClient.java (added)
+++ commons/proper/pool/trunk/doc/ReaderUtilClient.java Sat Dec 14 22:57:24 2013
@@ -0,0 +1,39 @@
+/*
+ * 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.
+ */
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+
+import org.apache.commons.pool2.impl.GenericObjectPool;
+
+/**
+ * Instantiates and uses a ReaderUtil. The GenericObjectPool supplied to the constructor
will have
+ * default configuration properties.
+ */
+public class ReaderUtilClient {
+
+    public static void main(String[] args) {
+        ReaderUtil readerUtil = new ReaderUtil(new GenericObjectPool<StringBuffer>(new
StringBufferFactory()));
+        Reader reader = new StringReader("foo");
+        try {
+            System.out.println(readerUtil.readToString(reader));
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+}

Propchange: commons/proper/pool/trunk/doc/ReaderUtilClient.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/pool/trunk/doc/ReaderUtilClient.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: commons/proper/pool/trunk/doc/StringBufferFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/doc/StringBufferFactory.java?rev=1550982&view=auto
==============================================================================
--- commons/proper/pool/trunk/doc/StringBufferFactory.java (added)
+++ commons/proper/pool/trunk/doc/StringBufferFactory.java Sat Dec 14 22:57:24 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.
+ */
+import org.apache.commons.pool2.BasePooledObjectFactory;
+import org.apache.commons.pool2.PooledObject;
+import org.apache.commons.pool2.impl.DefaultPooledObject;
+
+/**
+ * Example PooledObjectFactory for pooled StringBuffers.
+ */
+public class StringBufferFactory
+    extends BasePooledObjectFactory<StringBuffer> {
+
+    @Override
+    public StringBuffer create() {
+        return new StringBuffer();
+    }
+
+    /**
+     * Use the default PooledObject implementation.
+     */
+    @Override
+    public PooledObject<StringBuffer> wrap(StringBuffer buffer) {
+        return new DefaultPooledObject<StringBuffer>(buffer);
+    }
+
+    /**
+     * When an object is returned to the pool, clear the buffer.
+     */
+    @Override
+    public void passivateObject(PooledObject<StringBuffer> pooledObject) {
+        pooledObject.getObject().setLength(0);
+    }
+
+    // for all other methods, the no-op implementation
+    // in BasePooledObjectFactory will suffice
+}

Propchange: commons/proper/pool/trunk/doc/StringBufferFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/pool/trunk/doc/StringBufferFactory.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: commons/proper/pool/trunk/src/site/xdoc/examples.xml
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/site/xdoc/examples.xml?rev=1550982&r1=1550981&r2=1550982&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/site/xdoc/examples.xml (original)
+++ commons/proper/pool/trunk/src/site/xdoc/examples.xml Sat Dec 14 22:57:24 2013
@@ -19,7 +19,6 @@
    <properties>
       <title>Examples</title>
       <author email="dev@commons.apache.org">Apache Commons Documentation Team</author>
-      <author email="rwaldhoff@apache.org">Rodney Waldhoff</author>
       <revision>$Id$</revision>
    </properties>
 
@@ -75,103 +74,109 @@ public class ReaderUtil { 
         like this:
        </p>
 <source>
+import java.io.IOException;
+import java.io.Reader;
 import org.apache.commons.pool2.ObjectPool;
-import java.io.Reader; 
-import java.io.IOException; 
- 
-public class ReaderUtil { 
+
+public class ReaderUtil {
+    
     private ObjectPool&lt;StringBuffer&gt; pool;
- 
-    public ReaderUtil(ObjectPool&lt;StringBuffer&gt; pool) { 
+    
+    public ReaderUtil(ObjectPool&lt;StringBuffer&gt; pool) {
         this.pool = pool;
-    } 
- 
-    /** 
-     * Dumps the contents of the {@link Reader} to a 
-     * String, closing the {@link Reader} when done. 
-     */ 
-    public String readToString(Reader in) throws IOException { 
+    }
+
+    /**
+     * Dumps the contents of the {@link Reader} to a String, closing the {@link Reader} when
done.
+     */
+    public String readToString(Reader in)
+        throws IOException {
         StringBuffer buf = null;
-        try { 
+        try {
             buf = pool.borrowObject();
-            for(int c = in.read(); c != -1; c = in.read()) { 
-                buf.append((char)c); 
-            } 
-            return buf.toString(); 
-        } catch(IOException e) { 
-            throw e; 
-        } catch(Exception e) {
-            throw new RuntimeException("Unable to borrow buffer from pool" + 
-                    e.toString());
-        } finally { 
-            try { 
-                in.close(); 
-            } catch(Exception e) { 
-                // ignored 
-            } 
+            for (int c = in.read(); c != -1; c = in.read()) {
+                buf.append((char) c);
+            }
+            return buf.toString();
+        } catch (IOException e) {
+            throw e;
+        } catch (Exception e) {
+            throw new RuntimeException("Unable to borrow buffer from pool" + e.toString());
+        } finally {
             try {
-                if(null != buf) {
+                in.close();
+            } catch (Exception e) {
+                // ignored
+            }
+            try {
+                if (null != buf) {
                     pool.returnObject(buf);
                 }
-            } catch(Exception e) {
+            } catch (Exception e) {
                 // ignored
             }
-        } 
-    } 
+        }
+    }
 }
 </source>
       <p>
        Since we've constrained ourselves to the <code>ObjectPool</code> interface,
an arbitrary pool 
        implementation (returning, in our case, <code>StringBuffer</code>s) can
be used.  When a different
-       or "better" pool implemenatation comes along, we can simply drop it into our <code>ReaderUtil</code>
+       or "better" pool implementation comes along, we can simply drop it into our <code>ReaderUtil</code>
        without changing a line of code.  
       </p>
       </section>
-      <section name="A PoolableObjectFactory">
+      <section name="A PooledObjectFactory">
        <p>
-        Recall that Pool provides a simple toolkit for creating object pools.  The 
-        <code>PoolableObjectFactory</code> interface is an important part of
this toolkit.
-        <code>PoolableObjectFactory</code> defines lifecycle methods for pooled
objects. 
-        We can use it to separate the kinds of objects that are pooled and how they are 
-        created, persisted, or destroyed, from the pooling algorithm itself.
+        The implementations provided in pool2 wrap pooled objects in <code>PooledObject</code>
+        wrappers for internal use by the pool and object factories.  The <code>PooledObjectFactory</code>
+        interface defines lifecycle methods for pooled objects. The simplest way to implement
a
+        <code>PoolableObjectFactory</code> is to extend
+        <a href="./apidocs/org/apache/commons/pool2/BasePooledObjectFactory.html">
+        <code>BasePooledObjectFactory</code></a>.
        </p>
        <p>
-        Suppose we have an <code>ObjectPool</code> implementation that accepts
a 
-        <code>PoolableObjectFactory</code> (for example, any of the implementations
in the
-        <code>org.apache.commons.pool2.impl</code> package).  Then we need only
provide 
-        the factory implemenation in order to pool a new kind of object.  
-       </p>
-       <p>
-        Here's a <code>PoolableObjectFactory</code> implementation that creates
+        Here's a <code>PooledObjectFactory</code> implementation that creates
         <code>StringBuffer</code>s as used above.
        </p>
 <source>
-import org.apache.commons.pool2.BasePoolableObjectFactory; 
- 
-public class StringBufferFactory extends BasePoolableObjectFactory&lt;StringBuffer&gt;
{ 
-    // for makeObject we'll simply return a new buffer 
+import org.apache.commons.pool2.BasePooledObjectFactory;
+import org.apache.commons.pool2.PooledObject;
+import org.apache.commons.pool2.impl.DefaultPooledObject;
+
+public class StringBufferFactory
+    extends BasePooledObjectFactory&lt;StringBuffer&gt; {
+
     @Override
-    public StringBuffer makeObject() { 
-        return new StringBuffer(); 
-    } 
-     
-    // when an object is returned to the pool,  
-    // we'll clear it out 
+    public StringBuffer create() {
+        return new StringBuffer();
+    }
+
+    /**
+     * Use the default PooledObject implementation.
+     */
     @Override
-    public void passivateObject(StringBuffer obj) { 
-        obj.setLength(0); 
-    } 
-     
-    // for all other methods, the no-op  
-    // implementation in BasePoolableObjectFactory 
-    // will suffice 
+    public PooledObject&lt;StringBuffer&gt; wrap(StringBuffer buffer) {
+        return new DefaultPooledObject&lt;StringBuffer&gt;(buffer);
+    }
+
+    /**
+     * When an object is returned to the pool, clear the buffer.
+     */
+    @Override
+    public void passivateObject(PooledObject&lt;StringBuffer&gt; pooledObject) {
+        pooledObject.getObject().setLength(0);
+    }
+
+    // for all other methods, the no-op implementation
+    // in BasePooledObjectFactory will suffice
 }
 </source>
       <p>
        We can, for example, use this factory with the <code>GenericObjectPool</code>
to instantiate our
        <code>ReaderUtil</code> as follows:
       </p>
-<source>new ReaderUtil(new GenericObjectPool&lt;StringBuffer&gt;(new StringBufferFactory()))</source>
+<source>ReaderUtil readerUtil = new ReaderUtil(new GenericObjectPool&lt;StringBuffer&gt;(new
StringBufferFactory()));</source>
       </section>
    </body>
 </document>



Mime
View raw message