KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > eworld > service > ComputationStandalone


1 /**************************************************************************************
2  * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

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 JavaDoc;
17 import java.util.HashMap JavaDoc;
18
19
20 /**
21  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
22  */

23 public class ComputationStandalone {
24
25     private static final int WEAVING_FREQUENCY = new Integer JavaDoc(
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 JavaDoc EXPRESSION = "execution(int eworld.service.ComputationStandalone.fib(int))";
35
36     private static final String JavaDoc SYSTEM_ID = "eworld/wlw/aop";
37
38     private static final String JavaDoc CACHE_POINTCUT = "cache";
39
40     private static final String JavaDoc TRACE_POINTCUT = "trace";
41
42     private static final String JavaDoc CACHE_ADVICE = "cache";
43
44     private static final String JavaDoc 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             // flush the cache... as if we have a "onUndeploy callback.."
89
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 JavaDoc[] args) {
104
105         // if (args.length != 2) {
106
// System.err.println("fib(" + 3 + ") = " + fib(3));
107
//
108
// System.err.println("weaving in trace support");
109
// EWorldUtil.activate(SYSTEM_ID, TraceAspect.class.getName(), TRACE_ADVICE, EXPRESSION,
110
// TRACE_POINTCUT);
111
// EWorldUtil.hotswap("eworld.service");
112
// System.err.println("fib(" + 3 + ") = " + fib(3));
113
//
114
// System.err.println("weaving in cache support");
115
// EWorldUtil.activate(SYSTEM_ID, CacheAspect.class.getName(), CACHE_ADVICE, EXPRESSION,
116
// CACHE_POINTCUT);
117
// EWorldUtil.hotswap("eworld.service");
118
// System.err.println("fib(" + 3 + ") = " + fib(3));
119
// System.err.println("fib(" + 3 + ") = " + fib(3));
120
//
121
// System.err.println("un-weaving trace support");
122
// EWorldUtil.deactivate(SYSTEM_ID, TraceAspect.class.getName(), TRACE_ADVICE,
123
// TRACE_POINTCUT);
124
// EWorldUtil.hotswap("eworld.service");
125
// System.err.println("fib(" + 4 + ") = " + fib(4));
126
//
127
// System.err.println("un-weaving cache support");
128
// EWorldUtil.deactivate(SYSTEM_ID, CacheAspect.class.getName(), CACHE_ADVICE,
129
// CACHE_POINTCUT);
130
// EWorldUtil.hotswap("eworld.service");
131
// System.err.println("fib(" + 3 + ") = " + fib(3));
132
//
133
// System.err.println("weaving in trace support");
134
// EWorldUtil.activate(SYSTEM_ID, TraceAspect.class.getName(), TRACE_ADVICE, EXPRESSION,
135
// TRACE_POINTCUT);
136
// EWorldUtil.hotswap("eworld.service");
137
// System.err.println("fib(" + 3 + ") = " + fib(3));
138
//
139
// System.err.println("un-weaving trace support");
140
// EWorldUtil.deactivate(SYSTEM_ID, TraceAspect.class.getName(), TRACE_ADVICE,
141
// TRACE_POINTCUT);
142
// EWorldUtil.hotswap("eworld.service");
143
// System.err.println("fib(" + 4 + ") = " + fib(4));
144
//
145
// System.exit(0);
146
// //throw new IllegalArgumentException("number of iterations and sleep time must be
147
// specified");
148
// }
149
try {
150             int iterations = new Integer JavaDoc(args[0]).intValue();
151             long sleep = new Long JavaDoc(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 JavaDoc e) {
180             throw new WrappedRuntimeException(e);
181         }
182     }
183
184     /**
185      * Cache aspect.
186      */

187     public static class CacheAspect {
188
189         /** a static cache to flush it from the outside for demo purpose. Safe since aspect is singleton */
190         public static Map JavaDoc s_cache = new HashMap JavaDoc();
191
192         public Object JavaDoc cache(final JoinPoint joinPoint) throws Throwable JavaDoc {
193             MethodRtti mrtti = (MethodRtti) joinPoint.getRtti();
194             Integer JavaDoc parameter = (Integer JavaDoc) mrtti.getParameterValues()[0];
195             Integer JavaDoc cachedValue = (Integer JavaDoc) s_cache.get(parameter);
196             if (cachedValue == null) {
197                 System.err.println("not in cache");
198                 Object JavaDoc newValue = joinPoint.proceed(); // not found => calculate
199
s_cache.put(parameter, newValue);
200                 return newValue;
201             } else {
202                 System.err.println("using cache: " + cachedValue);
203                 return cachedValue; // return cached value
204
}
205         }
206     }
207
208     /**
209      * Trace aspect.
210      */

211     public static class TraceAspect {
212         private int m_level = 0;
213
214         public Object JavaDoc trace(final JoinPoint joinPoint) throws Throwable JavaDoc {
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 JavaDoc 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