Google
 
Web nishgoel.blogspot.com

Saturday, October 29, 2005

Message Driven POJO's

I read few blog entries about how Lingo can be used to write Message Driven POJO’s.

But doesn’t Lingo require having java and spring on both sender and receiver side for it to work.

This constraint is understandable for Spring HTTP since it’s just better version of RMI which anyway assumes a Java Interface at server side.

But for most real-world messaging solutions, usually we don’t have any control over who is putting the message. Message may come from some legacy application, external vendor etc. In these scenarios, I am not sure if Lingo approach can even be used.

Am I missing something here?

Tuesday, October 18, 2005

spring validation in business tier


While working for the spring validation in our business tier, I came across two main areas which needed some work.

1. Move the hard-coded validation rules from source code to commons-validator xml files. This is easy but tedious job. Use xdoclet tags to define spring validation rules for java bean. Xdoclet will generate the validation xml from it.

2. Unlike web tier, spring will not automatically call the validator for business service arguments. In web tier, once we set validation property in the controller to true, validation is automatically enabled. That’s not the case with business tier. So instead of calling validator explicitly in the beginning of every business service method, I used MethodInterceptor to that job.

Writing method interceptor was easy. But invoking validator was a little trickier!


If you look at validator interfaces validate method signature, it seems like this:
void validate(Object obj, Errors errors)

Here “obj” is the object to be validated. But where should we get "errors" implementation? Use BeanException for it.

Documentation is pretty sparse on validation in business tier. After lot of playing around, I got this piece of code working:

public class ValidatorInterceptor implements MethodInterceptor {

public Object invoke(MethodInvocation methodInvocation) throws Throwable {

Object[] args = methodInvocation.getArguments();

for (int i = 0; i <>

Object arg = args[i];

BeanException errors = new BeanException(arg, “arg”);

validator.validate(arg, errors);

if(errors.hasErrors()){

// do whatever you like

}

}

}

}

In the above code I am validating all the business service arguments.

For validator implementation, I used DefaultBeanValidator which is backed by commons-validator. You can use dependency injection in interceptor bean definition to set it.

For validation factory and validator bean definition, you have to download spring-validator jar from spring modules. Validation factory takes set of validation files as config locations. I specified validator-rules.xml and validation.xml for validation factory bean definition.

validator-rules.xml is the spring version of default validator file which comes packaged for struts validation rules. Other validation.xml is the custom validation rules file having application specific rules.

Let me know if you guys have any questions about this.

Friday, October 14, 2005

Spring Validator Framework

I am trying to make use of spring validator frameowrk for our business tier code. Even though spring documentation says that validator framework can be used both at client and business tier, there are hardly any examples I could found for using it at business tier.

Most of the examples make use of declarative validations for the form bean.

I know I can define the validation rules in the xml file, but how do I invoke the validator automatically for my business method. Currenlty I have to make explicit calls to validator in beginning of my business method. May be I have to write some kind of method interceptor for this?

Also I am looking into valang to define rules instead of using jakarta commons validator. Valang is part of spring modules sand box.

Wednesday, October 05, 2005

Spring caching module

Yesterday, I spent most part of the day to use Spring caching module in our project.
Right now we are using Spring JDBC via DAO's and that kind of pushed us to look into some caching mechanisms.

I am pretty impressed by the ease of use of Spring cache module. It basically abstracts out all the caching implementation details and gives us the flexibility to choose from JCS/EhCache/OsCache.

Make sure the business object being cached uses inner bean to define "target bean" to make auto-wiring to work. Even though we don't use auto-wiring feature, one of the Spring provided test class AbstractDependencyInjectionSpringContextTests uses auto-wiring by Type for setter injection.