1 package jfun.yan.monitoring; 2 3 import java.lang.reflect.InvocationTargetException ; 4 import java.lang.reflect.Method ; 5 import java.util.ArrayList ; 6 7 8 17 public class ComponentMonitorQueue { 18 private final ArrayList queue = new ArrayList (); 19 24 public synchronized ComponentMonitorQueue addComponentMonitor(ComponentMonitor mon){ 25 queue.add(mon); 26 return this; 27 } 28 33 public synchronized ComponentMonitorQueue addCtorMonitor(CtorMonitor mon){ 34 queue.add(mon); 35 return this; 36 } 37 42 public synchronized ComponentMonitorQueue addMethodMonitor(MethodMonitor mon){ 43 queue.add(mon); 44 return this; 45 } 46 51 public synchronized ComponentMonitorQueue addCetterMonitor(GetterMonitor mon){ 52 queue.add(mon); 53 return this; 54 } 55 60 public synchronized ComponentMonitorQueue addSetterMonitor(SetterMonitor mon){ 61 queue.add(mon); 62 return this; 63 } 64 69 public synchronized ComponentMonitorQueue addGetterMonitor(IndexedGetterMonitor mon){ 70 queue.add(mon); 71 return this; 72 } 73 78 public synchronized ComponentMonitorQueue addSetterMonitor(IndexedSetterMonitor mon){ 79 queue.add(mon); 80 return this; 81 } 82 private synchronized Object [] getMonitors(){ 83 return queue.toArray(); 84 } 85 89 public ComponentMonitor getComponentMonitor(){ 90 return (ComponentMonitor)java.lang.reflect.Proxy.newProxyInstance( 91 getClass().getClassLoader(), new Class []{ComponentMonitor.class}, 92 new java.lang.reflect.InvocationHandler (){ 93 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 94 return fire(method, args); 95 } 96 } 97 ); 98 } 99 private Object fire(Method mtd, Object [] args) 100 throws Throwable { 101 final Object [] monitors = getMonitors(); 102 for(int i=0; i<monitors.length; i++){ 103 final Object mon = monitors[i]; 104 if(mtd.getDeclaringClass().isInstance(mon)){ 105 try{ 106 mtd.invoke(mon, args); 107 } 108 catch(InvocationTargetException e){ 109 final Throwable 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 |