Converters

It can't have escaped your attention that JNDI is an object technology while .xml, .properties and .ini files are very noticeably text based. Simple-JNDI solves this via the org.osjava.sj.loader.convert.Converter interface.

    /**
     * Turn a String-based tree-structure into an Object. Additionally 
     * the type of object desired is known. This is usually a Java 
     * class, but this is not mandatory. 
     *
     * The properties structure is located at the point of the 
     * lookup key, so if the code asked for a com.example.Foo object, 
     * the properties structure would be everything below Foo.
     *
     * To get at the value of com.example.Foo itself, request the 
     * empty string, "".
     *
     * TODO: No way for a converter to know the delimiter-type. 
     *
     * @param Properties data structure
     * @param String type of object desired
     */
    Object convert(Properties properties, String type);
    

Converters are nice and easy to implement. It's recommended that you look at the code to some of the existing converter implementations, say the DataSource and Date converters.

Before implementing your own Converter, make sure there is not an existing implementation which you could use. The ConstructorConverter is a very generic converter that should be able to handle many of your basic conversions.

Existing Converters

The following is a brief list of the existing converter implementations.

  • DataSourceConverter - Default for javax.sql.DataSource. Four mandatory subparameters (url, driver, user, password) and one optional subparameter (pool).
  • DateConverter - Default for java.util.Date. One mandatory subparameter (format), and an empty String value is requried.
  • ConstructorConverter - Default for java.lang.Boolean. The empty String value is required, this converter will create anything with a single String argument to its constructor.
  • MapConverter - Loads all values in the properties object into a Map. (TODO: Why not just pass the Properties object straight back, it's a Map?).
  • NullConverter - Returns the empty String value.
  • BeanConverter - Creates an empty instance of the type, then calls setXxx(String) methods for each specified property.