Skip to content

Instantly share code, notes, and snippets.

@jesboat
Created April 3, 2014 02:13
Show Gist options
  • Save jesboat/9947125 to your computer and use it in GitHub Desktop.
Save jesboat/9947125 to your computer and use it in GitHub Desktop.
Sample of using `java.lang.reflect.Proxy`
package scratch;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
public class ProxySample {
public interface Iface {
public String uppercase(String forWho, String value);
public int add1(String forwho, int value);
}
public static class LocalImpl implements Iface {
@Override
public String uppercase(String forWho, String value) {
System.out.println("Local impl uppercasing " + value);
return value.toUpperCase();
}
@Override
public int add1(String forwho, int value) {
System.out.println("Local impl adding one to " + value);
return value + 1;
}
}
public static class RemoteImpl implements Iface {
@Override
public String uppercase(String forWho, String value) {
System.out.println("Pretend I'm remote machine; uppercasing " + value);
return value.toUpperCase();
}
@Override
public int add1(String forwho, int value) {
System.out.println("Pretend I'm a remote machine; adding one to " + value);
return value + 1;
}
}
public static Iface getSuitableServerFor(String email) {
return new RemoteImpl();
}
public static Iface makeDistributed(final Iface local) {
return (Iface) Proxy.newProxyInstance(
Iface.class.getClassLoader(),
new Class<?>[] { Iface.class },
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (args.length < 1 || ! (args[0] instanceof String))
throw new IllegalArgumentException("String email required as first argument");
String email = (String) args[0];
Iface target;
if (email.contains("remote")) {
target = getSuitableServerFor(email);
} else {
target = local;
}
try {
return method.invoke(target, args);
} catch (InvocationTargetException e) {
// The implementing method threw an exception; propagate the original
throw e.getCause();
} catch (IllegalAccessException e) {
throw new RuntimeException("Reflection failed?", e);
}
}
}
);
}
public static void main (String[] args) {
Iface local = new LocalImpl();
Iface distributed = makeDistributed(local);
// start serving `distributed`
for (String who : Arrays.asList("[email protected]", "[email protected]")) {
System.out.println("42 + 1 = " + distributed.add1(who, 42));
System.out.println("blurp: " + distributed.uppercase(who, "blurp"));
}
}
}
@jesboat
Copy link
Author

jesboat commented Apr 3, 2014

Expected output:

Local impl adding one to 42
42 + 1 =  43
Local impl uppercasing blurp
blurp:    BLURP
Pretend I'm a thrift client; adding one to 42
42 + 1 =  43
Pretend I'm a thrift client; uppercasing blurp
blurp:    BLURP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment