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.Startable; 13 import org.apache.avalon.framework.activity.Disposable; 14 import org.apache.avalon.framework.component.Component; 15 import org.apache.avalon.framework.component.ComponentManager; 16 import org.apache.avalon.framework.configuration.Configuration; 17 import org.apache.avalon.framework.context.Context; 18 import org.apache.avalon.excalibur.logger.LoggerManager; 19 import org.apache.avalon.framework.logger.Logger; 20 21 29 public class ThreadSafeComponentHandler implements ComponentHandler { 30 private Component m_instance; 31 private final ComponentFactory m_factory; 32 private boolean m_initialized = false; 33 private boolean m_disposed = false; 34 private final Logger m_logger; 35 36 41 protected ThreadSafeComponentHandler( final Class componentClass, 42 final Configuration config, 43 final ComponentManager manager, 44 final Context context ) 45 throws Exception 46 { 47 RoleManager roles = (RoleManager) context.get( Container.ROLE_MANAGER ); 48 LoggerManager logkit = (LoggerManager) context.get( Container.LOGGER_MANAGER ); 49 50 m_factory = new ComponentFactory( componentClass, config, manager, context, roles, logkit ); 51 m_logger = logkit.getLoggerForCategory("system.handler.threadsafe"); 52 } 53 54 public boolean isInitialized() 55 { 56 return m_initialized; 57 } 58 59 62 public void initialize() 63 throws Exception 64 { 65 if( m_initialized ) 66 { 67 return; 68 } 69 70 if (m_instance == null) 71 { 72 m_instance = (Component) this.m_factory.newInstance(); 73 } 74 75 if (m_logger.isDebugEnabled()) 76 { 77 if (this.m_factory != null) 78 { 79 m_logger.debug("ComponentHandler initialized for: " + this.m_factory.getCreatedClass().getName()); 80 } 81 else 82 { 83 m_logger.debug("ComponentHandler initialized for: " + this.m_instance.getClass().getName()); 84 } 85 } 86 87 m_initialized = true; 88 } 89 90 93 public final Component get() 94 throws Exception 95 { 96 if( ! m_initialized ) 97 { 98 throw new IllegalStateException ( "You cannot get a component from an uninitialized holder." ); 99 } 100 101 if( m_disposed ) 102 { 103 throw new IllegalStateException ( "You cannot get a component from a disposed holder" ); 104 } 105 106 return m_instance; 107 } 108 109 112 public void put( final Component component ) 113 { 114 if( !m_initialized ) 115 { 116 throw new IllegalStateException ( "You cannot put a component in an uninitialized holder." ); 117 } 118 } 119 120 123 public void dispose() 124 { 125 try { 126 if( null != m_factory ) 127 { 128 m_factory.decommission( m_instance ); 129 } 130 else 131 { 132 if( m_instance instanceof Startable ) 133 { 134 ((Startable)m_instance).stop(); 135 } 136 137 if( m_instance instanceof Disposable ) 138 { 139 ((Disposable)m_instance).dispose(); 140 } 141 } 142 143 m_instance = null; 144 } 145 catch( final Exception e ) 146 { 147 if (m_logger.isWarnEnabled()) 148 { 149 m_logger.warn( "Error decommissioning component: " + 150 m_factory.getCreatedClass().getName(), e ); 151 } 152 } 153 154 m_disposed = true; 155 } 156 } 157 | Popular Tags |