package org.soluvas.benchmarkemail;

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.common.TypeConverter;
import org.apache.xmlrpc.common.TypeConverterFactory;
import org.apache.xmlrpc.common.TypeConverterFactoryImpl;

import java.lang.reflect.Proxy;
import java.net.URL;

/**
 * @see #getBMEServices()
 */
public class BMEApi {
    private final XmlRpcClient client;

    public static InterfaceBMEApi getBMEServices() {
        InterfaceBMEApi result = null;
        try {
            // Create XML RPC Configuration
            final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
            config.setServerURL(new URL("http://api.benchmarkemail.com/1.3/"));
            config.setEnabledForExtensions(true);
            config.setConnectionTimeout(60 * 1000);

            // Create Object of XML RPC
            final XmlRpcClient clientx = new XmlRpcClient();
            clientx.setConfig(config);
            final BMEApi objCall = new BMEApi(clientx);

            result = (InterfaceBMEApi) objCall.newInstance(
                    InterfaceBMEApi.class.getClassLoader(),
                    InterfaceBMEApi.class);
        } catch (final Throwable t) {
            throw new BenchmarkEmailException("Failed to connect to BME services", t);
        }
        return result;
    }

    private BMEApi(final XmlRpcClient pClient) {
        this.client = pClient;
    }

    private Object newInstance(final ClassLoader oClassLoader, final Class<?> oClass) {
        return Proxy.newProxyInstance(oClassLoader, new Class[]{oClass},
                (pProxy, pMethod, pArgs) -> {
                    final String methodName = pMethod.getName();
                    Object result;
                    try {
                        TypeConverterFactory objConvert = new TypeConverterFactoryImpl();
                        result = client.execute(methodName, pArgs);
                        final TypeConverter typeConverter = objConvert
                                .getTypeConverter(pMethod.getReturnType());
                        return typeConverter.convert(result);
                    } catch (Exception e) {
                        String message = e.getMessage();
                        if ((message == null)
                                || (message.length() == 0)) {
                            message = "Unknown Error";
                        }
                        throw new BenchmarkEmailException(message, e);
                    }
                });
    }
}

@@ -1793,32 +1793,215 @@
           try %7B%0A
+                        if (%22hashCode%22.equals(methodName) && null == pArgs) %7B%0A                            return System.identityHashCode(pProxy);%0A                        %7D else %7B%0A    
                 
@@ -2090,32 +2090,36 @@
                 
+    
 result = client.
@@ -2142,24 +2142,28 @@
 me, pArgs);%0A
+    
             
@@ -2253,16 +2253,20 @@
         
+    
 .getType
@@ -2297,24 +2297,28 @@
 urnType());%0A
+    
             
@@ -2363,24 +2363,50 @@
 rt(result);%0A
+                        %7D%0A
             
@@ -2521,17 +2521,16 @@
      if 
-(
 (message
@@ -2541,54 +2541,20 @@
 null
-)%0A                               
  %7C%7C 
-(
 message.
 leng
@@ -2553,21 +2553,16 @@
 age.
-length() == 0
+isEmpty(
 )) %7B

fix hashCode() call