Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 package org.apache.xmlrpc.client.util;
00020
00021 import java.lang.reflect.InvocationHandler;
00022 import java.lang.reflect.Method;
00023 import java.lang.reflect.Proxy;
00024 import java.lang.reflect.UndeclaredThrowableException;
00025
00026 import org.apache.xmlrpc.client.XmlRpcClient;
00027 import org.apache.xmlrpc.common.TypeConverter;
00028 import org.apache.xmlrpc.common.TypeConverterFactory;
00029 import org.apache.xmlrpc.common.TypeConverterFactoryImpl;
00030 import org.apache.xmlrpc.common.XmlRpcInvocationException;
00031
00032
00040 public class ClientFactory {
00041 private final XmlRpcClient client;
00042 private final TypeConverterFactory typeConverterFactory;
00043 private boolean objectMethodLocal;
00044
00051 public ClientFactory(XmlRpcClient pClient, TypeConverterFactory pTypeConverterFactory) {
00052 typeConverterFactory = pTypeConverterFactory;
00053 client = pClient;
00054 }
00055
00064 public ClientFactory(XmlRpcClient pClient) {
00065 this(pClient, new TypeConverterFactoryImpl());
00066 }
00067
00070 public XmlRpcClient getClient() {
00071 return client;
00072 }
00073
00078 public boolean isObjectMethodLocal() {
00079 return objectMethodLocal;
00080 }
00081
00086 public void setObjectMethodLocal(boolean pObjectMethodLocal) {
00087 objectMethodLocal = pObjectMethodLocal;
00088 }
00089
00099 public Object newInstance(Class pClass) {
00100 return newInstance(Thread.currentThread().getContextClassLoader(), pClass);
00101 }
00102
00110 public Object newInstance(ClassLoader pClassLoader, Class pClass) {
00111 return newInstance(pClassLoader, pClass, pClass.getName());
00112 }
00113
00126 public Object newInstance(ClassLoader pClassLoader, final Class pClass, final String pRemoteName) {
00127 return Proxy.newProxyInstance(pClassLoader, new Class[]{pClass}, new InvocationHandler(){
00128 public Object invoke(Object pProxy, Method pMethod, Object[] pArgs) throws Throwable {
00129 if (isObjectMethodLocal() && pMethod.getDeclaringClass().equals(Object.class)) {
00130 return pMethod.invoke(pProxy, pArgs);
00131 }
00132 final String methodName;
00133 if (pRemoteName == null || pRemoteName.length() == 0) {
00134 methodName = pMethod.getName();
00135 } else {
00136 methodName = pRemoteName + "." + pMethod.getName();
00137 }
00138 Object result;
00139 try {
00140 result = client.execute(methodName, pArgs);
00141 } catch (XmlRpcInvocationException e) {
00142 Throwable t = e.linkedException;
00143 if (t instanceof RuntimeException) {
00144 throw t;
00145 }
00146 Class[] exceptionTypes = pMethod.getExceptionTypes();
00147 for (int i = 0; i < exceptionTypes.length; i++) {
00148 Class c = exceptionTypes[i];
00149 if (c.isAssignableFrom(t.getClass())) {
00150 throw t;
00151 }
00152 }
00153 throw new UndeclaredThrowableException(t);
00154 }
00155 TypeConverter typeConverter = typeConverterFactory.getTypeConverter(pMethod.getReturnType());
00156 return typeConverter.convert(result);
00157 }
00158 });
00159 }
00160 }