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


1.11
date	2001.08.29.19.42.49;	author leonardr;	state Exp;
branches;
next	1.10;

1.10
date	2001.08.28.23.02.04;	author leonardr;	state Exp;
branches;
next	1.9;

1.9
date	2001.08.22.16.01.00;	author leonardr;	state Exp;
branches;
next	1.8;

1.8
date	2001.08.21.17.26.45;	author leonardr;	state Exp;
branches;
next	1.7;

1.7
date	2001.08.21.01.14.22;	author leonardr;	state Exp;
branches;
next	1.6;

1.6
date	2001.08.20.23.36.54;	author leonardr;	state Exp;
branches;
next	1.5;

1.5
date	2001.08.20.19.09.11;	author leonardr;	state Exp;
branches;
next	1.4;

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

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

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

1.1
date	2001.08.17.18.11.37;	author leonardr;	state Exp;
branches;
next	;


desc
@@


1.11
log
@Moved integration-oriented code into new component package.
@
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" name
 * nor may "Tigris" 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.event;

import java.util.*;
import java.lang.*;

import org.apache.commons.util.exception.NestableException;
import org.apache.commons.util.GenerateUniqueId;

import org.apache.torque.om.BaseObject;
import org.apache.torque.om.ObjectKey;
import org.apache.turbine.util.Log;

import org.tigris.helm.component.ToolEventPropagationService;
import org.tigris.helm.component.PropagationFailed;
import org.tigris.helm.om.*;

/**
 * Class representing a tool event.
 *
 * @@author <a href="mailto:leonardr@@collab.net">Leonard Richardson</a>
 * @@version $Id: ToolEvent.java,v 1.10 2001/08/28 23:02:04 leonardr Exp $
 */
public class ToolEvent extends ClusterEvent
{
    public static final String ALL_TOOLS = "*";

    private String id;
    private Date timestamp;
    private String name;
    private String[] peers;
    private String[] keys;
    private String tool;

    /**
     * @@see org.tigris.helm.event.EventType#getType()
     */
    public short getType()
    {
        return TOOL_EVENT;
    }

    /**
     * Creates a new ToolEvent for broadcast to all tools.
     *
     * @@see #ToolEvent(String name, Object[] affected, String destination)
     */
    public ToolEvent(String name, Object[] affected)
    {
        this(name, affected, ALL_TOOLS);
    }

    /**
     * Creates a new ToolEvent destined for a specific tool.
     *
     * @@param name The name for this event.
     * @@param affected The objects involved in this event.
     * @@param tool The intended tool for this event.
     */
    public ToolEvent(String name, Object[] affected, String tool)
    {
        this.id = new GenerateUniqueId().getIdentifier();
        this.timestamp = new Date();
        this.tool = tool;
        this.name = name;
        peers = new String[affected.length];
        keys = new String[affected.length];
        for (int i = 0; i < affected.length; i++)
        {
            Object o = affected[i];
            String className = null;
            if (o instanceof BaseObject)
            {
                Class peerClass = null;
                Class superclass = o.getClass();

                //Find the superclass of this object which has the
                //peer.
                while (peerClass == null && superclass != null)
                {
                    className = superclass.getName() + "Peer";
                    try
                    {
                        peerClass = Class.forName(className);
                    }
                    catch (ClassNotFoundException e)
                    {
                        superclass = superclass.getSuperclass();
                    }
                }

                if (peerClass == null)
                {
                    Log.error("Could not find peer corresponding to " 
                              + o.getClass().getName());
                }
                keys[i] = ((BaseObject)o).getPrimaryKey().toString();
            }
            else
            {
                keys[i] = o.toString();
            }
            peers[i] = className;
        }
    }

    /**
     * Creates a new ToolEvent destined for a specific tool.
     *
     * @@param name The name for this event.
     * @@param classes The classes of object involved in this event.
     * @@param keys The IDs of objects involved in this event.
     * @@param tool The intended tool for this event.
     */
    public ToolEvent(String name, String tool, String[] classes, String[] keys)
    {
        this.id = new GenerateUniqueId().getIdentifier();
        this.timestamp = new Date();
        this.name = name;
        this.tool = tool;
        this.peers = classes;
        this.keys = keys;
    }

    /**
     * Returns the unique ID of this event.
     */
    public String getID()
    {
        return id;
    }

    /**
     * Returns the timestamp of this event.
     */
    public Date getTimestamp()
    {
        return timestamp;
    }
    
    /**
     * Returns the name of this event.
     */
    public String getName()
    {
        return name;
    }

    /**
     * Returns the interleaved list of classes and keys.
     */
    public String[] getObjects()
    {
        String[] objects = new String[peers.length+keys.length];
        int counter = 0;
        for (int i = 0; i < peers.length; i++)
        {
            objects[counter++] = peers[i];
            objects[counter++] = keys[i];
        }
        return objects;
    }

    /**
     * Returns the names of the peers of the objects involved in this
     * event.
     */
    public String[] getPeers()
    {
        return peers;
    }

    /**
     * Returns the keys of the objects involved in this event.
     */
    public String[] getKeys()
    {
        return keys;
    }

    /**
     * Returns the tool interested in this event.
     */
    public String getTool()
    {
        return tool;
    }

    // Static methods for firing off ToolEvents.

    public static void projectAdded(Project p)
    {
        propagateSingleArgumentEvent("projectAdded", p);
    }

    public static void projectDeleted(Project p)
    {
        propagateSingleArgumentEvent("projectDeleted", p);
    }

    public static void userAdded(User u)
    {
        propagateSingleArgumentEvent("userAdded", u);
    }

    public static void userDeleted(User u)
    {
        propagateSingleArgumentEvent("userDeleted", u);
    }

    private static void propagateSingleArgumentEvent
        (String name, Object object)
    {
        try
        {
            Object[] objects = {object};
            ToolEvent e = new ToolEvent(name, objects);
            ToolEventPropagationService.getService().propagate(e);
        }
        catch (NestableException e)
        {
            Log.error(e);
        }
    }
}
@


1.10
log
@The tool events now know that business objects don't always correspond naively to peers, and that it's okay to call a method using a subclass of the formal argument as the actual argument.
@
text
@d59 2
a60 1
import org.tigris.helm.tool.ToolEventPropagationService;
d67 1
a67 1
 * @@version $Id: ToolEvent.java,v 1.9 2001/08/22 16:01:00 leonardr Exp $
@


1.9
log
@Soft project deletes allow us to send the actual deleted project instead of just its ID.
@
text
@d66 1
a66 1
 * @@version $Id: ToolEvent.java,v 1.8 2001/08/21 17:26:45 leonardr Exp $
d115 1
a115 1
            String className = o.getClass().getName();
d118 23
a140 1
                className += "Peer";
@


1.8
log
@Added convenience method for ease in displaying an event's objects.
@
text
@d66 1
a66 1
 * @@version $Id: ToolEvent.java,v 1.7 2001/08/21 01:14:22 leonardr Exp $
d220 1
a220 1
        propagateSingleArgumentEvent("projectDeleted", p.getPrimaryKey());
@


1.7
log
@Tool events are now retried indefinitely. Generic tool events are rebroadcast to the specific tools which had failures.
@
text
@d66 1
a66 1
 * @@version $Id: ToolEvent.java,v 1.6 2001/08/20 23:36:54 leonardr Exp $
d169 15
@


1.6
log
@Made architectural changes neccessary to support rebroadcast on propagation failure.
@
text
@d66 1
a66 1
 * @@version $Id: ToolEvent.java,v 1.5 2001/08/20 19:09:11 leonardr Exp $
d102 1
a102 1
     * @@param destintation The destination for this event.
d108 1
a109 1
        this.tool = tool;
d126 2
d129 16
a144 1
        }
@


1.5
log
@Tool event propagation now has singleton failover (but not persistence over restarts).
@
text
@d66 1
a66 1
 * @@version $Id: ToolEvent.java,v 1.4 2001/08/17 21:51:50 leonardr Exp $
d70 1
a70 1
    public static final String BROADCAST_DESTINATION = "*";
d77 1
a77 1
    private String destination;    
d94 1
a94 1
        this(name, affected, BROADCAST_DESTINATION);
d104 1
a104 1
    public ToolEvent(String name, Object[] affected, String destination)
d109 1
a109 1
        this.destination = destination;
d169 8
@


1.4
log
@Replaced 'plugins' architecture with ToolEventPropagationService.
@
text
@d53 1
d66 1
a66 1
 * @@version $Id: ToolEvent.java,v 1.3 2001/08/17 18:47:22 dlr Exp $
d72 2
d90 1
a90 1
     * @@see #ToolEVent(String name, Object[] affected, String destination)
d106 2
d128 16
@


1.3
log
@Added JavaDoc to ctors.
@
text
@d51 3
d58 3
d65 1
a65 1
 * @@version $Id: ToolEvent.java,v 1.2 2001/08/17 18:45:11 dlr Exp $
d87 1
a87 1
     * @@see #ToolEVent(String name, BaseObject[] affected, String destination)
d89 1
a89 1
    public ToolEvent(String name, BaseObject[] affected)
d101 1
a101 1
    public ToolEvent(String name, BaseObject[] affected, String destination)
d109 13
a121 3
            BaseObject o = affected[i];
            peers[i] = o.getClass().getName() + "Peer";
            keys[i] = o.getPrimaryKey().toString();
d150 36
@


1.2
log
@Corrected JavaDoc.
@
text
@d59 1
a59 1
 * @@version $Id: ToolEvent.java,v 1.1 2001/08/17 18:11:37 leonardr Exp $
d80 2
d90 4
d95 1
a95 1
    public ToolEvent(String name, BaseObject[] objects, String destination)
d99 3
a101 3
        peers = new String[objects.length];
        keys = new String[objects.length];
        for (int i = 0; i < objects.length; i++)
d103 1
a103 1
            BaseObject o = objects[i];
@


1.1
log
@Implementation of an event which conveys changes in tool state.
@
text
@d59 1
a59 1
 * @@version $Id: ToolEvent.java,v 1.31 2001/05/14 12:40:12 edk Exp $
d112 2
a113 1
     * Returns the peers of the objects involved in this event.
@

