lovinterceptor.java

来自「一个很好的开源项目管理系统源代码」· Java 代码 · 共 85 行

JAVA
85
字号
package net.java.workeffort.service.support;import java.util.ArrayList;import java.util.List;import java.util.Map;import net.java.workeffort.infrastructure.LabelValueBean;import net.java.workeffort.infrastructure.cache.ICacheController;import net.java.workeffort.infrastructure.cache.OsCacheController;import net.java.workeffort.infrastructure.cache.RefreshRequiredException;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * Interceptor to allow caching of List of values. * <p> * This will intercept all methods with signature getCached*Lov called on the * <code>ListOfValuesService </code>. Methods with other signatures will not be * intercepted. So for example an invocation of method getXxxxxLov (note no * 'Cached' in name) will *not* be intercepted and will hit the database every * single time. * @see OsCacheController * @author Antony Joseph */public class LovInterceptor implements MethodInterceptor {    private static final Log logger = LogFactory.getLog(LovInterceptor.class);    private ICacheController cacheController;    public Object invoke(MethodInvocation methodInvocation) throws Throwable {        List lov = null;        boolean updated = false;        if (logger.isInfoEnabled()) {            logger.info("Lov interceptor: method name:" + methodInvocation.getMethod().getName()                    + ". target object:"                    + methodInvocation.getThis().getClass().getName());        }        // A RefreshRequiredException will occur when no value is found        // for the key in the cache. If this exception occurs you        // *absolutely* must either call the putObject() method to put the        // new object for that key in the cache or call        // cacheController.cancelUpdate() to let the cache know that you        // don't intent to set the value for the specific key.        // see OsCacheController.getObject().        try {            lov = (List) cacheController.getObject(methodInvocation.getMethod()                    .getName());        }        catch (RefreshRequiredException rfe) {            // invoke the method to get the lov            lov = (List) methodInvocation.proceed();            cacheController.putObject(methodInvocation.getMethod().getName(),                    lov);        }        finally {            // If something goes wrong we need to let the cache (oscache)            // release the lock on the cache entry.            if (!updated)                cacheController.cancelUpdate(methodInvocation.getMethod()                        .getName());        }        return lov;    }    /**     * @return Returns the cacheController.     */    public ICacheController getCacheController() {        return cacheController;    }    /**     * @param cacheController The cacheController to set. IOC     */    public void setCacheController(ICacheController cacheController) {        this.cacheController = cacheController;    }}

⌨️ 快捷键说明

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