001: package com.mockrunner.connector;
002:
003: import java.lang.reflect.Method;
004:
005: import javax.resource.ResourceException;
006: import javax.resource.cci.InteractionSpec;
007: import javax.resource.cci.Record;
008:
009: /**
010: * Implementor for IBM environments using the
011: * Web Services Invocation Framework
012: */
013: public class WSIFInteraction implements InteractionImplementor {
014: private boolean enabled;
015: private String isClassName;
016: private String requestPartName;
017: private Object requestPart;
018: private String responsePartName;
019: private Object responsePart;
020:
021: /**
022: * @param isClassName would be com.ibm.connector2.ims.ico.IMSInteractionSpec, com.ibm.connector2.cics.ECIInteractionSpec
023: */
024: public WSIFInteraction(String isClassName, String requestPartName,
025: Object requestPart, String responsePartName,
026: Object responsePart) {
027: super ();
028: this .isClassName = isClassName;
029: this .requestPartName = requestPartName;
030: this .requestPart = requestPart;
031: this .responsePartName = responsePartName;
032: this .responsePart = responsePart;
033: this .enabled = true;
034: }
035:
036: /**
037: * Enables this implementor.
038: */
039: public void enable() {
040: this .enabled = true;
041: }
042:
043: /**
044: * Disables this implementor. {@link #canHandle(InteractionSpec, Record, Record)}
045: * always returns <code>false</code>, if this implementor is disabled.
046: */
047: public void disable() {
048: this .enabled = false;
049: }
050:
051: public boolean canHandle(InteractionSpec interactionSpec,
052: Record actualRequest, Record actualResponse) {
053: if (!enabled)
054: return false;
055: Class specClass = getClassNamed(interactionSpec.getClass(),
056: isClassName);
057: if (specClass != null) {
058: try {
059: Class wsifMessageClass = getClassNamed(actualRequest
060: .getClass(),
061: "org.apache.wsif.base.WSIFDefaultMessage");
062: Class wsifFormatPartClass = getClassNamed(requestPart
063: .getClass(),
064: "com.ibm.wsif.format.jca.WSIFFormatPartImpl");
065: if (wsifMessageClass != null
066: && wsifFormatPartClass != null) {
067: Class[] gopParams = new Class[1];
068: gopParams[0] = String.class;
069: Method gop = wsifMessageClass.getMethod(
070: "getObjectPart", gopParams);
071: if (gop != null) {
072: Object[] gopArgs = new Object[1];
073: gopArgs[0] = requestPartName;
074: Object o1 = gop.invoke(actualRequest, gopArgs);
075: if (o1 != null) {
076: Class[] elementsParams = new Class[0];
077: Method elements = wsifFormatPartClass
078: .getMethod("elements",
079: elementsParams);
080: if (elements != null) {
081: Object[] elementsArgs = new Object[0];
082: Object map1 = elements.invoke(
083: requestPart, elementsArgs);
084: Object map2 = elements.invoke(o1,
085: elementsArgs);
086: if (map1.equals(map2)) {
087: return true;
088: }
089: }
090: }
091: }
092: }
093: } catch (Exception exc) {
094: return false;
095: }
096: }
097: return false;
098: }
099:
100: /**
101: * not implemented yet.
102: */
103: public Record execute(InteractionSpec interactionSpec,
104: Record actualRequest) throws ResourceException {
105: throw new RuntimeException(
106: this .getClass().getName()
107: + " does not implement public Record execute(InteractionSpec, Record)");
108: }
109:
110: public boolean execute(InteractionSpec interactionSpec,
111: Record actualRequest, Record actualResponse)
112: throws ResourceException {
113: if (!canHandle(interactionSpec, actualRequest, actualResponse))
114: return false;
115: try {
116: Class wsifMessageClass = getClassNamed(actualRequest
117: .getClass(),
118: "org.apache.wsif.base.WSIFDefaultMessage");
119: Class[] sopParams = new Class[2];
120: sopParams[0] = String.class;
121: sopParams[1] = Object.class;
122: Method sop = wsifMessageClass.getMethod("setObjectPart",
123: sopParams);
124: if (sop != null) {
125: Object[] sopArgs = new Object[2];
126: sopArgs[0] = responsePartName;
127: sopArgs[1] = responsePart;
128: sop.invoke(actualResponse, sopArgs);
129: return true;
130: }
131: } catch (Exception exc) {
132: ResourceException resExc = new ResourceException(
133: "execute() failed");
134: resExc.setLinkedException(exc);
135: throw resExc;
136: }
137: return false;
138: }
139:
140: /**
141: *
142: * @param cl
143: * @param className
144: * @return null if not found
145: */
146: private Class getClassNamed(Class cl, String className) {
147: if (cl == null) {
148: return null;
149: }
150: if (cl.getName().equals(className)) {
151: return cl;
152: }
153: Class[] classes = cl.getDeclaredClasses();
154: for (int current = 0; current < classes.length; current++) {
155: Class c = classes[current];
156: if (className.equals(c.getName())) {
157: return c;
158: }
159: }
160: return getClassNamed(cl.getSuperclass(), className);
161: }
162: }
|