KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > monitoring > ComponentMonitorQueue


1 package jfun.yan.monitoring;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.util.ArrayList JavaDoc;
6
7
8 /**
9  * <p>
10  * This class encapsulates a queue of various monitors.
11  * {@link #getComponentMonitor()} creates a ComponentMonitor instance
12  * that will sequentially call all the monitors in the queue.
13  * </p>
14  * @author Ben Yu.
15  *
16  */

17 public class ComponentMonitorQueue {
18   private final ArrayList JavaDoc queue = new ArrayList JavaDoc();
19   /**
20    * Add a ComponentMonitor object to the queue.
21    * @param mon the monitor object.
22    * @return this ComponentMonitorQueue object.
23    */

24   public synchronized ComponentMonitorQueue addComponentMonitor(ComponentMonitor mon){
25     queue.add(mon);
26     return this;
27   }
28   /**
29    * Add a CtorMonitor object to the queue.
30    * @param mon the monitor object.
31    * @return this ComponentMonitorQueue object.
32    */

33   public synchronized ComponentMonitorQueue addCtorMonitor(CtorMonitor mon){
34     queue.add(mon);
35     return this;
36   }
37   /**
38    * Add a MethodMonitor object to the queue.
39    * @param mon the monitor object.
40    * @return this ComponentMonitorQueue object.
41    */

42   public synchronized ComponentMonitorQueue addMethodMonitor(MethodMonitor mon){
43     queue.add(mon);
44     return this;
45   }
46   /**
47    * Add a GetterMonitor object to the queue.
48    * @param mon the monitor object.
49    * @return this ComponentMonitorQueue object.
50    */

51   public synchronized ComponentMonitorQueue addCetterMonitor(GetterMonitor mon){
52     queue.add(mon);
53     return this;
54   }
55   /**
56    * Add a SetterMonitor object to the queue.
57    * @param mon the monitor object.
58    * @return this ComponentMonitorQueue object.
59    */

60   public synchronized ComponentMonitorQueue addSetterMonitor(SetterMonitor mon){
61     queue.add(mon);
62     return this;
63   }
64   /**
65    * Add an IndexedGetterMonitor object to the queue.
66    * @param mon the monitor object.
67    * @return this ComponentMonitorQueue object.
68    */

69   public synchronized ComponentMonitorQueue addGetterMonitor(IndexedGetterMonitor mon){
70     queue.add(mon);
71     return this;
72   }
73   /**
74    * Add an IndexedSetterMonitor object to the queue.
75    * @param mon the monitor object.
76    * @return this ComponentMonitorQueue object.
77    */

78   public synchronized ComponentMonitorQueue addSetterMonitor(IndexedSetterMonitor mon){
79     queue.add(mon);
80     return this;
81   }
82   private synchronized Object JavaDoc[] getMonitors(){
83     return queue.toArray();
84   }
85   /**
86    * To create a ComponentMonitor instance that sequentially invoke
87    * the corresponding methods of all the monitors in the queue.
88    */

89   public ComponentMonitor getComponentMonitor(){
90     return (ComponentMonitor)java.lang.reflect.Proxy.newProxyInstance(
91       getClass().getClassLoader(), new Class JavaDoc[]{ComponentMonitor.class},
92       new java.lang.reflect.InvocationHandler JavaDoc(){
93         public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
94           return fire(method, args);
95         }
96       }
97     );
98   }
99   private Object JavaDoc fire(Method JavaDoc mtd, Object JavaDoc[] args)
100   throws Throwable JavaDoc{
101     final Object JavaDoc[] monitors = getMonitors();
102     for(int i=0; i<monitors.length; i++){
103       final Object JavaDoc mon = monitors[i];
104       if(mtd.getDeclaringClass().isInstance(mon)){
105         try{
106           mtd.invoke(mon, args);
107         }
108         catch(InvocationTargetException JavaDoc e){
109           final Throwable JavaDoc cause = e.getTargetException();
110           if(cause != null)
111             throw cause;
112           else
113             throw e;
114         }
115       }
116     }
117     return null;
118   }
119 }
120
Popular Tags