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