Maybe these code snippets will help:
Code:
public class BindAndValidateInterceptor extends BaseObject implements MethodInterceptor {
public Object invoke(MethodInvocation mi) throws Throwable {
Object[] args = mi.getArguments();
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof PersistableEntity) {
PersistableEntity validatable = (PersistableEntity) args[i];
if (logger.isDebugEnabled())
logger.debug("bindAndValidate calling for: " + validatable.toString());
validatable.bindAndValidate();
}
}
return mi.proceed();
}
}
Code:
/**
* Advisor for the {@link BindAndValidateInterceptor}.
*
* <p>
* Intended to be used with Spring's <code>DefaultAdvisorAutoProxyCreator</code>.
* </p>
*
* <p>
* Registers <code>BindAndValidateInterceptor</code> for every <code>Method</code>
* against a class that directly or through its superclasses implements
* {@link Dao} and has a signature of:
* <ul>
* <li>create(*, PersistableEntity, *)</li>
* <li>update(*, PersistableEntity, *)</li>
* </ul>
*
* <P>As shown, any additional method arguments aside from <code>PersistableEntity</code>
* will still cause the advisor to match them.
*
* <p>The above list may be refactored in the future to a property but at this time
* it is considered unnecessary.
*
* @author Ben Alex
* @version $Id: BindAndValidateAdvisor.java,v 1.1 2004/11/02 23:45:11 administrator Exp $
*/
public class BindAndValidateAdvisor extends StaticMethodMatcherPointcutAdvisor {
//~ Instance fields ========================================================
//~ Constructors ===========================================================
public BindAndValidateAdvisor(BindAndValidateInterceptor advice) {
super(advice);
if (advice == null) {
throw new AopConfigException(
"Cannot construct a BindAndValidateAdvisor using a "
+ "null BindAndValidateInterceptor");
}
}
//~ Methods ================================================================
public boolean matches(Method m, Class targetClass) {
if (!m.getName().equals("create") && !m.getName().equals("update")) {
return false;
}
// Iterate methods to see if any are assignable from PersistableEntity
Class[] params = m.getParameterTypes();
boolean found = false;
for (int i = 0; i < params.length; i++) {
if (params[i].isAssignableFrom(PersistableEntityLong.class)) {
found = true;
}
}
if (!found) {
return false;
}
// A suitable Method was found, so ensure this is a DAO
Class currentClass = targetClass;
while (currentClass != null) {
if (currentClass.isAssignableFrom(Dao.class)) {
return true;
}
currentClass = currentClass.getSuperclass();
}
return false;
}
}
Code:
public abstract class BusinessObject implements Serializable {
/**
* Ensures the object-wide state is correct.
*
* Will NEVER change the object state. Will test object-wide parameters,
* and as such SHOULD be called after the {@link #bindSupport()} method.
*
* @throws BindException if there is a problem
*/
public final void validate() throws BindException {
Class clazz = this.getClass();
Validator v = this.getInternalValidator();
if (v == null)
throw new IllegalArgumentException("Cannot validate as Validator not wired against: " + this);
if (v.supports(clazz)) {
Errors errors = new BindException(this, clazz.getName());
v.validate(this, errors);
if (errors.getErrorCount() > 0) {
throw (BindException) errors;
}
} else {
throw new IllegalArgumentException("Validator " + v.getClass().getName() + " does not support this class: " + clazz.getName());
}
}
public abstract Validator getInternalValidator();
/**
* Performs any object-wide tidying of properties etc. Should be called
* by business methods prior to presenting the object for saving.
*
* <P>Default implementation does nothing. Subclasses should override.
*/
public void bindSupport() {}
/**
* Delegates to the {@link #bindSupport()} and {@link #validate()} methods.
* Should be called by business methods needing to ensure they are in a
* valid state. This method
* SHOULD NOT be called from a MVC controller, as the MVC controller will
* separately invoke the <code>Validator</code> and {@link #bindSupport()}
* methods.
*/
public final void bindAndValidate() throws BindException {
this.bindSupport();
this.validate();
}
}
PersistableEntity extends BusinessObject.