KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > pool > test > ClassInstanceObjectFactory


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.pool.test;
9
10 import java.util.HashMap JavaDoc;
11
12 import org.apache.avalon.excalibur.pool.ObjectFactory;
13 import org.apache.avalon.framework.logger.Logger;
14
15 /**
16  * @author <a HREF="mailto:leif@silveregg.co.jp">Leif Mortenson</a>
17  * @version CVS $Revision: 1.2 $ $Date: 2002/01/25 01:24:27 $
18  * @since 4.1
19  */

20 public class ClassInstanceObjectFactory
21     implements ObjectFactory
22 {
23     private HashMap JavaDoc m_instances = new HashMap JavaDoc();
24     private Logger m_logger;
25     private Class JavaDoc m_clazz;
26     private int m_id;
27     
28     /*---------------------------------------------------------------
29      * Constructors
30      *-------------------------------------------------------------*/

31     /**
32      * Creates a reproducable log of activity in the provided StringBuffer
33      */

34     public ClassInstanceObjectFactory( Class JavaDoc clazz, Logger logger )
35     {
36         m_clazz = clazz;
37         m_logger = logger;
38         m_id = 1;
39     }
40     
41     /*---------------------------------------------------------------
42      * ObjectFactory Methods
43      *-------------------------------------------------------------*/

44     public Object JavaDoc newInstance() throws Exception JavaDoc
45     {
46         Object JavaDoc object = m_clazz.newInstance();
47         Integer JavaDoc id = new Integer JavaDoc( m_id++ );
48         
49         m_instances.put( object, id );
50         m_logger.debug( "ClassInstanceObjectFactory.newInstance() id:" + id );
51         
52         return object;
53     }
54     
55     public Class JavaDoc getCreatedClass()
56     {
57         return m_clazz;
58     }
59     
60     public void decommission( Object JavaDoc object ) throws Exception JavaDoc
61     {
62         Integer JavaDoc id = (Integer JavaDoc)m_instances.remove( object );
63         m_logger.debug( "ClassInstanceObjectFactory.decommission(a "
64             + object.getClass().getName() + ") id:" + id );
65     }
66 }
67
68
Popular Tags