1 8 package org.apache.avalon.excalibur.component; 9 10 import org.apache.avalon.framework.activity.Startable; 11 import org.apache.avalon.framework.activity.Disposable; 12 import org.apache.avalon.framework.component.Component; 13 import org.apache.avalon.framework.component.ComponentManager; 14 import org.apache.avalon.framework.configuration.Configuration; 15 import org.apache.avalon.framework.context.Context; 16 import org.apache.avalon.excalibur.logger.LogKitManager; 17 import org.apache.log.Logger; 18 19 27 public class ThreadSafeComponentHandler extends ComponentHandler { 28 private Component m_instance; 29 private final DefaultComponentFactory m_factory; 30 private boolean m_initialized = false; 31 private boolean m_disposed = false; 32 33 38 protected ThreadSafeComponentHandler( final Class componentClass, 39 final Configuration config, 40 final ComponentManager manager, 41 final Context context, 42 final RoleManager roles, 43 final LogKitManager logkit ) 44 throws Exception 45 { 46 m_factory = new DefaultComponentFactory( componentClass, config, manager, context, roles, logkit ); 47 } 48 49 54 protected ThreadSafeComponentHandler( final Component component ) 55 throws Exception 56 { 57 m_instance = component; 58 m_factory = null; 59 } 60 61 public void setLogger(Logger log) 62 { 63 if ( this.m_factory != null ) 64 { 65 m_factory.setLogger(log); 66 } 67 68 super.setLogger(log); 69 } 70 71 74 public void initialize() 75 throws Exception 76 { 77 if( m_initialized ) 78 { 79 return; 80 } 81 82 if (m_instance == null) 83 { 84 m_instance = (Component) this.m_factory.newInstance(); 85 } 86 87 if (getLogger().isDebugEnabled()) 88 { 89 if (this.m_factory != null) 90 { 91 getLogger().debug("ComponentHandler initialized for: " + this.m_factory.getCreatedClass().getName()); 92 } 93 else 94 { 95 getLogger().debug("ComponentHandler initialized for: " + this.m_instance.getClass().getName()); 96 } 97 } 98 99 m_initialized = true; 100 } 101 102 105 public final Component get() 106 throws Exception 107 { 108 if( ! m_initialized ) 109 { 110 throw new IllegalStateException ( "You cannot get a component from an uninitialized holder." ); 111 } 112 113 if( m_disposed ) 114 { 115 throw new IllegalStateException ( "You cannot get a component from a disposed holder" ); 116 } 117 118 return m_instance; 119 } 120 121 124 public void put( final Component component ) 125 { 126 if( !m_initialized ) 127 { 128 throw new IllegalStateException ( "You cannot put a component in an uninitialized holder." ); 129 } 130 } 131 132 135 public void dispose() 136 { 137 try { 138 if( null != m_factory ) 139 { 140 m_factory.decommission( m_instance ); 141 } 142 else 143 { 144 if( m_instance instanceof Startable ) 145 { 146 ((Startable)m_instance).stop(); 147 } 148 149 if( m_instance instanceof Disposable ) 150 { 151 ((Disposable)m_instance).dispose(); 152 } 153 } 154 155 m_instance = null; 156 } 157 catch( final Exception e ) 158 { 159 if (getLogger().isWarnEnabled()) 160 { 161 getLogger().warn( "Error decommissioning component: " + 162 m_factory.getCreatedClass().getName(), e ); 163 } 164 } 165 166 m_disposed = true; 167 } 168 } 169 | Popular Tags |