⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cacheinterceptor.java

📁 一个非常好的FRAMWRK!是一个外国组织做的!不!
💻 JAVA
字号:
/**
 * Copyright 2003-2005 the original author or authors.
 * 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 java.util.ArrayList;
import java.util.List;

import org.aopalliance.intercept.*;
import com.jdon.controller.model.*;
import com.jdon.model.*;
import com.jdon.util.*;

/**
 * 
 * Cache Interceptor all Interceptors are added in picoContainer
 * 
 * method match can be done by this class, CacheInterceptor only interceptor the
 * method getXXXXX.
 * 
 * @see AopInterceptorRegistry.java
 * 
 * <p>
 * </p>
 * @author banq
 */
public class CacheInterceptor implements MethodInterceptor {
    private final static String module = CacheInterceptor.class.getName();

    private ModelManager modelManager;

    public String match_MethodName = "get";

    private List isModelCache = new ArrayList();

    public CacheInterceptor(ModelManager modelManager) {
        this.modelManager = modelManager;
    }

    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
        }

        try {
            String dataKey = getArguments(invocation);
            if (dataKey == null) return invocation.proceed(); 
            Class modelClass = method.getReturnType();
            ModelKey modelKey = new ModelKey(dataKey, modelClass);
            Model model = modelManager.getCache(modelKey);
            if (model == null){                
                model = (Model) invocation.proceed(); //下一个interceptor
                Debug.logVerbose(" save to cache",    module);
                modelManager.addCache(modelKey, model);
            }
            return model;
        } catch (Exception e) {
            Debug.logError("CacheInterceptor Exception error:" + e, module);
        }
        return invocation.proceed();
    }
           

    /**
     * 1.check return type if is Model 2.check method name if include "get" 3.
     * if found them, cache this method
     * 
     * 
     * @param method
     *            Method
     * @return boolean
     */
    private boolean methodMatchsModelGET(Method method) {
        boolean condition = false;
        try {
            if (isModelCache.contains(method)) {
                condition = true;
                return condition;
            }

            String mehtodName = method.getName();
            if (method.getReturnType() == null)
                return condition; //无返回值,不做缓存
            Class returnClass = method.getReturnType();
            if (returnClass.getSuperclass() == null)
                return condition; //无返回值,不做缓存

            Debug.logVerbose("methodMatchsModelGET: returnClassName = "
                    + returnClass.getName(), module);
            if (Model.class.isAssignableFrom(returnClass)) {
                if (mehtodName.indexOf(match_MethodName) != -1) {
                    condition = true;
                    //method name include "getXXXX" and return Type is subClass
                    // of Model
                    isModelCache.add(method);
                }
            }
        } 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) {
	      
	      try {
	          Object[] args = invocation.getArguments();
	          if (args == null) return null;
	          if (args.length > 1) return null;//参数是一个主键
	    
	          return args[0].toString();
	      } catch (Exception ex) {
	          Debug.logError(" method:"+ invocation.getMethod().getName() 
	                         +" args is null or has not implements method: toString() ", module);
	          return null;
	      }
	    
	  }
  

    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 + -