001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.components.interceptors;
018:
019: import java.lang.reflect.Method;
020:
021: import org.aopalliance.aop.Advice;
022: import org.aopalliance.intercept.Interceptor;
023: import org.aopalliance.intercept.MethodInterceptor;
024: import org.aopalliance.intercept.MethodInvocation;
025: import org.apache.jetspeed.cache.general.GeneralCache;
026:
027: /**
028: * <p>
029: * AbstractCacheInterceptor
030: * </p>
031: * <p>
032: *
033: * </p>
034: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
035: * @version $Id: AbstractCacheInterceptor.java 516448 2007-03-09 16:25:47Z ate $
036: *
037: */
038: public abstract class AbstractCacheInterceptor implements Interceptor,
039: MethodInterceptor, Advice {
040:
041: protected GeneralCache cache;
042: protected String uniquePrefix;
043:
044: /**
045: *
046: */
047: public AbstractCacheInterceptor(GeneralCache cache,
048: String uniquePrefix) {
049: super ();
050: this .cache = cache;
051: this .uniquePrefix = uniquePrefix;
052: }
053:
054: /**
055: *
056: * @param cache
057: */
058: public AbstractCacheInterceptor(GeneralCache cache) {
059: this (cache, null);
060: }
061:
062: /**
063: *
064: * <p>
065: * buildKey
066: * </p>
067: *
068: * @param clazz
069: * @param method
070: * @param arg0
071: * @return
072: */
073: public static final String buildKey(String uniquePrefix, String arg0) {
074: return uniquePrefix + ":" + arg0;
075: }
076:
077: /**
078: *
079: * <p>
080: * invoke
081: * </p>
082: *
083: * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
084: * @param mi
085: * @return
086: * @throws Throwable
087: */
088: public Object invoke(MethodInvocation mi) throws Throwable {
089: Object[] args = mi.getArguments();
090: Method method = mi.getMethod();
091: if (args == null) {
092: throw new IllegalArgumentException(
093: method.getDeclaringClass()
094: + "."
095: + method.getName()
096: + "() receives no arguments. "
097: + "CacheInterceptor can only intercept methods that have at least (1) argument.");
098: }
099:
100: Object arg0 = args[0];
101: if (arg0 == null) {
102: throw new IllegalArgumentException(
103: "CacheInterceptor requires that the first argument passed to a cached be non-null");
104: }
105:
106: String prefix = null;
107: if (uniquePrefix != null) {
108: prefix = buildKey(uniquePrefix, arg0.toString());
109: } else {
110: prefix = buildKey(mi.getMethod().getDeclaringClass()
111: .getName(), arg0.toString());
112: }
113:
114: return doCacheOperation(mi, prefix);
115: }
116:
117: protected abstract Object doCacheOperation(MethodInvocation mi,
118: String uniqueKey) throws Throwable;
119:
120: }
|