01: package com.mockrunner.connector;
02:
03: import java.util.Iterator;
04: import java.util.Vector;
05:
06: import javax.resource.ResourceException;
07: import javax.resource.cci.InteractionSpec;
08: import javax.resource.cci.Record;
09:
10: /**
11: * This class can be used to add implementations of {@link InteractionImplementor}.
12: * The {@link com.mockrunner.mock.connector.cci.MockInteraction} delegates the
13: * <code>execute</code> calls to this class to find a suitable
14: * {@link InteractionImplementor} that can handle the request.
15: * The <code>execute</code> method of the first {@link InteractionImplementor}
16: * that returns <code>true</code> for {@link InteractionImplementor#canHandle} will
17: * be called.
18: */
19: public class InteractionHandler {
20: private Vector implementors = null;
21:
22: public InteractionHandler() {
23: implementors = new Vector();
24: }
25:
26: /**
27: * Add an implementation of {@link InteractionImplementor}, e.g.
28: * {@link StreamableRecordByteArrayInteraction} or {@link WSIFInteraction}.
29: * You can add more than one {@link InteractionImplementor}, the first
30: * one that can handle the request will be called.
31: * @param implementor the {@link InteractionImplementor}
32: */
33: public void addImplementor(InteractionImplementor implementor) {
34: implementors.add(implementor);
35: }
36:
37: /**
38: * Clears the list of current {@link InteractionImplementor} objects.
39: */
40: public void clearImplementors() {
41: implementors.clear();
42: }
43:
44: /**
45: * Delegator for {@link com.mockrunner.mock.connector.cci.MockInteraction}.
46: * Dispatches the call to the {@link InteractionImplementor} that
47: * returns <code>true</code> for {@link InteractionImplementor#canHandle}.
48: */
49: public Record execute(InteractionSpec is, Record request)
50: throws ResourceException {
51: Iterator iter = implementors.iterator();
52: while (iter.hasNext()) {
53: InteractionImplementor ii = (InteractionImplementor) iter
54: .next();
55: if (ii.canHandle(is, request, null)) {
56: return ii.execute(is, request);
57: }
58: }
59: return null;
60: }
61:
62: /**
63: * Delegator for {@link com.mockrunner.mock.connector.cci.MockInteraction}.
64: * Dispatches the call to the {@link InteractionImplementor} that
65: * returns <code>true</code> for {@link InteractionImplementor#canHandle}.
66: */
67: public boolean execute(InteractionSpec is, Record request,
68: Record response) throws ResourceException {
69: Iterator iter = implementors.iterator();
70: while (iter.hasNext()) {
71: InteractionImplementor ii = (InteractionImplementor) iter
72: .next();
73: if (ii.canHandle(is, request, response)) {
74: return ii.execute(is, request, response);
75: }
76: }
77: // do I need to throw here?
78: return false;
79: }
80: }
|