try {
Object result = null;
// Need to get the stuff here because this also initializes fieldInParameterNames
if (fieldIsConstructor) {
if (fieldConstructors.length <= 0)
throw new WSIFException(
"No constructor found that match the parts specified");
} else {
if (fieldMethods.length <= 0)
throw new WSIFException("No method named '"
+ fieldJavaOperationModel.getMethodName()
+ "' found that match the parts specified");
}
Object[] arguments = null;
Object part = null;
if ((fieldInParameterNames != null) && (fieldInParameterNames.length > 0)) {
arguments = new Object[fieldInParameterNames.length];
for (int i = 0; i < fieldInParameterNames.length; i++) {
try {
part = input.getObjectPart(fieldInParameterNames[i]);
arguments[i] = part;
} catch (WSIFException e) {
Trc.exception(e);
arguments[i] = null;
if (fieldOutParameterNames.length > 0) {
String outParameterName = null;
for (int j = 0; j < fieldOutParameterNames.length; j++) {
outParameterName =
fieldOutParameterNames[j];
if ((outParameterName != null)
&& (outParameterName
.equals(fieldInParameterNames[i]))) {
arguments[i] =
(fieldMethods[0].getParameterTypes()[i])
.newInstance();
usedOutputParam = true;
}
}
}
}
}
}
boolean invokedOK = false;
if (fieldIsConstructor) {
for (int a = 0; a < fieldConstructors.length; a++) {
try {
// Get a set of arguments which are compatible with the ctor
Object[] compatibleArguments =
getCompatibleArguments(fieldConstructors[a].getParameterTypes(), arguments);
// If we didn't get any arguments then the parts aren't compatible with the ctor
if (compatibleArguments == null)
break;
// Parts are compatible so invoke the ctor with the compatible set
Trc.event(
this,
"Invoking constructor ",
fieldConstructors[a],
" with arguments ",
compatibleArguments);
result =
fieldConstructors[a].newInstance(
compatibleArguments);
Trc.event(
this,
"Returned from constructor, result is ",
result);
fieldPort.setObjectReference(result);
invokedOK = true;
break;
} catch (IllegalArgumentException ia) {
Trc.ignoredException(ia);
// Ignore and try next constructor
}
}
if (!invokedOK)
throw new WSIFException("Failed to call constructor for object in Java operation");
// Side effect: Initialize port's object reference
} else {
if (fieldIsStatic) {
for (int a = 0; a < fieldMethods.length; a++) {
if (usedOutputParam) {
for (int i = 0; i < fieldInParameterNames.length; i++) {
String outParameterName = null;
for (int j = 0; j < fieldOutParameterNames.length; j++) {
outParameterName = fieldOutParameterNames[j];
if ((outParameterName != null)
&& (outParameterName.equals(fieldInParameterNames[i]))) {
arguments[i] = (fieldMethods[a].getParameterTypes()[i]).newInstance();
}
}
}
}
try {
// Get a set of arguments which are compatible with the method
Object[] compatibleArguments =
getCompatibleArguments(fieldMethods[a].getParameterTypes(), arguments);
// If we didn't get any arguments then the parts aren't compatible with the method
if (compatibleArguments == null)
break;
// Parts are compatible so invoke the method with the compatible set
Trc.event(
this,
"Invoking method ",
fieldMethods[a],
" with arguments ",
compatibleArguments);
result =
fieldMethods[a].invoke(
null,
compatibleArguments);
Trc.event(
this,
"Returned from method, result is ",
result);
chosenMethod = fieldMethods[a];
invokedOK = true;
break;
} catch (IllegalArgumentException ia) {
Trc.ignoredException(ia);
// Ignore and try next method
}
}
if (!invokedOK)
throw new WSIFException(
"Failed to invoke method '" + fieldJavaOperationModel.getMethodName() + "'");
} else {
for (int a = 0; a < fieldMethods.length; a++) {
if (usedOutputParam) {
for (int i = 0; i < fieldInParameterNames.length; i++) {
String outParameterName = null;
for (int j = 0; j < fieldOutParameterNames.length; j++) {
outParameterName = fieldOutParameterNames[j];
if ((outParameterName != null)
&& (outParameterName.equals(fieldInParameterNames[i]))) {
arguments[i] = (fieldMethods[a].getParameterTypes()[i]).newInstance();
}
}
}
}
try {
// Get a set of arguments which are compatible with the method
Object[] compatibleArguments =
getCompatibleArguments(fieldMethods[a].getParameterTypes(), arguments);
// If we didn't get any arguments then the parts aren't compatible with the method
if (compatibleArguments == null)
break;
// Parts are compatible so invoke the method with the compatible set
Object objRef = fieldPort.getObjectReference();
Trc.event(
this,
"Invoking object ",
objRef,
" method ",
fieldMethods[a],
" with arguments ",
compatibleArguments);
result =
fieldMethods[a].invoke(
objRef,
compatibleArguments);
Trc.event(
this,
"Returned from method, result is ",
result);
chosenMethod = fieldMethods[a];
invokedOK = true;
break;
} catch (IllegalArgumentException ia) {
Trc.ignoredException(ia);
// Ignore and try next method
}
}
if (!invokedOK)
throw new WSIFException(
"Failed to invoke method '" + fieldJavaOperationModel.getMethodName() + "'");
}
String outParameterName = null;
if (fieldOutParameterNames != null && fieldOutParameterNames.length > 0) {
output.setName(getOutputMessageName());
outParameterName = (String) fieldOutParameterNames[0];
if (outParameterName != null) {
output.setObjectPart(
outParameterName,
getCompatibleReturn(chosenMethod, result));
// Should we use the class of the method signature instead here ?
}
if (arguments != null) {
for (int i = 1; i < fieldOutParameterNames.length; i++) {
outParameterName = fieldOutParameterNames[i];
if (outParameterName != null) {
try {
for (int r = 0; r < fieldInParameterNames.length; r++) {
if (outParameterName.equals(fieldInParameterNames[r])) {
output.setObjectPart(outParameterName, arguments[r]);
break;
}
}
} catch (WSIFException e) {
Trc.ignoredException(e);
//ignore
}
}
}
}
}
}
} catch (InvocationTargetException ex) {
Trc.exception(ex);
Throwable invocationFault = ex.getTargetException();
String className = invocationFault.getClass().getName();
Map faultMessageInfos = getFaultMessageInfos();
FaultMessageInfo faultMessageInfo =
(FaultMessageInfo) faultMessageInfos.get(className);
if ((faultMessageInfo != null)
&& (faultMessageInfo.fieldPartName != null)) { // Found fault
Object faultPart = invocationFault;
// Should we use the class of the method signature here ?
fault.setObjectPart(faultMessageInfo.fieldPartName, faultPart);
fault.setName(faultMessageInfo.fieldMessageName);
if (faultMessageInfo.fieldMessageName != null) {
Fault wsdlFault = fieldBindingOperationModel.getOperation().getFault(faultMessageInfo.fieldMessageName);
if (wsdlFault != null) {
fault.setMessageDefinition(wsdlFault.getMessage());
}
}
operationSucceeded = false;
} else {
// Try to find a matching class:
Class invocationFaultClass = invocationFault.getClass();
Class tempClass = null;
Iterator it = faultMessageInfos.values().iterator();
boolean found = false;
while (it.hasNext()) {
faultMessageInfo = (FaultMessageInfo) it.next();
try {
tempClass =
Class.forName(
faultMessageInfo.fieldFormatType,
true,
Thread.currentThread().getContextClassLoader());
if (tempClass.isAssignableFrom(invocationFaultClass)) {
found = true;
Object faultPart = invocationFault;
// Should we use the class of the method signature here ?
fault.setObjectPart(faultMessageInfo.fieldPartName, faultPart);
fault.setName(faultMessageInfo.fieldMessageName);
if (faultMessageInfo.fieldMessageName != null) {
Fault wsdlFault = fieldBindingOperationModel.getOperation().getFault(faultMessageInfo.fieldMessageName);
if (wsdlFault != null) {
fault.setMessageDefinition(wsdlFault.getMessage());
}
}
operationSucceeded = false;
}
} catch (Exception exc) { // Nothing to do - just try the next one...
Trc.ignoredException(exc);
}
}
if (!found) {
throw new WSIFException("Operation failed!", invocationFault);
}
}
} catch (Exception ex) {
Trc.exception(ex);
// Log message
MessageLogger.log(
"WSIF.0005E",
"Java",
fieldJavaOperationModel.getMethodName());
throw new WSIFException(
this
+ " : Could not invoke '"
+ fieldJavaOperationModel.getMethodName()
+ "'",
ex);