All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tacitknowledge.util.migration.MigrationBroadcaster Maven / Gradle / Ivy

There is a newer version: 1.4.2
Show newest version
/* Copyright 2004 Tacit Knowledge
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tacitknowledge.util.migration;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Manages the MessageListener that are associated with a
 * Migration instance. 
 * 
 * @author  Scott Askew ([email protected])
 */
class MigrationBroadcaster
{
    /**
     * Used by notifyListeners to indicate that listeners should
     * be informed that a task is about to start. 
     */
    public static final int TASK_START = 1;

    /**
     * Used by notifyListeners to indicate that listeners should
     * be informed that a task has successfully completed. 
     */
    public static final int TASK_SUCCESS = 2;

    /**
     * Used by notifyListeners to indicate that listeners should
     * be informed that a task failed. 
     */
    public static final int TASK_FAILED = 3;
    
    /**
     * The listeners interested in being notified of migration task events.
     */
    private List listeners = new ArrayList();

    /**
     * Notifies all registered listeners of a migration task event.
     * 
     * @param  task the task that is being or that has been executed
     * @param  context the context in which the task was executed
     * @param  eventType TASK_START, TASK_SUCCESS, or TASK_FAIL
     * @param  e the exception thrown by the task if the task failed
     * @throws MigrationException if one of the listeners threw an exception 
     */
    public void notifyListeners(MigrationTask task, MigrationContext context,
        MigrationException e, int eventType) throws MigrationException
    {
        for (Iterator i = listeners.iterator(); i.hasNext();)
        {
            MigrationListener listener = (MigrationListener) i.next();
            switch (eventType)
            {
                case TASK_START :
                    listener.migrationStarted(task, context);
                    break;

                case TASK_SUCCESS :
                    listener.migrationSuccessful(task, context);
                    break;

                case TASK_FAILED :
                    listener.migrationFailed(task, context, e);
                    break;

                default :
                    throw new IllegalArgumentException("Unknown event type");
            }
        }
    }

    /**
     * Notifies all registered listeners of a migration task event.
     * 
     * @param  task the task that is being or that has been executed
     * @param  context the context in which the task was executed
     * @param  eventType TASK_START, TASK_SUCCESS, or TASK_FAIL
     * @throws MigrationException if one of the listeners threw an exception 
     */
    public void notifyListeners(MigrationTask task, MigrationContext context,
        int eventType) throws MigrationException
    {
        notifyListeners(task, context, null, eventType);
    }
    
    /**
     * Registers the given MigrationListener as being interested
     * in migration task events.
     * 
     * @param listener the listener to add; may not be null
     */
    public void addListener(MigrationListener listener)
    {
        if (listener == null)
        {
            throw new IllegalArgumentException("listener cannot be null");
        }
        listeners.add(listener);
    }
    
    /**
     * Removes the given MigrationListener from the list of listeners
     * associated with the Migration instance.
     * 
     * @param  listener the listener to add; may not be null
     * @return true if the listener was located and removed,
     *         otherwise false.
     */
    public boolean removeListener(MigrationListener listener)
    {
        if (listener == null)
        {
            throw new IllegalArgumentException("listener cannot be null");
        }
        return listeners.remove(listener);
    }
    
    /**
     * Get the list of listeners
     * 
     * @return List of MigrationListener objects
     */
    public List getListeners()
    {
        return listeners;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy