20021209 Monday December 09, 2002

Application Configuration - Roller Style 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();
}

Posted in Roller at Dec 09 2002, 08:25:40 AM MST 2 Comments

Comments:

Hey, you're toString() reflection thing is nice! Check it in! Sorry, I've still got some bad habits I picked up from old auto-code-generation helpers.

Posted by Lance on December 09, 2002 at 03:01 PM MST #

Oh, and I'd been wanting something to tryout Betwixt on for some time. When I thought of RollerConfig it was the obvious solution (I looked at using Castor first but it was much more complicated for such a "one-off").

Posted by Lance on December 09, 2002 at 03:03 PM MST #

Post a Comment:
  • HTML Syntax: Allowed
Click me to subscribe
Matt Raible is a Web Architecture Consultant specializing in open source frameworks.
« May 2012
SunMonTueWedThuFriSat
  
1
2
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today

Recent Entries

Tag Cloud