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