KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > caching > CachingAspect


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 examples.caching;
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
14 import org.codehaus.aspectwerkz.joinpoint.MethodSignature;
15 import org.codehaus.aspectwerkz.joinpoint.MethodRtti;
16 import org.codehaus.aspectwerkz.joinpoint.Rtti;
17 import org.codehaus.aspectwerkz.AspectContext;
18 import org.codehaus.aspectwerkz.AspectContext;
19 import org.codehaus.aspectwerkz.AspectContext;
20
21 /**
22  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
23  * @Aspect perInstance name=NAME
24  */

25 public class CachingAspect {
26
27     /**
28      * The cross-cutting info.
29      */

30     private final AspectContext m_info;
31
32     /**
33      * The cache.
34      */

35     private final Map JavaDoc m_cache = new HashMap JavaDoc();
36
37     /**
38      * We are interested in cross-cutting info, therefore we have added a constructor that takes a
39      * cross-cutting infor instance as its only parameter.
40      *
41      * @param info the cross-cutting info
42      */

43     public CachingAspect(final AspectContext info) {
44         m_info = info;
45     }
46
47     /**
48      * @Before call(int examples.caching.Pi.getPiDecimal(int)) && withincode(int
49      * examples.caching.main(String[]))
50      */

51     public void invocationCounter(final JoinPoint joinPoint) throws Throwable JavaDoc {
52         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
53         CacheStatistics.addMethodInvocation(signature.getName(), signature.getParameterTypes());
54     }
55
56     /**
57      * @Around execution(int examples.caching.Pi.getPiDecimal(int))
58      */

59     public Object JavaDoc cache(final JoinPoint joinPoint) throws Throwable JavaDoc {
60         MethodRtti mrtti = (MethodRtti) joinPoint.getRtti();
61         final Long JavaDoc hash = new Long JavaDoc(calculateHash(mrtti));
62         final Object JavaDoc cachedResult = m_cache.get(hash);
63         if (cachedResult != null) {
64             System.out.println("using cache");
65             CacheStatistics.addCacheInvocation(mrtti.getName(), mrtti.getParameterTypes());
66             System.out.println("parameter: timeout = " + m_info.getParameter("timeout"));
67             return cachedResult;
68         }
69         final Object JavaDoc result = joinPoint.proceed();
70         m_cache.put(hash, result);
71         return result;
72     }
73
74     // ============ Utility methods ============
75

76     private long calculateHash(final MethodRtti rtti) {
77         int result = 17;
78         result = 37 * result + rtti.getName().hashCode();
79         Object JavaDoc[] parameters = rtti.getParameterValues();
80         for (int i = 0, j = parameters.length; i < j; i++) {
81             result = 37 * result + parameters[i].hashCode();
82         }
83         return result;
84     }
85 }
Popular Tags