views:

815

answers:

3

I'm implementing an EJB-based system in JBoss.

One of my message driven beans will be responsible for sending emails. I want the email template to be stored externally (probably as XML) so that it can easily be changed without having to change the code/redeploy the bean, etc.

Where should this file be placed and how do I reference it?

+1  A: 

This question and this blog post refer to property files, but what is discussed also applies for other kind of files. One solution would be to place the XML file in a standard location and read it like this (code taken from the link above):

String path = System.getProperty("catalina.base")

                  + System.getProperty("file.seperator")

                  + "YOUR_FILE.properties";

FileInputStream fis = new FileInputStream(path);

Instead of catalina.base you can use user.dir or even define your own environmental variable (mypath_to_xml_file) and read it with System.getProperty.

Another solution would be to use JNDI to define the path to the file.

kgiannakakis
A: 

Or put it in a database where you can get at it via SQL client. Load it on startup or maybe poll to check for timestamp changes and reload it.

What kind of template do you mean? Is it a Velocity template that you populate in a mail merge fashion?
One thing that I like about the database design is that you can add so much more information to the schema that's over and above a mere file. You can include timestamp, user ID of the person who updated the template, version, meta data, system name, a boolean to indicate "live" or "dormant", etc. Your EJB can be really smart about which template it chooses. You might find yourself with even more options than you'll have if you simply park files on the server drive. They'll be available to other apps that way as well. Could be a nice design if you run with it a little bit.

If you add it to the WEB-INF/classes directory of an exploded deployment you can easily reference it using "getResourceAsStream()". Add a polling feature if you don't want to have to bounce the server.

duffymo
+2  A: 

The JBoss documentation specifies that the environment variable jboss.server.data.dir is the "location available for use by services that want to store content in the file system". See here for more details. You can get the value of the variable by

System.getProperty("jboss.server.data.dir");

And, as shown in the link, the location of the server/[config]/data directory will be returned. Store the template file there when you deploy your app, and instruct your admins to modify it there.

Armadillo
Just tried this successfully. It seems the best solution for us right now - however I'm a little confused as I thought it was against the EJB spec to be able to access the file system?
William
Also, would this cause a problem if we were to get to a stage where we needed to run multiple JBoss servers in a cluster - i.e. would the same file need to be duplicated on each one? The alternative I'm now considering is storing the info on the JNDI tree, which seems more scaleable.
William
Yes, there would be an issue with accessing the file system in a clustered deployment. In that case, I believe, file access is just too simplistic and another solution would be preferable. If you're running a system that merits a clustered environment, maybe you could consider a configuration service that would hide the details of accessing the template info.
Armadillo