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