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 *
009: *****************************************************************************/package org.picocontainer.monitors;
010:
011: import java.lang.reflect.Constructor;
012: import java.lang.reflect.Method;
013: import java.lang.reflect.Member;
014: import java.util.ArrayList;
015: import java.util.Collection;
016: import java.util.List;
017:
018: import org.picocontainer.ComponentMonitor;
019: import org.picocontainer.PicoException;
020: import org.picocontainer.PicoLifecycleException;
021: import org.picocontainer.ComponentAdapter;
022: import org.picocontainer.MutablePicoContainer;
023: import org.picocontainer.PicoContainer;
024:
025: /**
026: * A {@link ComponentMonitor} which collects lifecycle failures
027: * and rethrows them on demand after the failures.
028: *
029: * @author Paul Hammant
030: * @author Mauro Talevi
031: */
032: public final class LifecycleComponentMonitor implements
033: ComponentMonitor {
034:
035: /**
036: * Delegate for chained component monitors.
037: */
038: private final ComponentMonitor delegate;
039:
040: private final List<RuntimeException> lifecycleFailures = new ArrayList<RuntimeException>();
041:
042: public LifecycleComponentMonitor(ComponentMonitor delegate) {
043: this .delegate = delegate;
044: }
045:
046: public LifecycleComponentMonitor() {
047: this (new NullComponentMonitor());
048: }
049:
050: public <T> Constructor<T> instantiating(PicoContainer container,
051: ComponentAdapter<T> componentAdapter,
052: Constructor<T> constructor) {
053: return delegate.instantiating(container, componentAdapter,
054: constructor);
055: }
056:
057: public <T> void instantiated(PicoContainer container,
058: ComponentAdapter<T> componentAdapter,
059: Constructor<T> constructor, Object instantiated,
060: Object[] parameters, long duration) {
061: delegate.instantiated(container, componentAdapter, constructor,
062: instantiated, parameters, duration);
063: }
064:
065: public <T> void instantiationFailed(PicoContainer container,
066: ComponentAdapter<T> componentAdapter,
067: Constructor<T> constructor, Exception cause) {
068: delegate.instantiationFailed(container, componentAdapter,
069: constructor, cause);
070: }
071:
072: public void invoking(PicoContainer container,
073: ComponentAdapter<?> componentAdapter, Member member,
074: Object instance) {
075: delegate
076: .invoking(container, componentAdapter, member, instance);
077: }
078:
079: public void invoked(PicoContainer container,
080: ComponentAdapter<?> componentAdapter, Method method,
081: Object instance, long duration) {
082: delegate.invoked(container, componentAdapter, method, instance,
083: duration);
084: }
085:
086: public void invocationFailed(Member member, Object instance,
087: Exception cause) {
088: delegate.invocationFailed(member, instance, cause);
089: }
090:
091: public void lifecycleInvocationFailed(
092: MutablePicoContainer container,
093: ComponentAdapter<?> componentAdapter, Method method,
094: Object instance, RuntimeException cause) {
095: lifecycleFailures.add(cause);
096: try {
097: delegate.lifecycleInvocationFailed(container,
098: componentAdapter, method, instance, cause);
099: } catch (PicoLifecycleException e) {
100: // do nothing, exception already logged for later rethrow.
101: }
102: }
103:
104: public Object noComponentFound(MutablePicoContainer container,
105: Object componentKey) {
106: return delegate.noComponentFound(container, componentKey);
107: }
108:
109: public void rethrowLifecycleFailuresException() {
110: throw new LifecycleFailuresException(lifecycleFailures);
111: }
112:
113: /**
114: * Subclass of {@link PicoException} that is thrown when the collected
115: * lifecycle failures need to be be collectively rethrown.
116: *
117: * @author Paul Hammant
118: * @author Mauro Talevi
119: */
120: public final class LifecycleFailuresException extends PicoException {
121:
122: /**
123: * Serialization UUID.
124: */
125: private static final long serialVersionUID = -5627971463694211121L;
126:
127: private final List<RuntimeException> lifecycleFailures;
128:
129: public LifecycleFailuresException(
130: List<RuntimeException> lifecycleFailures) {
131: this .lifecycleFailures = lifecycleFailures;
132: }
133:
134: public String getMessage() {
135: StringBuffer message = new StringBuffer();
136: for (Object lifecycleFailure : lifecycleFailures) {
137: Exception failure = (Exception) lifecycleFailure;
138: message.append(failure.getMessage()).append("; ");
139: }
140: return message.toString();
141: }
142:
143: public Collection<RuntimeException> getFailures() {
144: return lifecycleFailures;
145: }
146: }
147: }
|