1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.components.interceptors;
18
19 import java.lang.reflect.Method;
20
21 import org.aopalliance.aop.Advice;
22 import org.aopalliance.intercept.Interceptor;
23 import org.aopalliance.intercept.MethodInterceptor;
24 import org.aopalliance.intercept.MethodInvocation;
25 import org.apache.jetspeed.cache.general.GeneralCache;
26
27 /***
28 * <p>
29 * AbstractCacheInterceptor
30 * </p>
31 * <p>
32 *
33 * </p>
34 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
35 * @version $Id: AbstractCacheInterceptor.java 516448 2007-03-09 16:25:47Z ate $
36 *
37 */
38 public abstract class AbstractCacheInterceptor implements Interceptor, MethodInterceptor, Advice
39 {
40
41 protected GeneralCache cache;
42 protected String uniquePrefix;
43
44 /***
45 *
46 */
47 public AbstractCacheInterceptor( GeneralCache cache, String uniquePrefix )
48 {
49 super();
50 this.cache = cache;
51 this.uniquePrefix = uniquePrefix;
52 }
53
54 /***
55 *
56 * @param cache
57 */
58 public AbstractCacheInterceptor( GeneralCache cache )
59 {
60 this(cache, null);
61 }
62
63
64 /***
65 *
66 * <p>
67 * buildKey
68 * </p>
69 *
70 * @param clazz
71 * @param method
72 * @param arg0
73 * @return
74 */
75 public static final String buildKey( String uniquePrefix, String arg0 )
76 {
77 return uniquePrefix + ":" + arg0 ;
78 }
79
80
81 /***
82 *
83 * <p>
84 * invoke
85 * </p>
86 *
87 * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
88 * @param mi
89 * @return
90 * @throws Throwable
91 */
92 public Object invoke( MethodInvocation mi ) throws Throwable
93 {
94 Object[] args = mi.getArguments();
95 Method method = mi.getMethod();
96 if (args == null)
97 {
98 throw new IllegalArgumentException(method.getDeclaringClass() + "." + method.getName()
99 + "() receives no arguments. "
100 + "CacheInterceptor can only intercept methods that have at least (1) argument.");
101 }
102
103 Object arg0 = args[0];
104 if(arg0 == null)
105 {
106 throw new IllegalArgumentException("CacheInterceptor requires that the first argument passed to a cached be non-null");
107 }
108
109 String prefix = null;
110 if(uniquePrefix != null)
111 {
112 prefix = buildKey(uniquePrefix, arg0.toString());
113 }
114 else
115 {
116 prefix = buildKey(mi.getMethod().getDeclaringClass().getName(), arg0.toString());
117 }
118
119 return doCacheOperation(mi, prefix);
120 }
121
122 protected abstract Object doCacheOperation( MethodInvocation mi, String uniqueKey ) throws Throwable;
123
124 }