File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes .properties vs JNDI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark ".properties vs JNDI" Watch ".properties vs JNDI" New topic
Author

.properties vs JNDI

marcus don
Ranch Hand

Joined: Jan 26, 2005
Posts: 38
Hi all,
may you please explain me if using a .properities file is
against the specs (don't try to search for a resource on filesystem) and if I should use a jndi context instead ?

Example:
I need to store a fix tax rate in an external file (or similar).
May I use .properties or I should put this value in a .xml (something like ejb-jar.xml... or I don't know ... but reachable by jndi)

Thanks
Jeff Walker
Ranch Hand

Joined: Apr 25, 2004
Posts: 116
Originally posted by marcus don:
Hi all,
may you please explain me if using a .properities file is
against the specs (don't try to search for a resource on filesystem) and if I should use a jndi context instead ?

Example:
I need to store a fix tax rate in an external file (or similar).
May I use .properties or I should put this value in a .xml (something like ejb-jar.xml... or I don't know ... but reachable by jndi)

Thanks


Very good question.
Page 625 of HF-EJB shows that no use of java.io is allowed, but you can read properties by using the ResourceBundle class, which itself, does not use java.io package. Now, if you call the static method getBundle(), I believe it will attempt to return to you a ResourceBundle that represents the Locale you are trying to find.

It will search in memory first, but if it can't find it, then it appends ".properties" to the Locale string you passed in to getBundle() and searches the file system for an applicable properties file. Then the ResourceBundle returned to you (which is an abstract class), will actually be an instantiation of a concrete class called PropertyResourceBundle. Once this happens, PropertyResourceBundle does use the java.io package.

So in the end, I would guess that, no you can't attempt to retrieve a Locale using ResourceBundle, because it could (probably will) end up using the java.io package.
Kathy, Bert??
-jeff walker
Keerthi P
Ranch Hand

Joined: Aug 19, 2003
Posts: 203
Yes. I think Jeff is correct. There is no other way to access a .properties file than using (though indirectly) java.io classes. So it is not advisable to use it in EJBs. I would rather use the environment entries if such requirement should arise.


Cheers.<br />Keerthi<br />(SCJP, SCWCD, SCBCD)
marcus don
Ranch Hand

Joined: Jan 26, 2005
Posts: 38
Thank you very much!
I'm sure you are correct.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
You must not assume that ResourceBundle probably will end up using the java.io package as this is an implementation issue that we must never be concerned about.

Bearing in mind that the JNDI tree is not designed to be used as a storage area and that all developers should be familiar with a properties file, I would strongly recommend the use of a properties file in an EJB container - with the caveat that a ResourceBundle is created from the contents of the file.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
marcus don
Ranch Hand

Joined: Jan 26, 2005
Posts: 38
Kathy, Bert???
sawan parihar
Ranch Hand

Joined: Aug 24, 2004
Posts: 250
Hi,
In my case I am using a server startup file( like T3StartupDef in case of weblogic ) that reads the properties file and then can use the values of the properties file later. Also a environment entery is a good option.

Using ResourceBundle doesn't seems that right.

sawan


Sawan<br />SCJP,SCWCD,SCBCD<br /> <br />Every exit is an entry somewhere.
marcus don
Ranch Hand

Joined: Jan 26, 2005
Posts: 38
Hi all,
My biggest trouble is to understand if using .properties
is a consolidated "best practices" but against the specs.

I feel comfortable and more HF-EJB/specs compliant in using JNDI tree.

Kathy, Valentin, Bert you are my last hope to fully remove this misunderstanding.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Page 625 of HF-EJB shows that no use of java.io is allowed, but you can read properties by using the ResourceBundle class, which itself, does not use java.io package.
Wrong, java.util.ResourceBundle does use the java.io classes for reading in the properties file. BUT, as Roger rightly mentioned, this is an implementation issue. You won't be able to find any java.io occurrence in the public interface of the ResourceBundle class.

Roger has a second point when he says that environment entries are not designed to store the kind of properties we usually have to store in property files. Environment entries are a good choice when 1) there are not too many properties; and 2) when those properties will not have to be changed too often.

Depending on the context, I would probably go with a plain old property file that I would put on the classpath somewhere and load using the ResourceBundle class...

In my case I am using a server startup file( like T3StartupDef in case of weblogic ) that reads the properties file and then can use the values of the properties file later.
Since this solution is proprietary, it can be a good choice in only one case: when you know that your application will always stay on your Weblogic server and that you will never be asked to migrate to another application server at some point in the future. Otherwise you might run the risk to forget some property files on your weblogic server file system and make your migrated version crashed on the new server...

Other opinions:
Which do You Prefer: Properties or Environment Entries
How to read "Resource bundle" from EJB
[ May 03, 2005: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Senthil B Kumar
Ranch Hand

Joined: Feb 09, 2004
Posts: 140
we had a Property file which maps few data and thier multiple attributes. So we had prepared an XML file for it.

Say..
<Elements>
<Element-Param>
<Element-Data>X</Element-Data>
<Element-Quantity>10</Element-Quantity>
<Element-Quality-Level>A</Element-Quality-Level>
</Element-Param>
<Element-Param>
<Element-Data>Y</Element-Data>
<Element-Quantity>5</Element-Quantity>
<Element-Quality-Level>D</Element-Quality-Level>
</Element-Param>
</Elements>

Here the problem we faced was, we cannot read the xml file with resourcebundle.. we wrote the utility class, which does the parsing and reads the data. But reading the file we werent able to do using resourcebundle, so this file was not put into the EAR file and it was given seperately and its path was configured in Web.xml

is there any way, that i can put the xml file inside the ear file and read it in my Servlet ?


Work like you don't need the money. Love like you've never been hated. Dance like nobody's watching. Sing like nobody's listening. Live like it's Heaven on Earth.
Currently I Reside Here WEBlog
Keerthi P
Ranch Hand

Joined: Aug 19, 2003
Posts: 203
You can do it. If you are using JAXP based parsers, you can use Classloader.getResourceAsStream() and pass a reference to the XML file to the 'InputSource' of the 'DocumentHandler'. I was able to implement this for parsing a number of XML files that contained sophisticated rules. I packaged the XML files as part of the .ear file and transformed them to Java objects from web-layer components. It worked flawlessly in OS/390 (Mainframe) too.

Note: In the case mentioned above the parser was invoked by web-layer components (Custom JSP Tags and Application Controller Servlet).
[ May 03, 2005: Message edited by: Keerthi P ]
marcus don
Ranch Hand

Joined: Jan 26, 2005
Posts: 38
Thank you all!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: .properties vs JNDI