KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Random JavaDoc;
22
23 import org.apache.commons.collections.StaticBucketMap;
24 import org.apache.excalibur.event.Sink;
25 import org.apache.excalibur.event.command.RepeatedCommand;
26
27 /**
28  * This interface is for a PoolManager that creates pools that are managed
29  * asynchronously. The contract is that the controller type is specified in
30  * the constructor.
31  *
32  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
33  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:34 $
34  * @since 4.1
35  */

36 public class DefaultPoolManager implements PoolManager
37 {
38     private final Random JavaDoc m_keyGenerator;
39     private final Map JavaDoc m_keyMap = new StaticBucketMap();
40     private final Map JavaDoc m_factoryMap = new StaticBucketMap();
41
42     public DefaultPoolManager()
43     {
44         this( null );
45     }
46
47     public DefaultPoolManager( final Sink commandSink )
48     {
49         m_keyGenerator = new Random JavaDoc();
50
51         if( null != commandSink )
52         {
53             try
54             {
55                 commandSink.enqueue( new PoolManagerCommand( m_keyMap ) );
56             }
57             catch( Exception JavaDoc e )
58             {
59                 // ignore silently for now
60
}
61         }
62     }
63
64     /**
65      * Return a managed pool that has a controller.
66      */

67     public Pool getManagedPool( ObjectFactory factory, int initialEntries )
68         throws Exception JavaDoc
69     {
70         ManagablePool pool = (ManagablePool)m_factoryMap.get( factory );
71
72         if( null == pool )
73         {
74             final long poolKey = getKey();
75             pool = new VariableSizePool( factory, initialEntries, poolKey );
76             m_keyMap.put( pool, new Long JavaDoc( poolKey ) );
77             m_factoryMap.put( factory, pool );
78         }
79
80         return pool;
81     }
82
83     /**
84      * Return a new key for the pool and controller.
85      */

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