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