001: /*****************************************************************************
002: * Copyright (C) PicoContainer Organization. All rights reserved. *
003: * ------------------------------------------------------------------------- *
004: * The software in this package is published under the terms of the BSD *
005: * style license a copy of which has been included with this distribution in *
006: * the LICENSE.txt file. *
007: * *
008: * Original code by Mauro Talevi *
009: *****************************************************************************/package org.picocontainer.gems.monitors;
010:
011: import static org.picocontainer.monitors.ComponentMonitorHelper.methodToString;
012: import static org.picocontainer.monitors.ComponentMonitorHelper.memberToString;
013: import static org.picocontainer.monitors.ComponentMonitorHelper.ctorToString;
014: import static org.picocontainer.monitors.ComponentMonitorHelper.parmsToString;
015:
016: import java.io.Serializable;
017: import java.lang.reflect.Constructor;
018: import java.lang.reflect.Method;
019: import java.lang.reflect.Member;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.picocontainer.ComponentMonitor;
024: import org.picocontainer.ComponentAdapter;
025: import org.picocontainer.MutablePicoContainer;
026: import org.picocontainer.PicoContainer;
027: import org.picocontainer.monitors.ComponentMonitorHelper;
028: import org.picocontainer.monitors.NullComponentMonitor;
029:
030: /**
031: * A {@link ComponentMonitor} which writes to a Commons Logging {@link Log Log} instance.
032: * The Log instance can either be injected or, if not set, the {@link LogFactory LogFactory}
033: * will be used to retrieve it at every invocation of the monitor.
034: *
035: * @author Paul Hammant
036: * @author Mauro Talevi
037: */
038: public class CommonsLoggingComponentMonitor implements
039: ComponentMonitor, Serializable {
040:
041: /**
042: * Serialization UUID.
043: */
044: private static final long serialVersionUID = 5863003718112457388L;
045:
046: /**
047: * Commons Logger.
048: */
049: private Log log;
050:
051: /**
052: * Delegate for component monitor chains.
053: */
054: private final ComponentMonitor delegate;
055:
056: /**
057: * Creates a CommonsLoggingComponentMonitor with no Log instance set.
058: * The {@link LogFactory LogFactory} will be used to retrieve the Log instance
059: * at every invocation of the monitor.
060: */
061: public CommonsLoggingComponentMonitor() {
062: delegate = new NullComponentMonitor();
063: }
064:
065: /**
066: * Creates a CommonsLoggingComponentMonitor with a given Log instance class.
067: * The class name is used to retrieve the Log instance.
068: *
069: * @param logClass the class of the Log
070: */
071: public CommonsLoggingComponentMonitor(Class<?> logClass) {
072: this (logClass.getName());
073: }
074:
075: /**
076: * Creates a CommonsLoggingComponentMonitor with a given Log instance name. It uses the
077: * {@link LogFactory LogFactory} to create the Log instance.
078: *
079: * @param logName the name of the Log
080: */
081: public CommonsLoggingComponentMonitor(String logName) {
082: this (LogFactory.getLog(logName));
083: }
084:
085: /**
086: * Creates a CommonsLoggingComponentMonitor with a given Log instance
087: *
088: * @param log the Log to write to
089: */
090: public CommonsLoggingComponentMonitor(Log log) {
091: this ();
092: this .log = log;
093: }
094:
095: /**
096: * Creates a CommonsLoggingComponentMonitor with a given Log instance class.
097: * The class name is used to retrieve the Log instance.
098: *
099: * @param logClass the class of the Log
100: * @param delegate the delegate
101: */
102: public CommonsLoggingComponentMonitor(Class<?> logClass,
103: ComponentMonitor delegate) {
104: this (logClass.getName(), delegate);
105: }
106:
107: /**
108: * Creates a CommonsLoggingComponentMonitor with a given Log instance name. It uses the
109: * {@link LogFactory LogFactory} to create the Log instance.
110: *
111: * @param logName the name of the Log
112: * @param delegate the delegate
113: */
114: public CommonsLoggingComponentMonitor(String logName,
115: ComponentMonitor delegate) {
116: this (LogFactory.getLog(logName), delegate);
117: }
118:
119: /**
120: * Creates a CommonsLoggingComponentMonitor with a given Log instance
121: *
122: * @param log the Log to write to
123: * @param delegate the delegate
124: */
125: public CommonsLoggingComponentMonitor(Log log,
126: ComponentMonitor delegate) {
127: this .log = log;
128: this .delegate = delegate;
129: }
130:
131: public <T> Constructor<T> instantiating(PicoContainer container,
132: ComponentAdapter<T> componentAdapter,
133: Constructor<T> constructor) {
134: Log log = getLog(constructor);
135: if (log.isDebugEnabled()) {
136: log.debug(ComponentMonitorHelper.format(
137: ComponentMonitorHelper.INSTANTIATING,
138: ctorToString(constructor)));
139: }
140: return delegate.instantiating(container, componentAdapter,
141: constructor);
142: }
143:
144: public <T> void instantiated(PicoContainer container,
145: ComponentAdapter<T> componentAdapter,
146: Constructor<T> constructor, Object instantiated,
147: Object[] parameters, long duration) {
148: Log log = getLog(constructor);
149: if (log.isDebugEnabled()) {
150: log.debug(ComponentMonitorHelper.format(
151: ComponentMonitorHelper.INSTANTIATED,
152: ctorToString(constructor), duration, instantiated
153: .getClass().getName(),
154: parmsToString(parameters)));
155: }
156: delegate.instantiated(container, componentAdapter, constructor,
157: instantiated, parameters, duration);
158: }
159:
160: public <T> void instantiationFailed(PicoContainer container,
161: ComponentAdapter<T> componentAdapter,
162: Constructor<T> constructor, Exception cause) {
163: Log log = getLog(constructor);
164: if (log.isWarnEnabled()) {
165: log.warn(ComponentMonitorHelper.format(
166: ComponentMonitorHelper.INSTANTIATION_FAILED,
167: ctorToString(constructor), cause.getMessage()),
168: cause);
169: }
170: delegate.instantiationFailed(container, componentAdapter,
171: constructor, cause);
172: }
173:
174: public void invoking(PicoContainer container,
175: ComponentAdapter<?> componentAdapter, Member member,
176: Object instance) {
177: Log log = getLog(member);
178: if (log.isDebugEnabled()) {
179: log.debug(ComponentMonitorHelper.format(
180: ComponentMonitorHelper.INVOKING,
181: memberToString(member), instance));
182: }
183: delegate
184: .invoking(container, componentAdapter, member, instance);
185: }
186:
187: public void invoked(PicoContainer container,
188: ComponentAdapter<?> componentAdapter, Method method,
189: Object instance, long duration) {
190: Log log = getLog(method);
191: if (log.isDebugEnabled()) {
192: log.debug(ComponentMonitorHelper.format(
193: ComponentMonitorHelper.INVOKED,
194: methodToString(method), instance, duration));
195: }
196: delegate.invoked(container, componentAdapter, method, instance,
197: duration);
198: }
199:
200: public void invocationFailed(Member member, Object instance,
201: Exception cause) {
202: Log log = getLog(member);
203: if (log.isWarnEnabled()) {
204: log.warn(ComponentMonitorHelper.format(
205: ComponentMonitorHelper.INVOCATION_FAILED,
206: memberToString(member), instance, cause
207: .getMessage()), cause);
208: }
209: delegate.invocationFailed(member, instance, cause);
210: }
211:
212: public void lifecycleInvocationFailed(
213: MutablePicoContainer container,
214: ComponentAdapter<?> componentAdapter, Method method,
215: Object instance, RuntimeException cause) {
216: Log log = getLog(method);
217: if (log.isWarnEnabled()) {
218: log.warn(ComponentMonitorHelper.format(
219: ComponentMonitorHelper.LIFECYCLE_INVOCATION_FAILED,
220: methodToString(method), instance, cause
221: .getMessage()), cause);
222: }
223: delegate.lifecycleInvocationFailed(container, componentAdapter,
224: method, instance, cause);
225: }
226:
227: public Object noComponentFound(MutablePicoContainer container,
228: Object componentKey) {
229: Log log = this .log != null ? this .log : LogFactory
230: .getLog(ComponentMonitor.class);
231: if (log.isWarnEnabled()) {
232: log.warn(ComponentMonitorHelper.format(
233: ComponentMonitorHelper.NO_COMPONENT, componentKey));
234: }
235: return delegate.noComponentFound(container, componentKey);
236: }
237:
238: protected Log getLog(Member member) {
239: if (log != null) {
240: return log;
241: }
242: return LogFactory.getLog(member.getDeclaringClass());
243: }
244:
245: }
|