1 8 package org.apache.avalon.excalibur.system.handler; 9 10 import org.apache.avalon.excalibur.system.Container; 11 import org.apache.avalon.excalibur.system.RoleManager; 12 import org.apache.avalon.framework.activity.Disposable; 13 import org.apache.avalon.framework.component.Component; 14 import org.apache.avalon.framework.component.ComponentManager; 15 import org.apache.avalon.framework.configuration.Configuration; 16 import org.apache.avalon.framework.context.Context; 17 import org.apache.avalon.excalibur.pool.Pool; 18 import org.apache.avalon.excalibur.pool.Poolable; 19 import org.apache.avalon.excalibur.pool.PoolManager; 20 import org.apache.avalon.excalibur.logger.LoggerManager; 21 import org.apache.avalon.framework.logger.Logger; 22 import org.apache.log.Hierarchy; 23 24 32 public class PoolableComponentHandler implements ComponentHandler { 33 34 private final ComponentFactory m_factory; 35 36 37 private final Pool m_pool; 38 39 40 private boolean m_initialized = false; 41 42 43 private boolean m_disposed = false; 44 45 46 private final Logger m_logger; 47 48 49 54 protected PoolableComponentHandler( final Class componentClass, 55 final Configuration config, 56 final ComponentManager manager, 57 final Context context ) 58 throws Exception 59 { 60 RoleManager roles = (RoleManager) context.get( Container.ROLE_MANAGER ); 61 LoggerManager logkit = (LoggerManager) context.get( Container.LOGGER_MANAGER ); 62 m_factory = 63 new ComponentFactory( componentClass, config, manager, context, roles, logkit ); 64 65 PoolManager poolManager = (PoolManager) context.get( Container.POOL_MANAGER ); 66 m_pool = poolManager.getManagedPool( m_factory, config.getAttributeAsInteger( "pool-min", 4 ) ); 67 m_logger = logkit.getLoggerForCategory("system.handler.poolable"); 68 } 69 70 public boolean isInitialized() 71 { 72 return m_initialized; 73 } 74 75 78 public void initialize() 79 { 80 if( m_initialized ) 81 { 82 return; 83 } 84 85 if (m_logger.isDebugEnabled()) 86 { 87 m_logger.debug( "ComponentHandler initialized for: " + 88 m_factory.getCreatedClass().getName() ); 89 } 90 91 m_initialized = true; 92 } 93 94 97 public Component get() 98 throws Exception 99 { 100 if( ! m_initialized ) 101 { 102 throw new IllegalStateException ( "You cannot get a component from an " + 103 "uninitialized holder." ); 104 } 105 106 if( m_disposed ) 107 { 108 throw new IllegalStateException ( "You cannot get a component from " + 109 "a disposed holder" ); 110 } 111 112 return (Component)m_pool.get(); 113 } 114 115 118 public void put( final Component component ) 119 { 120 if( !m_initialized ) 121 { 122 throw new IllegalStateException ( "You cannot put a component in " + 123 "an uninitialized holder." ); 124 } 125 126 m_pool.put( (Poolable)component ); 127 } 128 129 132 public void dispose() 133 { 134 if( m_factory instanceof Disposable ) 135 { 136 ((Disposable)m_factory).dispose(); 137 } 138 139 m_disposed = true; 140 } 141 } 142 | Popular Tags |