the best way to store program setup  
Author Message
column.column





PostPosted: 2008-1-24 20:51:00 Top

java-programmer, the best way to store program setup 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
 
Daniel Pitts





PostPosted: 2008-1-25 3:40:00 Top

java-programmer >> the best way to store program setup On Jan 24, 4:50 am, email***@***.com wrote:
> 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.
 
lexaux





PostPosted: 2008-1-25 5:03:00 Top

java-programmer >> the best way to store program setup 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.

On Jan 24, 9:40爌m, Daniel Pitts <email***@***.com> wrote:
> On Jan 24, 4:50 am, email***@***.com wrote:
>
> > 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.

 
 
Martin Gregorie





PostPosted: 2008-1-25 8:04:00 Top

java-programmer >> the best way to store program setup email***@***.com wrote:
> 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.


--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
 
 
Lew





PostPosted: 2008-1-25 8:32:00 Top

java-programmer >> the best way to store program setup lexaux wrote:
> Hello!

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.

--
Lew