/**
 * Copyright 2015 Palantir Technologies
 *
 * Licensed under the BSD-3 License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://opensource.org/licenses/BSD-3-Clause
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.palantir.atlasdb.keyvalue.remoting.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import com.palantir.atlasdb.keyvalue.api.KeyValueService;
import com.palantir.atlasdb.keyvalue.partition.endpoint.KeyValueEndpoint;
import com.palantir.atlasdb.keyvalue.partition.exception.ClientVersionTooOldException;

/**
 * This is to inject remote partition map service URI to a VersionTooOldException. New partition map
 * can be downloaded from that service.
 *
 * It is meant to be used by <code>KeyValueEndpoint</code>, because it has both KeyValueService reference
 * and the URI of the corresponding PartitionMapService.
 *
 * @see KeyValueEndpoint
 * @see KeyValueService
 *
 * @author htarasiuk
 *
 */
public class FillInUrlProxy implements InvocationHandler {

    final KeyValueService remoteKvs;
    final String pmsUri;

    private FillInUrlProxy(KeyValueService delegate,
                           String pmsUri) {
        this.remoteKvs = delegate;
        this.pmsUri = pmsUri;
    }

    public static KeyValueService newFillInUrlProxy(KeyValueService delegate, String pmsUri) {
        FillInUrlProxy handler = new FillInUrlProxy(delegate, pmsUri);
        return (KeyValueService) Proxy.newProxyInstance( KeyValueService.class.getClassLoader(),
                new Class<?>[] { KeyValueService.class }, handler);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        try {
            return method.invoke(remoteKvs, args);
        } catch (InvocationTargetException e) {
            Throwable cause = e.getCause();
            if (cause instanceof ClientVersionTooOldException) {
            	throw new ClientVersionTooOldException(pmsUri);
            }
            throw cause;
        }
    }

}
@@ -1017,16 +1017,105 @@
 ception;
+%0Aimport com.palantir.atlasdb.keyvalue.partition.exception.EndpointVersionTooOldException;
 %0A%0A/**%0A *
@@ -2527,24 +2527,171 @@
           %7D%0A
+            if (cause instanceof EndpointVersionTooOldException) %7B%0A                throw new EndpointVersionTooOldException(pmsUri);%0A            %7D%0A
             

Update the FillInUrlProxy to support EndpointVersionTooOldException.