1 8 package org.apache.avalon.excalibur.system.handler; 9 10 import org.apache.avalon.excalibur.system.RoleManager; 11 import org.apache.avalon.framework.activity.Startable; 12 import org.apache.avalon.framework.activity.Disposable; 13 import org.apache.avalon.framework.activity.Initializable; 14 import org.apache.avalon.framework.component.ComponentManager; 15 import org.apache.avalon.framework.component.Composable; 16 import org.apache.avalon.framework.configuration.Configurable; 17 import org.apache.avalon.framework.configuration.Configuration; 18 import org.apache.avalon.framework.parameters.Parameterizable; 19 import org.apache.avalon.framework.parameters.Parameters; 20 import org.apache.avalon.framework.context.Context; 21 import org.apache.avalon.framework.context.Contextualizable; 22 import org.apache.avalon.framework.logger.Logger; 23 import org.apache.avalon.framework.logger.LogEnabled; 24 import org.apache.avalon.framework.thread.ThreadSafe; 25 import org.apache.avalon.excalibur.pool.ObjectFactory; 26 import org.apache.avalon.excalibur.logger.LoggerManager; 27 28 36 public class ComponentFactory 37 implements ObjectFactory, ThreadSafe 38 { 39 42 private Class m_componentClass; 43 44 46 private Context m_context; 47 48 50 private ComponentManager m_componentManager; 51 52 54 private Configuration m_configuration; 55 56 58 private RoleManager m_roles; 59 60 62 private LoggerManager m_logManager; 63 64 66 private Logger m_logger; 67 68 77 public ComponentFactory( final Class componentClass, 78 final Configuration configuration, 79 final ComponentManager componentManager, 80 final Context context, 81 final RoleManager roles, 82 final LoggerManager logkit ) 83 { 84 m_componentClass = componentClass; 85 m_configuration = configuration; 86 m_componentManager = componentManager; 87 m_context = context; 88 m_roles = roles; 89 m_logManager = logkit; 90 m_logger = m_logManager.getLoggerForCategory("system.factory"); 91 } 92 93 public Object newInstance() 94 throws Exception 95 { 96 final Object component = m_componentClass.newInstance(); 97 98 if (m_logger.isDebugEnabled()) 99 { 100 m_logger.debug( "ComponentFactory creating new instance of " + 101 m_componentClass.getName() + "." ); 102 } 103 104 if( component instanceof LogEnabled ) 105 { 106 final String logger = m_configuration.getAttribute( "logger", null ); 107 if( null == logger ) 108 { 109 m_logger.debug( "no logger attribute available, using standard logger" ); 110 ((LogEnabled)component).enableLogging( m_logManager.getDefaultLogger() ); 111 } 112 else 113 { 114 m_logger.debug( "logger attribute is " + logger ); 115 ((LogEnabled)component).enableLogging( m_logManager.getLoggerForCategory( logger ) ); 116 } 117 } 118 119 if( component instanceof Contextualizable ) 120 { 121 ((Contextualizable)component).contextualize( m_context ); 122 } 123 124 if( component instanceof Composable ) 125 { 126 ((Composable)component).compose( m_componentManager ); 127 } 128 129 if( component instanceof Configurable ) 130 { 131 ((Configurable)component).configure( m_configuration ); 132 } 133 134 if( component instanceof Parameterizable ) 135 { 136 ((Parameterizable)component). 137 parameterize( Parameters.fromConfiguration( m_configuration ) ); 138 } 139 140 if( component instanceof Initializable ) 141 { 142 ((Initializable)component).initialize(); 143 } 144 145 if( component instanceof Startable ) 146 { 147 ((Startable)component).start(); 148 } 149 150 return component; 151 } 152 153 public final Class getCreatedClass() 154 { 155 return m_componentClass; 156 } 157 158 public final void decommission( final Object component ) 159 throws Exception 160 { 161 if (m_logger.isDebugEnabled()) 162 { 163 m_logger.debug( "ComponentFactory decommissioning instance of " + 164 m_componentClass.getName() + "." ); 165 } 166 167 if( component instanceof Startable ) 168 { 169 ((Startable)component).stop(); 170 } 171 172 if( component instanceof Disposable ) 173 { 174 ((Disposable)component).dispose(); 175 } 176 } 177 } 178 | Popular Tags |