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 Hammaant                                            *
009     *****************************************************************************/
010    
011    package org.picocontainer.monitors;
012    
013    import java.io.PrintWriter;
014    import java.io.Writer;
015    import java.lang.reflect.Constructor;
016    import java.lang.reflect.Method;
017    
018    import org.picocontainer.ComponentMonitor;
019    
020    /**
021     * A {@link ComponentMonitor} which writes to a {@link Writer}. 
022     * 
023     * @author Paul Hammant
024     * @author Aslak Hellesøy
025     * @author Mauro Talevi
026     * @version $Revision: 1882 $
027     */
028    public class WriterComponentMonitor extends AbstractComponentMonitor {
029    
030        private PrintWriter out;
031        private final ComponentMonitor delegate;
032    
033        public WriterComponentMonitor(Writer out) {
034            this.out = new PrintWriter(out);
035            delegate = new DefaultComponentMonitor();
036        }
037    
038        public WriterComponentMonitor(Writer out, ComponentMonitor delegate) {
039            this.out = new PrintWriter(out);
040            this.delegate = delegate;
041        }
042    
043        public void instantiating(Constructor constructor) {
044            out.println(format(INSTANTIATING, new Object[]{toString(constructor)}));
045            delegate.instantiating(constructor);
046        }
047    
048        public void instantiated(Constructor constructor, long duration) {
049            out.println(format(INSTANTIATED, new Object[]{toString(constructor), new Long(duration)}));
050            delegate.instantiated(constructor, duration);
051        }
052    
053        public void instantiated(Constructor constructor, Object instantiated, Object[] injected, long duration) {
054            out.println(format(INSTANTIATED2, new Object[]{toString(constructor), new Long(duration), instantiated.getClass().getName(), toString(injected)}));
055            delegate.instantiated(constructor, instantiated, injected, duration);
056        }
057    
058        public void instantiationFailed(Constructor constructor, Exception cause) {
059            out.println(format(INSTANTIATION_FAILED, new Object[]{toString(constructor), cause.getMessage()}));
060            delegate.instantiationFailed(constructor, cause);
061        }
062    
063        public void invoking(Method method, Object instance) {
064            out.println(format(INVOKING, new Object[]{toString(method), instance}));
065            delegate.invoking(method, instance);
066        }
067    
068        public void invoked(Method method, Object instance, long duration) {
069            out.println(format(INVOKED, new Object[]{toString(method), instance, new Long(duration)}));
070            delegate.invoked(method, instance, duration);
071        }
072    
073        public void invocationFailed(Method method, Object instance, Exception cause) {
074            out.println(format(INVOCATION_FAILED, new Object[]{toString(method), instance, cause.getMessage()}));
075            delegate.invocationFailed(method, instance, cause);
076        }
077    
078        public void lifecycleInvocationFailed(Method method, Object instance, RuntimeException cause) {
079            out.println(format(LIFECYCLE_INVOCATION_FAILED, new Object[]{toString(method), instance, cause.getMessage()}));
080            delegate.lifecycleInvocationFailed(method, instance, cause);
081        }
082    
083    }