    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.OutputStream;
    020    import java.io.PrintStream;
    021    import java.io.Serializable;
    022    import java.lang.reflect.Constructor;
    023    import java.lang.reflect.Member;
    024    import java.lang.reflect.Method;
    025
    026    import org.picocontainer.ComponentAdapter;
    027    import org.picocontainer.ComponentMonitor;
    028    import org.picocontainer.MutablePicoContainer;
    029    import org.picocontainer.PicoContainer;
    030    import org.picocontainer.Injector;
    031    import org.picocontainer.Behavior;
    032
    033    /**
    034     * A {@link ComponentMonitor} which writes to a {@link OutputStream}.
    035     * This is typically used to write to a console.
    036     *
    037     * @author Paul Hammant
    038     * @author Aslak Helles&oslash;y
    039     * @author Mauro Talevi
    040     * @todo  After serialization, the output printstream is null.
    041     */
    042    @SuppressWarnings("serial")
    043    public class ConsoleComponentMonitor implements ComponentMonitor, Serializable {
    044
    045            /**
    046             * The outgoing print stream.
    047             */
    048        private final transient PrintStream out;
    049
    050        /**
    051         * Delegate component monitor (for component monitor chains).
    052         */
    053        private final ComponentMonitor delegate;
    054
    055        /**
    056         * Constructs a console component monitor that sends output to <tt>System.out</tt>.
    057         */
    058        public ConsoleComponentMonitor() {
    059            this(System.out);
    060        }
    061
    062        /**
    063         * Constructs a console component monitor that sends output to the specified output stream.
    064         *
    065         * @param out  the designated output stream.  Options include System.out, Socket streams, File streams,
    066         * etc.
    067         */
    068        public ConsoleComponentMonitor(OutputStream out) {
    069            this(out, new NullComponentMonitor());
    070        }
    071
    072        /**
    073         * Constructs a console component monitor chain that sends output to the specified output stream
    074         * and then sends all events to the delegate component monitor.
    075         * @param out the output stream of choice.
    076         * @param delegate the next monitor in the component monitor chain to receive event information.
    077         */
    078        public ConsoleComponentMonitor(OutputStream out, ComponentMonitor delegate) {
    079            this.out = new PrintStream(out);
    080            this.delegate = delegate;
    081        }
    082
    083        public <T> Constructor<T> instantiating(PicoContainer container, ComponentAdapter<T> componentAdapter,
    084                                         Constructor<T> constructor
    085        ) {
    086            out.println(format(ComponentMonitorHelper.INSTANTIATING, ctorToString(constructor)));
    087            return delegate.instantiating(container, componentAdapter, constructor);
    088        }
    089
    090        public <T> void instantiated(PicoContainer container, ComponentAdapter<T> componentAdapter,
    091                                 Constructor<T> constructor,
    092                                 Object instantiated,
    093                                 Object[] parameters,
    094                                 long duration) {
    095            out.println(format(ComponentMonitorHelper.INSTANTIATED, ctorToString(constructor), duration, instantiated.getClass().getName(), parmsToString(parameters)));
    096            delegate.instantiated(container, componentAdapter, constructor, instantiated, parameters, duration);
    097        }
    098
    099        public <T> void instantiationFailed(PicoContainer container,
    100                                        ComponentAdapter<T> componentAdapter,
    101                                        Constructor<T> constructor,
    102                                        Exception cause) {
    103            out.println(format(ComponentMonitorHelper.INSTANTIATION_FAILED, ctorToString(constructor), cause.getMessage()));
    104            delegate.instantiationFailed(container, componentAdapter, constructor, cause);
    105        }
    106
    107        public void invoking(PicoContainer container,
    108                             ComponentAdapter<?> componentAdapter,
    109                             Member member,
    110                             Object instance) {
    111            out.println(format(ComponentMonitorHelper.INVOKING, memberToString(member), instance));
    112            delegate.invoking(container, componentAdapter, member, instance);
    113        }
    114
    115        public void invoked(PicoContainer container,
    116                            ComponentAdapter<?> componentAdapter,
    117                            Method method,
    118                            Object instance,
    119                            long duration) {
    120            out.println(format(ComponentMonitorHelper.INVOKED, methodToString(method), instance, duration));
    121            delegate.invoked(container, componentAdapter, method, instance, duration);
    122        }
    123
    124        public void invocationFailed(Member member, Object instance, Exception cause) {
    125            out.println(format(ComponentMonitorHelper.INVOCATION_FAILED, memberToString(member), instance, cause.getMessage()));
    126            delegate.invocationFailed(member, instance, cause);
    127        }
    128
    129        public void lifecycleInvocationFailed(MutablePicoContainer container,
    130                                              ComponentAdapter<?> componentAdapter, Method method,
    131                                              Object instance,
    132                                              RuntimeException cause) {
    133            out.println(format(ComponentMonitorHelper.LIFECYCLE_INVOCATION_FAILED, methodToString(method), instance, cause.getMessage()));
    134            delegate.lifecycleInvocationFailed(container, componentAdapter, method, instance, cause);
    135        }
    136
    137        public Object noComponentFound(MutablePicoContainer container, Object componentKey) {
    138            out.println(format(ComponentMonitorHelper.NO_COMPONENT, componentKey));
    139            return delegate.noComponentFound(container, componentKey);
    140        }
    141
    142        public Injector newInjector(Injector injector) {
    143            return delegate.newInjector(injector);
    144        }
    145
    146        /** {@inheritDoc} **/
    147        public Behavior newBehavior(Behavior behavior) {
    148            return delegate.newBehavior(behavior);
    149        }
    150
    151    }