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 static org.picocontainer.monitors.ComponentMonitorHelper.ctorToString;
014    import static org.picocontainer.monitors.ComponentMonitorHelper.format;
015    import static org.picocontainer.monitors.ComponentMonitorHelper.memberToString;
016    import static org.picocontainer.monitors.ComponentMonitorHelper.methodToString;
017    import static org.picocontainer.monitors.ComponentMonitorHelper.parmsToString;
018    
019    import java.io.PrintWriter;
020    import java.io.Writer;
021    import java.lang.reflect.Constructor;
022    import java.lang.reflect.Member;
023    import java.lang.reflect.Method;
024    
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    
032    /**
033     * A {@link ComponentMonitor} which writes to a {@link Writer}. 
034     * 
035     * @author Paul Hammant
036     * @author Aslak Hellesøy
037     * @author Mauro Talevi
038     */
039    public class WriterComponentMonitor implements ComponentMonitor {
040    
041        private final PrintWriter out;
042        private final ComponentMonitor delegate;
043    
044        public WriterComponentMonitor(Writer out) {
045            this(out, new NullComponentMonitor());
046        }
047    
048        public WriterComponentMonitor(Writer out, ComponentMonitor delegate) {
049            this.out = new PrintWriter(out);
050            this.delegate = delegate;
051        }
052    
053        public <T> Constructor<T> instantiating(PicoContainer container, ComponentAdapter<T> componentAdapter,
054                                         Constructor<T> constructor) {
055            out.println(format(ComponentMonitorHelper.INSTANTIATING, ctorToString(constructor)));
056            return delegate.instantiating(container, componentAdapter, constructor);
057        }
058    
059        public <T> void instantiated(PicoContainer container, ComponentAdapter<T> componentAdapter,
060                                 Constructor<T> constructor,
061                                 Object instantiated,
062                                 Object[] injected,
063                                 long duration) {
064            out.println(format(ComponentMonitorHelper.INSTANTIATED, ctorToString(constructor), duration, instantiated.getClass().getName(), parmsToString(injected)));
065            delegate.instantiated(container, componentAdapter, constructor, instantiated, injected, duration);
066        }
067    
068        public <T> void instantiationFailed(PicoContainer container,
069                                        ComponentAdapter<T> componentAdapter,
070                                        Constructor<T> constructor,
071                                        Exception cause) {
072            out.println(format(ComponentMonitorHelper.INSTANTIATION_FAILED, ctorToString(constructor), cause.getMessage()));
073            delegate.instantiationFailed(container, null, constructor, cause);
074        }
075    
076        public Object invoking(PicoContainer container,
077                               ComponentAdapter<?> componentAdapter,
078                               Member member,
079                               Object instance, Object[] args) {
080            out.println(format(ComponentMonitorHelper.INVOKING, memberToString(member), instance));
081            return delegate.invoking(container, componentAdapter, member, instance, args);
082        }
083    
084        public void invoked(PicoContainer container,
085                            ComponentAdapter<?> componentAdapter,
086                            Member member,
087                            Object instance,
088                            long duration, Object[] args, Object retVal) {
089            out.println(format(ComponentMonitorHelper.INVOKED, methodToString(member), instance, duration));
090            delegate.invoked(container, componentAdapter, member, instance, duration, args, retVal);
091        }
092    
093        public void invocationFailed(Member member, Object instance, Exception cause) {
094            out.println(format(ComponentMonitorHelper.INVOCATION_FAILED, memberToString(member), instance, cause.getMessage()));
095            delegate.invocationFailed(member, instance, cause);
096        }
097    
098        public void lifecycleInvocationFailed(MutablePicoContainer container,
099                                              ComponentAdapter<?> componentAdapter, Method method,
100                                              Object instance,
101                                              RuntimeException cause) {
102            out.println(format(ComponentMonitorHelper.LIFECYCLE_INVOCATION_FAILED, methodToString(method), instance, cause.getMessage()));
103            delegate.lifecycleInvocationFailed(container, componentAdapter, method, instance, cause);
104        }
105    
106        public Object noComponentFound(MutablePicoContainer container, Object componentKey) {
107            out.println(format(ComponentMonitorHelper.NO_COMPONENT, componentKey));
108            return delegate.noComponentFound(container, componentKey);
109        }
110    
111        public Injector newInjector(Injector injector) {
112            return delegate.newInjector(injector);
113        }
114    
115        /** {@inheritDoc} **/
116        public Behavior newBehavior(Behavior behavior) {
117            return delegate.newBehavior(behavior);
118        }
119    
120    
121    }