head	1.30;
access;
symbols;
locks; strict;
comment	@# @;


1.30
date	2001.05.21.21.06.01;	author leonardr;	state dead;
branches;
next	1.29;

1.29
date	2001.05.02.19.23.06;	author dlr;	state Exp;
branches;
next	1.28;

1.28
date	2001.04.20.15.30.21;	author dlr;	state Exp;
branches;
next	1.27;

1.27
date	2001.04.18.22.39.20;	author dlr;	state Exp;
branches;
next	1.26;

1.26
date	2001.04.12.20.52.12;	author dlr;	state Exp;
branches;
next	1.25;

1.25
date	2001.04.12.20.46.59;	author leonardr;	state Exp;
branches;
next	1.24;

1.24
date	2001.04.12.19.05.36;	author dlr;	state Exp;
branches;
next	1.23;

1.23
date	2001.04.12.18.49.56;	author dlr;	state Exp;
branches;
next	1.22;

1.22
date	2001.04.12.04.41.06;	author leonardr;	state Exp;
branches;
next	1.21;

1.21
date	2001.04.12.01.45.05;	author dlr;	state Exp;
branches;
next	1.20;

1.20
date	2001.04.12.01.17.44;	author dlr;	state Exp;
branches;
next	1.19;

1.19
date	2001.04.12.01.00.16;	author dlr;	state Exp;
branches;
next	1.18;

1.18
date	2001.04.12.00.56.31;	author leonardr;	state Exp;
branches;
next	1.17;

1.17
date	2001.04.12.00.43.47;	author dlr;	state Exp;
branches;
next	1.16;

1.16
date	2001.04.12.00.26.44;	author dlr;	state Exp;
branches;
next	1.15;

1.15
date	2001.04.11.20.15.18;	author dlr;	state Exp;
branches;
next	1.14;

1.14
date	2001.04.11.09.34.36;	author dlr;	state Exp;
branches;
next	1.13;

1.13
date	2001.04.11.09.33.56;	author dlr;	state Exp;
branches;
next	1.12;

1.12
date	2001.04.11.08.55.05;	author dlr;	state Exp;
branches;
next	1.11;

1.11
date	2001.04.07.01.34.10;	author dlr;	state Exp;
branches;
next	1.10;

1.10
date	2001.04.07.01.30.39;	author dlr;	state Exp;
branches;
next	1.9;

1.9
date	2001.04.07.01.28.31;	author dlr;	state Exp;
branches;
next	1.8;

1.8
date	2001.04.07.01.18.49;	author dlr;	state Exp;
branches;
next	1.7;

1.7
date	2001.04.07.01.14.53;	author dlr;	state Exp;
branches;
next	1.6;

1.6
date	2001.04.07.01.13.52;	author dlr;	state Exp;
branches;
next	1.5;

1.5
date	2001.04.06.21.18.11;	author dlr;	state Exp;
branches;
next	1.4;

1.4
date	2001.04.06.20.47.41;	author leonardr;	state Exp;
branches;
next	1.3;

1.3
date	2001.04.06.20.41.03;	author dlr;	state Exp;
branches;
next	1.2;

1.2
date	2001.04.06.20.39.11;	author dlr;	state Exp;
branches;
next	1.1;

1.1
date	2001.04.06.18.12.18;	author dlr;	state Exp;
branches;
next	;


desc
@@


1.30
log
@Moved clustering features out of RMI and into the database.
@
text
@/* ================================================================
 * Copyright (c) 2001 CollabNet.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by CollabNet <http://www.collab.net/>."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 *
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 *
 * 5. Products derived from this software may not use the "Tigris" or
 * "Helm" names nor may "Tigris" or "Helm" appear in their names without
 * prior written permission of CollabNet.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */

package org.tigris.helm.tool.cache;

import java.util.*;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javax.servlet.ServletConfig;

import org.apache.turbine.util.Log;
import org.apache.turbine.services.InitializationException;
import org.apache.turbine.services.TurbineServices;
import org.apache.turbine.services.BaseUnicastRemoteService;

import org.tigris.helm.tool.cluster.ClusterObject;
import org.tigris.helm.tool.cluster.ClusterMediator;
import org.tigris.helm.tool.cluster.ClusteredColleague;
import org.tigris.helm.tool.cluster.MediatedEvent;
import org.tigris.helm.tool.cluster.PollEvent;

/**
 * The registry of all object caches in a JVM.  Communicates cache
 * invalidation events to the cluster mediator, which in turn relays
 * them to all JVMs in its cluster.
 *
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 * @@author <a href="mailto:leonardr@@collab.net">Leonard Richardson</a>
 */
public class CacheRegistry extends BaseUnicastRemoteService
    implements ClusteredColleague
{
    /**
     * The name of this service.  Used to retrive it from the broker.
     */
    public static final String SERVICE_NAME = "CacheRegistry";

    /**
     * Flag indicating whether in debug mode.
     */
    private static final boolean DEBUG = true;

    private Map cacheRegistry;

    /**
     * A wrapper method for getting the single instance of this
     * service from the Turbine services list.
     *
     * @@return Turbine's single instance of this service.
     */
    public static final CacheRegistry getService()
    {
        return (CacheRegistry)TurbineServices
            .getInstance().getService(CacheRegistry.SERVICE_NAME);
    }

    public CacheRegistry()
        throws RemoteException
    {
        cacheRegistry = new Hashtable();
    }

    /**
     * Performs early initialization of this service.
     */
    public void init(ServletConfig config)
        throws InitializationException
    {
        try
        {
            register();
            setInit(true);
        }
        catch (RemoteException e)
        {
            Log.error(SERVICE_NAME + " registration with cluster mediator " +
                      "failed", e);
        }
    }

    public void register()
        throws RemoteException
    {
        getMediator().register(this);
    }

    public void unregister()
        throws RemoteException
    {
        getMediator().unregister(this);
    }

    /**
     * Registers a cache with this registry.
     *
     * @@param key   The key by which to access the provided cache.
     * @@param cache The cache to register.
     */
    public void register(Object key, RegisterableCache cache)
        throws RemoteException
    {
        cacheRegistry.put(key, cache);
    }

    public void handleEvent(MediatedEvent event)
        throws RemoteException
    {
        if (event instanceof PollEvent)
        {
            // Respond to the poll.
            sendEvent(new PollEvent(this, event));
        }
        else if (event instanceof CacheInvalidationEvent)
        {
            invalidate((CacheInvalidationEvent) event);
        }
        else
        {
            Log.warn("Unhandled cluster event sent to CacheRegistryManager: " +
                     event.toString());
        }
    }

    public void sendEvent(MediatedEvent event)
        throws RemoteException
    {
        getMediator().handleEvent(event);
    }

    private final void invalidate(CacheInvalidationEvent event)
    {
        Iterator keys = cacheRegistry.keySet().iterator();
        while (keys.hasNext())
        {
            Object key = keys.next();
            RegisterableCache cache = 
                (RegisterableCache) cacheRegistry.get(key);
            if (DEBUG)
            {
                String msg = "CacheRegistry invalidating cache " + key;
                Object src = event.getSource();
                if (src != null) msg += " for " + src;
                Log.debug(msg);
            }
            cache.invalidate(event.getSource());
        }
    }

    /**
     * Macro to retrieve the registry manager for this cluster.
     */
    private final CacheRegistryManagerSpec getMediator()
        throws RemoteException
    {
        return (CacheRegistryManagerSpec) ClusterObject.get
            (CacheRegistryManager.CLUSTER_KEY);
    }
}
@


1.29
log
@Documented getService() wrapper method.
@
text
@@


1.28
log
@Added semi-selective cache invalidation which invalidates particular caches on a write instead of all the caches.
@
text
@d89 6
@


1.27
log
@SequencedHashtable, BufferCache, and BaseUnicastRemoteService have been moved into Turbine.  Changed references accordingly.
@
text
@d183 1
a183 2
            // NOTE: Passing null to invalidate the entire cache.
            cache.invalidate(null);
@


1.26
log
@Better debugging output.
@
text
@d58 1
a64 1
import org.tigris.helm.tool.rmi.BaseUnicastRemoteService;
@


1.25
log
@Cleaned up debug messages.
@
text
@d82 5
d176 7
a182 2
            Log.debug("CacheRegistry invalidating cache " 
                      + key.toString() + " for " + event.getSource());
@


1.24
log
@Pushed base Turbine Service implementation into BaseUnicastRemoteService.
@
text
@a140 1
        Log.debug("Handling event.");
a147 1
            Log.debug("It's a cache invalidation event.");
d168 5
a172 4
            RegisterableCache cache =
                (RegisterableCache) cacheRegistry.get(keys.next());
            Log.debug("CacheRegistry invalidating cache " + cache + " for " +
                      event.getSource());
@


1.23
log
@Added a bit more header JavaDoc.
@
text
@d56 2
a57 1
import org.apache.turbine.services.*;
d64 1
a64 2

import org.apache.turbine.util.Log;
d74 2
a75 2
public class CacheRegistry extends UnicastRemoteObject
    implements ClusteredColleague, Service
a83 7
    // Turbine Service interface implementation baggage.
    private boolean isInitialized;
    private InitableBroker initableBroker;
    private String name;
    private Properties properties;
    private ServiceBroker serviceBroker;

d96 3
a110 1
            Log.debug("Stack trace:", e);
a186 84
    }

    // Turbine Service interface

    public void setInitableBroker( InitableBroker broker )
    {
        this.initableBroker = broker;
    }

    public InitableBroker getInitableBroker()
    {
        return initableBroker;
    }

    public void init(Object data)
        throws InitializationException
    {
        init((ServletConfig) data);
    }

    public void init() throws InitializationException
    {
        // No late initialization to do.
    }

    protected void setInit(boolean value)
    {
        this.isInitialized = value;
    }
    
    public boolean getInit()
    {
        return isInitialized;
    }

    /**
     * Shuts down this service.
     */
    public final void shutdown()
    {
        setInit(false);
        try
        {
            unregister();
        }
        catch (Exception e)
        {
            Log.error(SERVICE_NAME + ": shutdown failed", e);
        }

    }

    public Properties getProperties()
    {
        if (name == null)
        {
            return null;
        }

        if (properties == null)
        {
            properties = getServiceBroker().getProperties(name);
        }
        return properties;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String setName()
    {
        return name;
    }

    public ServiceBroker getServiceBroker()
    {
        return serviceBroker;
    }

    public void setServiceBroker(ServiceBroker broker)
    {
        this.serviceBroker = broker;
@


1.22
log
@Added debugging code and moved RARPeer and ProjectPeer to the new caching system.
@
text
@d67 3
a69 1
 * The registry of all object caches in a JVM.
@


1.21
log
@Properly iterator the caches during invalidation.
@
text
@d144 1
d152 1
@


1.20
log
@* Added SERVICE_NAME constant.

* Switched from incorrect MEDIATOR_URL constant to correct
CacheRegistryManager.CLUSTER_KEY URL.

* Added static getService() singleton accessor method.
@
text
@d168 2
a169 2
        Iterator iter = cacheRegistry.keySet().iterator();
        while (iter.hasNext())
d171 2
a172 1
            RegisterableCache cache = (RegisterableCache) iter.next();
@


1.19
log
@Implemented init(ServletConfig).
@
text
@d76 1
a76 1
     * RMI URL fragment to the mediator.
d78 1
a78 2
    public static final String MEDIATOR_URL =
        "org.tigris.helm.tool.cache.CacheRegistryManager";
d89 7
a95 1
    private CacheRegistry()
a103 1
        // TODO: Add proper error handling.
d185 2
a186 1
        return (CacheRegistryManagerSpec) ClusterObject.get(MEDIATOR_URL);
@


1.18
log
@Make CacheRegistry a Turbine service.
@
text
@d83 1
a83 1

d100 11
a110 2
        //register();
        //setInit(true);
@


1.17
log
@Checking in start of Turbine Service impl for Leonard to finish.  We
can't extend TurbineBaseService because we must extend
UnicastRemoteObject so we can have our Remote interface shipped over
the wire.
@
text
@d73 1
a73 1
    implements ClusteredColleague
d83 7
d173 84
@


1.16
log
@Added some debugging trace.
@
text
@a48 1
import java.io.Serializable;
d52 2
d56 1
d72 2
a73 1
public class CacheRegistry implements ClusteredColleague, Serializable
a80 2
    private static CacheRegistry instance = new CacheRegistry();

d83 2
a84 1
    public static CacheRegistry getInstance()
d86 1
a86 1
        return instance;
d89 2
a90 1
    private CacheRegistry()
d92 3
a94 1
        cacheRegistry = new Hashtable();
@


1.15
log
@Make use of the real Remote interface, not the rmic generated stub
class.
@
text
@d147 2
@


1.14
log
@Cast appropriately for the _Stub instance.
@
text
@d155 1
a155 1
    private final CacheRegistryManager_Stub getMediator()
d158 1
a158 1
        return (CacheRegistryManager_Stub) ClusterObject.get(MEDIATOR_URL);
@


1.13
log
@The mediator's _Stub class will be returned, rather than an instance
of the medaitor itself.
@
text
@d158 1
a158 1
        return (CacheRegistryManager) ClusterObject.get(MEDIATOR_URL);
@


1.12
log
@Implement Serializable so we can be sent over the wire via RMI.
@
text
@d155 1
a155 1
    private final CacheRegistryManager getMediator()
@


1.11
log
@Changed first param of cache register method to be the more generic
Object type instead of Class, as we will be moving away from Class as
soon as Peers can easily be into singletons.
@
text
@d49 1
d70 1
a70 1
public class CacheRegistry implements ClusteredColleague
@


1.10
log
@* JavaDoc'd getMediator() macro.

* Made invalidate() final.
@
text
@d109 1
a109 1
    public void register(Class key, RegisterableCache cache)
@


1.9
log
@Completed implementation of invalidate(CacheInvalidationEvent).
@
text
@d140 1
a140 1
    private void invalidate(CacheInvalidationEvent event)
d151 3
@


1.8
log
@Made CacheRegistry itself a singleton so that there is only one
registry of Helm caches per JVM.
@
text
@a141 1
        //ClusterObject.get("org.tigris.helm.cache");
d145 3
a147 1
            // TODO: Clear cache.
@


1.7
log
@Added JavaDoc for register(Class key, RegisterableCache cache).
@
text
@d77 2
d81 6
a86 1
    public CacheRegistry()
@


1.6
log
@Added cache registration method, keyed by the registering class.
@
text
@d96 6
@


1.5
log
@Added and made use of getMeditator() and supporting cast.
@
text
@d96 6
@


1.4
log
@Implemented unregister.
@
text
@d55 1
d71 6
a78 2
    private ClusterMediator cacheRegistryManager;

a81 2
        // TODO: Get a handle to the mediator.
        cacheRegistryManager = null;
d87 1
a87 1
        cacheRegistryManager.register(this);
d93 1
a93 1
        cacheRegistryManager.unregister(this);
d118 1
a118 1
        cacheRegistryManager.handleEvent(event);
d129 6
@


1.3
log
@Import Turbine logging.
@
text
@d60 2
d85 6
@


1.2
log
@Import PollEvent.
@
text
@d53 2
@


1.1
log
@Cross JVM communication services.
@
text
@d56 1
d86 6
a91 1
        if (event instanceof CacheInvalidationEvent)
d97 2
a98 1
            // TODO: ?
d110 1
@

