struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Mitchell" <jmitch...@apache.org>
Subject Re: Message Resources From Database
Date Fri, 02 May 2003 14:38:53 GMT
Sorry for taking so long to respond.


> ----- Original Message -----
> From: "M Nadzaruddin Kahar" <mnadzaruddin@hotmail.com>
> To: <jmitchell@apache.org>
> Sent: Thursday, May 01, 2003 7:41 AM
> Subject: Message Resources From Database
>
>
> Hey James,
>
> 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.

>
> Anyway, I followed the struts documentation in trying to create a custom
> MessageResources from a database. According to the documentation, you 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 you 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.

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. 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.

And that's why I created OJBMessageResources.

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

>
> 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. I don't know a whole lot about fulcrum but I
> must
> have stumbled upon some message in a forum or something that you can
> actually synchronize it with the current data in your database.
>
> Thanks.
> Naz


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



--
James Mitchell
Software Developer/Struts Evangelist
http://www.open-tools.org



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-dev-help@jakarta.apache.org


Mime
View raw message