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.defaults;
012    
013    import java.io.Serializable;
014    import java.lang.reflect.Constructor;
015    import java.lang.reflect.Method;
016    
017    import org.picocontainer.ComponentMonitor;
018    import org.picocontainer.monitors.DefaultComponentMonitor;
019    
020    /**
021     * <p>
022     * A {@link ComponentMonitor monitor} which delegates to another monitor.
023     * It provides a {@link DefaultComponentMonitor default ComponentMonitor},
024     * but does not allow to use <code>null</code> for the delegate.
025     * </p>
026     * <p>
027     * It also supports a {@link ComponentMonitorStrategy monitor strategy} 
028     * that allows to change the delegate.
029     * </p>
030     * 
031     * @author Mauro Talevi
032     * @version $Revision: $
033     * @since 1.2
034     */
035    public class DelegatingComponentMonitor implements ComponentMonitor, ComponentMonitorStrategy, Serializable {
036    
037        private  ComponentMonitor delegate;
038        
039        /**
040         * Creates a DelegatingComponentMonitor with a given delegate
041         * @param delegate the ComponentMonitor to which this monitor delegates
042         */
043        public DelegatingComponentMonitor(ComponentMonitor delegate) {
044            checkMonitor(delegate);
045            this.delegate = delegate;
046        }
047    
048        /**
049         * Creates a DelegatingComponentMonitor with an instance of 
050         * {@link DefaultComponentMonitor}.
051         */
052        public DelegatingComponentMonitor() {
053            this(DefaultComponentMonitor.getInstance());
054        }
055        
056        public void instantiating(Constructor constructor) {
057            delegate.instantiating(constructor);
058        }
059    
060        public void instantiated(Constructor constructor, long duration) {
061            delegate.instantiated(constructor, duration);
062        }
063    
064        public void instantiated(Constructor constructor, Object instantiated, Object[] injected, long duration) {
065            delegate.instantiated(constructor, instantiated, injected, duration);
066        }
067    
068        public void instantiationFailed(Constructor constructor, Exception e) {
069            delegate.instantiationFailed(constructor, e);
070        }
071    
072        public void invoking(Method method, Object instance) {
073            delegate.invoking(method, instance);
074        }
075    
076        public void invoked(Method method, Object instance, long duration) {
077            delegate.invoked(method, instance, duration);
078        }
079    
080        public void invocationFailed(Method method, Object instance, Exception e) {
081            delegate.invocationFailed(method, instance, e);
082        }
083    
084        public void lifecycleInvocationFailed(Method method, Object instance, RuntimeException cause) {
085            delegate.lifecycleInvocationFailed(method,instance, cause);
086        }
087    
088        /**
089         * If the delegate supports a {@link ComponentMonitorStrategy monitor strategy},
090         * this is used to changed the monitor while keeping the same delegate.
091         * Else the delegate is replaced by the new monitor.
092         * {@inheritDoc}
093         */
094        public void changeMonitor(ComponentMonitor monitor) {
095            checkMonitor(monitor);
096            if ( delegate instanceof ComponentMonitorStrategy ){
097                ((ComponentMonitorStrategy)delegate).changeMonitor(monitor);
098            } else {
099                delegate = monitor;
100            }
101        }
102    
103        public ComponentMonitor currentMonitor() {
104            if ( delegate instanceof ComponentMonitorStrategy ){
105                return ((ComponentMonitorStrategy)delegate).currentMonitor();
106            } else {
107                return delegate;
108            }
109        }
110        
111        private void checkMonitor(ComponentMonitor monitor) {
112            if ( monitor == null ){
113                throw new NullPointerException("monitor");
114            }
115        }
116    
117    }