1 18 19 package alt.jiapi.event; 20 21 import java.lang.reflect.Method ; 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import org.apache.log4j.Category; 27 28 import alt.jiapi.InstrumentationContext; 29 import alt.jiapi.InstrumentationDescriptor; 30 import alt.jiapi.Runtime; 31 32 import alt.jiapi.instrumentor.ChainInstrumentor; 33 import alt.jiapi.instrumentor.InstrumentorChain; 34 import alt.jiapi.instrumentor.HeadInstrumentor; 35 import alt.jiapi.instrumentor.Hook; 36 import alt.jiapi.instrumentor.MethodCallInstrumentor; 37 import alt.jiapi.instrumentor.MethodDispatcherInstrumentor; 38 import alt.jiapi.instrumentor.MethodEntryStrategy; 39 import alt.jiapi.instrumentor.MethodReturnStrategy; 40 import alt.jiapi.instrumentor.GrepInstrumentor; 41 42 55 public class MethodEventProducer extends EventProducer { 56 59 private List listeners = new ArrayList (); 60 61 62 69 public MethodEventProducer(InstrumentationDescriptor id) { 70 this(id, "*"); 71 } 72 73 74 85 public MethodEventProducer(InstrumentationDescriptor id, String resolution) { 86 super(resolution); 87 if (true) { 88 id.addInstrumentor(new MethodEventInstrumentor(this)); 89 return; 90 } 91 92 try { 93 95 ChainInstrumentor entryDispatcher = new MethodDispatcherInstrumentor(); 97 GrepInstrumentor grepEntry = 98 new GrepInstrumentor(new MethodEntryStrategy()); 99 100 102 grepEntry.setResolutions(getResolutions()); 103 104 ChainInstrumentor entryCall 105 = new MethodCallInstrumentor(new MethodEntryHook(this)); 106 107 InstrumentorChain entryChain = new InstrumentorChain(); 108 entryChain.add(entryDispatcher); 109 entryChain.add(grepEntry); 110 entryChain.add(entryCall); 112 113 114 ChainInstrumentor exitDispatcher = new MethodDispatcherInstrumentor(); 116 GrepInstrumentor grepExit 117 = new GrepInstrumentor(new MethodReturnStrategy()); 118 119 grepExit.setResolutions(getResolutions()); 120 121 ChainInstrumentor head = new HeadInstrumentor(); 122 ChainInstrumentor exitCall = 123 new MethodCallInstrumentor(new MethodExitHook(this)); 124 125 InstrumentorChain exitChain = new InstrumentorChain(); 126 exitChain.add(exitDispatcher); 127 exitChain.add(grepExit); 128 exitChain.add(head); 129 exitChain.add(exitCall); 130 131 id.addInstrumentor(entryChain); 133 id.addInstrumentor(exitChain); 134 } 135 catch (Exception e) { 136 e.printStackTrace(); 138 } 139 } 140 141 142 146 public synchronized void addMethodListener(MethodListener mel) { 147 listeners.add(mel); 148 } 149 150 154 public synchronized void removeMethodListener(MethodListener mel) { 155 listeners.remove(mel); 156 } 157 158 162 public void methodEntered(Object sourceObject, String methodName) { 163 if (!isProtected(sourceObject)) { 164 fireMethodEnterEvent(sourceObject, methodName); 165 } 166 else { 167 } 170 } 171 172 176 public void methodExited(Object sourceObject, String methodName) { 177 if (!isProtected(sourceObject)) { 178 fireMethodExitEvent(sourceObject, methodName); 179 } 180 else { 181 } 184 } 185 186 191 protected synchronized void fireMethodEnterEvent(Object sourceObject, 192 String methodName) { 193 Iterator i = listeners.iterator(); 194 MethodEvent event = new MethodEvent(this, sourceObject, methodName, 195 MethodEvent.METHOD_ENTERED); 196 197 while (i.hasNext()) { 198 MethodListener ml = (MethodListener)i.next(); 199 ml.methodEntered(event); 200 } 201 } 202 203 208 protected synchronized void fireMethodExitEvent(Object sourceObject, 209 String methodName) { 210 Iterator i = listeners.iterator(); 211 MethodEvent event = new MethodEvent(this, sourceObject, methodName, 212 MethodEvent.METHOD_EXITED); 213 214 while (i.hasNext()) { 215 MethodListener ml = (MethodListener)i.next(); 216 ml.methodExited(event); 217 } 218 } 219 } 220 221 | Popular Tags |