Monday, September 21, 2009

Loading Message Resources From Database

Instead of loading message resource from properties file, here is the code to how to use database as a message resource bundle in struts applcation.

DBPropertyMessageResources.java

public class DBPropertyMessageResources extends MessageResources {

public DBPropertyMessageResources(MessageResourcesFactory factory, String config,boolean returnNull) {

super(factory, config,returnNull);

this.factory=factory;

this.config=config;

this.returnNull=false;

}

public String getMessage(Locale locale, String key) {

String result = DBPropertyMessageService.getMessage(locale, key,

returnNull);

// we need to flush the cache of the super class MessageResource as

//we want to have real dynamic data here (no caches

//except the one in DBPropertyMessageService)

formats.clear();

return result;

}

}


DBPropertyMessageResourcesFactory.java


public class DBPropertyMessageResourcesFactory extends

MessageResourcesFactory {

public MessageResources createResources(String config) {

return new DBPropertyMessageResources(this, config, this.returnNull);

}

}


DBPropertyMessageService.java


public class DBPropertyMessageService {

public static String getMessage(Locale locale, String key, boolean returnNull) {

key=null;

returnNull=true;

locale=null;

// do the jdbc coding to get the value from the database using the key

return "sparkle software";

}

}


struts-config.xml

<message-resources factory="com.sparkle.resources.DBPropertyMessageResourcesFactory" parameter="param1"/>

1 comment: