the best way to store program setup

C

column.column

Hello,

What is the best way to store program configuration settings? Each
class has its own parameters that might be useful just for current
class. There are some parameters that might be useful in whole
program. Probably main class could have confifiguration for whole
program, but how I could access them from class that is inside main
class?

Thank you
 
K

Knute Johnson

Hello,

What is the best way to store program configuration settings? Each
class has its own parameters that might be useful just for current
class. There are some parameters that might be useful in whole
program. Probably main class could have confifiguration for whole
program, but how I could access them from class that is inside main
class?

Thank you

I like to use java.util.Properties and save them to a file.

As to passing data from one class to another there are numerous
possibilities and numerous problems. The best situation is that each
class has its own data and doesn't need any from any other class. That
can be difficult to achieve without perfect planning. Data can be
passed in the constructor but passing references to data that can change
can cause big problems. And then you've got threading issues. Probably
the simplest thing you can do is to try to keep all the code that needs
access to the same data in the same class. It can make for bigger
classes but everything is a trade off.
 
D

Daniel Pitts

Hello,

What is the best way to store program configuration settings? Each
class has its own parameters that might be useful just for current
class. There are some parameters that might be useful in whole
program. Probably main class could have confifiguration for whole
program, but how I could access them from class that is inside main
class?

Thank you

Alternatively, look into the Spring framework and consider using
Dependency Injection.
 
L

lexaux

Hello!

I think, this can be achieved with serializati on - all you need, is
to identify the application state data to persist, and make it as a
separate class(set of classes). After that you can use plenty of a
tools/libs/facilities to serialize data to a stream, and read it from
stream.

This enables remoted configuration - when the need appears, you can
change file stream to socket stream, and read some settings from
server with minimal change of code.

We considered using JAXB xml serialization to a gzipped stream.
Settings stored so are completely transparent and editable with any
XML editor.

Hope this would help!

Alex.
 
M

Martin Gregorie

Hello,

What is the best way to store program configuration settings? Each
class has its own parameters that might be useful just for current
class. There are some parameters that might be useful in whole
program. Probably main class could have confifiguration for whole
program, but how I could access them from class that is inside main
class?
In my current project I've written a class to hold it. This parses the
configuration file, stores the contents, and has a set of getters to
give the application access to the values. Its invoked from main(). From
you can pass single values to subordinate classes via the constructor
or, if the class needs several values, pass a reference to the
configuration data class.

I'm not using XML for the config file: instead, for readability I'm
using a text file that can contain a mixture of comments and config.
data as "name = value" pairs. Reasons?
- The initial target OS is Linux. This format is familiar to sysadmins
- its easy to edit. I don't need to write a program to maintain it.
- its easy to parse
- different users have different config files so it can't be put in a
JAR file.
- the class uses a search path: .:/usr.local/etc:/etc
to find the config file. This ensures that individual user's
config files can override a system-wide default configuration

This works well for my requirements. It suits *NIX type operating
systems. The search path can be customized, so it will also work for
other operating systems.
 
L

Lew

lexaux said:

Please do not top-post. Use trim-and-inline posting.
I think, this [to store program configuration settings] can be achieved with serializati on -
all you need, is to identify the application state data to persist, and make it as a
separate class(set of classes). After that you can use plenty of a
tools/libs/facilities to serialize data to a stream, and read it from
stream.

A few of the most common techniques: JAXB (as you mentioned), XML-RPC (as used
in SOAP messages), java.io.Serializable, Serializable over RMI, the Java API
Properties and ResourceBundles mechanisms, other text files like CSV, and
roll-yer-own. Probably the most successful approaches use either properties
files or deployment descriptors, as with Apache Tomcat or Apache Web Server.

I'd avoid java.io.Serializable for configuration or simple data transfer. Its
purpose is to serialize object graphs. Many data transfer matters, and likely
all configuration matters, are much simpler than that.

XML descriptor files seem to be the industry's consensus in modern times on
how to configure things. Consider Tomcat's server.xml and web.xml
descriptors, for example.

XML is nice because, as you stated, it is "completely transparent and
editable", indeed, generally human-readable. XML configuration files make for
explicable documentation of deployment scenarios. That you can drop such a
document into a deployment and have its system behave accordingly is an
amazing synergy of operational and documentary purpose.

I wouldn't limit oneself to "one descriptor file - one class". Split up
descriptors according to the needs of the operations personnel, not the
developers. Ops won't care about what class is doing what, unless your code
breaks the system. They will care about, "Where are the database parameters?"
"Where are the memory parameters?" "Where are the driver parameters?"

We need to show operations more respect. Design for the folks in the trenches.
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top