1 8 package org.apache.avalon.excalibur.component; 9 10 import org.apache.avalon.framework.activity.Disposable; 11 import org.apache.avalon.framework.component.Component; 12 import org.apache.avalon.framework.component.ComponentManager; 13 import org.apache.avalon.framework.configuration.Configuration; 14 import org.apache.avalon.framework.context.Context; 15 import org.apache.avalon.excalibur.pool.Poolable; 16 import org.apache.avalon.excalibur.logger.LogKitManager; 17 import org.apache.log.Logger; 18 19 27 public class PoolableComponentHandler extends ComponentHandler { 28 29 private final DefaultComponentFactory m_factory; 30 31 32 private final DefaultComponentPool m_pool; 33 34 35 private boolean m_initialized = false; 36 37 38 private boolean m_disposed = false; 39 40 45 protected PoolableComponentHandler( final Class componentClass, 46 final Configuration config, 47 final ComponentManager manager, 48 final Context context, 49 final RoleManager roles, 50 final LogKitManager logkit ) 51 throws Exception 52 { 53 m_factory = 54 new DefaultComponentFactory( componentClass, config, manager, context, roles, logkit ); 55 56 int min = config.getAttributeAsInteger("pool-min", DefaultComponentPool.DEFAULT_POOL_SIZE/4); 57 int max = config.getAttributeAsInteger("pool-max", DefaultComponentPool.DEFAULT_POOL_SIZE); 58 int grow = config.getAttributeAsInteger("pool-grow", min); 59 DefaultComponentPoolController controller = 60 new DefaultComponentPoolController(grow); 61 62 m_pool = new DefaultComponentPool( m_factory, controller, min, max ); 63 } 64 65 68 public void setLogger( final Logger logger ) 69 { 70 m_factory.setLogger( logger ); 71 m_pool.setLogger( logger ); 72 73 super.setLogger( logger ); 74 } 75 76 79 public void initialize() 80 { 81 if( m_initialized ) 82 { 83 return; 84 } 85 86 try 87 { 88 m_pool.initialize(); 89 } 90 catch( Exception e ) 91 { 92 if (getLogger().isErrorEnabled()) 93 { 94 getLogger().error( "Cannot use component: " + 95 m_factory.getCreatedClass().getName(), e ); 96 } 97 } 98 99 if (getLogger().isDebugEnabled()) 100 { 101 getLogger().debug( "ComponentHandler initialized for: " + 102 m_factory.getCreatedClass().getName() ); 103 } 104 105 m_initialized = true; 106 } 107 108 111 public Component get() 112 throws Exception 113 { 114 if( ! m_initialized ) 115 { 116 throw new IllegalStateException ( "You cannot get a component from an " + 117 "uninitialized holder." ); 118 } 119 120 if( m_disposed ) 121 { 122 throw new IllegalStateException ( "You cannot get a component from " + 123 "a disposed holder" ); 124 } 125 126 return (Component)m_pool.get(); 127 } 128 129 132 public void put( final Component component ) 133 { 134 if( !m_initialized ) 135 { 136 throw new IllegalStateException ( "You cannot put a component in " + 137 "an uninitialized holder." ); 138 } 139 140 m_pool.put( (Poolable)component ); 141 } 142 143 146 public void dispose() 147 { 148 m_pool.dispose(); 149 150 if( m_factory instanceof Disposable ) 151 { 152 ((Disposable)m_factory).dispose(); 153 } 154 155 m_disposed = true; 156 } 157 } 158 | Popular Tags |