package org.jboss.axis.client;
import org.jboss.axis.AxisFault;
import org.jboss.axis.Constants;
import org.jboss.axis.description.OperationDesc;
import org.jboss.axis.description.ParameterDesc;
import org.jboss.axis.enums.Style;
import org.jboss.axis.enums.Use;
import org.jboss.axis.utils.JavaUtils;
import org.jboss.logging.Logger;
import javax.activation.DataHandler;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.holders.Holder;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Map;
public class AxisClientProxy implements InvocationHandler
{
private static Logger log = Logger.getLogger(AxisClientProxy.class.getName());
private Call call;
private QName portName;
AxisClientProxy(Call call, QName portName)
{
this.call = call;
this.portName = portName; }
private Object[] proxyParams2CallParams(Object[] proxyParams)
throws JavaUtils.HolderException, ServiceException
{
ArrayList callParams = new ArrayList();
OperationDesc operationDesc = call.getOperation();
if (operationDesc == null || proxyParams == null)
{
return proxyParams;
}
boolean isDocumentLiteral = Style.DOCUMENT.equals(operationDesc.getStyle()) && Use.LITERAL.equals(operationDesc.getUse());
if (isDocumentLiteral)
{
int proxyIndex = 0;
ArrayList opParams = operationDesc.getParameters();
for (int i = 0; i < opParams.size(); i++)
{
ParameterDesc parameterDesc = (ParameterDesc)opParams.get(i);
Class javaType = parameterDesc.getJavaType();
if (javaType == null || proxyParams[proxyIndex] == null)
{
callParams.add(proxyParams[proxyIndex++]);
continue;
}
Class proxyType = proxyParams[proxyIndex].getClass();
if (javaType.isAssignableFrom(proxyType))
{
callParams.add(proxyParams[proxyIndex++]);
continue;
}
if (JavaUtils.isConvertable(proxyType, javaType))
{
Object param = JavaUtils.convert(proxyParams[proxyIndex++], javaType);
callParams.add(param);
continue;
}
boolean ctorFound = false;
Constructor[] ctors = javaType.getConstructors();
for (int j = 0; j < ctors.length; j++)
{
boolean paramsMatch = true;
Constructor ctor = ctors[j];
Class[] ctorTypes = ctor.getParameterTypes();
if (ctorTypes.length > 0)
{
Object[] ctorVals = new Object[ctorTypes.length];
for (int k = 0; paramsMatch && k < ctorTypes.length; k++)
{
if (ctorTypes[k].isAssignableFrom(proxyParams[proxyIndex + k].getClass()))
ctorVals[k] = proxyParams[proxyIndex + k];
else
paramsMatch = false;
}
if (paramsMatch)
{
try
{
Object inst = ctor.newInstance(ctorVals);
proxyIndex += ctorVals.length;
callParams.add(inst);
ctorFound = true;
break;
}
catch (Exception e)
{
throw new ServiceException("Cannot map proxy params to: " + javaType);
}
}
}
}
if (ctorFound == false)
callParams.add(proxyParams[proxyIndex++]);
}
}
else
{
for (int i = 0; proxyParams != null && i < proxyParams.length; i++)
{
Object param = proxyParams[i];
ParameterDesc paramDesc = operationDesc.getParameter(i);
if (paramDesc == null)
throw new ServiceException("Cannot obtain parameter " + i + " for: " + operationDesc);
if (paramDesc.getMode() == ParameterDesc.INOUT)
{
callParams.add(JavaUtils.getHolderValue((Holder)param));
}
else if (paramDesc.getMode() == ParameterDesc.IN)
{
callParams.add(param);
}
}
}
return callParams.toArray();
}
private void proxyReturn2CallReturn(Class proxyReturn)
{
OperationDesc operationDesc = call.getOperation();
if (operationDesc != null)
{
Class operationReturn = operationDesc.getReturnClass();
if (proxyReturn != null && operationReturn != null)
{
if (proxyReturn.equals(DataHandler.class))
{
operationDesc.setReturnClass(DataHandler.class);
operationDesc.setReturnType(Constants.MIME_DATA_HANDLER);
}
boolean isConvertible = JavaUtils.isConvertable(operationReturn, proxyReturn);
if (isConvertible == false)
isConvertible = getDocLitResultWrapper(operationReturn, proxyReturn) != null;
if (isConvertible == false)
{
log.debug("Fixing return class: " + operationReturn + " -> " + proxyReturn);
operationDesc.setReturnClass(proxyReturn);
}
}
if (proxyReturn == null && operationReturn != null)
{
log.debug("Forcing return class to null: " + operationReturn);
operationDesc.setReturnClass(null);
}
}
}
private Method getDocLitResultWrapper(Class callReturn, Class proxyReturn)
{
Method getter = null;
OperationDesc operationDesc = call.getOperation();
boolean isDocumentLiteral = Style.DOCUMENT.equals(operationDesc.getStyle()) && Use.LITERAL.equals(operationDesc.getUse());
if (isDocumentLiteral)
{
Method[] methods = callReturn.getMethods();
for (int i = 0; getter == null && i < methods.length; i++)
{
Method method = methods[i];
if (method.getName().startsWith("get") && method.getParameterTypes().length == 0 && proxyReturn.isAssignableFrom(method.getReturnType()))
{
log.debug("Trying to unwrap proxy return with: " + method);
getter = method;
}
}
}
return getter;
}
private void callOutputParams2proxyParams(Object[] proxyParams)
throws JavaUtils.HolderException
{
OperationDesc operationDesc = call.getOperation();
if (operationDesc == null || proxyParams == null)
{
return;
}
Map outputParams = call.getOutputParams();
for (int i = 0; i < operationDesc.getNumParams(); i++)
{
Object param = proxyParams[i];
ParameterDesc paramDesc = operationDesc.getParameter(i);
if (paramDesc.getMode() != ParameterDesc.IN)
{
Object value = outputParams.get(paramDesc.getQName());
if (Holder.class.isAssignableFrom(value.getClass()))
value = JavaUtils.getHolderValue(value);
JavaUtils.setHolderValue((Holder)param, value);
}
}
}
public Object invoke(Object o, Method method, Object[] objects)
throws Throwable
{
try
{
if (method.getName().equals("_setProperty"))
{
call.setProperty((String)objects[0], objects[1]);
return null;
}
else if (method.getName().equals("_getProperty"))
{
return call.getProperty((String)objects[0]);
}
else if (method.getName().equals("hashCode"))
{
return new Integer(call.hashCode());
}
else if (method.getName().equals("toString"))
{
return call.toString();
}
else if (method.getName().equals("equals"))
{
return new Boolean(o == objects[0]);
}
else
{
Object outValue;
Object[] paramsCall;
if ((call.getTargetEndpointAddress() != null) && (call.getPortName() != null))
{
call.setOperation(method.getName());
paramsCall = proxyParams2CallParams(objects);
proxyReturn2CallReturn(method.getReturnType());
outValue = call.invoke(paramsCall);
}
else if (portName != null)
{
call.setOperation(portName, method.getName());
paramsCall = proxyParams2CallParams(objects);
proxyReturn2CallReturn(method.getReturnType());
outValue = call.invoke(paramsCall);
}
else
{
paramsCall = objects;
proxyReturn2CallReturn(method.getReturnType());
outValue = call.invoke(method.getName(), paramsCall);
}
callOutputParams2proxyParams(objects);
outValue = callReturn2ProxyReturn(outValue, method.getReturnType());
return outValue;
}
}
catch (AxisFault af)
{
if (af.detail != null)
{
throw af.detail;
}
throw af;
}
}
private Object callReturn2ProxyReturn(Object callReturn, Class proxyReturnType)
{
if (callReturn == null)
return callReturn;
if (JavaUtils.isConvertable(callReturn, proxyReturnType))
return JavaUtils.convert(callReturn, proxyReturnType);
if (callReturn instanceof ArrayList)
return convertArrayList(callReturn);
Class callReturnType = callReturn.getClass();
Method docLitResultWrapper = getDocLitResultWrapper(callReturnType, proxyReturnType);
if (docLitResultWrapper != null)
{
try
{
Object proxyReturn = docLitResultWrapper.invoke(callReturn, new Object[]{});
return proxyReturn;
}
catch (Exception e)
{
log.error("Cannot unwrap call return", e);
}
}
log.warn("Cannot convert call return " + callReturnType.getName() + " to: " + proxyReturnType.getName());
return callReturn;
}
private Object convertArrayList(Object outValue)
{
Object value = ((ArrayList)outValue).toArray();
if (value.getClass().isArray())
{
if (!value.getClass().getComponentType().isPrimitive())
{
int len = java.lang.reflect.Array.getLength(value);
Class type = null;
for (int x = 0; x < len; x++)
{
Object o = java.lang.reflect.Array.get(value, x);
if (o != null)
{
if (type == null)
{
type = o.getClass();
}
else
{
if (!type.getName().equals(o.getClass().getName()))
{
type = null;
break;
}
}
}
}
if (type != null)
{
Object convertedArray = java.lang.reflect.Array.newInstance(type, len);
System.arraycopy(value, 0, convertedArray, 0, len);
value = convertedArray;
}
}
}
return value;
}
public Call getCall()
{
return call;
}
}