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