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.activity.Initializable; 13 import org.apache.avalon.framework.component.ComponentManager; 14 import org.apache.avalon.framework.component.Composable; 15 import org.apache.avalon.framework.configuration.Configurable; 16 import org.apache.avalon.framework.configuration.Configuration; 17 import org.apache.avalon.framework.parameters.Parameterizable; 18 import org.apache.avalon.framework.parameters.Parameters; 19 import org.apache.avalon.framework.context.Context; 20 import org.apache.avalon.framework.context.Contextualizable; 21 import org.apache.avalon.framework.logger.AbstractLoggable; 22 import org.apache.avalon.framework.logger.Loggable; 23 import org.apache.avalon.framework.logger.LogEnabled; 24 import org.apache.avalon.framework.logger.LogKitLogger; 25 import org.apache.avalon.framework.thread.ThreadSafe; 26 import org.apache.avalon.excalibur.pool.ObjectFactory; 27 import org.apache.avalon.excalibur.logger.LogKitManager; 28 import org.apache.avalon.excalibur.logger.LogKitManageable; 29 30 38 public class DefaultComponentFactory 39 extends AbstractLoggable 40 implements ObjectFactory, ThreadSafe 41 { 42 45 private Class m_componentClass; 46 47 49 private Context m_context; 50 51 53 private ComponentManager m_componentManager; 54 55 57 private Configuration m_configuration; 58 59 61 private RoleManager m_roles; 62 63 65 private LogKitManager m_logkit; 66 67 69 private org.apache.avalon.framework.logger.Logger m_logEnabledLogger; 70 71 80 public DefaultComponentFactory( final Class componentClass, 81 final Configuration configuration, 82 final ComponentManager componentManager, 83 final Context context, 84 final RoleManager roles, 85 final LogKitManager logkit ) 86 { 87 m_componentClass = componentClass; 88 m_configuration = configuration; 89 m_componentManager = componentManager; 90 m_context = context; 91 m_roles = roles; 92 m_logkit = logkit; 93 } 94 95 public Object newInstance() 96 throws Exception 97 { 98 final Object component = m_componentClass.newInstance(); 99 100 if (getLogger().isDebugEnabled()) 101 { 102 getLogger().debug( "ComponentFactory creating new instance of " + 103 m_componentClass.getName() + "." ); 104 } 105 106 if( component instanceof LogEnabled ) 107 { 108 if( null == m_logkit || null == m_configuration ) 109 { 110 ((LogEnabled)component).enableLogging( getLogEnabledLogger() ); 111 } 112 else 113 { 114 final String logger = m_configuration.getAttribute( "logger", null ); 115 if( null == logger ) 116 { 117 getLogger().debug( "no logger attribute available, using standard logger" ); 118 ((LogEnabled)component).enableLogging( getLogEnabledLogger() ); 119 } 120 else 121 { 122 getLogger().debug( "logger attribute is " + logger ); 123 ((LogEnabled)component).enableLogging( new LogKitLogger( m_logkit.getLogger( logger ) ) ); 124 } 125 } 126 } 127 128 if( component instanceof Loggable ) 129 { 130 if( null == m_logkit || null == m_configuration ) 131 { 132 ((Loggable)component).setLogger( getLogger() ); 133 } 134 else 135 { 136 final String logger = m_configuration.getAttribute( "logger", null ); 137 if( null == logger ) 138 { 139 getLogger().debug( "no logger attribute available, using standard logger" ); 140 ((Loggable)component).setLogger( getLogger() ); 141 } 142 else 143 { 144 getLogger().debug( "logger attribute is " + logger ); 145 ((Loggable)component).setLogger( m_logkit.getLogger( logger ) ); 146 } 147 } 148 } 149 150 if( component instanceof Contextualizable ) 151 { 152 ((Contextualizable)component).contextualize( m_context ); 153 } 154 155 if( component instanceof Composable ) 156 { 157 ((Composable)component).compose( m_componentManager ); 158 } 159 160 if ( component instanceof RoleManageable ) 161 { 162 ((RoleManageable)component).setRoleManager( m_roles ); 163 } 164 165 if ( component instanceof LogKitManageable ) 166 { 167 ((LogKitManageable)component).setLogKitManager( m_logkit ); 168 } 169 170 if( component instanceof Configurable ) 171 { 172 ((Configurable)component).configure( m_configuration ); 173 } 174 175 if( component instanceof Parameterizable ) 176 { 177 ((Parameterizable)component). 178 parameterize( Parameters.fromConfiguration( m_configuration ) ); 179 } 180 181 if( component instanceof Initializable ) 182 { 183 ((Initializable)component).initialize(); 184 } 185 186 if( component instanceof Startable ) 187 { 188 ((Startable)component).start(); 189 } 190 191 return component; 192 } 193 194 public final Class getCreatedClass() 195 { 196 return m_componentClass; 197 } 198 199 public final void decommission( final Object component ) 200 throws Exception 201 { 202 if (getLogger().isDebugEnabled()) 203 { 204 getLogger().debug( "ComponentFactory decommissioning instance of " + 205 m_componentClass.getName() + "." ); 206 } 207 208 if( component instanceof Startable ) 209 { 210 ((Startable)component).stop(); 211 } 212 213 if( component instanceof Disposable ) 214 { 215 ((Disposable)component).dispose(); 216 } 217 } 218 219 protected org.apache.avalon.framework.logger.Logger getLogEnabledLogger() 220 { 221 if ( null == m_logEnabledLogger ) 222 { 223 m_logEnabledLogger = new LogKitLogger( getLogger() ); 224 } 225 226 return m_logEnabledLogger; 227 } 228 } 229 | Popular Tags |