• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loading a properties file from within an EJB

 
Vincent Hernandez
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before going on with this post, I apologize if this isn't the best place to put this question, but after reading the thread .properties vs JNDI, I see my question is related.

I'm trying to load a .properties file from within an .ear file. In other words, I have a properties file inside my .ear file, and I'm trying to find the right way of loading the file. Currently my solution which works is utilizing the getResourceAsStream() method which returns an InputStream object which will be used in Properties.load(InputStream), therefore loading the properties. This works flawlessly either when the ear is exploded or running locally or when it's deployed.

However, when I read the above mentioned thread, I see using java.io goes against the EJB specs. First of all, is this true? Secondly, if it is true, is my current technique in violation of this?

I'm finding some conflict with this because I also have seen threads regarding the use of Struts where the struts-specific files make use of getResourceAsStream. This is partly where I'm finding some confusion.

I will stress I am using this properties file for READING ONLY. I am NOT writing to it in any way shape or form. Thanks a bunch.

PS -- If any Moderator feels that this is not appropriate for this forum, please move it. Thank you
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Currently my solution which works is utilizing the getResourceAsStream() method which returns an InputStream object which will be used in Properties.load(InputStream), therefore loading the properties. This works flawlessly either when the ear is exploded or running locally or when it's deployed.

Which is a pretty good solution. However there is a pitfall with this approach: be careful how you set your classpath. If your system classpath will point to an older version of your properties file and you deploy your ear with a newer version, then a very unexpected thing will happen: the older file will be loaded. Check your container classloading architecture first.

However, when I read the above mentioned thread, I see using java.io goes against the EJB specs. First of all, is this true? Secondly, if it is true, is my current technique in violation of this?

Your technique is just fine and many developer uses it. Sometimes they discover the pitfall above in a very hard way :
Regards.
 
Vincent Hernandez
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Valentin! I was very concerned about the classpath issue as well, and I can say from my end I don't allow for multiple versions of the properties file to be lying around in the system.

I still would appreciate if anyone else can provide additional feedback.

Thanks,
Vincent
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, when I read the above mentioned thread, I see using java.io goes against the EJB specs. First of all, is this true? Secondly, if it is true, is my current technique in violation of this?

Your current technique is certainly in violation of the EJB spec. Load the file into a ResourceBundle, there is plenty of documentation on how to do this.
 
Vincent Hernandez
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your current technique is certainly in violation of the EJB spec. Load the file into a ResourceBundle, there is plenty of documentation on how to do this.


I see you're the person who stated the same fact in the thread I listed in my original post, but can you please point me to where exactly this is stated in the EJB spec?

Also, I'm curious about the ResourceBundle. Isn't this intended for Locale-specific settings with regard to language?
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look in 2.4.1.2 Programming Restrictions of the EJB 2.0 Spec:

An enterprise bean must not use the java.io package to attempt to access files and directories in the file system.


Resource bundles contain key/value pairs. Once you have created a resource bundle from your properties file, the most useful methods are likely to be getKeys() and getString().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!