8.8. Initializing the Application Runtime Environment

To get your application up and running, there are some things you need to do every time the WAF runtime starts up. Initializers exist to help you execute such code.

package com.example.binder;

import com.arsdigita.db.DbHelper;
import com.arsdigita.domain.DomainObject;
import com.arsdigita.domain.DomainObjectInstantiator;
import com.arsdigita.persistence.DataObject;
import com.arsdigita.persistence.pdl.ManifestSource;
import com.arsdigita.persistence.pdl.NameFilter;
import com.arsdigita.runtime.CompoundInitializer;
import com.arsdigita.runtime.DomainInitEvent;
import com.arsdigita.runtime.PDLInitializer;
import com.arsdigita.runtime.RuntimeConfig;
import org.apache.log4j.Logger;

/**
 * Initializes the binder application.
 *
 * @see com.example.binder
 * @author Justin Ross
 */
public class Initializer extends CompoundInitializer {
    private static final Logger s_log = Logger.getLogger(Initializer.class);

    public Initializer() {
        final String url = RuntimeConfig.getConfig().getJDBCURL();
        final int database = DbHelper.getDatabaseFromURL(url);

        add(new PDLInitializer
            (new ManifestSource
             ("binder.pdl.mf",
              new NameFilter(DbHelper.getDatabaseSuffix(database), "pdl"))));
    }

    public void init(final DomainInitEvent e) {
        super.init(e);

        e.getFactory().registerInstantiator
            (Binder.BASE_DATA_OBJECT_TYPE,
             new DomainObjectInstantiator() {
                 protected final DomainObject doNewInstance
                         (final DataObject data) {
                     return new Binder(data);
                 }
             });
    }
}

Example 8-7. binder/src/com/example/binder/Initializer

In the section above, you defined object-relational mapping metadata. The PDLInitializer serves to load that metadata into the runtime. This allows the persistence layer to interpret requests for the data behind your persistent object types.

The Binder and Note persistent object types have corresponding domain classes written in Java. The domain APIs will return the Java objects, but to do so we must map the object type to something that can instantiate the Java class.

    public void init(final DomainInitEvent e) {
        super.init(e);

        e.getFactory().registerInstantiator
            (Binder.BASE_DATA_OBJECT_TYPE,
             new DomainObjectInstantiator() {
                 protected final DomainObject doNewInstance
                         (final DataObject data) {
                     return new Binder(data);
                 }
             });
    }

Example 8-8. Coupling data objects to domain object instantiators

When your package is loaded, the package tool will store the initializer class name you provide in the database. When the CCM runtime starts up, it will read out all the registered initializers and run them in order to prepare the environment.