1 19 package org.apache.avalon.excalibur.logger; 20 21 import org.apache.avalon.framework.activity.Disposable; 22 import org.apache.avalon.framework.configuration.Configurable; 23 import org.apache.avalon.framework.configuration.Configuration; 24 import org.apache.avalon.framework.configuration.ConfigurationException; 25 import org.apache.avalon.framework.container.ContainerUtil; 26 import org.apache.avalon.framework.context.Context; 27 import org.apache.avalon.framework.context.ContextException; 28 import org.apache.avalon.framework.context.Contextualizable; 29 import org.apache.avalon.framework.logger.LogEnabled; 30 import org.apache.avalon.framework.logger.Logger; 31 import org.apache.avalon.framework.parameters.ParameterException; 32 import org.apache.avalon.framework.parameters.Parameterizable; 33 import org.apache.avalon.framework.parameters.Parameters; 34 import org.apache.avalon.framework.service.ServiceException; 35 import org.apache.avalon.framework.service.ServiceManager; 36 import org.apache.avalon.framework.service.Serviceable; 37 import org.apache.avalon.framework.thread.ThreadSafe; 38 39 48 49 public final class DefaultLoggerManager 50 implements LoggerManager, 51 ThreadSafe, 52 LogEnabled, 53 Contextualizable, 54 Configurable, 55 Serviceable, 56 Disposable 57 { 58 59 private LoggerManager m_loggermanager; 60 61 62 private Context m_context; 63 64 65 private Logger m_logger; 66 67 68 private String m_prefix; 69 70 71 private ServiceManager m_manager; 72 73 74 private boolean m_disposeManager = false; 75 76 79 public DefaultLoggerManager() 80 { 81 } 82 83 86 public DefaultLoggerManager( String prefix ) 87 { 88 m_prefix = prefix; 89 } 90 91 96 public void enableLogging( final Logger logger ) 97 { 98 m_logger = logger; 99 } 100 101 104 public final Logger getLoggerForCategory( final String categoryName ) 105 { 106 return m_loggermanager.getLoggerForCategory( categoryName ); 107 } 108 109 113 public final Logger getDefaultLogger() 114 { 115 return m_loggermanager.getDefaultLogger(); 116 } 117 118 124 public final void contextualize( final Context context ) 125 throws ContextException 126 { 127 m_context = context; 128 } 129 130 136 public final void configure( final Configuration configuration ) 137 throws ConfigurationException 138 { 139 final String className = configuration.getAttribute( "manager-class", null ); 141 142 if( null != className ) 143 { 144 final String prefix = configuration.getAttribute( "prefix", m_prefix ); 146 147 try 149 { 150 if( null == prefix ) 151 { 152 m_loggermanager = (LoggerManager)Class.forName( className ).newInstance(); 153 } 154 else 155 { 156 m_loggermanager = (LoggerManager)Class.forName( className ) 157 .getConstructor( new Class []{String .class} ) 158 .newInstance( new Object []{prefix} ); 159 } 160 } 161 catch( Exception e ) 162 { 163 throw new ConfigurationException( "Unable to create new logger manager for class " + className, e ); 164 } 165 166 ContainerUtil.enableLogging(m_loggermanager, m_logger ); 168 169 try 170 { 171 ContainerUtil.contextualize( m_loggermanager, m_context ); 172 } 173 catch( ContextException ce ) 174 { 175 throw new ConfigurationException( "Unable to contextualize new logger manager.", ce ); 176 } 177 178 try 179 { 180 ContainerUtil.service( m_loggermanager, m_manager ); 181 } 182 catch (ServiceException se ) 183 { 184 throw new ConfigurationException("Unable to service new logger manager.", se); 185 } 186 187 if( m_loggermanager instanceof Configurable ) 188 { 189 ( (Configurable)m_loggermanager ).configure( configuration.getChildren()[ 0 ] ); 190 } 191 else if( m_loggermanager instanceof Parameterizable ) 192 { 193 try 194 { 195 ( (Parameterizable)m_loggermanager ).parameterize( Parameters.fromConfiguration( configuration.getChildren()[ 0 ] ) ); 196 } 197 catch( ParameterException pe ) 198 { 199 throw new ConfigurationException( "Unable to parameterize new logger manager.", pe ); 200 } 201 } 202 203 try 204 { 205 ContainerUtil.initialize( m_loggermanager ); 206 } 207 catch (Exception e ) 208 { 209 throw new ConfigurationException("Unable to initialize new logger manager."); 210 } 211 } 212 else 213 { 214 final String roleName = configuration.getAttribute( "manager-role", null ); 216 if( null == roleName ) 217 { 218 throw new ConfigurationException( "The LoggerManager needs either a manager-role or a manager-class" ); 219 } 220 221 try 222 { 223 m_loggermanager = (LoggerManager)m_manager.lookup( roleName ); 224 m_disposeManager = true; 225 } 226 catch( ServiceException e ) 227 { 228 throw new ConfigurationException( "Unable to lookup logger manager with role " + roleName ); 229 } 230 } 231 } 232 233 public void service( ServiceManager manager ) 234 throws ServiceException 235 { 236 m_manager = manager; 237 } 238 239 public void dispose() 240 { 241 if( m_disposeManager && null != m_manager ) 242 { 243 m_manager.release( m_loggermanager ); 244 } 245 m_manager = null; 246 m_loggermanager = null; 247 m_disposeManager = false; 248 } 249 250 } 251 | Popular Tags |