1 10 11 package org.picocontainer.gems.monitors; 12 13 import java.io.Serializable ; 14 import java.lang.reflect.Constructor ; 15 import java.lang.reflect.Method ; 16 import java.lang.reflect.Member ; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.picocontainer.ComponentMonitor; 21 import org.picocontainer.monitors.AbstractComponentMonitor; 22 import org.picocontainer.monitors.DefaultComponentMonitor; 23 24 25 34 public class CommonsLoggingComponentMonitor extends AbstractComponentMonitor implements Serializable { 35 36 private Log log; 37 private final ComponentMonitor delegate; 38 39 44 public CommonsLoggingComponentMonitor() { 45 delegate = new DefaultComponentMonitor(); 46 } 47 48 54 public CommonsLoggingComponentMonitor(Class logClass) { 55 this(logClass.getName()); 56 } 57 58 64 public CommonsLoggingComponentMonitor(String logName) { 65 this(LogFactory.getLog(logName)); 66 } 67 68 73 public CommonsLoggingComponentMonitor(Log log) { 74 this(); 75 this.log = log; 76 } 77 78 84 public CommonsLoggingComponentMonitor(Class logClass, ComponentMonitor delegate) { 85 this(logClass.getName(), delegate); 86 } 87 88 94 public CommonsLoggingComponentMonitor(String logName, ComponentMonitor delegate) { 95 this(LogFactory.getLog(logName), delegate); 96 } 97 98 103 public CommonsLoggingComponentMonitor(Log log, ComponentMonitor delegate) { 104 this.log = log; 105 this.delegate = delegate; 106 } 107 108 109 public void instantiating(Constructor constructor) { 110 Log log = getLog(constructor); 111 if (log.isDebugEnabled()) { 112 log.debug(format(INSTANTIATING, new Object []{toString(constructor)})); 113 } 114 delegate.instantiating(constructor); 115 } 116 117 public void instantiated(Constructor constructor, long duration) { 118 Log log = getLog(constructor); 119 if (log.isDebugEnabled()) { 120 log.debug(format(INSTANTIATED, new Object []{toString(constructor), new Long (duration)})); 121 } 122 delegate.instantiated(constructor, duration); 123 } 124 125 public void instantiated(Constructor constructor, Object instantiated, Object [] parameters, long duration) { 126 Log log = getLog(constructor); 127 if (log.isDebugEnabled()) { 128 log.debug(format(INSTANTIATED2, new Object []{toString(constructor), new Long (duration), instantiated.getClass().getName(), toString(parameters)})); 129 } 130 delegate.instantiated(constructor, instantiated, parameters, duration); 131 } 132 133 public void instantiationFailed(Constructor constructor, Exception cause) { 134 Log log = getLog(constructor); 135 if (log.isWarnEnabled()) { 136 log.warn(format(INSTANTIATION_FAILED, new Object []{toString(constructor), cause.getMessage()}), cause); 137 } 138 delegate.instantiationFailed(constructor, cause); 139 } 140 141 public void invoking(Method method, Object instance) { 142 Log log = getLog(method); 143 if (log.isDebugEnabled()) { 144 log.debug(format(INVOKING, new Object []{toString(method), instance})); 145 } 146 delegate.invoking(method, instance); 147 } 148 149 public void invoked(Method method, Object instance, long duration) { 150 Log log = getLog(method); 151 if (log.isDebugEnabled()) { 152 log.debug(format(INVOKED, new Object []{toString(method), instance, new Long (duration)})); 153 } 154 delegate.invoked(method, instance, duration); 155 } 156 157 public void invocationFailed(Method method, Object instance, Exception cause) { 158 Log log = getLog(method); 159 if (log.isWarnEnabled()) { 160 log.warn(format(INVOCATION_FAILED, new Object []{toString(method), instance, cause.getMessage()}), cause); 161 } 162 delegate.invocationFailed(method, instance, cause); 163 } 164 165 public void lifecycleInvocationFailed(Method method, Object instance, RuntimeException cause) { 166 Log log = getLog(method); 167 if (log.isWarnEnabled()) { 168 log.warn(format(LIFECYCLE_INVOCATION_FAILED, new Object []{toString(method), instance, cause.getMessage()}), cause); 169 } 170 delegate.lifecycleInvocationFailed(method, instance, cause); 171 } 172 173 protected Log getLog(Member member) { 174 if ( log != null ){ 175 return log; 176 } 177 return LogFactory.getLog(member.getDeclaringClass()); 178 } 179 180 } 181 | Popular Tags |