All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.distributeme.test.jsonrpc.jsonrpc.generated.ClientEchoServiceImpl Maven / Gradle / Ivy


package org.distributeme.test.jsonrpc.jsonrpc.generated;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
import org.distributeme.test.jsonrpc.EchoService;
import cz.eman.jsonrpc.shared.JsonTransformer;
import cz.eman.jsonrpc.shared.bo.JsonRpcNormalResponse;
import cz.eman.jsonrpc.shared.bo.JsonRpcErrorResponse;
import cz.eman.jsonrpc.shared.bo.JsonRpcRequest;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.JsonNodeFactory;
import org.distributeme.core.ClientSideCallContext;
import org.distributeme.core.DiscoveryMode;
import org.distributeme.core.RegistryUtil;
import org.distributeme.core.ServiceDescriptor;
import org.distributeme.core.exception.NoConnectionToServerException;
import org.distributeme.core.failing.FailDecision;
import org.distributeme.core.failing.FailingStrategy;
import org.mortbay.io.ByteArrayBuffer;
import org.mortbay.jetty.client.ContentExchange;
import org.mortbay.jetty.client.HttpClient;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.distributeme.core.concurrencycontrol.ConcurrencyControlStrategy;
import org.distributeme.core.interceptor.ClientSideRequestInterceptor;
import org.distributeme.core.interceptor.InterceptionContext;
import org.distributeme.core.interceptor.InterceptionPhase;
import org.distributeme.core.interceptor.InterceptorRegistry;
import org.distributeme.core.interceptor.InterceptorResponse;
import org.distributeme.core.exception.DistributemeRuntimeException;
import org.distributeme.core.exception.NoConnectionToServerException;
import org.distributeme.core.exception.ServiceUnavailableException;
import org.distributeme.core.Defaults;

public class ClientEchoServiceImpl implements ClientEchoService,EchoService {

	private static Logger log = LoggerFactory.getLogger(ClientEchoServiceImpl.class);

	private org.distributeme.test.jsonrpc.EchoService implementation;

	private long lastAccess;
	private long created;

	private volatile ConcurrentMap delegates = new ConcurrentHashMap();

	private DiscoveryMode discoveryMode = DiscoveryMode.AUTO;
	private HttpClient client;
	// No class-wide-router set, skipping.

	// CONCURRENCY CONTROL
	// Class wide concurrency control strategy 
	private ConcurrencyControlStrategy clazzWideCCStrategy = Defaults.getDefaultConcurrencyControlStrategy();

	private ConcurrencyControlStrategy echoCCStrategy_longaValue = clazzWideCCStrategy;
	private ConcurrencyControlStrategy echoObjectParam1CCStrategy_javalangObjectaValue = clazzWideCCStrategy;
	private ConcurrencyControlStrategy echoObjectParamCCStrategy_orgdistributemetestjsonrpcAaValue = clazzWideCCStrategy;
	private ConcurrencyControlStrategy echoManyParamsCCStrategy_intind_javalangStringstrNum_javalangLongincremt = clazzWideCCStrategy;
	// CONCURRENCY CONTROL end

	// Method wide routers if applicable 
	// Method wide routers END 

	// Failing
	// Class wide failing strategy 
	private FailingStrategy clazzWideFailingStrategy = Defaults.getDefaultFailingStrategy();

	private FailingStrategy echoFailingStrategy_longaValue = clazzWideFailingStrategy;
	private FailingStrategy echoObjectParam1FailingStrategy_javalangObjectaValue = clazzWideFailingStrategy;
	private FailingStrategy echoObjectParamFailingStrategy_orgdistributemetestjsonrpcAaValue = clazzWideFailingStrategy;
	private FailingStrategy echoManyParamsFailingStrategy_intind_javalangStringstrNum_javalangLongincremt = clazzWideFailingStrategy;
	// Failing end

	private ServiceDescriptor lookupDescriptor;
	private AtomicInteger requestID = new AtomicInteger(0);

	public ClientEchoServiceImpl(org.distributeme.test.jsonrpc.EchoService anImplementation){
		created = System.currentTimeMillis();
		implementation = anImplementation;
		discoveryMode = DiscoveryMode.AUTO;
	}

	private ClientEchoServiceImpl(final ServiceDescriptor lookupDescriptor){
		this.lookupDescriptor = lookupDescriptor;
	}
	public List echo(long aValue, Map __transportableCallContext) throws org.distributeme.test.jsonrpc.EchoServiceException{
		ArrayNode jsonNode = JsonNodeFactory.instance.arrayNode();
		ObjectMapper m = new ObjectMapper();
		jsonNode.add(aValue);
		jsonNode.add(m.valueToTree(__transportableCallContext));
		final List retList = new ArrayList();
		try {
		String response = callHttpClient("echo", jsonNode);
		if(response == null) return null;
		final List tempRetList = (List) JsonTransformer.toObject(response, List.class);
		retList.add(Long.valueOf(String.valueOf(tempRetList.get(0))));
		retList.add(tempRetList.get(1));
		return retList;
		} catch (IOException e) {
			log.error("echo()", e);
			throw new RuntimeException(e);
		}
	}

	public List echoObjectParam1(java.lang.Object aValue, Map __transportableCallContext) throws org.distributeme.test.jsonrpc.EchoServiceException{
		ArrayNode jsonNode = JsonNodeFactory.instance.arrayNode();
		ObjectMapper m = new ObjectMapper();
		jsonNode.add(m.valueToTree(aValue));
		jsonNode.add(m.valueToTree(__transportableCallContext));
		final List retList = new ArrayList();
		try {
		String response = callHttpClient("echoObjectParam1", jsonNode);
		if(response == null) return null;
		final List tempRetList = (List) JsonTransformer.toObject(response, List.class);
		JsonNode arrayNode = m.readTree(response);
		retList.add(m.readValue(arrayNode.get(0), java.lang.Object.class));
		retList.add(tempRetList.get(1));
		return retList;
		} catch (IOException e) {
			log.error("echoObjectParam1()", e);
			throw new RuntimeException(e);
		}
	}

	public List echoObjectParam(org.distributeme.test.jsonrpc.A aValue, Map __transportableCallContext) throws org.distributeme.test.jsonrpc.EchoServiceException{
		ArrayNode jsonNode = JsonNodeFactory.instance.arrayNode();
		ObjectMapper m = new ObjectMapper();
		jsonNode.add(m.valueToTree(aValue));
		jsonNode.add(m.valueToTree(__transportableCallContext));
		final List retList = new ArrayList();
		try {
		String response = callHttpClient("echoObjectParam", jsonNode);
		if(response == null) return null;
		final List tempRetList = (List) JsonTransformer.toObject(response, List.class);
		JsonNode arrayNode = m.readTree(response);
		retList.add(m.readValue(arrayNode.get(0), org.distributeme.test.jsonrpc.A.class));
		retList.add(tempRetList.get(1));
		return retList;
		} catch (IOException e) {
			log.error("echoObjectParam()", e);
			throw new RuntimeException(e);
		}
	}

	public List echoManyParams(int ind, java.lang.String strNum, java.lang.Long incremt, Map __transportableCallContext) throws org.distributeme.test.jsonrpc.EchoServiceException, java.io.IOException{
		ArrayNode jsonNode = JsonNodeFactory.instance.arrayNode();
		ObjectMapper m = new ObjectMapper();
		jsonNode.add(ind);
		jsonNode.add(m.valueToTree(strNum));
		jsonNode.add(m.valueToTree(incremt));
		jsonNode.add(m.valueToTree(__transportableCallContext));
		final List retList = new ArrayList();
		try {
		String response = callHttpClient("echoManyParams", jsonNode);
		if(response == null) return null;
		final List tempRetList = (List) JsonTransformer.toObject(response, List.class);
		JsonNode arrayNode = m.readTree(response);
		retList.add(m.readValue(arrayNode.get(0), org.distributeme.test.jsonrpc.A.class));
		retList.add(tempRetList.get(1));
		return retList;
		} catch (IOException e) {
			log.error("echoManyParams()", e);
			throw new RuntimeException(e);
		}
	}


	public long echo(long aValue) throws org.distributeme.test.jsonrpc.EchoServiceException{
		return echo(aValue, (ClientSideCallContext)null);
	} //...public long echo(long aValue) throws org.distributeme.test.jsonrpc.EchoServiceException

	public java.lang.Object echoObjectParam1(java.lang.Object aValue) throws org.distributeme.test.jsonrpc.EchoServiceException{
		return echoObjectParam1(aValue, (ClientSideCallContext)null);
	} //...public java.lang.Object echoObjectParam1(java.lang.Object aValue) throws org.distributeme.test.jsonrpc.EchoServiceException

	public org.distributeme.test.jsonrpc.A echoObjectParam(org.distributeme.test.jsonrpc.A aValue) throws org.distributeme.test.jsonrpc.EchoServiceException{
		return echoObjectParam(aValue, (ClientSideCallContext)null);
	} //...public org.distributeme.test.jsonrpc.A echoObjectParam(org.distributeme.test.jsonrpc.A aValue) throws org.distributeme.test.jsonrpc.EchoServiceException

	public org.distributeme.test.jsonrpc.A echoManyParams(int ind, java.lang.String strNum, java.lang.Long incremt) throws org.distributeme.test.jsonrpc.EchoServiceException, java.io.IOException{
		return echoManyParams(ind, strNum, incremt, (ClientSideCallContext)null);
	} //...public org.distributeme.test.jsonrpc.A echoManyParams(int ind, java.lang.String strNum, java.lang.Long incremt) throws org.distributeme.test.jsonrpc.EchoServiceException, java.io.IOException


	public long echo(long aValue, org.distributeme.core.ClientSideCallContext diMeCallContext) throws org.distributeme.test.jsonrpc.EchoServiceException{
		List __fromServerSide = null;;
		Exception exceptionInMethod = null;
		if (diMeCallContext == null)
			diMeCallContext = new ClientSideCallContext("echo");
		if (discoveryMode==DiscoveryMode.AUTO && diMeCallContext.getServiceId()==null)
			diMeCallContext.setServiceId(EchoServiceConstants.getServiceId());

		HashMap __transportableCallContext = diMeCallContext.getTransportableCallContext();
		// Initialize interceptors
		List diMeInterceptors = InterceptorRegistry.getInstance().getClientSideRequestInterceptors();
		InterceptionContext diMeInterceptionContext = new InterceptionContext();
		// Concurrency control, client side - start
		echoCCStrategy_longaValue.notifyClientSideCallStarted(diMeCallContext);

		ArrayList diMeParameters = new ArrayList();
		diMeParameters.add(aValue);
		diMeCallContext.setParameters(diMeParameters);
		try{
			diMeInterceptionContext.setCurrentPhase(InterceptionPhase.BEFORE_SERVICE_CALL);
			for (ClientSideRequestInterceptor interceptor : diMeInterceptors){
				InterceptorResponse interceptorResponse = interceptor.beforeServiceCall(diMeCallContext, diMeInterceptionContext);
				switch(interceptorResponse.getCommand()){
				case ABORT:
					if (interceptorResponse.getException() instanceof RuntimeException)
						throw (RuntimeException) interceptorResponse.getException();
					if (interceptorResponse.getException() instanceof org.distributeme.test.jsonrpc.EchoServiceException)
						throw (org.distributeme.test.jsonrpc.EchoServiceException) interceptorResponse.getException();
					throw new RuntimeException("Interceptor exception",interceptorResponse.getException());
				case RETURN:
					return ((Long)interceptorResponse.getReturnValue()).longValue();
				case CONTINUE:
					break;
				default:
					throw new IllegalStateException("Unsupported or unexpected command from interceptor " + interceptorResponse.getCommand()+ " in phase:"+diMeInterceptionContext.getCurrentPhase());
				} //...switch
			} //...for
			// Reparse parameters in case an interceptor modified them
			aValue = ((Long)diMeParameters.get(0)).longValue();
			__fromServerSide = getDelegate(diMeCallContext.getServiceId()).echo(aValue,  __transportableCallContext);
			__transportableCallContext.putAll(((HashMap)__fromServerSide.get(1)));
			return ((Long)__fromServerSide.get(0)).longValue();
		} catch(NoConnectionToServerException e) {
			exceptionInMethod = e;
		} finally {
		// Concurrency control, client side - end
			echoCCStrategy_longaValue.notifyClientSideCallFinished(diMeCallContext);
		diMeInterceptionContext.setCurrentPhase(InterceptionPhase.AFTER_SERVICE_CALL);
		if (__fromServerSide!=null){
			diMeInterceptionContext.setReturnValue(__fromServerSide.get(0));
		}
		diMeInterceptionContext.setException(exceptionInMethod);
		boolean diMeReturnOverriden = false;
		for (ClientSideRequestInterceptor interceptor : diMeInterceptors){
			InterceptorResponse interceptorResponse = interceptor.afterServiceCall(diMeCallContext, diMeInterceptionContext);
			switch(interceptorResponse.getCommand()){
			case ABORT:
				if (interceptorResponse.getException() instanceof RuntimeException)
					throw (RuntimeException) interceptorResponse.getException();
				if (interceptorResponse.getException() instanceof org.distributeme.test.jsonrpc.EchoServiceException)
					throw (org.distributeme.test.jsonrpc.EchoServiceException) interceptorResponse.getException();
				throw new RuntimeException("Interceptor exception",interceptorResponse.getException());
			case RETURN:
				return ((Long)interceptorResponse.getReturnValue()).longValue();
			case OVERWRITE_RETURN_AND_CONTINUE:
				__fromServerSide.set(0, interceptorResponse.getReturnValue());
				diMeInterceptionContext.setReturnValue(interceptorResponse.getReturnValue());
				diMeReturnOverriden = true;
				break;
			case CONTINUE:
				break;
			default:
				throw new IllegalStateException("Unsupported or unexpected command from interceptor " + interceptorResponse.getCommand()+ " in phase:"+diMeInterceptionContext.getCurrentPhase());
			} //...switch
		} //...for
		if (diMeReturnOverriden)
			return ((Long)__fromServerSide.get(0)).longValue();
		}
		// Failing
		if (exceptionInMethod!=null){
			FailDecision failDecision = echoFailingStrategy_longaValue.callFailed(diMeCallContext);
			if (failDecision.getTargetService()!=null)
				diMeCallContext.setServiceId(failDecision.getTargetService());
			switch(failDecision.getReaction()){
				case RETRY:
					return echo(aValue, diMeCallContext.increaseCallCount());
				case RETRYONCE:
					// Only retry if its the first call
					if (!diMeCallContext.isFirstCall())
						break;
					return echo(aValue, diMeCallContext.increaseCallCount());
				case FAIL:
				default:
				// Fail or default is to do nothing at all and let the request fail
			} //...switch(failDecision)
		}
		// fail through, if we are here, we must have had an exception before.
		throw mapException(exceptionInMethod);
	}

	public java.lang.Object echoObjectParam1(java.lang.Object aValue, org.distributeme.core.ClientSideCallContext diMeCallContext) throws org.distributeme.test.jsonrpc.EchoServiceException{
		List __fromServerSide = null;;
		Exception exceptionInMethod = null;
		if (diMeCallContext == null)
			diMeCallContext = new ClientSideCallContext("echoObjectParam1");
		if (discoveryMode==DiscoveryMode.AUTO && diMeCallContext.getServiceId()==null)
			diMeCallContext.setServiceId(EchoServiceConstants.getServiceId());

		HashMap __transportableCallContext = diMeCallContext.getTransportableCallContext();
		// Initialize interceptors
		List diMeInterceptors = InterceptorRegistry.getInstance().getClientSideRequestInterceptors();
		InterceptionContext diMeInterceptionContext = new InterceptionContext();
		// Concurrency control, client side - start
		echoObjectParam1CCStrategy_javalangObjectaValue.notifyClientSideCallStarted(diMeCallContext);

		ArrayList diMeParameters = new ArrayList();
		diMeParameters.add(aValue);
		diMeCallContext.setParameters(diMeParameters);
		try{
			diMeInterceptionContext.setCurrentPhase(InterceptionPhase.BEFORE_SERVICE_CALL);
			for (ClientSideRequestInterceptor interceptor : diMeInterceptors){
				InterceptorResponse interceptorResponse = interceptor.beforeServiceCall(diMeCallContext, diMeInterceptionContext);
				switch(interceptorResponse.getCommand()){
				case ABORT:
					if (interceptorResponse.getException() instanceof RuntimeException)
						throw (RuntimeException) interceptorResponse.getException();
					if (interceptorResponse.getException() instanceof org.distributeme.test.jsonrpc.EchoServiceException)
						throw (org.distributeme.test.jsonrpc.EchoServiceException) interceptorResponse.getException();
					throw new RuntimeException("Interceptor exception",interceptorResponse.getException());
				case RETURN:
					return (java.lang.Object) interceptorResponse.getReturnValue();
				case CONTINUE:
					break;
				default:
					throw new IllegalStateException("Unsupported or unexpected command from interceptor " + interceptorResponse.getCommand()+ " in phase:"+diMeInterceptionContext.getCurrentPhase());
				} //...switch
			} //...for
			// Reparse parameters in case an interceptor modified them
			aValue = (java.lang.Object) diMeParameters.get(0);
			__fromServerSide = getDelegate(diMeCallContext.getServiceId()).echoObjectParam1(aValue,  __transportableCallContext);
			__transportableCallContext.putAll(((HashMap)__fromServerSide.get(1)));
			return (java.lang.Object) __fromServerSide.get(0);
		} catch(NoConnectionToServerException e) {
			exceptionInMethod = e;
		} finally {
		// Concurrency control, client side - end
			echoObjectParam1CCStrategy_javalangObjectaValue.notifyClientSideCallFinished(diMeCallContext);
		diMeInterceptionContext.setCurrentPhase(InterceptionPhase.AFTER_SERVICE_CALL);
		if (__fromServerSide!=null){
			diMeInterceptionContext.setReturnValue(__fromServerSide.get(0));
		}
		diMeInterceptionContext.setException(exceptionInMethod);
		boolean diMeReturnOverriden = false;
		for (ClientSideRequestInterceptor interceptor : diMeInterceptors){
			InterceptorResponse interceptorResponse = interceptor.afterServiceCall(diMeCallContext, diMeInterceptionContext);
			switch(interceptorResponse.getCommand()){
			case ABORT:
				if (interceptorResponse.getException() instanceof RuntimeException)
					throw (RuntimeException) interceptorResponse.getException();
				if (interceptorResponse.getException() instanceof org.distributeme.test.jsonrpc.EchoServiceException)
					throw (org.distributeme.test.jsonrpc.EchoServiceException) interceptorResponse.getException();
				throw new RuntimeException("Interceptor exception",interceptorResponse.getException());
			case RETURN:
				return (java.lang.Object) interceptorResponse.getReturnValue();
			case OVERWRITE_RETURN_AND_CONTINUE:
				__fromServerSide.set(0, interceptorResponse.getReturnValue());
				diMeInterceptionContext.setReturnValue(interceptorResponse.getReturnValue());
				diMeReturnOverriden = true;
				break;
			case CONTINUE:
				break;
			default:
				throw new IllegalStateException("Unsupported or unexpected command from interceptor " + interceptorResponse.getCommand()+ " in phase:"+diMeInterceptionContext.getCurrentPhase());
			} //...switch
		} //...for
		if (diMeReturnOverriden)
			return (java.lang.Object) __fromServerSide.get(0);
		}
		// Failing
		if (exceptionInMethod!=null){
			FailDecision failDecision = echoObjectParam1FailingStrategy_javalangObjectaValue.callFailed(diMeCallContext);
			if (failDecision.getTargetService()!=null)
				diMeCallContext.setServiceId(failDecision.getTargetService());
			switch(failDecision.getReaction()){
				case RETRY:
					return echoObjectParam1(aValue, diMeCallContext.increaseCallCount());
				case RETRYONCE:
					// Only retry if its the first call
					if (!diMeCallContext.isFirstCall())
						break;
					return echoObjectParam1(aValue, diMeCallContext.increaseCallCount());
				case FAIL:
				default:
				// Fail or default is to do nothing at all and let the request fail
			} //...switch(failDecision)
		}
		// fail through, if we are here, we must have had an exception before.
		throw mapException(exceptionInMethod);
	}

	public org.distributeme.test.jsonrpc.A echoObjectParam(org.distributeme.test.jsonrpc.A aValue, org.distributeme.core.ClientSideCallContext diMeCallContext) throws org.distributeme.test.jsonrpc.EchoServiceException{
		List __fromServerSide = null;;
		Exception exceptionInMethod = null;
		if (diMeCallContext == null)
			diMeCallContext = new ClientSideCallContext("echoObjectParam");
		if (discoveryMode==DiscoveryMode.AUTO && diMeCallContext.getServiceId()==null)
			diMeCallContext.setServiceId(EchoServiceConstants.getServiceId());

		HashMap __transportableCallContext = diMeCallContext.getTransportableCallContext();
		// Initialize interceptors
		List diMeInterceptors = InterceptorRegistry.getInstance().getClientSideRequestInterceptors();
		InterceptionContext diMeInterceptionContext = new InterceptionContext();
		// Concurrency control, client side - start
		echoObjectParamCCStrategy_orgdistributemetestjsonrpcAaValue.notifyClientSideCallStarted(diMeCallContext);

		ArrayList diMeParameters = new ArrayList();
		diMeParameters.add(aValue);
		diMeCallContext.setParameters(diMeParameters);
		try{
			diMeInterceptionContext.setCurrentPhase(InterceptionPhase.BEFORE_SERVICE_CALL);
			for (ClientSideRequestInterceptor interceptor : diMeInterceptors){
				InterceptorResponse interceptorResponse = interceptor.beforeServiceCall(diMeCallContext, diMeInterceptionContext);
				switch(interceptorResponse.getCommand()){
				case ABORT:
					if (interceptorResponse.getException() instanceof RuntimeException)
						throw (RuntimeException) interceptorResponse.getException();
					if (interceptorResponse.getException() instanceof org.distributeme.test.jsonrpc.EchoServiceException)
						throw (org.distributeme.test.jsonrpc.EchoServiceException) interceptorResponse.getException();
					throw new RuntimeException("Interceptor exception",interceptorResponse.getException());
				case RETURN:
					return (org.distributeme.test.jsonrpc.A) interceptorResponse.getReturnValue();
				case CONTINUE:
					break;
				default:
					throw new IllegalStateException("Unsupported or unexpected command from interceptor " + interceptorResponse.getCommand()+ " in phase:"+diMeInterceptionContext.getCurrentPhase());
				} //...switch
			} //...for
			// Reparse parameters in case an interceptor modified them
			aValue = (org.distributeme.test.jsonrpc.A) diMeParameters.get(0);
			__fromServerSide = getDelegate(diMeCallContext.getServiceId()).echoObjectParam(aValue,  __transportableCallContext);
			__transportableCallContext.putAll(((HashMap)__fromServerSide.get(1)));
			return (org.distributeme.test.jsonrpc.A) __fromServerSide.get(0);
		} catch(NoConnectionToServerException e) {
			exceptionInMethod = e;
		} finally {
		// Concurrency control, client side - end
			echoObjectParamCCStrategy_orgdistributemetestjsonrpcAaValue.notifyClientSideCallFinished(diMeCallContext);
		diMeInterceptionContext.setCurrentPhase(InterceptionPhase.AFTER_SERVICE_CALL);
		if (__fromServerSide!=null){
			diMeInterceptionContext.setReturnValue(__fromServerSide.get(0));
		}
		diMeInterceptionContext.setException(exceptionInMethod);
		boolean diMeReturnOverriden = false;
		for (ClientSideRequestInterceptor interceptor : diMeInterceptors){
			InterceptorResponse interceptorResponse = interceptor.afterServiceCall(diMeCallContext, diMeInterceptionContext);
			switch(interceptorResponse.getCommand()){
			case ABORT:
				if (interceptorResponse.getException() instanceof RuntimeException)
					throw (RuntimeException) interceptorResponse.getException();
				if (interceptorResponse.getException() instanceof org.distributeme.test.jsonrpc.EchoServiceException)
					throw (org.distributeme.test.jsonrpc.EchoServiceException) interceptorResponse.getException();
				throw new RuntimeException("Interceptor exception",interceptorResponse.getException());
			case RETURN:
				return (org.distributeme.test.jsonrpc.A) interceptorResponse.getReturnValue();
			case OVERWRITE_RETURN_AND_CONTINUE:
				__fromServerSide.set(0, interceptorResponse.getReturnValue());
				diMeInterceptionContext.setReturnValue(interceptorResponse.getReturnValue());
				diMeReturnOverriden = true;
				break;
			case CONTINUE:
				break;
			default:
				throw new IllegalStateException("Unsupported or unexpected command from interceptor " + interceptorResponse.getCommand()+ " in phase:"+diMeInterceptionContext.getCurrentPhase());
			} //...switch
		} //...for
		if (diMeReturnOverriden)
			return (org.distributeme.test.jsonrpc.A) __fromServerSide.get(0);
		}
		// Failing
		if (exceptionInMethod!=null){
			FailDecision failDecision = echoObjectParamFailingStrategy_orgdistributemetestjsonrpcAaValue.callFailed(diMeCallContext);
			if (failDecision.getTargetService()!=null)
				diMeCallContext.setServiceId(failDecision.getTargetService());
			switch(failDecision.getReaction()){
				case RETRY:
					return echoObjectParam(aValue, diMeCallContext.increaseCallCount());
				case RETRYONCE:
					// Only retry if its the first call
					if (!diMeCallContext.isFirstCall())
						break;
					return echoObjectParam(aValue, diMeCallContext.increaseCallCount());
				case FAIL:
				default:
				// Fail or default is to do nothing at all and let the request fail
			} //...switch(failDecision)
		}
		// fail through, if we are here, we must have had an exception before.
		throw mapException(exceptionInMethod);
	}

	public org.distributeme.test.jsonrpc.A echoManyParams(int ind, java.lang.String strNum, java.lang.Long incremt, org.distributeme.core.ClientSideCallContext diMeCallContext) throws org.distributeme.test.jsonrpc.EchoServiceException, java.io.IOException{
		List __fromServerSide = null;;
		Exception exceptionInMethod = null;
		if (diMeCallContext == null)
			diMeCallContext = new ClientSideCallContext("echoManyParams");
		if (discoveryMode==DiscoveryMode.AUTO && diMeCallContext.getServiceId()==null)
			diMeCallContext.setServiceId(EchoServiceConstants.getServiceId());

		HashMap __transportableCallContext = diMeCallContext.getTransportableCallContext();
		// Initialize interceptors
		List diMeInterceptors = InterceptorRegistry.getInstance().getClientSideRequestInterceptors();
		InterceptionContext diMeInterceptionContext = new InterceptionContext();
		// Concurrency control, client side - start
		echoManyParamsCCStrategy_intind_javalangStringstrNum_javalangLongincremt.notifyClientSideCallStarted(diMeCallContext);

		ArrayList diMeParameters = new ArrayList();
		diMeParameters.add(ind);
		diMeParameters.add(strNum);
		diMeParameters.add(incremt);
		diMeCallContext.setParameters(diMeParameters);
		try{
			diMeInterceptionContext.setCurrentPhase(InterceptionPhase.BEFORE_SERVICE_CALL);
			for (ClientSideRequestInterceptor interceptor : diMeInterceptors){
				InterceptorResponse interceptorResponse = interceptor.beforeServiceCall(diMeCallContext, diMeInterceptionContext);
				switch(interceptorResponse.getCommand()){
				case ABORT:
					if (interceptorResponse.getException() instanceof RuntimeException)
						throw (RuntimeException) interceptorResponse.getException();
					if (interceptorResponse.getException() instanceof org.distributeme.test.jsonrpc.EchoServiceException)
						throw (org.distributeme.test.jsonrpc.EchoServiceException) interceptorResponse.getException();
					if (interceptorResponse.getException() instanceof java.io.IOException)
						throw (java.io.IOException) interceptorResponse.getException();
					throw new RuntimeException("Interceptor exception",interceptorResponse.getException());
				case RETURN:
					return (org.distributeme.test.jsonrpc.A) interceptorResponse.getReturnValue();
				case CONTINUE:
					break;
				default:
					throw new IllegalStateException("Unsupported or unexpected command from interceptor " + interceptorResponse.getCommand()+ " in phase:"+diMeInterceptionContext.getCurrentPhase());
				} //...switch
			} //...for
			// Reparse parameters in case an interceptor modified them
			ind = ((Integer)diMeParameters.get(0)).intValue();
			strNum = (java.lang.String) diMeParameters.get(1);
			incremt = (java.lang.Long) diMeParameters.get(2);
			__fromServerSide = getDelegate(diMeCallContext.getServiceId()).echoManyParams(ind, strNum, incremt,  __transportableCallContext);
			__transportableCallContext.putAll(((HashMap)__fromServerSide.get(1)));
			return (org.distributeme.test.jsonrpc.A) __fromServerSide.get(0);
		} catch(NoConnectionToServerException e) {
			exceptionInMethod = e;
		} finally {
		// Concurrency control, client side - end
			echoManyParamsCCStrategy_intind_javalangStringstrNum_javalangLongincremt.notifyClientSideCallFinished(diMeCallContext);
		diMeInterceptionContext.setCurrentPhase(InterceptionPhase.AFTER_SERVICE_CALL);
		if (__fromServerSide!=null){
			diMeInterceptionContext.setReturnValue(__fromServerSide.get(0));
		}
		diMeInterceptionContext.setException(exceptionInMethod);
		boolean diMeReturnOverriden = false;
		for (ClientSideRequestInterceptor interceptor : diMeInterceptors){
			InterceptorResponse interceptorResponse = interceptor.afterServiceCall(diMeCallContext, diMeInterceptionContext);
			switch(interceptorResponse.getCommand()){
			case ABORT:
				if (interceptorResponse.getException() instanceof RuntimeException)
					throw (RuntimeException) interceptorResponse.getException();
				if (interceptorResponse.getException() instanceof org.distributeme.test.jsonrpc.EchoServiceException)
					throw (org.distributeme.test.jsonrpc.EchoServiceException) interceptorResponse.getException();
				if (interceptorResponse.getException() instanceof java.io.IOException)
					throw (java.io.IOException) interceptorResponse.getException();
				throw new RuntimeException("Interceptor exception",interceptorResponse.getException());
			case RETURN:
				return (org.distributeme.test.jsonrpc.A) interceptorResponse.getReturnValue();
			case OVERWRITE_RETURN_AND_CONTINUE:
				__fromServerSide.set(0, interceptorResponse.getReturnValue());
				diMeInterceptionContext.setReturnValue(interceptorResponse.getReturnValue());
				diMeReturnOverriden = true;
				break;
			case CONTINUE:
				break;
			default:
				throw new IllegalStateException("Unsupported or unexpected command from interceptor " + interceptorResponse.getCommand()+ " in phase:"+diMeInterceptionContext.getCurrentPhase());
			} //...switch
		} //...for
		if (diMeReturnOverriden)
			return (org.distributeme.test.jsonrpc.A) __fromServerSide.get(0);
		}
		// Failing
		if (exceptionInMethod!=null){
			FailDecision failDecision = echoManyParamsFailingStrategy_intind_javalangStringstrNum_javalangLongincremt.callFailed(diMeCallContext);
			if (failDecision.getTargetService()!=null)
				diMeCallContext.setServiceId(failDecision.getTargetService());
			switch(failDecision.getReaction()){
				case RETRY:
					return echoManyParams(ind, strNum, incremt, diMeCallContext.increaseCallCount());
				case RETRYONCE:
					// Only retry if its the first call
					if (!diMeCallContext.isFirstCall())
						break;
					return echoManyParams(ind, strNum, incremt, diMeCallContext.increaseCallCount());
				case FAIL:
				default:
				// Fail or default is to do nothing at all and let the request fail
			} //...switch(failDecision)
		}
		// fail through, if we are here, we must have had an exception before.
		throw mapException(exceptionInMethod);
	}


	private void notifyDelegateFailed(){
		notifyDelegateFailed(EchoServiceConstants.getServiceId());
	}

	private void notifyDelegateFailed(String serviceId){
		delegates.remove(serviceId);
	}

	private ClientEchoService getDelegate() throws NoConnectionToServerException{
		return getDelegate(EchoServiceConstants.getServiceId());
	}

	private ClientEchoService getDelegate(String serviceId) throws NoConnectionToServerException{
		// if no serviceid is provided, fallback to default resolve with manual mode
		if (serviceId==null)
			return getDelegate();
		ClientEchoService delegate = delegates.get(serviceId);
		if (delegate==null){
			try{
				delegate = lookup(serviceId);
				delegates.putIfAbsent(serviceId, delegate);
			}catch(Exception e){
			// //TODO - generate and throw typed exception.
				log.error("getDelegate()", e);
				throw new NoConnectionToServerException("Couldn't lookup delegate because: "+e.getMessage()+" at "+RegistryUtil.describeRegistry(), e);
			}//try
		} //...first if (del==null) 
		return delegate;
	} //...fun

	private ClientEchoService lookup(String serviceId) throws NoConnectionToServerException{
		// //first we need to lookup target host.
		ServiceDescriptor toLookup = new ServiceDescriptor(ServiceDescriptor.Protocol.JSONRPC, serviceId);
		ServiceDescriptor targetService = RegistryUtil.resolve(toLookup);
		if (targetService==null)
			throw new RuntimeException("Can't resolve host for an instance of "+EchoServiceConstants.getServiceId());
		return new ClientEchoServiceImpl(targetService);
	}

	// Service adapter methods
	public long getCreationTimestamp(){ return created; }
	public long getLastAccessTimestamp(){ return lastAccess; }
	public EchoService getImplementation() { return implementation; }
	private String callHttpClient(final String methodName, final JsonNode paramNode) {
		if(client ==null) {
			client = new HttpClient();
			client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
			if(System.getProperty("distributeme.json.ssl") != null) {
				client.setKeyStoreLocation(System.getProperty("distributeme.json.ssl.keystore"));
				client.setKeyStorePassword(System.getProperty("distributeme.json.ssl.password"));
				client.setKeyManagerPassword(System.getProperty("distributeme.json.ssl.keyPassword"));
				if(System.getProperty("distributeme.json.ssl.truststore") != null) {
					client.setTrustStoreLocation(System.getProperty("distributeme.json.ssl.truststore"));
				}
				if(System.getProperty("distributeme.json.ssl.trustPassword") != null) {
					client.setTrustStorePassword(System.getProperty("distributeme.json.ssl.trustPassword"));
				}
			}
			try {
				client.start();
			} catch (Exception e) {
				log.error("callHttpClient()", e);
				throw new RuntimeException(e);
			}
		}
		final ContentExchange exchange = new ContentExchange();
		exchange.setMethod("POST");
		if(System.getProperty("distributeme.json.ssl") == null) {
		exchange.setURL("http://" + lookupDescriptor.getHost() + ":" + lookupDescriptor.getPort() + "/servlets/" + lookupDescriptor.getInstanceId());
		} else {
		exchange.setURL("https://" + lookupDescriptor.getHost() + ":" + lookupDescriptor.getPort() + "/servlets/" + lookupDescriptor.getInstanceId());
		}
		JsonRpcRequest jsonRpcRequest = new JsonRpcRequest();
		jsonRpcRequest.setId(requestID.incrementAndGet() + "");
		jsonRpcRequest.setJsonrpc("2.0");
		jsonRpcRequest.setMethod(methodName);
		jsonRpcRequest.setParams(paramNode);
		exchange.setRequestContent(new ByteArrayBuffer(JsonTransformer.toJson(jsonRpcRequest)));
		try {
			if (log.isDebugEnabled()) {
				log.debug("client: send request to server: " + exchange.toString());
			}
			client.send(exchange);
			exchange.waitForDone();
			if(exchange.getResponseContent().contains("error")) {
				JsonRpcErrorResponse jsonRpcErrorResponse = (JsonRpcErrorResponse) JsonTransformer.toObject(exchange.getResponseContent(), JsonRpcErrorResponse.class);
				throw new RuntimeException("callHttpClient(" + methodName + " , " + paramNode + ") "  + jsonRpcErrorResponse.getError());
			}
			JsonRpcNormalResponse jsonRpcResponse = (JsonRpcNormalResponse) JsonTransformer.toObject(exchange.getResponseContent(), JsonRpcNormalResponse.class);
			if (log.isDebugEnabled()) {
				log.debug("client: send request to server: " + exchange.toString());
			}
			if(jsonRpcResponse.getResult() == null) {
				log.debug("client: received response from server: jsonRpcResponse.getResult() == null.") ;
				return null;
			}
			if (log.isDebugEnabled()) {
				log.debug("client: client: received response from server: " + jsonRpcResponse.getResult().toString());
			}
			return jsonRpcResponse.getResult().toString();
		} catch (IOException e) {
			log.error("callHttpClient()", e);
			throw new RuntimeException(e);
		} catch (InterruptedException e) {
			log.error("callHttpClient()", e);
			throw new RuntimeException(e);
		}
	}


	private DistributemeRuntimeException mapException(Exception in){
		if (in instanceof DistributemeRuntimeException)
			return (DistributemeRuntimeException) in;
		return new ServiceUnavailableException("Unexpected exception: "+in.getMessage()+" " + in.getClass().getName(), in);
	}
}