KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > DelegatingComponentMonitor


1 /*****************************************************************************
2  * Copyright (C) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by Mauro Talevi *
9  *****************************************************************************/

10
11 package org.picocontainer.defaults;
12
13 import java.io.Serializable JavaDoc;
14 import java.lang.reflect.Constructor JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16
17 import org.picocontainer.ComponentMonitor;
18 import org.picocontainer.monitors.DefaultComponentMonitor;
19
20 /**
21  * <p>
22  * A {@link ComponentMonitor monitor} which delegates to another monitor.
23  * It provides a {@link DefaultComponentMonitor default ComponentMonitor},
24  * but does not allow to use <code>null</code> for the delegate.
25  * </p>
26  * <p>
27  * It also supports a {@link ComponentMonitorStrategy monitor strategy}
28  * that allows to change the delegate.
29  * </p>
30  *
31  * @author Mauro Talevi
32  * @version $Revision: $
33  * @since 1.2
34  */

35 public class DelegatingComponentMonitor implements ComponentMonitor, ComponentMonitorStrategy, Serializable JavaDoc {
36
37     private ComponentMonitor delegate;
38     
39     /**
40      * Creates a DelegatingComponentMonitor with a given delegate
41      * @param delegate the ComponentMonitor to which this monitor delegates
42      */

43     public DelegatingComponentMonitor(ComponentMonitor delegate) {
44         checkMonitor(delegate);
45         this.delegate = delegate;
46     }
47
48     /**
49      * Creates a DelegatingComponentMonitor with an instance of
50      * {@link DefaultComponentMonitor}.
51      */

52     public DelegatingComponentMonitor() {
53         this(DefaultComponentMonitor.getInstance());
54     }
55     
56     public void instantiating(Constructor JavaDoc constructor) {
57         delegate.instantiating(constructor);
58     }
59
60     public void instantiated(Constructor JavaDoc constructor, long duration) {
61         delegate.instantiated(constructor, duration);
62     }
63
64     public void instantiated(Constructor JavaDoc constructor, Object JavaDoc instantiated, Object JavaDoc[] injected, long duration) {
65         delegate.instantiated(constructor, instantiated, injected, duration);
66     }
67
68     public void instantiationFailed(Constructor JavaDoc constructor, Exception JavaDoc e) {
69         delegate.instantiationFailed(constructor, e);
70     }
71
72     public void invoking(Method JavaDoc method, Object JavaDoc instance) {
73         delegate.invoking(method, instance);
74     }
75
76     public void invoked(Method JavaDoc method, Object JavaDoc instance, long duration) {
77         delegate.invoked(method, instance, duration);
78     }
79
80     public void invocationFailed(Method JavaDoc method, Object JavaDoc instance, Exception JavaDoc e) {
81         delegate.invocationFailed(method, instance, e);
82     }
83
84     public void lifecycleInvocationFailed(Method JavaDoc method, Object JavaDoc instance, RuntimeException JavaDoc cause) {
85         delegate.lifecycleInvocationFailed(method,instance, cause);
86     }
87
88     /**
89      * If the delegate supports a {@link ComponentMonitorStrategy monitor strategy},
90      * this is used to changed the monitor while keeping the same delegate.
91      * Else the delegate is replaced by the new monitor.
92      * {@inheritDoc}
93      */

94     public void changeMonitor(ComponentMonitor monitor) {
95         checkMonitor(monitor);
96         if ( delegate instanceof ComponentMonitorStrategy ){
97             ((ComponentMonitorStrategy)delegate).changeMonitor(monitor);
98         } else {
99             delegate = monitor;
100         }
101     }
102
103     public ComponentMonitor currentMonitor() {
104         if ( delegate instanceof ComponentMonitorStrategy ){
105             return ((ComponentMonitorStrategy)delegate).currentMonitor();
106         } else {
107             return delegate;
108         }
109     }
110     
111     private void checkMonitor(ComponentMonitor monitor) {
112         if ( monitor == null ){
113             throw new NullPointerException JavaDoc("monitor");
114         }
115     }
116
117 }
118
Popular Tags