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.framework.logger.AbstractLoggable; 16 import org.apache.avalon.excalibur.logger.LogKitManager; 17 import org.apache.log.Logger; 18 19 27 class DefaultComponentHandler 28 extends ComponentHandler 29 { 30 31 private final DefaultComponentFactory m_factory; 32 33 34 private boolean m_initialized = false; 35 36 37 private boolean m_disposed = false; 38 39 44 protected DefaultComponentHandler( final Class componentClass, 45 final Configuration config, 46 final ComponentManager manager, 47 final Context context, 48 final RoleManager roles, 49 final LogKitManager logkit ) 50 throws Exception 51 { 52 m_factory = new DefaultComponentFactory( componentClass, config, manager, context, roles, logkit ); 53 } 54 55 58 public void setLogger( final Logger logger ) 59 { 60 m_factory.setLogger( logger ); 61 62 super.setLogger( logger ); 63 } 64 65 68 public void initialize() 69 { 70 if( m_initialized ) 71 { 72 return; 73 } 74 75 if (getLogger().isDebugEnabled()) 76 { 77 getLogger().debug("ComponentHandler initialized for: " + this.m_factory.getCreatedClass().getName()); 78 } 79 m_initialized = true; 80 } 81 82 85 public Component get() 86 throws Exception 87 { 88 if( ! m_initialized ) 89 { 90 throw new IllegalStateException ( "You cannot get a component from an uninitialized holder." ); 91 } 92 93 if( m_disposed ) 94 { 95 throw new IllegalStateException ( "You cannot get a component from a disposed holder" ); 96 } 97 98 return (Component)m_factory.newInstance(); 99 } 100 101 104 public void put( final Component component ) 105 { 106 if( !m_initialized ) 107 { 108 throw new IllegalStateException ( "You cannot put a component in an uninitialized holder." ); 109 } 110 111 try 112 { 113 m_factory.decommission( component ); 114 } 115 catch( final Exception e ) 116 { 117 if (getLogger().isWarnEnabled()) 118 { 119 getLogger().warn( "Error decommissioning component: " + 120 m_factory.getCreatedClass().getName(), e); 121 } 122 } 123 } 124 125 128 public void dispose() 129 { 130 try 131 { 132 134 if( m_factory instanceof Disposable ) 135 { 136 ((Disposable)m_factory).dispose(); 137 } 138 } 139 catch( final Exception e ) 140 { 141 if (getLogger().isWarnEnabled()) 142 { 143 getLogger().warn( "Error decommissioning component: " + 144 m_factory.getCreatedClass().getName(), e ); 145 } 146 } 147 148 m_disposed = true; 149 } 150 } 151 | Popular Tags |