Package jestr.examples.example6

This example illustrates initializer classes.


Class Summary

Package jestr.examples.example6 Description

This example illustrates initializer classes. You can run it with "ant -f runexample6.xml" from the root of the distribution.

Suppose we need to perform some form of initialization not possible via the property file interface. This sometimes happens in the case of enumerated types where both the values and their corresponding mnemonics are stored in a database, rather than in static final fields. For this example let's assume that our acctState field from Example 3 is such an enumerated type, and that an "account_state" table exists to hold the mappings. Our Account class would be modified as follows:

    public class Account {
        private Balance currentBalance = new Balance(100.00, "USD", new Date());
        private Balance endingBalance = new Balance(62.44, "USD", new Date() );
        private Balance ledgerBalance = new Balance(89.75, "USD", new Date());
        private String id = "123";
        private int acctState = 2;

If we wish the acctState to log descriptively, we could introduce an initializer to look up the current mapping and generate a "jestr.stringifier.acctState.constants" property setting to establish this mapping. The initializer class is shown below:

    public class MyInitializer implements jestr.conf.props.Initializer {
        private List readFromDatabase() {
            List result = new ArrayList();
            result.add( new String[] { "OPEN", "0" } );
            result.add( new String[] { "CLOSED", "1" } );
            result.add( new String[] { "INACTIVE", "2" } );
            return result;
        public void preInitialize(Factory factory, Properties props, File file) throws Exception {
            List nameValuePairs = readFromDatabase();
            String value = "";
            for( int i = 0; i < nameValuePairs.size(); i++ ) {
                String[] nvp = (String[])nameValuePairs.get(i);
                value += nvp[0] + "=" + nvp[1] + ",";
            value = StringUtils.chompLast(value, ",");
            props.put( "jestr.stringifier.acctState.constants", value );

        public void postInitialize(Factory factory, Properties props, File file) throws Exception {

Note that this initializer doesn't really fetch values from the database, since this would make the example dependent on a particular database schema. However, it should be easy to see how the readFromDatabase() method could be modified to pull this information from an actual database. The file follows:




This is just like the from Example 3, except that neither "constants" nor "classDeclaringConstants" is set. Instead, we leave it to the initializer to set the "constants" property to values retrieved from the database. The output is the same as in Example 3:

        Balance currentBalance = Balance(100.0 USD as of Sat Mar 20 14:52:16 EST 2004)
        Balance endingBalance = Balance(62.44 USD as of Sat Mar 20 14:52:16 EST 2004)
        Balance ledgerBalance = Balance(89.75 USD as of Sat Mar 20 14:52:16 EST 2004)
        String id = "123"
        int acctState = INACTIVE(2)

The "acctState" attribute prints as its mnemonic, with the corresponding value in parentheses, just as in Example 3.

Copyright (c) 2001-2003 - Apache Software Foundation