View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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 }