Matt RaibleMatt Raible is a Java Champion and Developer Advocate at Okta. developer.okta.com

The JHipster Mini-Book The JHipster Mini-Book is a guide to getting started with hip technologies today: Angular, Bootstrap, and Spring Boot. All of these frameworks are wrapped up in an easy-to-use project called JHipster.

This book shows you how to build an app with JHipster, and guides you through the plethora of tools, techniques and options you can use. Furthermore, it explains the UI and API building blocks so you understand the underpinnings of your great application.

For book updates, follow @jhipster-book on Twitter.

10+ YEARS


Over 10 years ago, I wrote my first blog post. Since then, I've authored books, had kids, traveled the world, found Trish and blogged about it all.

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