bloritsch 2002/09/25 17:34:17 Modified: event/src/java/org/apache/excalibur/mpool PoolUtil.java Added: event/src/java/org/apache/excalibur/mpool Resettable.java Log: Make PoolUtil more palletable--it will use the legacy Recyclable interface if it is in the classpath, or it will use the new Resettable interface Revision Changes Path 1.3 +88 -18 jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/mpool/PoolUtil.java Index: PoolUtil.java =================================================================== RCS file: /home/cvs/jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/mpool/PoolUtil.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- PoolUtil.java 25 Sep 2002 15:50:26 -0000 1.2 +++ PoolUtil.java 26 Sep 2002 00:34:17 -0000 1.3 @@ -1,16 +1,66 @@ /* - * Created by IntelliJ IDEA. - * User: bloritsch - * Date: Sep 25, 2002 - * Time: 11:47:49 AM - * To change template for new class use - * Code Style | Class Templates options (Tools | IDE Options). - */ + + ============================================================================ + The Apache Software License, Version 1.1 + ============================================================================ + + Copyright (C) @year@ The Apache Software Foundation. All rights reserved. + + Redistribution and use in source and binary forms, with or without modifica- + tion, 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 "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation" + must not be used to endorse or promote products derived from this software + without prior written permission. For written permission, please contact + [EMAIL PROTECTED] + + 5. Products derived from this software may not be called "Apache", 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 (INCLU- + DING, 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.excalibur.mpool; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; + +/** + * The PoolUtil class performs the reflection magic that is necessary to work + * with the legacy Recyclable interface in the + * <a href="http://jakarta.apache.org/avalon/excalibur/pool">Pool</a> package. + * It also works with the new Resettable interface in MPool. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> + * @version CVS $Revision$ $Date$ + */ public final class PoolUtil { private final static Object[] EMPTY = new Object[] {}; @@ -18,24 +68,44 @@ private PoolUtil() {} + /** + * This method will either call "reset" on Resettable objects, + * or it will call "recycle" on Recyclable objects. + * + * @param obj The object you want recycled. + * @return the same object + */ public static Object recycle( final Object obj ) { - try + if ( obj instanceof Resettable ) + { + ( (Resettable) obj).reset(); + } + else { - Class klass = obj.getClass(); - Method recycle = klass.getMethod( "recycle", EMPTY_ARGS ); + try + { + Class klass = obj.getClass(); + Class recyclable = klass.getClassLoader().loadClass( "org.apache.avalon.excalibur.pool.Recyclable" ); - if ( Modifier.isPublic( recycle.getModifiers() ) && - recycle.getReturnType().equals( void.class ) ) + if ( recyclable.isAssignableFrom( klass ) ) + { + recycleLegacy( obj ); + } + } + catch (Exception e) { - recycle.invoke( obj, EMPTY ); + // No recyclable interface } } - catch (Exception e) - { - // Not a recyclable object--don't worry about it - } return obj; + } + + private static void recycleLegacy( final Object obj ) throws Exception + { + Class klass = obj.getClass(); + Method recycle = klass.getMethod( "recycle", EMPTY_ARGS ); + recycle.invoke( obj, EMPTY ); } } 1.1 jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/mpool/Resettable.java Index: Resettable.java =================================================================== /* ============================================================================ The Apache Software License, Version 1.1 ============================================================================ Copyright (C) @year@ The Apache Software Foundation. All rights reserved. Redistribution and use in source and binary forms, with or without modifica- tion, 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 "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact [EMAIL PROTECTED] 5. Products derived from this software may not be called "Apache", 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 (INCLU- DING, 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.excalibur.mpool; /** * This interface standardizes the behaviour of a resettable object. * A resettable object is defined as an object that can be used to * encapsulate another object without being altered by its content. * Therefore, a resettable object may be reset and reused many times. * * This is helpful in cases where resettable objects are continously * created and destroyed, causing a much greater amount of garbage to * be collected by the JVM garbage collector. By making it resettable, * it is possible to reduce the GC execution time, thus incrementing the * overall performance of a process and decrementing the chance of * memory overflow. * * Every implementation must provide their own method to allow this * recyclable object to be reused by setting its content. * * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> * @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a> * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a> * @version CVS $Revision: 1.1 $ $Date: 2002/09/26 00:34:17 $ * @since 4.0 */ public interface Resettable { /** * This method should be implemented to remove all costly resources * in object. These resources can be object references, database connections, * threads, etc. What is categorised as "costly" resources is determined on * a case by case analysis. */ void reset(); }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
