My Garbage Collector

Hamdi Makni's Blog

Our more repeated code in classes is the data input validations perform, because it’s very important to validate transported object between applications, and between application layers.
On a classic web context, we have some validation form framework like Struts Validator Framework, who can validate form in the client side and server side, based on xml files. On web service context, payload validation is a basic function proposed with the most web service framework. These classic web and web services frameworks send formatted message to client if any data validations fail, and block performing the standard processing. Otherwise, processing continues.
In my case, I have an application core class with a public method invoked from other internal classes, or from another application layer, for example, a DAO method invoked from a service layer. Usually, we need to validate input parameters in your public methods. So we have generally to write some code like this, in the top of your method:

public void myMethod (MethodIN inParameter){
//input method validation
throw new Exception(…);
throw new Exception(…);
if(inParameter.aMyFirstBean.getCodeAsString() == null)
throw new Exception(…);
if(inParameter.aMyFirstBean.getCodeAsString().length() <2 || inParameter.aMyFirstBean.getCodeAsString().length() > 40)
throw new Exception(…);
//do something with the input parameters


This code is repeated every time when you have to use a mandatory input or strict formatted parameters.
Spring-modules have a validation package witch can used to validate data beans property with annotation or with xml document. In the xml file we must define the validation rules for every class and every property you want to validate. So I think that we can use spring-modules validator to validate input methods by using a method interceptor. The interceptor, wired in an aop-proxy of any service bean of my application, intercepts some methods, when you want to validate inputs, and proceed with the validation perform. The validator is configured in the application context file, and uses an xml file to get the validation rules.
So when the method is called, we fetch received object as input parameter, and put it to the “validation machine”. When fails, the interceptor throw an exception and stop method execution.

import java.util.Iterator;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springmodules.validation.bean.BeanValidator;

* @author Hamdi MAKNI
public class InputBeanValidator implements MethodInterceptor {

private BeanValidator beanValidator;
private Log log = LogFactory.getLog(this.getClass());

public Object invoke (MethodInvocation methodInvocation) throws Throwable {
Object[] arguments = methodInvocation.getArguments();
Errors errors = new BindException(this, this.getClass().toString());
String errorMessage = "";
for (int i = 0; i < arguments.length; i++) {
Object object = arguments[i];
if (object != null) {
Errors errorss = new BindException(object, object.getClass().toString());
beanValidator.validate(object, errorss);
// errors.getAllErrors().addAll(errorss.getAllErrors());
for (Iterator j = errorss.getAllErrors().iterator(); j.hasNext();) {
FieldError err = (FieldError );
if (errorss.hasErrors())
for (Iterator j = errors.getAllErrors().iterator(); j.hasNext();) {
Error err = (Error );
throw new Exception(errorMessage);
Object out = (Object ) methodInvocation.proceed();
return out;


And then, we need a proxy creator to create your service with a method interceptor. The interceptor





and we have to write an xml file whith the validation rules, like that:



Post a Comment