Author: athimel Date: 2011-01-12 10:38:21 +0100 (Wed, 12 Jan 2011) New Revision: 458 Url: http://nuiton.org/repositories/revision/sandbox/458 Log: Implement a client side proxy Added: testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/RemoteProxyFactory.java testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/ServiceExporterMain.java Removed: testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIImpl.java testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIInterface.java testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/Client.java Modified: testStandaloneRmi/src/main/java/org/nuiton/sandbox/business/ServiceImpl.java testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceExporter.java testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/ClientExporter.java Modified: testStandaloneRmi/src/main/java/org/nuiton/sandbox/business/ServiceImpl.java =================================================================== --- testStandaloneRmi/src/main/java/org/nuiton/sandbox/business/ServiceImpl.java 2011-01-12 09:12:32 UTC (rev 457) +++ testStandaloneRmi/src/main/java/org/nuiton/sandbox/business/ServiceImpl.java 2011-01-12 09:38:21 UTC (rev 458) @@ -5,10 +5,19 @@ */ public class ServiceImpl implements ServiceInterface { + protected static final int number = (int)(Math.random() * 100); + @Override public String testExchange(String whoIsCalling, Integer intValue) { - System.out.println(String.format("Incoming call from %s with value: (%d) - Here I'm (%s)", whoIsCalling, intValue, System.getProperty("java.rmi.server.hostname"))); - String result = String.format("Hey %s, what is this fucking number '%d' ?", whoIsCalling, intValue); + System.out.println(String.format("Incoming call from %s with value: (%d). To guess: (%d)", whoIsCalling, intValue, number)); + String result; + if (intValue == number) { + result = String.format("Congrats %s, you found the correct number : '%d' !", whoIsCalling, intValue); + } else if (intValue < number) { + result = String.format("%s, your number is too low : '%d'", whoIsCalling, intValue); + } else { + result = String.format("%s, your number is too high : '%d'", whoIsCalling, intValue); + } return result; } Added: testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/RemoteProxyFactory.java =================================================================== --- testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/RemoteProxyFactory.java (rev 0) +++ testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/RemoteProxyFactory.java 2011-01-12 09:38:21 UTC (rev 458) @@ -0,0 +1,45 @@ +package org.nuiton.sandbox.rmi; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.rmi.Remote; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; + +/** + * @author Arnaud Thimel <thimel@codelutin.com> + */ +public class RemoteProxyFactory { + + protected final static int PORT = 12345; + + protected static <T> T createProxy(final Class<T> serviceInterface) throws Exception { + String rmiName = serviceInterface.getName(); + T result = createProxy(rmiName, serviceInterface); + return result; + } + + protected static <T> T createProxy(String rmiName, final Class<T> serviceInterface) throws Exception { + + Registry registry = LocateRegistry.getRegistry("10.1.1.85", PORT); + final RemoteMethodExecutor stub = (RemoteMethodExecutor)registry.lookup(rmiName); + + InvocationHandler handler = new InvocationHandler() { + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + + String methodName = method.getName(); + Class<?>[] parametersType = method.getParameterTypes(); + + Object result = stub.execute(methodName, parametersType, args); + + return result; + } + }; + T proxy = (T) Proxy.newProxyInstance(ServiceExporter.class.getClassLoader(), new Class<?>[]{serviceInterface}, handler); + return proxy; + } + + +} Modified: testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceExporter.java =================================================================== --- testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceExporter.java 2011-01-12 09:12:32 UTC (rev 457) +++ testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceExporter.java 2011-01-12 09:38:21 UTC (rev 458) @@ -40,8 +40,13 @@ return result; } - public <E> void registerService(E instance, String name) throws RemoteException { + public <E> void registerService(Class<E> serviceInterface, E instance) throws RemoteException { + String rmiName = serviceInterface.getName(); + registerService(rmiName, instance); + } + public <E> void registerService(String rmiName, E instance) throws RemoteException { + testRmiConfig(); // Create the proxy and let him be a stub @@ -50,12 +55,7 @@ // Bind into the registry Registry registry = getRegistry(); - registry.rebind(name, stub); + registry.rebind(rmiName, stub); } - public static void main(String[] args) throws Exception { - new ServiceExporter().registerService(new ServiceImpl(), "toto"); - System.err.println("Service2 ready"); - } - } Deleted: testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIImpl.java =================================================================== --- testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIImpl.java 2011-01-12 09:12:32 UTC (rev 457) +++ testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIImpl.java 2011-01-12 09:38:21 UTC (rev 458) @@ -1,58 +0,0 @@ -package org.nuiton.sandbox.rmi; - -import java.rmi.ConnectException; -import java.rmi.Remote; -import java.rmi.RemoteException; -import java.rmi.registry.LocateRegistry; -import java.rmi.registry.Registry; -import java.rmi.server.UnicastRemoteObject; - -/** - * @author Arnaud Thimel <thimel@codelutin.com> - */ -public class ServiceRMIImpl implements ServiceRMIInterface { - - private static final int PORT = 12345; - - public ServiceRMIImpl() { - } - - public static void main(String[] args) throws Exception { - - testRmiConfig(); - - ServiceRMIImpl impl = new ServiceRMIImpl(); - Remote stub = UnicastRemoteObject.exportObject(impl, 0); - - Registry registry = getRegistry(); - registry.bind("MyService", stub); - - System.err.println("Service ready"); - } - - protected static void testRmiConfig() { - String rmiHost = System.getProperty("java.rmi.server.hostname"); - if (rmiHost == null || "".equals(rmiHost.trim())) { - System.err.println("Warning, server might not have been initialized properly, please specify '-Djava.rmi.server.hostname=<IP-address>'"); - } - } - - protected static Registry getRegistry() throws RemoteException { - Registry result; - try { - result = LocateRegistry.getRegistry(PORT); - result.list(); // To test that registry has been created. Exception will be thrown is registry cannot be called - } catch (ConnectException ce) { - System.err.println("Registry not found, creating a new one"); - result = LocateRegistry.createRegistry(PORT); - } - return result; - } - - @Override - public String testExchange(String whoIsCalling, Integer intValue) throws RemoteException { - System.out.println(String.format("Incoming call from %s with value: (%d) - Here I'm (%s)", whoIsCalling, intValue, System.getProperty("java.rmi.server.hostname"))); - return String.format("Hey %s, what is this fucking number '%d' ?", whoIsCalling, intValue); - } - -} Deleted: testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIInterface.java =================================================================== --- testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIInterface.java 2011-01-12 09:12:32 UTC (rev 457) +++ testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIInterface.java 2011-01-12 09:38:21 UTC (rev 458) @@ -1,15 +0,0 @@ -package org.nuiton.sandbox.rmi; - -import org.nuiton.sandbox.business.ServiceInterface; - -import java.rmi.Remote; -import java.rmi.RemoteException; - -/** - * @author Arnaud Thimel <thimel@codelutin.com> - */ -public interface ServiceRMIInterface extends Remote { - - public abstract String testExchange(String whoIsCalling, Integer intValue) throws RemoteException; - -} Deleted: testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/Client.java =================================================================== --- testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/Client.java 2011-01-12 09:12:32 UTC (rev 457) +++ testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/Client.java 2011-01-12 09:38:21 UTC (rev 458) @@ -1,19 +0,0 @@ -package org.nuiton.sandbox.rmi; - -import java.rmi.registry.LocateRegistry; -import java.rmi.registry.Registry; - -/** - * @author Arnaud Thimel <thimel@codelutin.com> - */ -public class Client { - - private static final int PORT = 12345; - public static void main(String[] args) throws Exception { - Registry registry = LocateRegistry.getRegistry("10.1.1.85", PORT); - ServiceRMIInterface stub = (ServiceRMIInterface) registry.lookup("MyService"); - String response = stub.testExchange(System.getProperty("user.name"), 123); - System.out.println("response: " + response); - } - -} Modified: testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/ClientExporter.java =================================================================== --- testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/ClientExporter.java 2011-01-12 09:12:32 UTC (rev 457) +++ testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/ClientExporter.java 2011-01-12 09:38:21 UTC (rev 458) @@ -16,12 +16,9 @@ */ public class ClientExporter { - private static final int PORT = 12345; public static void main(String[] args) throws Throwable { - Registry registry = LocateRegistry.getRegistry("10.1.1.85", PORT); - RemoteMethodExecutor stub = (RemoteMethodExecutor)registry.lookup("toto"); - - Object result = stub.execute("testExchange", new Class<?>[]{String.class, Integer.class}, new Object[] {System.getProperty("user.name"), 123}); + ServiceInterface service = RemoteProxyFactory.createProxy(ServiceInterface.class); + String result = service.testExchange(System.getProperty("user.name"), 50); System.out.println(result); } Added: testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/ServiceExporterMain.java =================================================================== --- testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/ServiceExporterMain.java (rev 0) +++ testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/ServiceExporterMain.java 2011-01-12 09:38:21 UTC (rev 458) @@ -0,0 +1,16 @@ +package org.nuiton.sandbox.rmi; + +import org.nuiton.sandbox.business.ServiceImpl; +import org.nuiton.sandbox.business.ServiceInterface; + +/** + * @author Arnaud Thimel <thimel@codelutin.com> + */ +public class ServiceExporterMain { + + public static void main(String[] args) throws Exception { + new ServiceExporter().registerService(ServiceInterface.class, new ServiceImpl()); + System.err.println("Service is ready, waiting for incoming calls"); + } + +}