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 package org.ros.internal.node.xmlrpc;
00019
00020 import org.apache.xmlrpc.XmlRpcException;
00021 import org.apache.xmlrpc.client.TimingOutCallback;
00022 import org.apache.xmlrpc.client.XmlRpcClient;
00023 import org.apache.xmlrpc.common.TypeConverter;
00024 import org.apache.xmlrpc.common.TypeConverterFactory;
00025 import org.apache.xmlrpc.common.TypeConverterFactoryImpl;
00026
00027 import java.lang.reflect.InvocationHandler;
00028 import java.lang.reflect.Method;
00029 import java.lang.reflect.Proxy;
00030 import java.lang.reflect.UndeclaredThrowableException;
00031
00042 public class XmlRpcClientFactory<T extends org.ros.internal.node.xmlrpc.XmlRpcEndpoint> {
00043
00044 private final XmlRpcClient client;
00045 private final TypeConverterFactory typeConverterFactory;
00046
00047 private boolean objectMethodLocal;
00048
00059 public XmlRpcClientFactory(XmlRpcClient pClient, TypeConverterFactory pTypeConverterFactory) {
00060 typeConverterFactory = pTypeConverterFactory;
00061 client = pClient;
00062 }
00063
00076 public XmlRpcClientFactory(XmlRpcClient pClient) {
00077 this(pClient, new TypeConverterFactoryImpl());
00078 }
00079
00083 public XmlRpcClient getClient() {
00084 return client;
00085 }
00086
00091 public boolean isObjectMethodLocal() {
00092 return objectMethodLocal;
00093 }
00094
00099 public void setObjectMethodLocal(boolean pObjectMethodLocal) {
00100 objectMethodLocal = pObjectMethodLocal;
00101 }
00102
00120 public Object newInstance(ClassLoader pClassLoader, final Class<T> pClass,
00121 final String pRemoteName, final int timeout) {
00122 return Proxy.newProxyInstance(pClassLoader, new Class[] { pClass }, new InvocationHandler() {
00123 @Override
00124 public Object invoke(Object pProxy, Method pMethod, Object[] pArgs) throws Throwable {
00125 if (isObjectMethodLocal() && pMethod.getDeclaringClass().equals(Object.class)) {
00126 return pMethod.invoke(pProxy, pArgs);
00127 }
00128 final String methodName;
00129 if (pRemoteName == null || pRemoteName.length() == 0) {
00130 methodName = pMethod.getName();
00131 } else {
00132 methodName = pRemoteName + "." + pMethod.getName();
00133 }
00134 Object result;
00135 try {
00136 TimingOutCallback callback = new TimingOutCallback(timeout);
00137 client.executeAsync(methodName, pArgs, callback);
00138 result = callback.waitForResponse();
00139 } catch (TimingOutCallback.TimeoutException e) {
00140 throw new XmlRpcTimeoutException(e);
00141 } catch (InterruptedException e) {
00142 throw new XmlRpcTimeoutException(e);
00143 } catch (UndeclaredThrowableException e) {
00144 throw new RuntimeException(e);
00145 } catch (XmlRpcException e) {
00146 Throwable linkedException = e.linkedException;
00147 if (linkedException == null) {
00148 throw new RuntimeException(e);
00149 }
00150 Class<?>[] exceptionTypes = pMethod.getExceptionTypes();
00151 for (int i = 0; i < exceptionTypes.length; i++) {
00152 Class<?> c = exceptionTypes[i];
00153 if (c.isAssignableFrom(linkedException.getClass())) {
00154 throw linkedException;
00155 }
00156 }
00157 throw new RuntimeException(linkedException);
00158 }
00159 TypeConverter typeConverter =
00160 typeConverterFactory.getTypeConverter(pMethod.getReturnType());
00161 return typeConverter.convert(result);
00162 }
00163 });
00164 }
00165 }