Lance jumps into the application configuration debate:
To comment on Jeff's
continuing configuration quest, I would add my vote to trying out
Betwixt/Digester as the engine. I used this to build the RollerConfig
object (for Roller, duh) and it was stupid-simple. I spent a couple
hours futzing around trying to get it "perfect" (Betwixt has some
inclusion/exclusion rules that I was unable to get to do what I want)
but it was unnecessary work. So Jeff, if you want an example, check
out the Roller cvs for RollerConfig. The roller-config.xml is here if you want an example of that as well.
I thought Roller used the Castor-way I suggested, but nope, it uses Betwixt. Betwixt is a commons project, so it's got to be good, eh? I especially like the ease of reading, writing the XML files. From RollerConfig.java:
/**
* Read the RollerConfig from a file,
* as specified by an InputStream.
*/
public static RollerConfig readConfig(InputStream in)
{
try
{
BeanReader reader = new BeanReader();
reader.registerBeanClass( RollerConfig.class );
Object obj = reader.parse( in );
if (obj instanceof RollerConfig)
{
RollerConfig alpha = (RollerConfig) obj;
//System.out.println("RollerConfig readConfig:"alpha);
return alpha;
}
}
catch (Exception e)
{
System.out.println("Exception reading RollerConfig:" + e.getMessage());
}
return new RollerConfig();
}
/**
* Write RollerConfig to file,
* as specified by a String path.
*/
public void writeConfig(String path) throws Exception
{
FileOutputStream out = null;
try
{
out = new FileOutputStream(path);
this.writeConfig( out );
}
finally
{
try {
if (out != null) out.close();
} catch (java.io.IOException ioe) {
System.err.println("RollerConfig.writeConfig() unable to close OutputStream");
}
}
}
/**
* Write RollerConfig to file,
* as specified by an OutputStream.
*/
public void writeConfig(OutputStream out) throws Exception
{
BeanWriter writer = new BeanWriter(out);
writer.enablePrettyPrint();
writer.setIndent(" ");
writer.setWriteIDs(false);
writer.write(this);
}
One thing I noticed in this class is the long and painful-looking toString()
method. How about a little reflection to improve that sucker:
public String toString() {
StringBuffer results = new StringBuffer();
Class clazz = getClass();
results.append(getClass().getName() + "\n");
Field[] fields = clazz.getDeclaredFields();
try {
AccessibleObject.setAccessible(fields, true);
for (int i = 0; i < fields.length; i++) {
results.append(
"\t"
+ fields[i].getName()
+ "="
+ fields[i].get(this)
+ "\n");
}
} catch (Exception e) {
// ignored!
}
return results.toString();
}