M Nadzaruddin Kahar:

I found the thread with regards to creating message resources from a database. I couldn't get to the sample that James Mitchell has worked on. That's why I am posting this message. Another reason is that I got frustrated trying to find the proper message id for a particular message in nagoya.apache.org archive list. I only used the msgId given in the URL. I guess it doesn't work that way because each time I sent a request to struts-dev-get.xxx@jakarta.apache.org (xxx=msgId) I kept getting the wrong message.

Anyway, I followed the struts documentation in trying to create a custom MessageResources from a database. According to the documentation, we need to subclass both MessageResources and MessageResourcesFactory. I did that and I named both of them DatabaseMessageResources and DatabaseMessageResourcesFactory respectively. However, later on I realized that method MessageResourcesFactory.createResources only accepts a string. Since every time a factory is created it's casted to MessageResourcesFactory, no matter what we do the subclass ( add some methods to accept servlet context for example ) won't be seen. So all I could do was to play around with String config parameter. It was pretty much useless because I wanna make use the datasource created from struts-config.xml. Since the datasource is stored in the servlet context, and if I can't pass the servlet context to my DatabaseMessageResources, I couldn't do anything. Then I thought perhaps I could simply hard code the connection details within DatabaseMessageResources itself. This proved pointless because if I do it that way, I won't be using the connection pool since the connection pool is stored in the servlet context as well. So I am pretty much stuck.

Later on I found out James Mitchell has implemented some sort of work around for this. So I am kind of curious as to how he managed to do it. I don't know why the design of MessageResources is pretty much limited in perspective. It only looks at retrieving the resources from properties files. It's not flexible at all. To be honest I am not a fan of storing resources in files. It's going to get cumbersome to maintain and besides it's difficult to enforce data security on them. If we put them in a database, it's a lot easier to establish their relationship with the rest of the data model of an application.

The framework of a company that I have worked for used cache to store the resources. It puts it in the session and it's initialized by the controller servlet. I think someone mentioned about using cache as well, which I think is definitely a good idea. I don't know a whole lot about fulcrum but I must have stumbled upon some message in a forum or something that we can actually synchronize it with the current data in our database.

So can someone tell me how to work around this problem please.


Kent Sølvsten Rasmussen:

Have You considered the following idea?

  1. Create a class with a static reference to a datasource (so it is accessible from DatabaseMessageRessources).

2. Create a plugin fetching the datasource from the servlet context, and setting the aforementioned reference to this datasource.


James Mitchell:

 > I found the thread with regards to creating message resources from a 
 > database. I couldn't get to the sample you've worked on. That's why I am 
 > emailing you. I tried to email you with that telocity email address. I 
 > guess you no longer use it. 

That's correct. I switched ISPs recently and so I lost that address. I use the apache.org one now, because I can easily switch where it forwards to.

 > ... So I am pretty much stuck ... 

Yep, that's the way it was implemented, I've done my own grumbling over this same issue, but it's not going to change before 1.1.

 > Later on I found out you've implemented some sort of work around for this. 

Sort of.

 > So I am kind of curious as to how you managed to do it. I hope you don't 
 > mind sharing a tip or two. I don't know why the design of MessageResources 
 > was pretty much limited in perspective. It only looked at retrieving the 
 > resources from properties files. 

I agree!!

 > ... It's not flexible at all. ...  

And that's why I created OJBMessageResources.

The download is available here: http://sourceforge.net/project/showfiles.php?group_id=49385&release_id=154972

 > The framework of a company that I have worked for used cache to store the 
 > resources. It puts it in the session and it's initialized by the 
 > controller servlet. 

The Struts framework also caches the key-value pairs. It even goes one step further, it caches the "formatted" pairs.

So (for example) if you are using:

some.value=Please don't forget to fill in the ${0} field.

...then when you call it, you pass arg0="First Name", then struts only has to do the substitution one time. It isn't configurable, but you can grab the internal field and dump the map (as I do) to acheive no-cache.

> I think someone mentioned about using cache as well, which I think
> is definitely a good idea ...

Using the OJBMessageResources is quite simple:

Here's what I did today, while writing this response.

I downloaded the struts nightly build from last night. Then took the struts-example.war and...

 - renamed to struts-example.zip 
 - unzipped it 
 - moved to tomcat/webapps 
 - copied jars to tomcat/webapps/struts-example/WEB-INF/lib 
   - jdbc2_0 (you know what I mean) 
   - mm.mysql (ditto) 
   - ojb-msg-res.jar (from ojb download) 
   - jakarta-ojb-0.9.4.jar 
   - log4j.jar 
 - copied ojb files (from ojb download) 
    (you can change these to suit your needs, or use them "as is") 
   - OJB.properties 
   - repository.dtd 
   - repository.xml 
   - repository_internal.xml 
   - repository_user.xml 
 - ran 2 sql scripts against mysql 
   - mysql.sql 
   - struts-example-data.sql 
 - changed message-resource in struts-config to use custom factory 

     <message-resources 
          factory="org.apache.struts.util.OJBMessageResourcesFactory" 
        parameter="." 
     /> 

 - started the webapp 

André Wegmüller 2005-05-11:

See how to load (and reload) message ressources from a database on this page: DatabaseMessageResourcesHowto

  • No labels