1 8 package org.apache.avalon.excalibur.pool; 9 10 import java.security.SecureRandom ; 11 import org.apache.avalon.excalibur.command.RepeatedCommand; 12 import org.apache.avalon.excalibur.event.Queue; 13 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 27 public class DefaultPoolManager implements PoolManager 28 { 29 private final long m_managerKey; 30 private final SecureRandom m_keyGenerator; 31 private final HashMap m_keyMap = new HashMap (); 32 private final HashMap m_factoryMap = new HashMap (); 33 34 public DefaultPoolManager() 35 { 36 this( null ); 37 } 38 39 public DefaultPoolManager( final Queue commandQueue ) 40 { 41 m_keyGenerator = new SecureRandom (); 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 e) 51 { 52 } 54 } 55 } 56 57 60 public Pool getManagedPool( ObjectFactory factory, int initialEntries ) 61 throws Exception 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 ( poolKey ) ); 70 m_factoryMap.put( factory, pool ); 71 } 72 73 return pool; 74 } 75 76 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 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 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 113 { 114 Iterator i = m_map.keySet().iterator(); 115 116 while (i.hasNext()) 117 { 118 ManagablePool pool = (ManagablePool) i.next(); 119 long key = ((Long ) 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 |