1 8 package eworld.service; 9 10 import org.codehaus.aspectwerkz.extension.hotswap.EWorldUtil; 11 import org.codehaus.aspectwerkz.joinpoint.JoinPoint; 12 import org.codehaus.aspectwerkz.joinpoint.MethodRtti; 13 import org.codehaus.aspectwerkz.joinpoint.MemberSignature; 14 import org.codehaus.aspectwerkz.exception.WrappedRuntimeException; 15 16 import java.util.Map ; 17 import java.util.HashMap ; 18 19 20 23 public class ComputationStandalone { 24 25 private static final int WEAVING_FREQUENCY = new Integer ( 26 System 27 .getProperty("weaving.frequency") 28 ).intValue(); 29 30 private static final boolean USE_CACHE = System.getProperty("cache").equals("true"); 31 32 private static final boolean USE_TRACE = System.getProperty("trace").equals("true"); 33 34 private static final String EXPRESSION = "execution(int eworld.service.ComputationStandalone.fib(int))"; 35 36 private static final String SYSTEM_ID = "eworld/wlw/aop"; 37 38 private static final String CACHE_POINTCUT = "cache"; 39 40 private static final String TRACE_POINTCUT = "trace"; 41 42 private static final String CACHE_ADVICE = "cache"; 43 44 private static final String TRACE_ADVICE = "trace"; 45 46 public static int fib(int n) { 47 if (n < 2) { 48 System.err.println(n + "."); 49 return 1; 50 } else { 51 System.err.print(n + ","); 52 return fib(n - 1) + fib(n - 2); 53 } 54 } 55 56 private static void weave() { 57 if (USE_CACHE) { 58 System.err.println("weaving in cache support"); 59 EWorldUtil.activate( 60 SYSTEM_ID, 61 CacheAspect.class.getName(), 62 CACHE_ADVICE, 63 EXPRESSION, 64 CACHE_POINTCUT 65 ); 66 } 67 if (USE_TRACE) { 68 System.err.println("weaving in trace support"); 69 EWorldUtil.activate( 70 SYSTEM_ID, 71 TraceAspect.class.getName(), 72 TRACE_ADVICE, 73 EXPRESSION, 74 TRACE_POINTCUT 75 ); 76 } 77 } 78 79 private static void unWeave() { 80 if (USE_CACHE) { 81 System.err.println("un-weaving cache support"); 82 EWorldUtil.deactivate( 83 SYSTEM_ID, 84 CacheAspect.class.getName(), 85 CACHE_ADVICE, 86 CACHE_POINTCUT 87 ); 88 System.err.println("** Flushing the cache..."); 90 CacheAspect.s_cache.clear(); 91 } 92 if (USE_TRACE) { 93 System.err.println("un-weaving trace support"); 94 EWorldUtil.deactivate( 95 SYSTEM_ID, 96 TraceAspect.class.getName(), 97 TRACE_ADVICE, 98 TRACE_POINTCUT 99 ); 100 } 101 } 102 103 public static void main(String [] args) { 104 105 try { 150 int iterations = new Integer (args[0]).intValue(); 151 long sleep = new Long (args[1]).longValue(); 152 int counter = 0; 153 boolean isWeaved = false; 154 while (true) { 155 System.out.println( 156 "TraceAspect weave status = " 157 + EWorldUtil.isWeaved(SYSTEM_ID, TraceAspect.class.getName()) 158 ); 159 System.out.println( 160 "CacheAspect weave status = " 161 + EWorldUtil.isWeaved(SYSTEM_ID, CacheAspect.class.getName()) 162 ); 163 counter++; 164 Thread.sleep(sleep); 165 System.err.println("fib(" + iterations + ") = " + fib(iterations)); 166 if (USE_CACHE || USE_TRACE) { 167 if ((counter %= WEAVING_FREQUENCY) == 0) { 168 if (isWeaved) { 169 unWeave(); 170 isWeaved = false; 171 } else { 172 weave(); 173 isWeaved = true; 174 } 175 EWorldUtil.hotswap("eworld.service"); 176 } 177 } 178 } 179 } catch (InterruptedException e) { 180 throw new WrappedRuntimeException(e); 181 } 182 } 183 184 187 public static class CacheAspect { 188 189 190 public static Map s_cache = new HashMap (); 191 192 public Object cache(final JoinPoint joinPoint) throws Throwable { 193 MethodRtti mrtti = (MethodRtti) joinPoint.getRtti(); 194 Integer parameter = (Integer ) mrtti.getParameterValues()[0]; 195 Integer cachedValue = (Integer ) s_cache.get(parameter); 196 if (cachedValue == null) { 197 System.err.println("not in cache"); 198 Object newValue = joinPoint.proceed(); s_cache.put(parameter, newValue); 200 return newValue; 201 } else { 202 System.err.println("using cache: " + cachedValue); 203 return cachedValue; } 205 } 206 } 207 208 211 public static class TraceAspect { 212 private int m_level = 0; 213 214 public Object trace(final JoinPoint joinPoint) throws Throwable { 215 MemberSignature signature = (MemberSignature) joinPoint.getSignature(); 216 indent(); 217 System.out.println( 218 "--> " 219 + signature.getDeclaringType().getName() 220 + "::" 221 + signature.getName() 222 ); 223 m_level++; 224 final Object result = joinPoint.proceed(); 225 m_level--; 226 indent(); 227 System.out.println( 228 "<-- " 229 + signature.getDeclaringType().getName() 230 + "::" 231 + signature.getName() 232 ); 233 return result; 234 } 235 236 private void indent() { 237 for (int i = 0; i < m_level; i++) { 238 System.out.print(" "); 239 } 240 } 241 } 242 } | Popular Tags |