KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > pool > DefaultPoolManager


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;
9
10 import java.security.SecureRandom JavaDoc;
11 import org.apache.avalon.excalibur.command.RepeatedCommand;
12 import org.apache.avalon.excalibur.event.Queue;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 /**
19  * This interface is for a PoolManager that creates pools that are managed
20  * asynchronously. The contract is that the controller type is specified in
21  * the constructor.
22  *
23  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
24  * @version CVS $Revision: 1.5 $ $Date: 2002/01/28 19:29:47 $
25  * @since 4.1
26  */

27 public class DefaultPoolManager implements PoolManager
28 {
29     private final long m_managerKey;
30     private final SecureRandom JavaDoc m_keyGenerator;
31     private final HashMap JavaDoc m_keyMap = new HashMap JavaDoc();
32     private final HashMap JavaDoc m_factoryMap = new HashMap JavaDoc();
33
34     public DefaultPoolManager()
35     {
36         this( null );
37     }
38
39     public DefaultPoolManager( final Queue commandQueue )
40     {
41         m_keyGenerator = new SecureRandom JavaDoc();
42         m_managerKey = m_keyGenerator.nextLong();
43
44         if ( null != commandQueue )
45         {
46             try
47             {
48                 commandQueue.enqueue( new PoolManagerCommand( m_keyMap ) );
49             }
50             catch (Exception JavaDoc e)
51             {
52                 // ignore silently for now
53
}
54         }
55     }
56
57     /**
58      * Return a managed pool that has a controller.
59      */

60     public Pool getManagedPool( ObjectFactory factory, int initialEntries )
61         throws Exception JavaDoc
62     {
63         ManagablePool pool = (ManagablePool) m_factoryMap.get( factory );
64
65         if ( null == pool )
66         {
67             final long poolKey = getKey();
68             pool = new VariableSizePool( factory, initialEntries, poolKey );
69             m_keyMap.put( pool, new Long JavaDoc( poolKey ) );
70             m_factoryMap.put( factory, pool );
71         }
72
73         return pool;
74     }
75
76     /**
77      * Return a new key for the pool and controller.
78      */

79     private final long getKey()
80     {
81         return m_keyGenerator.nextLong();
82     }
83
84     private final static class PoolManagerCommand implements RepeatedCommand
85     {
86         private final Map JavaDoc m_map;
87         private final int m_min = 4;
88         private final int m_max = 256;
89         private final int m_grow = 4;
90
91         protected PoolManagerCommand( Map JavaDoc map )
92         {
93             m_map = map;
94         }
95
96         public long getDelayInterval()
97         {
98             return 10 * 1000L;
99         }
100
101         public long getRepeatInterval()
102         {
103             return 10 * 1000L;
104         }
105
106         public int getNumberOfRepeats()
107         {
108             return 0;
109         }
110
111         public void execute()
112             throws Exception JavaDoc
113         {
114             Iterator JavaDoc i = m_map.keySet().iterator();
115
116             while (i.hasNext())
117             {
118                 ManagablePool pool = (ManagablePool) i.next();
119                 long key = ((Long JavaDoc) m_map.get( pool )).longValue();
120                 int size = pool.size( key );
121
122                 if ( size < m_min )
123                 {
124                     pool.grow( m_grow, key );
125                 }
126
127                 if ( size > m_max )
128                 {
129                     pool.shrink( m_grow, key );
130                 }
131             }
132         }
133     }
134 }
135
Popular Tags