ResourceBundle vs System.getProperty

I

iksrazal

Our team is trying to decide how to load an IP address and port used
by a single class that creates a socket. This class is invoked in a
servlet container.

My preference is for ResourceBundle. Another developer feels strongly
about System.getProperties.

Any clear rules on which method is best for particular situations?

iksrazal
 
L

Lothar Kimmeringer

On 5 Aug 2003 06:58:55 -0700, iksrazal wrote:

[ResourceBundle vs. Properties]
Any clear rules on which method is best for particular situations?

The main purpose of a resource-bundle is localizing GUIs or
other things. The ResourceBundle-objects is trying to get the
appropriate file for a given Locale. Retrieving a value with
a ResourceBundle means that there is a value being expected
to be defined in a file. An exception will be thrown otherwise.

The main-purpose of a Properties-object is to keep key-value-pairs
with the ability to specify default-values when trying to retrieve
one. So this is mainly used for configuration-stuff. With that
you can write something for initializing a server:

ServerSocket ss = new ServerSocket(
Integer.parseInt(myProps.getProperty("server.port", "1234")));

So if the value is not set in the properties-file the default
1234 will be used. No exception will be thrown if there is
no value being set in a file, null will be returned instead.

The only advantage that might let somebody use ResourceBundle
instead of Properties is that ResourceBundle is getting throught
the CLASSPATH to get a bundle, but that can also be done with
a properties-file when using the getResourceAsStream-functionality
of a ClassLoader.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
J

Joseph Millar

Our team is trying to decide how to load an IP address and port used
by a single class that creates a socket. This class is invoked in a
servlet container.

My preference is for ResourceBundle. Another developer feels strongly
about System.getProperties.

Any clear rules on which method is best for particular situations?

Definitely don't put anything in the System.getProperties()
object unless there is no other way. Whether you use a
ResourceBundle or a Properties object, or the new Preferences
is up to you and what best fits in your app. Also be aware
that in a security contrained environment, you might not be
able to get/set into the system prop object in order to
protect it from some malicious/errant piece of code.

The only time I would consider putting something in the
system properties is if the value were global in nature, and
would be useful to a wide range of servlets.

Just my opinion.

--Joe
 
R

Roedy Green

My preference is for ResourceBundle. Another developer feels strongly
about System.getProperties.

ResoureceBundle implies it will be locale specific.
System.getProperties implies it just a configuration parm with no
locale-specific default.

This is the sort of thing you often want to change at the last minute.
It might be better to put it in its own properties file, where it will
be easy to find and modify. You can change the ip without shutting
down the entire servlet womb. see
http://mindprod.com/jgloss/properties.html

In the CyberView Plus app I did, there are a set of preconfigured IPS
the user can select at logon, with one being the default. I also let
him to a write in URL/IP if none of them are suitable. That is client
side. For server side, you don't want any user interaction.
 
J

Joseph Millar

Can anyone elaborate a bit on "you might not be
able to get/set into the system prop object" ?

This is going to depend very heavily on individual servlet
environments. For example, whoever is running your servlet
may have a SecurityManager installed. It's common practice
in such situations to disallow the setting of system
properties. These key value pairs are global in scope and
if someone changes or addes a value for their own purpose,
it may have consequences on some one else's code.

Individual properties for a servlet or group of servlets is
best handled at their own level, not at the level of the
entire JVM. So unless you have something truelly global
in scope, don't even think about putting it here.

There is a tendancy among beginning Java programmers to think
they have full and exclusive use of the JVM environment.
In a standalone Java application, this is probably true,
but in server environments like servlets and EJB, etc. this
is not true. So I think it's a good idea to assume you do
not have this kind of control and design accordingly. It
will usually yield a cleaner and more portable design/system.

--Joe
 
L

Lothar Kimmeringer

ResourceBundle can be used for localization, but doesn't have to be:

That's the reason why I said "the main purpose".
Two things. First, ip's and ports are the types of things that always
changing

On the server-side in general no.
- hard-coded defaults don't work for very long. Second, using
getResourceAsStream() can do the same thing as ResourceBundle, but
only if you have access to ServletContext - not all sub-classes will.
Does that make a case for ResourceBundle?

I'm not sure what you mean with the ServletContext.
this.getClass().getClassLoader().getResourceAsStream(...)
should work from every place (not in a static context of
course but there you can write new Object().getClass ...)


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top