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

📄 methodcacheinterceptor.java

📁 动态实现基于角色的权限管理Acegi+hibernate
💻 JAVA
字号:
package sample.auth.interceptor;

import java.io.Serializable;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.acegisecurity.intercept.AbstractSecurityInterceptor;
import org.acegisecurity.intercept.InterceptorStatusToken;
import org.acegisecurity.intercept.ObjectDefinitionSource;
import org.acegisecurity.intercept.method.MethodDefinitionSource;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;

public class MethodCacheInterceptor extends AbstractSecurityInterceptor implements MethodInterceptor {

    private MethodDefinitionSource objectDefinitionSource;
    private Cache cache;
    //~ Methods ================================================================
    EhCacheManagerFactoryBean a;
    public Cache getCache() {
		return cache;
	}

	public void setCache(Cache cache) {
		this.cache = cache;
	}

	public void setObjectDefinitionSource(MethodDefinitionSource newSource) {
        this.objectDefinitionSource = newSource;
    }

    public MethodDefinitionSource getObjectDefinitionSource() {
        return this.objectDefinitionSource;
    }

    public Class getSecureObjectClass() {
        return MethodInvocation.class;
    }

    /**
     * This method should be used to enforce security on a
     * <code>MethodInvocation</code>.
     *
     * @param mi The method being invoked which requires a security decision
     *
     * @return The returned value from the method invocation
     *
     * @throws Throwable if any error occurs
     */
    public Object invoke(MethodInvocation invocation) throws Throwable {
    	
    	String targetName  = invocation.getMethod().getDeclaringClass().getName(); 
    	
        String methodName  = invocation.getMethod().getName(); 
        Object[] arguments = invocation.getArguments(); 
       // String d = invocation.getMethod().getDeclaringClass().getName();
        Object result = null;
        String cacheKey = getCacheKey(targetName, methodName, arguments);
        
        Element element = cache.get(cacheKey); 

        if (element == null) { 
        	InterceptorStatusToken token = super.beforeInvocation(invocation);
           
            try {
                result = invocation.proceed();
            } finally {
                result = super.afterInvocation(token, result);
            }
            
            element = new Element(cacheKey, (Serializable) result); 
            cache.put(element); 
       } 
       return element.getValue(); 
    }

    public ObjectDefinitionSource obtainObjectDefinitionSource() {
        return this.objectDefinitionSource;
    }
    
    /** 
     *创建一个缓存对象的标识: targetName.methodName.argument0.argument1... 
     */ 
    private String getCacheKey(String targetName, 
                               String methodName, 
                               Object[] arguments) { 
      StringBuffer sb = new StringBuffer(); 
      sb.append(targetName) 
        .append(".").append(methodName); 
      if ((arguments != null) && (arguments.length != 0)) { 
        for (int i=0; i<arguments.length; i++) { 
          sb.append(".") 
            .append(arguments[i]); 
        } 
      } 

      return sb.toString(); 
    } 

    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -