ClientFactory.java
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one
00003  * or more contributor license agreements.  See the NOTICE file
00004  * distributed with this work for additional information
00005  * regarding copyright ownership.  The ASF licenses this file
00006  * to you under the Apache License, Version 2.0 (the
00007  * "License"); you may not use this file except in compliance
00008  * with the License.  You may obtain a copy of the License at
00009  *
00010  *   http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing,
00013  * software distributed under the License is distributed on an
00014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00015  * KIND, either express or implied.  See the License for the
00016  * specific language governing permissions and limitations
00017  * under the License.    
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 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49