Chaining notifiers

Notifiers can be chained by a secondary store implementation. Notifier chaining is accomplished using these APIs:

Example 10.7, “Notifier chaining” shows the chaining of notifiers.

Example 10.7. Notifier chaining

//     $Revision: 1.1.2.2 $
package com.kabira.snippets.store;

import com.kabira.platform.Transaction;
import com.kabira.platform.annotation.Managed;
import com.kabira.store.Record;

/**
 * Chaining secondary store notifiers
 * <p>
 * <h2> Target Nodes</h2>
 * <ul>
 * <li> <b>domainnode</b> = A
 * </ul>
 */
public class Chaining
{
    @Managed
    private static class A
    {
    };

    private static class Notifier extends Record<A>
    {
        private static void installNotifer()
        {
            //
            //    Get previously installed notifier (if any)
            //    This must be done before we create a new notifier
            //
            Record<A> previous = (Record<A>) Record.getNotifier(A.class);
            new Notifier(previous);
        }

        private Notifier(Record<A> previous)
        {
            super(A.class);

            //
            //    Save off previous notifier (may be null)
            //
            m_chained = previous;
        }

        @Override
        public void created(A a)
        {
            System.out.println("INFO: created " + a);

            //
            //    Call chained notifer (if any)
            //
            if (m_chained != null)
            {
                m_chained.created(a);
            }
        }
        private final Record<A> m_chained;
    }

    /**
     * Main entry point
     *
     * @param args Not used
     */
    public static void main(final String[] args)
    {
        new Transaction("Initialization")
        {
            @Override
            protected void run()
            {
                Notifier.installNotifer();
                Notifier.installNotifer();
                Notifier.installNotifer();
            }
        }.execute();

        new Transaction("Create Objects")
        {

            @Override
            protected void run() throws Transaction.Rollback
            {
                new A();
            }
        }.execute();
    }
}

Example 10.7, “Notifier chaining” outputs these messages when executed:

INFO: created com.kabira.snippets.store.Chaining$A@fce9ff01
INFO: created com.kabira.snippets.store.Chaining$A@fce9ff01
INFO: created com.kabira.snippets.store.Chaining$A@fce9ff01