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