001: /**************************************************************************************
002: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package eworld.service;
008:
009: import org.codehaus.aspectwerkz.extension.hotswap.EWorldUtil;
010: import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
011: import org.codehaus.aspectwerkz.joinpoint.MethodRtti;
012: import org.codehaus.aspectwerkz.joinpoint.MemberSignature;
013: import org.codehaus.aspectwerkz.exception.WrappedRuntimeException;
014:
015: import java.util.Map;
016: import java.util.HashMap;
017:
018: /**
019: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
020: */
021: public class ComputationStandalone {
022:
023: private static final int WEAVING_FREQUENCY = new Integer(System
024: .getProperty("weaving.frequency")).intValue();
025:
026: private static final boolean USE_CACHE = System
027: .getProperty("cache").equals("true");
028:
029: private static final boolean USE_TRACE = System
030: .getProperty("trace").equals("true");
031:
032: private static final String EXPRESSION = "execution(int eworld.service.ComputationStandalone.fib(int))";
033:
034: private static final String SYSTEM_ID = "eworld/wlw/aop";
035:
036: private static final String CACHE_POINTCUT = "cache";
037:
038: private static final String TRACE_POINTCUT = "trace";
039:
040: private static final String CACHE_ADVICE = "cache";
041:
042: private static final String TRACE_ADVICE = "trace";
043:
044: public static int fib(int n) {
045: if (n < 2) {
046: System.err.println(n + ".");
047: return 1;
048: } else {
049: System.err.print(n + ",");
050: return fib(n - 1) + fib(n - 2);
051: }
052: }
053:
054: private static void weave() {
055: if (USE_CACHE) {
056: System.err.println("weaving in cache support");
057: EWorldUtil.activate(SYSTEM_ID, CacheAspect.class.getName(),
058: CACHE_ADVICE, EXPRESSION, CACHE_POINTCUT);
059: }
060: if (USE_TRACE) {
061: System.err.println("weaving in trace support");
062: EWorldUtil.activate(SYSTEM_ID, TraceAspect.class.getName(),
063: TRACE_ADVICE, EXPRESSION, TRACE_POINTCUT);
064: }
065: }
066:
067: private static void unWeave() {
068: if (USE_CACHE) {
069: System.err.println("un-weaving cache support");
070: EWorldUtil.deactivate(SYSTEM_ID, CacheAspect.class
071: .getName(), CACHE_ADVICE, CACHE_POINTCUT);
072: // flush the cache... as if we have a "onUndeploy callback.."
073: System.err.println("** Flushing the cache...");
074: CacheAspect.s_cache.clear();
075: }
076: if (USE_TRACE) {
077: System.err.println("un-weaving trace support");
078: EWorldUtil.deactivate(SYSTEM_ID, TraceAspect.class
079: .getName(), TRACE_ADVICE, TRACE_POINTCUT);
080: }
081: }
082:
083: public static void main(String[] args) {
084:
085: // if (args.length != 2) {
086: // System.err.println("fib(" + 3 + ") = " + fib(3));
087: //
088: // System.err.println("weaving in trace support");
089: // EWorldUtil.activate(SYSTEM_ID, TraceAspect.class.getName(), TRACE_ADVICE, EXPRESSION,
090: // TRACE_POINTCUT);
091: // EWorldUtil.hotswap("eworld.service");
092: // System.err.println("fib(" + 3 + ") = " + fib(3));
093: //
094: // System.err.println("weaving in cache support");
095: // EWorldUtil.activate(SYSTEM_ID, CacheAspect.class.getName(), CACHE_ADVICE, EXPRESSION,
096: // CACHE_POINTCUT);
097: // EWorldUtil.hotswap("eworld.service");
098: // System.err.println("fib(" + 3 + ") = " + fib(3));
099: // System.err.println("fib(" + 3 + ") = " + fib(3));
100: //
101: // System.err.println("un-weaving trace support");
102: // EWorldUtil.deactivate(SYSTEM_ID, TraceAspect.class.getName(), TRACE_ADVICE,
103: // TRACE_POINTCUT);
104: // EWorldUtil.hotswap("eworld.service");
105: // System.err.println("fib(" + 4 + ") = " + fib(4));
106: //
107: // System.err.println("un-weaving cache support");
108: // EWorldUtil.deactivate(SYSTEM_ID, CacheAspect.class.getName(), CACHE_ADVICE,
109: // CACHE_POINTCUT);
110: // EWorldUtil.hotswap("eworld.service");
111: // System.err.println("fib(" + 3 + ") = " + fib(3));
112: //
113: // System.err.println("weaving in trace support");
114: // EWorldUtil.activate(SYSTEM_ID, TraceAspect.class.getName(), TRACE_ADVICE, EXPRESSION,
115: // TRACE_POINTCUT);
116: // EWorldUtil.hotswap("eworld.service");
117: // System.err.println("fib(" + 3 + ") = " + fib(3));
118: //
119: // System.err.println("un-weaving trace support");
120: // EWorldUtil.deactivate(SYSTEM_ID, TraceAspect.class.getName(), TRACE_ADVICE,
121: // TRACE_POINTCUT);
122: // EWorldUtil.hotswap("eworld.service");
123: // System.err.println("fib(" + 4 + ") = " + fib(4));
124: //
125: // System.exit(0);
126: // //throw new IllegalArgumentException("number of iterations and sleep time must be
127: // specified");
128: // }
129: try {
130: int iterations = new Integer(args[0]).intValue();
131: long sleep = new Long(args[1]).longValue();
132: int counter = 0;
133: boolean isWeaved = false;
134: while (true) {
135: System.out.println("TraceAspect weave status = "
136: + EWorldUtil.isWeaved(SYSTEM_ID,
137: TraceAspect.class.getName()));
138: System.out.println("CacheAspect weave status = "
139: + EWorldUtil.isWeaved(SYSTEM_ID,
140: CacheAspect.class.getName()));
141: counter++;
142: Thread.sleep(sleep);
143: System.err.println("fib(" + iterations + ") = "
144: + fib(iterations));
145: if (USE_CACHE || USE_TRACE) {
146: if ((counter %= WEAVING_FREQUENCY) == 0) {
147: if (isWeaved) {
148: unWeave();
149: isWeaved = false;
150: } else {
151: weave();
152: isWeaved = true;
153: }
154: EWorldUtil.hotswap("eworld.service");
155: }
156: }
157: }
158: } catch (InterruptedException e) {
159: throw new WrappedRuntimeException(e);
160: }
161: }
162:
163: /**
164: * Cache aspect.
165: */
166: public static class CacheAspect {
167:
168: /** a static cache to flush it from the outside for demo purpose. Safe since aspect is singleton */
169: public static Map s_cache = new HashMap();
170:
171: public Object cache(final JoinPoint joinPoint) throws Throwable {
172: MethodRtti mrtti = (MethodRtti) joinPoint.getRtti();
173: Integer parameter = (Integer) mrtti.getParameterValues()[0];
174: Integer cachedValue = (Integer) s_cache.get(parameter);
175: if (cachedValue == null) {
176: System.err.println("not in cache");
177: Object newValue = joinPoint.proceed(); // not found => calculate
178: s_cache.put(parameter, newValue);
179: return newValue;
180: } else {
181: System.err.println("using cache: " + cachedValue);
182: return cachedValue; // return cached value
183: }
184: }
185: }
186:
187: /**
188: * Trace aspect.
189: */
190: public static class TraceAspect {
191: private int m_level = 0;
192:
193: public Object trace(final JoinPoint joinPoint) throws Throwable {
194: MemberSignature signature = (MemberSignature) joinPoint
195: .getSignature();
196: indent();
197: System.out.println("--> "
198: + signature.getDeclaringType().getName() + "::"
199: + signature.getName());
200: m_level++;
201: final Object result = joinPoint.proceed();
202: m_level--;
203: indent();
204: System.out.println("<-- "
205: + signature.getDeclaringType().getName() + "::"
206: + signature.getName());
207: return result;
208: }
209:
210: private void indent() {
211: for (int i = 0; i < m_level; i++) {
212: System.out.print(" ");
213: }
214: }
215: }
216: }
|