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