r457 - in testStandaloneRmi/src: main/java/org/nuiton/sandbox/business main/java/org/nuiton/sandbox/rmi test/java/org/nuiton/sandbox/rmi
Author: athimel Date: 2011-01-12 10:12:32 +0100 (Wed, 12 Jan 2011) New Revision: 457 Url: http://nuiton.org/repositories/revision/sandbox/457 Log: First Exporter implem Added: testStandaloneRmi/src/main/java/org/nuiton/sandbox/business/ServiceImpl.java testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/RemoteMethodExecutor.java testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/RemoteMethodExecutorImpl.java testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceExporter.java testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIImpl.java testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/ClientExporter.java Removed: testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceImpl.java Modified: testStandaloneRmi/src/main/java/org/nuiton/sandbox/business/ServiceInterface.java testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIInterface.java Added: testStandaloneRmi/src/main/java/org/nuiton/sandbox/business/ServiceImpl.java =================================================================== --- testStandaloneRmi/src/main/java/org/nuiton/sandbox/business/ServiceImpl.java (rev 0) +++ testStandaloneRmi/src/main/java/org/nuiton/sandbox/business/ServiceImpl.java 2011-01-12 09:12:32 UTC (rev 457) @@ -0,0 +1,15 @@ +package org.nuiton.sandbox.business; + +/** + * @author Arnaud Thimel <thimel@codelutin.com> + */ +public class ServiceImpl implements ServiceInterface { + + @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); + return result; + } + +} Modified: testStandaloneRmi/src/main/java/org/nuiton/sandbox/business/ServiceInterface.java =================================================================== --- testStandaloneRmi/src/main/java/org/nuiton/sandbox/business/ServiceInterface.java 2011-01-11 13:05:58 UTC (rev 456) +++ testStandaloneRmi/src/main/java/org/nuiton/sandbox/business/ServiceInterface.java 2011-01-12 09:12:32 UTC (rev 457) @@ -1,7 +1,10 @@ package org.nuiton.sandbox.business; +/** + * @author Arnaud Thimel <thimel@codelutin.com> + */ public interface ServiceInterface { - public abstract String testExchange(Integer intValue); + public abstract String testExchange(String whoIsCalling, Integer intValue); } Added: testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/RemoteMethodExecutor.java =================================================================== --- testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/RemoteMethodExecutor.java (rev 0) +++ testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/RemoteMethodExecutor.java 2011-01-12 09:12:32 UTC (rev 457) @@ -0,0 +1,13 @@ +package org.nuiton.sandbox.rmi; + +import java.rmi.Remote; +import java.rmi.RemoteException; + +/** + * @author Arnaud Thimel <thimel@codelutin.com> + */ +public interface RemoteMethodExecutor extends Remote { + + public abstract Object execute(String methodName, Class<?>[] parametersType, Object[] args) throws RemoteException; + +} Added: testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/RemoteMethodExecutorImpl.java =================================================================== --- testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/RemoteMethodExecutorImpl.java (rev 0) +++ testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/RemoteMethodExecutorImpl.java 2011-01-12 09:12:32 UTC (rev 457) @@ -0,0 +1,32 @@ +package org.nuiton.sandbox.rmi; + +import java.lang.reflect.Method; +import java.rmi.RemoteException; + +/** + * @author Arnaud Thimel <thimel@codelutin.com> + */ +public class RemoteMethodExecutorImpl<T> implements RemoteMethodExecutor { + + protected T service; + + public RemoteMethodExecutorImpl(T service) { + if (service == null) { + throw new NullPointerException("Service cannot be null"); + } + this.service = service; + } + + @Override + public Object execute(String methodName, Class<?>[] parametersType, Object[] args) throws RemoteException { + Object result; + try { + Method method = service.getClass().getMethod(methodName, parametersType); + result = method.invoke(service, args); + } catch (Exception eee) { + throw new RemoteException("Unable to delegate method call to service", eee); + } + return result; + } + +} Added: testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceExporter.java =================================================================== --- testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceExporter.java (rev 0) +++ testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceExporter.java 2011-01-12 09:12:32 UTC (rev 457) @@ -0,0 +1,61 @@ +package org.nuiton.sandbox.rmi; + +import org.nuiton.sandbox.business.ServiceImpl; +import org.nuiton.sandbox.business.ServiceInterface; +import sun.security.jca.ServiceId; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +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 ServiceExporter { + + private static final int PORT = 12345; + + protected 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 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; + } + + public <E> void registerService(E instance, String name) throws RemoteException { + + testRmiConfig(); + + // Create the proxy and let him be a stub + RemoteMethodExecutorImpl<E> executor = new RemoteMethodExecutorImpl<E>(instance); + Remote stub = UnicastRemoteObject.exportObject(executor, 0); + + // Bind into the registry + Registry registry = getRegistry(); + registry.rebind(name, 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/ServiceImpl.java =================================================================== --- testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceImpl.java 2011-01-11 13:05:58 UTC (rev 456) +++ testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceImpl.java 2011-01-12 09:12:32 UTC (rev 457) @@ -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 ServiceImpl implements ServiceRMIInterface { - - private static final int PORT = 12345; - - public ServiceImpl() { - } - - public static void main(String[] args) throws Exception { - - testRmiConfig(); - - ServiceImpl impl = new ServiceImpl(); - 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); - } - -} Copied: testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIImpl.java (from rev 456, testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceImpl.java) =================================================================== --- testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIImpl.java (rev 0) +++ testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIImpl.java 2011-01-12 09:12:32 UTC (rev 457) @@ -0,0 +1,58 @@ +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); + } + +} Modified: testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIInterface.java =================================================================== --- testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIInterface.java 2011-01-11 13:05:58 UTC (rev 456) +++ testStandaloneRmi/src/main/java/org/nuiton/sandbox/rmi/ServiceRMIInterface.java 2011-01-12 09:12:32 UTC (rev 457) @@ -1,6 +1,7 @@ package org.nuiton.sandbox.rmi; -import java.io.Serializable; +import org.nuiton.sandbox.business.ServiceInterface; + import java.rmi.Remote; import java.rmi.RemoteException; Copied: testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/ClientExporter.java (from rev 456, testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/Client.java) =================================================================== --- testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/ClientExporter.java (rev 0) +++ testStandaloneRmi/src/test/java/org/nuiton/sandbox/rmi/ClientExporter.java 2011-01-12 09:12:32 UTC (rev 457) @@ -0,0 +1,28 @@ +package org.nuiton.sandbox.rmi; + +import org.nuiton.sandbox.business.ServiceInterface; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.rmi.Remote; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.rmi.server.RemoteObjectInvocationHandler; + +/** + * @author Arnaud Thimel <thimel@codelutin.com> + */ +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}); + System.out.println(result); + } + +}
participants (1)
-
athimelï¼ users.nuiton.org