001// 
002// Name
003//  $RCSfile: Notifier.java,v $
004// 
005// COPYRIGHT
006//  Copyright 2014 Cloud Software Group, Inc. ALL RIGHTS RESERVED.
007//  Cloud Software Group, Inc. Confidential Information
008//
009// History
010//  $Revision: 1.1.2.3 $ $Date: 2014/04/09 02:32:27 $
011
012package com.kabira.platform.flusher;
013
014import com.kabira.platform.annotation.Managed;
015import com.kabira.platform.ManagedClassError;
016import com.kabira.platform.ResourceUnavailableException;
017
018/**
019  * ManagedObject flush notifier.
020  */
021@Managed
022public abstract class Notifier<T> extends NotifierBase
023{
024    final String mTargetClassName;
025
026    // Force use of the parameterized constructor
027    private Notifier()
028    {
029        mTargetClassName = null;
030    }
031
032    /**
033     * Constructor
034     *
035     * @param flushTypeClass The managed class to receive
036     *                       flush notifications for.
037     *
038     * @exception ManagedClassError <code>flushTypeClass</code> is
039     * not a Managed object.
040     *
041     * @see com.kabira.platform.CacheManager.CacheFlusher#setNotifier
042     */
043    protected Notifier(Class<T> flushTypeClass)
044        throws ManagedClassError
045    {
046        mTargetClassName = flushTypeClass.getName();
047
048        try
049        {
050            FlushManagerUtil.audit(mTargetClassName);
051        }
052        catch (ResourceUnavailableException ex)
053        {
054            throw new ManagedClassError(ex.getMessage());
055        }
056    }
057
058    /**
059     * When registered (via CacheFlusher.setNotifier())
060     * called by the system for each time an instance of type T
061     * is being flushed.
062     *
063     * <p>
064     * Returning false causes the flush of <code>flushTarget</code>
065     * to be skipped, and <code>flushTarget</code> to be moved to the
066     * end of the LRU list for objects of that type.
067     *
068     * <p>
069     * Called from within a transaction.
070     *
071     * @param flushTarget The object being flushed.
072     *
073     * @return true for the flush to continue.
074     * <p>
075     * false to retain the object in memory.
076     *
077     * @see com.kabira.platform.CacheManager.CacheFlusher#flush
078     * @see com.kabira.platform.CacheManager.CacheFlusher#setNotifier
079     */
080    public abstract boolean isFlushable(T flushTarget);
081
082    //
083    // package private implementation of modeled Notifier.canFlush
084    // where we map the Object back to the user's type T.
085    //
086    @SuppressWarnings("unchecked")
087    boolean canFlush(Object object)
088    {
089        return isFlushable((T)object);
090    }
091}