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 Mauro Talevi *
009: *****************************************************************************/package org.picocontainer.monitors;
010:
011: import java.io.Serializable;
012: import java.lang.reflect.Constructor;
013: import java.lang.reflect.Method;
014: import java.lang.reflect.Member;
015:
016: import org.picocontainer.ComponentMonitor;
017: import org.picocontainer.ComponentMonitorStrategy;
018: import org.picocontainer.ComponentAdapter;
019: import org.picocontainer.MutablePicoContainer;
020: import org.picocontainer.PicoContainer;
021: import org.picocontainer.monitors.NullComponentMonitor;
022:
023: /**
024: * <p>
025: * A {@link ComponentMonitor monitor} which delegates to another monitor.
026: * It provides a {@link NullComponentMonitor default ComponentMonitor},
027: * but does not allow to use <code>null</code> for the delegate.
028: * </p>
029: * <p>
030: * It also supports a {@link org.picocontainer.ComponentMonitorStrategy monitor strategy}
031: * that allows to change the delegate.
032: * </p>
033: *
034: * @author Mauro Talevi
035: */
036: public class AbstractComponentMonitor implements ComponentMonitor,
037: ComponentMonitorStrategy, Serializable {
038:
039: /**
040: * Serialization UUID.
041: */
042: private static final long serialVersionUID = 4978870257460414077L;
043:
044: /**
045: * Delegate monitor to allow for component monitor chaining.
046: */
047: private ComponentMonitor delegate;
048:
049: /**
050: * Creates a AbstractComponentMonitor with a given delegate
051: * @param delegate the ComponentMonitor to which this monitor delegates
052: */
053: public AbstractComponentMonitor(ComponentMonitor delegate) {
054: checkMonitor(delegate);
055: this .delegate = delegate;
056: }
057:
058: /**
059: * Creates a AbstractComponentMonitor with an instance of
060: * {@link NullComponentMonitor}.
061: */
062: public AbstractComponentMonitor() {
063: this (new NullComponentMonitor());
064: }
065:
066: public <T> Constructor<T> instantiating(PicoContainer container,
067: ComponentAdapter<T> componentAdapter,
068: Constructor<T> constructor) {
069: return delegate.instantiating(container, componentAdapter,
070: constructor);
071: }
072:
073: public <T> void instantiated(PicoContainer container,
074: ComponentAdapter<T> componentAdapter,
075: Constructor<T> constructor, Object instantiated,
076: Object[] injected, long duration) {
077: delegate.instantiated(container, componentAdapter, constructor,
078: instantiated, injected, duration);
079: }
080:
081: public <T> void instantiationFailed(PicoContainer container,
082: ComponentAdapter<T> componentAdapter,
083: Constructor<T> constructor, Exception e) {
084: delegate.instantiationFailed(container, componentAdapter,
085: constructor, e);
086: }
087:
088: public void invoking(PicoContainer container,
089: ComponentAdapter<?> componentAdapter, Member member,
090: Object instance) {
091: delegate
092: .invoking(container, componentAdapter, member, instance);
093: }
094:
095: public void invoked(PicoContainer container,
096: ComponentAdapter<?> componentAdapter, Method method,
097: Object instance, long duration) {
098: delegate.invoked(container, componentAdapter, method, instance,
099: duration);
100: }
101:
102: public void invocationFailed(Member member, Object instance,
103: Exception e) {
104: delegate.invocationFailed(member, instance, e);
105: }
106:
107: public void lifecycleInvocationFailed(
108: MutablePicoContainer container,
109: ComponentAdapter<?> componentAdapter, Method method,
110: Object instance, RuntimeException cause) {
111: delegate.lifecycleInvocationFailed(container, componentAdapter,
112: method, instance, cause);
113: }
114:
115: public Object noComponentFound(MutablePicoContainer container,
116: Object componentKey) {
117: return delegate.noComponentFound(container, componentKey);
118: }
119:
120: /**
121: * If the delegate supports a {@link ComponentMonitorStrategy monitor strategy},
122: * this is used to changed the monitor while keeping the same delegate.
123: * Else the delegate is replaced by the new monitor.
124: * {@inheritDoc}
125: */
126: public void changeMonitor(ComponentMonitor monitor) {
127: checkMonitor(monitor);
128: if (delegate instanceof ComponentMonitorStrategy) {
129: ((ComponentMonitorStrategy) delegate)
130: .changeMonitor(monitor);
131: } else {
132: delegate = monitor;
133: }
134: }
135:
136: public ComponentMonitor currentMonitor() {
137: if (delegate instanceof ComponentMonitorStrategy) {
138: return ((ComponentMonitorStrategy) delegate)
139: .currentMonitor();
140: } else {
141: return delegate;
142: }
143: }
144:
145: private void checkMonitor(ComponentMonitor monitor) {
146: if (monitor == null) {
147: throw new NullPointerException("monitor");
148: }
149: }
150:
151: }
|