com.discursive.dao.generic.spring.FinderIntroductionInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of generic-dao Show documentation
Show all versions of generic-dao Show documentation
A generics DAO pattern/library
The newest version!
package com.discursive.dao.generic.spring;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.beanutils.MethodUtils;
import org.hibernate.LockMode;
import org.springframework.aop.IntroductionInterceptor;
import com.discursive.dao.generic.hibernate.FinderExecutor;
public class FinderIntroductionInterceptor implements IntroductionInterceptor {
public static final String FIND = "find";
public static final String ITERATE = "iterate";
public static final String SCROLL = "scroll";
public static final String LOCK = "lock";
public static final String UNIQUE = "unique";
public static final String COUNT = "count";
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
Object target = (Object) methodInvocation.getThis();
String invokeMethod = null;
LockMode lockMode = LockMode.NONE;
String methodName = methodInvocation.getMethod().getName();
Object[] arguments = methodInvocation.getArguments();
boolean done = false;
methodName = methodName.toLowerCase();
while(!done) {
if (methodName.startsWith(FIND)) {
invokeMethod = "executeFinder";
methodName = methodName.substring(FIND.length());
} else if (methodName.startsWith(LOCK)) {
lockMode = LockMode.UPGRADE;
methodName = methodName.substring(LOCK.length());
} else if (methodName.startsWith(ITERATE)) {
invokeMethod = "executeIterator";
methodName = methodName.substring(ITERATE.length());
} else if (methodName.startsWith(SCROLL)) {
invokeMethod = "executeScroller";
methodName = methodName.substring(SCROLL.length());
} else if (methodName.startsWith(UNIQUE)) {
invokeMethod = "executeUnique";
methodName = methodName.substring(UNIQUE.length());
} else if (methodName.startsWith(COUNT)) {
invokeMethod = "executeCount";
methodName = methodName.substring(COUNT.length());
} else {
done = true;
}
}
if (arguments == null) {
arguments = new Object[0];
}
if (invokeMethod != null) {
return MethodUtils.invokeExactMethod(target, invokeMethod, new Object[] { methodInvocation.getMethod(),
arguments, lockMode });
} else {
return methodInvocation.proceed();
}
}
@SuppressWarnings("unchecked")
public boolean implementsInterface(Class intf) {
return intf.isInterface() && FinderExecutor.class.isAssignableFrom(intf);
}
}