001 package com.mockrunner.connector;
002
003 import java.util.Iterator;
004 import java.util.Vector;
005
006 import javax.resource.ResourceException;
007 import javax.resource.cci.InteractionSpec;
008 import javax.resource.cci.Record;
009
010 /**
011 * This class can be used to add implementations of {@link InteractionImplementor}.
012 * The {@link com.mockrunner.mock.connector.cci.MockInteraction} delegates the
013 * <code>execute</code> calls to this class to find a suitable
014 * {@link InteractionImplementor} that can handle the request.
015 * The <code>execute</code> method of the first {@link InteractionImplementor}
016 * that returns <code>true</code> for {@link InteractionImplementor#canHandle} will
017 * be called.
018 */
019 public class InteractionHandler
020 {
021 private Vector implementors = null;
022
023 public InteractionHandler()
024 {
025 implementors = new Vector();
026 }
027
028 /**
029 * Add an implementation of {@link InteractionImplementor}, e.g.
030 * {@link StreamableRecordByteArrayInteraction} or {@link WSIFInteraction}.
031 * You can add more than one {@link InteractionImplementor}, the first
032 * one that can handle the request will be called.
033 * @param implementor the {@link InteractionImplementor}
034 */
035 public void addImplementor(InteractionImplementor implementor)
036 {
037 implementors.add(implementor);
038 }
039
040 /**
041 * Clears the list of current {@link InteractionImplementor} objects.
042 */
043 public void clearImplementors()
044 {
045 implementors.clear();
046 }
047
048 /**
049 * Delegator for {@link com.mockrunner.mock.connector.cci.MockInteraction}.
050 * Dispatches the call to the {@link InteractionImplementor} that
051 * returns <code>true</code> for {@link InteractionImplementor#canHandle}.
052 */
053 public Record execute(InteractionSpec is, Record request) throws ResourceException
054 {
055 Iterator iter = implementors.iterator();
056 while (iter.hasNext())
057 {
058 InteractionImplementor ii = (InteractionImplementor) iter.next();
059 if (ii.canHandle(is, request, null))
060 {
061 return ii.execute(is, request);
062 }
063 }
064 return null;
065 }
066
067 /**
068 * Delegator for {@link com.mockrunner.mock.connector.cci.MockInteraction}.
069 * Dispatches the call to the {@link InteractionImplementor} that
070 * returns <code>true</code> for {@link InteractionImplementor#canHandle}.
071 */
072 public boolean execute(InteractionSpec is, Record request, Record response) throws ResourceException
073 {
074 Iterator iter = implementors.iterator();
075 while (iter.hasNext())
076 {
077 InteractionImplementor ii = (InteractionImplementor) iter.next();
078 if (ii.canHandle(is, request, response))
079 {
080 return ii.execute(is, request, response);
081 }
082 }
083 // do I need to throw here?
084 return false;
085 }
086 }