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 java.io.OutputStream; 014 import java.io.PrintStream; 015 import java.lang.reflect.Constructor; 016 import java.lang.reflect.Method; 017 018 import org.picocontainer.ComponentMonitor; 019 020 /** 021 * A {@link ComponentMonitor} which writes to a {@link OutputStream}. 022 * This is typically used to write to a console. 023 * 024 * @author Paul Hammant 025 * @author Aslak Hellesøy 026 * @author Mauro Talevi 027 * @version $Revision: 3225 $ 028 */ 029 public class ConsoleComponentMonitor extends AbstractComponentMonitor { 030 031 private PrintStream out; 032 private final ComponentMonitor delegate; 033 034 public ConsoleComponentMonitor(OutputStream out) { 035 this(out, new DefaultComponentMonitor()); 036 } 037 038 public ConsoleComponentMonitor(OutputStream out, ComponentMonitor delegate) { 039 this.out = new PrintStream(out); 040 this.delegate = delegate; 041 } 042 043 public void instantiating(Constructor constructor) { 044 out.println(format(INSTANTIATING, new Object[]{toString(constructor)})); 045 delegate.instantiating(constructor); 046 } 047 048 public void instantiated(Constructor constructor, long duration) { 049 out.println(format(INSTANTIATED, new Object[]{toString(constructor), new Long(duration)})); 050 delegate.instantiated(constructor, duration); 051 } 052 053 public void instantiated(Constructor constructor, Object instantiated, Object[] parameters, long duration) { 054 out.println(format(INSTANTIATED2, new Object[]{toString(constructor), new Long(duration), instantiated.getClass().getName(), toString(parameters)})); 055 delegate.instantiated(constructor, instantiated, parameters, duration); 056 } 057 058 public void instantiationFailed(Constructor constructor, Exception cause) { 059 out.println(format(INSTANTIATION_FAILED, new Object[]{toString(constructor), cause.getMessage()})); 060 delegate.instantiationFailed(constructor, cause); 061 } 062 063 public void invoking(Method method, Object instance) { 064 out.println(format(INVOKING, new Object[]{toString(method), instance})); 065 delegate.invoking(method, instance); 066 } 067 068 public void invoked(Method method, Object instance, long duration) { 069 out.println(format(INVOKED, new Object[]{toString(method), instance, new Long(duration)})); 070 delegate.invoked(method, instance, duration); 071 } 072 073 public void invocationFailed(Method method, Object instance, Exception cause) { 074 out.println(format(INVOCATION_FAILED, new Object[]{toString(method), instance, cause.getMessage()})); 075 delegate.invocationFailed(method, instance, cause); 076 } 077 078 public void lifecycleInvocationFailed(Method method, Object instance, RuntimeException cause) { 079 out.println(format(LIFECYCLE_INVOCATION_FAILED, new Object[]{toString(method), instance, cause.getMessage()})); 080 delegate.lifecycleInvocationFailed(method, instance, cause); 081 } 082 083 }