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

com.mockrunner.connector.InteractionHandler Maven / Gradle / Ivy

There is a newer version: 2.0.7
Show newest version
package com.mockrunner.connector;

import java.util.Iterator;
import java.util.Vector;

import javax.resource.ResourceException;
import javax.resource.cci.InteractionSpec;
import javax.resource.cci.Record;

/**
 * This class can be used to add implementations of {@link InteractionImplementor}.
 * The {@link com.mockrunner.mock.connector.cci.MockInteraction} delegates the 
 * execute calls to this class to find a suitable 
 * {@link InteractionImplementor} that can handle the request.
 * The execute method of the first {@link InteractionImplementor} 
 * that returns true for {@link InteractionImplementor#canHandle} will
 * be called.
 */
public class InteractionHandler 
{
    private Vector implementors = null;

    public InteractionHandler()
    {
        implementors = new Vector();
    }

    /**
     * Add an implementation of {@link InteractionImplementor}, e.g.
     * {@link StreamableRecordByteArrayInteraction} or {@link WSIFInteraction}.
     * You can add more than one {@link InteractionImplementor}, the first
     * one that can handle the request will be called.
     * @param implementor the {@link InteractionImplementor}
     */
    public void addImplementor(InteractionImplementor implementor)
    {
        implementors.add(implementor);
    }

    /**
     * Clears the list of current {@link InteractionImplementor} objects.
     */
    public void clearImplementors()
    {
        implementors.clear();
    }

    /**
     * Delegator for {@link com.mockrunner.mock.connector.cci.MockInteraction}.
     * Dispatches the call to the {@link InteractionImplementor} that
     * returns true for {@link InteractionImplementor#canHandle}.
     */
    public Record execute(InteractionSpec is, Record request) throws ResourceException
    {
        for (Object implementor : implementors) {
            InteractionImplementor ii = (InteractionImplementor) implementor;
            if (ii.canHandle(is, request, null)) {
                return ii.execute(is, request);
            }
        }
        return null;
    }

    /**
     * Delegator for {@link com.mockrunner.mock.connector.cci.MockInteraction}.
     * Dispatches the call to the {@link InteractionImplementor} that
     * returns true for {@link InteractionImplementor#canHandle}.
     */
    public boolean execute(InteractionSpec is, Record request, Record response) throws ResourceException
    {
        for (Object implementor : implementors) {
            InteractionImplementor ii = (InteractionImplementor) implementor;
            if (ii.canHandle(is, request, response)) {
                return ii.execute(is, request, response);
            }
        }
        // do I need to throw here?
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy