Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Java Validation With Dynamic Proxies

By Eric Olson
2005-05-14


The invocation handler

The invocation handler class is where all of the data validation logic is handled. The invocation handler class will also delegate method calls to a real implementation class in order to process the core business logic. Listing 4 shows an invocation handler that is not tied to any specific business object, and could therefore be used along with any business object that needs to be validated. Notice how the validation code in the invoke() method below is nearly identical to the code from Listing 3; in fact, I could use the same validator service here as I did earlier.

Listing 4. The invocation handler

/**

* This is the object to which methods are delegated if they are not
* handled directly by this invocation handler. Typically, this is the
* real implementation of the business object interface.
*/
private Object delegate = null;

/**
* Create a new invocation handler for the given delegate.
* @param delegate the object to which method calls are delegated if
* they are not handled directly by this invocation handler.
*/
public BusinessObjectInvocationHandler(Object delegate) {
this.delegate = delegate;
}

/**
* Processes a method call.
* @param proxy the proxy instance upon which the method was called.
* @param method the method that was invoked.
* @param args the arguments to the method call.
*/
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {

// call the validator:
BusinessObjectValidationService.validate(proxy, method.getName(), args);

// could perform any other method pre-processing routines here...

/* validation succeeded, so invoke the method on the delegate. I
only catch the InvocationTargetException here so that I can
unwrap it and throw the contained target exception. If a checked
exception is thrown by this method that is not assignable to any of
the exception types declared in the throws clause of the interface
method, then an UndeclaredThrowableException containing the
exception that was thrown by this method will be thrown by the
method invocation on the proxy instance.
*/
Object retVal = null;
try {
retVal = method.invoke(delegate, args);
} catch (InvocationTargetException ite) {
/* the method invocation threw an exception, so "unwrap" it and
throw it.
*/
throw ite.getTargetException();
}

// could do method post-processing routines here if necessary...

return retVal;
}
As you can see, this implementation of the invocation handler makes use of a generic validator service, the same as in Listing 3. For an alternative solution, I could have created an invocation handler that looks more like the one from Listing 2, where the validation code is run directly in the invocation handler. In this case, I would have used the invocation handler itself to check whether the method called was the setPassword() method and length and null checks would happen directly in the handler. While this approach works to decouple the interface's validation logic from its core business code, it isn't very reusable or configurable. I'll continue with the generic-validator implementation in the next section, where you'll really begin to see the value of reusable and reconfigurable code.

Tutorial Pages:
» Decouple validation processes from your business object implementations
» Tightly coupled validation
» Loosely coupled validation
» The dynamic proxy approach
» The invocation handler
» The business object implementation
» The business object factory
» Drawbacks of dynamic proxies
» Other uses for dynamic proxies
» Conclusion
» Resources


First published by

Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT