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 PerThreadComponentHandler implements ComponentHandler { 30 private ThreadLocalComponent 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 PerThreadComponentHandler ( 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_instance = new ThreadLocalComponent( m_factory ); 52 m_logger = logkit.getLoggerForCategory("system.handler.perthread"); 53 } 54 55 public boolean isInitialized() 56 { 57 return m_initialized; 58 } 59 60 63 public void initialize() 64 throws Exception 65 { 66 if( m_initialized ) 67 { 68 return; 69 } 70 71 if (m_logger.isDebugEnabled()) 72 { 73 if (this.m_factory != null) 74 { 75 m_logger.debug("ComponentHandler initialized for: " + this.m_factory.getCreatedClass().getName()); 76 } 77 else 78 { 79 m_logger.debug("ComponentHandler initialized for: " + this.m_instance.getClass().getName()); 80 } 81 } 82 83 m_initialized = true; 84 } 85 86 89 public final Component get() 90 throws Exception 91 { 92 if( ! m_initialized ) 93 { 94 throw new IllegalStateException ( "You cannot get a component from an uninitialized holder." ); 95 } 96 97 if( m_disposed ) 98 { 99 throw new IllegalStateException ( "You cannot get a component from a disposed holder" ); 100 } 101 102 return m_instance.getComponent(); 103 } 104 105 108 public void put( final Component component ) 109 { 110 if( !m_initialized ) 111 { 112 throw new IllegalStateException ( "You cannot put a component in an uninitialized holder." ); 113 } 114 } 115 116 119 public void dispose() 120 { 121 try { 122 m_factory.decommission( m_instance.getComponent() ); 123 124 m_instance = null; 125 } 126 catch( final Exception e ) 127 { 128 if (m_logger.isWarnEnabled()) 129 { 130 m_logger.warn( "Error decommissioning component: " + 131 m_factory.getCreatedClass().getName(), e ); 132 } 133 } 134 135 m_disposed = true; 136 } 137 138 private final static class ThreadLocalComponent extends ThreadLocal 139 { 140 private final ComponentFactory m_factory; 141 142 protected ThreadLocalComponent(ComponentFactory factory) 143 { 144 m_factory = factory; 145 } 146 147 protected Object initialValue() 148 { 149 try 150 { 151 return m_factory.newInstance(); 152 } 153 catch (Exception e) 154 { 155 return null; 156 } 157 } 158 159 public Component getComponent() 160 { 161 return (Component) this.get(); 162 } 163 } 164 } 165 | Popular Tags |