KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > cache > CacheWrapper


1 /*
2   Copyright (C) 2002 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.cache;
19
20 import java.util.Hashtable JavaDoc;
21 import org.aopalliance.intercept.ConstructorInvocation;
22 import org.aopalliance.intercept.MethodInvocation;
23 import org.apache.log4j.Logger;
24 import org.objectweb.jac.aspects.timestamp.Timestamps;
25 import org.objectweb.jac.core.AspectComponent;
26 import org.objectweb.jac.core.Interaction;
27 import org.objectweb.jac.core.NameRepository;
28 import org.objectweb.jac.core.Wrapper;
29 import org.objectweb.jac.util.ObjectArray;
30
31 /**
32  * The wrapper of the CacheAC.
33  * @see CacheAC
34  */

35 public class CacheWrapper extends Wrapper {
36     static final Logger logger = Logger.getLogger("cache");
37
38     public CacheWrapper(AspectComponent ac) {
39         super(ac);
40     }
41
42     public CacheWrapper(AspectComponent ac, String JavaDoc stampsName) {
43         super(ac);
44         this.stampsName = stampsName;
45     }
46
47     String JavaDoc stampsName;
48     Timestamps stamps;
49
50     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
51         return cache((Interaction)invocation);
52     }
53    
54     Hashtable JavaDoc cache = new Hashtable JavaDoc();
55    
56     public Object JavaDoc cache(Interaction interaction) {
57         logger.debug("cache "+interaction+"?");
58         // Synchronization is not crucial: we may only loose cached value
59
MethodCache methodCache = (MethodCache)cache.get(interaction.method);
60         if (methodCache==null) {
61             if (stampsName!=null) {
62                 if (stamps == null)
63                     stamps = (Timestamps)NameRepository.get().getObject(stampsName);
64             }
65             methodCache = new MethodCache(stamps);
66             cache.put(interaction.method,methodCache);
67         }
68         Object JavaDoc[] argsArray = new Object JavaDoc[interaction.args.length];
69         System.arraycopy(interaction.args, 0, argsArray, 0, interaction.args.length);
70         int[] ignoredArgs = (int[])interaction.method.getAttribute(CacheAC.IGNORED_PARAMETERS);
71         ObjectArray args = new ObjectArray(argsArray);
72
73         MethodCache.Entry entry = methodCache.getEntry(args,ignoredArgs);
74         if (entry!=null) {
75             return entry.value;
76         }
77         Object JavaDoc result = proceed(interaction);
78         methodCache.putEntry(args,result);
79         return result;
80     }
81
82     /**
83      * Invalidates the cache
84      */

85     void invalidate() {
86         cache.clear();
87     }
88
89 }
90
Popular Tags