📄 cacheinterceptor.java
字号:
/**
* Copyright 2005 Jdon.com
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jdon.aop.interceptor;
import java.lang.reflect.*;
import org.aopalliance.intercept.*;
import com.jdon.controller.model.*;
import com.jdon.model.*;
import com.jdon.util.*;
/**
* 缓存拦截器
* 所有拦截器是存放在PicoContainer中,
*
* Cache Interceptor
* all Interceptors are added in picoContainer
* @see AopInterceptorRegistry.java
*
* <p></p>
* @author banq
* @version 1.0
*/
public class CacheInterceptor implements MethodInterceptor {
private final static String module = CacheInterceptor.class.getName();
private ModelManager modelManager;
public String match_MethodName = "get";
public CacheInterceptor(ModelManager modelManager){
this.modelManager = modelManager;
}
/**
* Cache实现方法
* 1.检查返回类型是否为Model
* 2.检查方法名中是否有get;
* 3.实现Model 缓存。
*
* 拦截成功,就直接返回, 该拦截器适合是倒数第一,紧靠数据源
*
* @param invocation MethodInvocation
* @throws Throwable
* @return Object
*/
public Object invoke(MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod();
Debug.logVerbose(" enter cacheInteceptor method:" + method.getName(), module);
if (!methodMatchsModelGET(method)) {
Debug.logVerbose(" cacheInteceptor don't action, enter next invocation.proceed()", module);
return invocation.proceed(); //下一个interceptor
}
String dataKey = getArguments(invocation);
Class modelClass = method.getReturnType();
ModelKey modelKey = new ModelKey(dataKey, modelClass);
Model model = modelManager.getCache(modelKey);
if (model == null){
model = (Model) invocation.proceed(); //下一个interceptor
if ( (model != null) && (dataKey != null) && (model.isCacheble())) {
modelManager.addCache(modelKey, model);
}
}
return model;
}
/**
* * 1.检查返回类型是否为Model
* 2.检查方法名中是否有get;
* @param method Method
* @return boolean
*/
private boolean methodMatchsModelGET(Method method) {
boolean condition = false;
try {
String mehtodName = method.getName();
if (method.getReturnType() == null)return condition; //无返回值,不做缓存
Class returnClass = method.getReturnType();
if (returnClass.getSuperclass() == null)return condition; //无返回值,不做缓存
String returnClassName = returnClass.getSuperclass().getName();
Debug.logVerbose("returnClassName = " + returnClassName, module);
if ( (returnClassName.equalsIgnoreCase("com.jdon.controller.model.Model"))
&& (mehtodName.indexOf(match_MethodName) != -1)) {
condition = true;
}
} catch (Exception ex) {
Debug.logError("Exception error:" + ex, module);
} catch (Throwable the) {
Debug.logError("Throwable error:" + the, module);
}
return condition;
}
/**
* 组合参数数值为一个字符串
* 这些参数必须实现toString();
*
* @param invocation MethodInvocation
* @return String
*/
public String getArguments(MethodInvocation invocation) {
Object[] args = invocation.getArguments();
if (args == null) return null;
StringBuffer buff = new StringBuffer();
for (int i = 0; i < args.length; i++) {
buff.append(getArgString(args[i]));
}
return buff.toString();
}
private String getArgString(Object arg) {
String res = "";
try {
res = arg.toString();
} catch (Exception ex) {
Debug.logError(" arg: " + arg.getClass().getName() +
" has not implements method: toString() ", module);
}
return res;
}
public String getMatch_MethodName() {
return match_MethodName;
}
public void setMatch_MethodName(String match_MethodName) {
this.match_MethodName = match_MethodName;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -