KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > gems > monitors > CommonsLoggingComponentMonitor


1 /*****************************************************************************
2  * Copyright (C) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by Mauro Talevi *
9  *****************************************************************************/

10
11 package org.picocontainer.gems.monitors;
12
13 import java.io.Serializable JavaDoc;
14 import java.lang.reflect.Constructor JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16 import java.lang.reflect.Member JavaDoc;
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 /**
26  * A {@link ComponentMonitor} which writes to a Commons Logging {@link Log Log} instance.
27  * The Log instance can either be injected or, if not set, the {@link LogFactory LogFactory}
28  * will be used to retrieve it at every invocation of the monitor.
29  *
30  * @author Paul Hammant
31  * @author Mauro Talevi
32  * @version $Revision: $
33  */

34 public class CommonsLoggingComponentMonitor extends AbstractComponentMonitor implements Serializable JavaDoc {
35
36     private Log log;
37     private final ComponentMonitor delegate;
38
39     /**
40      * Creates a CommonsLoggingComponentMonitor with no Log instance set.
41      * The {@link LogFactory LogFactory} will be used to retrieve the Log instance
42      * at every invocation of the monitor.
43      */

44     public CommonsLoggingComponentMonitor() {
45         delegate = new DefaultComponentMonitor();
46     }
47
48     /**
49      * Creates a CommonsLoggingComponentMonitor with a given Log instance class.
50      * The class name is used to retrieve the Log instance.
51      *
52      * @param logClass the class of the Log
53      */

54     public CommonsLoggingComponentMonitor(Class JavaDoc logClass) {
55         this(logClass.getName());
56     }
57
58     /**
59      * Creates a CommonsLoggingComponentMonitor with a given Log instance name. It uses the
60      * {@link LogFactory LogFactory} to create the Log instance.
61      *
62      * @param logName the name of the Log
63      */

64     public CommonsLoggingComponentMonitor(String JavaDoc logName) {
65         this(LogFactory.getLog(logName));
66     }
67
68     /**
69      * Creates a CommonsLoggingComponentMonitor with a given Log instance
70      *
71      * @param log the Log to write to
72      */

73     public CommonsLoggingComponentMonitor(Log log) {
74         this();
75         this.log = log;
76     }
77
78     /**
79      * Creates a CommonsLoggingComponentMonitor with a given Log instance class.
80      * The class name is used to retrieve the Log instance.
81      *
82      * @param logClass the class of the Log
83      */

84     public CommonsLoggingComponentMonitor(Class JavaDoc logClass, ComponentMonitor delegate) {
85         this(logClass.getName(), delegate);
86     }
87
88     /**
89      * Creates a CommonsLoggingComponentMonitor with a given Log instance name. It uses the
90      * {@link LogFactory LogFactory} to create the Log instance.
91      *
92      * @param logName the name of the Log
93      */

94     public CommonsLoggingComponentMonitor(String JavaDoc logName, ComponentMonitor delegate) {
95         this(LogFactory.getLog(logName), delegate);
96     }
97
98     /**
99      * Creates a CommonsLoggingComponentMonitor with a given Log instance
100      *
101      * @param log the Log to write to
102      */

103     public CommonsLoggingComponentMonitor(Log log, ComponentMonitor delegate) {
104         this.log = log;
105         this.delegate = delegate;
106     }
107
108
109     public void instantiating(Constructor JavaDoc constructor) {
110         Log log = getLog(constructor);
111         if (log.isDebugEnabled()) {
112             log.debug(format(INSTANTIATING, new Object JavaDoc[]{toString(constructor)}));
113         }
114         delegate.instantiating(constructor);
115     }
116
117     public void instantiated(Constructor JavaDoc constructor, long duration) {
118         Log log = getLog(constructor);
119         if (log.isDebugEnabled()) {
120             log.debug(format(INSTANTIATED, new Object JavaDoc[]{toString(constructor), new Long JavaDoc(duration)}));
121         }
122         delegate.instantiated(constructor, duration);
123     }
124
125     public void instantiated(Constructor JavaDoc constructor, Object JavaDoc instantiated, Object JavaDoc[] parameters, long duration) {
126         Log log = getLog(constructor);
127         if (log.isDebugEnabled()) {
128             log.debug(format(INSTANTIATED2, new Object JavaDoc[]{toString(constructor), new Long JavaDoc(duration), instantiated.getClass().getName(), toString(parameters)}));
129         }
130         delegate.instantiated(constructor, instantiated, parameters, duration);
131     }
132
133     public void instantiationFailed(Constructor JavaDoc constructor, Exception JavaDoc cause) {
134         Log log = getLog(constructor);
135         if (log.isWarnEnabled()) {
136             log.warn(format(INSTANTIATION_FAILED, new Object JavaDoc[]{toString(constructor), cause.getMessage()}), cause);
137         }
138         delegate.instantiationFailed(constructor, cause);
139     }
140
141     public void invoking(Method JavaDoc method, Object JavaDoc instance) {
142         Log log = getLog(method);
143         if (log.isDebugEnabled()) {
144             log.debug(format(INVOKING, new Object JavaDoc[]{toString(method), instance}));
145         }
146         delegate.invoking(method, instance);
147     }
148
149     public void invoked(Method JavaDoc method, Object JavaDoc instance, long duration) {
150         Log log = getLog(method);
151         if (log.isDebugEnabled()) {
152             log.debug(format(INVOKED, new Object JavaDoc[]{toString(method), instance, new Long JavaDoc(duration)}));
153         }
154         delegate.invoked(method, instance, duration);
155     }
156
157     public void invocationFailed(Method JavaDoc method, Object JavaDoc instance, Exception JavaDoc cause) {
158         Log log = getLog(method);
159         if (log.isWarnEnabled()) {
160             log.warn(format(INVOCATION_FAILED, new Object JavaDoc[]{toString(method), instance, cause.getMessage()}), cause);
161         }
162         delegate.invocationFailed(method, instance, cause);
163     }
164
165     public void lifecycleInvocationFailed(Method JavaDoc method, Object JavaDoc instance, RuntimeException JavaDoc cause) {
166         Log log = getLog(method);
167         if (log.isWarnEnabled()) {
168             log.warn(format(LIFECYCLE_INVOCATION_FAILED, new Object JavaDoc[]{toString(method), instance, cause.getMessage()}), cause);
169         }
170         delegate.lifecycleInvocationFailed(method, instance, cause);
171     }
172
173     protected Log getLog(Member JavaDoc member) {
174         if ( log != null ){
175             return log;
176         }
177         return LogFactory.getLog(member.getDeclaringClass());
178     }
179
180 }
181
Popular Tags