KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > mpool > PoolUtil


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.mpool;
18
19 import java.lang.reflect.Method JavaDoc;
20
21 /**
22  * The PoolUtil class performs the reflection magic that is necessary to work
23  * with the legacy Recyclable interface in the
24  * <a HREF="http://jakarta.apache.org/avalon/excalibur/pool">Pool</a> package.
25  * It also works with the new Resettable interface in MPool.
26  *
27  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
28  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:34 $
29  */

30 public final class PoolUtil
31 {
32     private final static Object JavaDoc[] EMPTY = new Object JavaDoc[]{};
33     private final static Class JavaDoc[] EMPTY_ARGS = new Class JavaDoc[]{};
34
35     private PoolUtil()
36     {
37     }
38
39     /**
40      * This method will either call "reset" on Resettable objects,
41      * or it will call "recycle" on Recyclable objects.
42      *
43      * @param obj The object you want recycled.
44      * @return the same object
45      */

46     public static Object JavaDoc recycle( final Object JavaDoc obj )
47     {
48         if( obj instanceof Resettable )
49         {
50             ( (Resettable)obj ).reset();
51         }
52         else
53         {
54             try
55             {
56                 Class JavaDoc klass = obj.getClass();
57                 Class JavaDoc recyclable = klass.getClassLoader().loadClass( "org.apache.avalon.excalibur.pool.Recyclable" );
58
59                 if( recyclable.isAssignableFrom( klass ) )
60                 {
61                     recycleLegacy( obj );
62                 }
63             }
64             catch( Exception JavaDoc e )
65             {
66                 // No recyclable interface
67
}
68         }
69
70         return obj;
71     }
72
73     private static void recycleLegacy( final Object JavaDoc obj ) throws Exception JavaDoc
74     {
75         Class JavaDoc klass = obj.getClass();
76         Method JavaDoc recycle = klass.getMethod( "recycle", EMPTY_ARGS );
77         recycle.invoke( obj, EMPTY );
78     }
79 }
80
Popular Tags