Template design pattern in action part1

So finally I got time to complete the blog 🙂

I’ll discuss about how we can solve the problem using templating design pattern. So what we have done, we have written a Template class “CacheTemplate” having a method getCachedData.
getCachedData takes 2 parameters
key: Key for which we have data cached
cacheCallback: If we don’t have data cached then we will call this cacheCallback to get the data and store it into cache.


public class CacheTemplate {
private CacheProvider cacheProvider;
public  T getCachedData(String key,
CacheCallback cacheCallback) {
T data = (T) cacheProvider.get(key);
if (data == null) {
data = cacheCallback.cacheOperation();
cacheProvider.put(key, data);
}
return data;
}
}

Now taking forward the example taken in previos blog, let us apply this template to the “getPersons” method


public List getPersons() {

return cacheTemplate.getCachedData("persons", new CacheCallback>() {
@Override
public List cacheOperation() {
_getPersons();
}
}
}

private List _getPersons() {
persons = //Business logic to get persons;
return persons;
}

Now if you compare the current implementation with the previous implementation and check the concerns we had they all are resolved.
1.) Now our business logic of retrieving the persons is at one place.
2.) Now we have a generic implementation of managing cache.

Property file reader

In my current project we have some property files and we need to read properties from those property files, so we come up with an approach to read property files. The main points that we need to focus was
1.) The properties should be loaded only once, In a way we need singleton behavior
2.) We can have multiple property files
One assumption that we have is property files will always be located in root class path

Now for the solution of this problem we thought of using Enum (effective java ;)) as they internally provide the feature of singleton behavior, also for multiple property file situation we created multiple instances corresponding property file.

So the code that we have written is like this


public enum PropertiesReader {
     SEARCH("search.properties"),APP("app.properties");

     Properties properties;
     private Logger log = LoggerFactory.getLogger(PropertiesReader.class);

     private PropertiesReader(String propertyFile) {
   properties = new Properties();
          try {
               InputStream inputStream =    this.getClass().getClassLoader().getResourceAsStream(propertyFile);
               properties.load(inputStream);
          }
          catch (IOException e) {
               log.error(propertyFile + " was not found in classpath");
          }
   }

     public String getValue(String key) {
          return (String) properties.get(key);
     }
}

So now
if we want to read a property from search.properties file
assertThat(PropertiesReader.SEARCH.getValue(“max.pages.to.display”)

if we want to read a property from app.properties file
assertThat(PropertiesReader.APP.getValue(“debug_mode”)

JSR-303

JSR-303
The main idea behind JSR-303 is to have a common approach for validation at all places of the application in a simplistic way. The bean validation is built keeping in mind the validation process as a kind of meta information for a bean, so if you say that a property of bean should be not null or the content of a bean property should match to the format of e-mail or telephone. Such kind of information can be treated as meta-information of bean properties. One of the other motive for having JSR-303 into picture is to remove the boilerplate code introduced due to the need of bean validation at multiple places in the application, such as at UI level, while persisting a bean or performing some business operation.

JSR-303 provides bean validation using two ways, either you can configure validations using annotations on properties or using a XML validation descriptor. Annotations can be used as Meta-data for bean validation in case of some simple validations such as not null, e-mail, telephone number check. XML validation descriptor is used for some complex validations or context aware validation.

JSR-303 also defines API for Java Bean validation, so this API can be used to programmatically validate the bean .

Bean validation JSR-303

In my current project we are working on a web-application using JBoss Seam. One of the very common requirements while working on a web application is validating the data contained in a bean. Seam is powered with annotation based validation, seam uses Hibernate Validator for this feature. Hibernate validator is a reference implementation of Bean Validation (JSR-303).JSR-303 defines a meta-data model and API for JavaBean validation based on annotations.
In my coming posts I’ll be focussing on JSR-303 and it’s reference implementations. Some of the posts I’m thinking of right now
– Bean Validation JSR-303
– Hibernate Reference implementation
– Using Hibernate Validation, accessing it programmatically
– Creating custom annotation validator

2010 Focus

So a new year has begun it’s the time to plan things for this year. It’s good to plan things in advance 🙂 atleast you dont need to plan every time, you already have things that you need to focus right :).
Initially I’ll be focussing on Seam, as in my current project I’m working on seam, since it’s a new technology for me so a lot needs to be learnt in it.
Other things on which I’ll be wroking will be Java EE6, Spring, JPA . Other then this I’ll be picking something as of interest so right now I’m thinking of mobile applications.
Let’s see how things go this year. Happy blogging 🙂