Listing 5: AOP Method Execution Timer

 

package jbriscoe.article.spring.caching.interceptor;

import java.io.Serializable;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MethodCachingInterceptor implements MethodInterceptor {

	private CacheManager cacheManager;
	private final Log log = LogFactory.getLog(getClass());
	private final String CACHE_DATA_KEY = "CACHE_DATA";

	public Object invoke(final MethodInvocation methodInvocation)
			throws Throwable {
		final String targetMethodName = methodInvocation.getMethod().getName();

		if (log.isInfoEnabled()) {
		log.info("Attempting cacheManager.getCache first run (no args lookup).");
		}

		Cache cache = cacheManager.getCache(targetMethodName);

		if (cache == null) { // Lets try with args now.
			if (log.isInfoEnabled()) {
				log.info("No cache found in no args lookup.");
			}
			final Object[] methodArgs = methodInvocation.getArguments();
			if (methodArgs != null) {
				if (log.isInfoEnabled()) {
					log.info("Args on method attempting args cache lookup config.");
				}
				final StringBuffer cacheName = new StringBuffer(targetMethodName);
				cacheName.append("(");
				for (int i = 0; i < methodArgs.length; i++) {
					cacheName.append(methodArgs[i].toString());
					if (i + 1 != methodArgs.length) {
						cacheName.append(",");
					}
				}
				cacheName.append(")");
				if (log.isInfoEnabled()) {
					log.info("Attempting cacheManager.getCache with args lookup:"
							+ cacheName);
				}
				cache = cacheManager.getCache(cacheName.toString());
			}
		}

		boolean methodInvocationProceed = false;
		boolean methodInvocationCache = false;

		Object methodReturn = null;

		if (cache != null) {
			if (log.isInfoEnabled()) {
				log.info("Cache Config. Found: " + cache.getName());
			}
			final Element cacheElement = cache.get(CACHE_DATA_KEY);
			if (cacheElement == null) {
				methodInvocationProceed = true;
			} else {
				if (log.isInfoEnabled()) {
					log.info("Cache Element Found");
				}
				methodReturn = cacheElement.getValue();
				if (log.isInfoEnabled()) {
					log.info("Using Cached Element for methodReturn.");
				}
			}
			methodInvocationCache = true;
		}

		if (cache == null || methodInvocationProceed) {
			methodReturn = methodInvocation.proceed();
			if (methodInvocationCache) {
				final Element newCacheElement = new Element(CACHE_DATA_KEY,
						(Serializable) methodReturn);
				cache.put(newCacheElement);
				if (log.isInfoEnabled()) {
					log.info("Created new CacheElement entry and stored in cache.");
				}
			}
		}

		return methodReturn;
	}

	/**
	 * @return the cacheManager
	 */
	public CacheManager getCacheManager() {
		return cacheManager;
	}

	/**
	 * @param cacheManager
	 *          the cacheManager to set
	 */
	public void setCacheManager(CacheManager cacheManager) {
		this.cacheManager = cacheManager;
	}

}

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.