public Registry getObject ( final String command ) throws Exception { String host; int port; int sep = command.indexOf(':'); if ( sep < 0 ) { port = new Random().nextInt(65535); host = command; } else { host = command.substring(0, sep); port = Integer.valueOf(command.substring(sep + 1)); } ObjID id = new ObjID(new Random().nextInt()); // RMI registry TCPEndpoint te = new TCPEndpoint(host, port); UnicastRef ref = new UnicastRef(new LiveRef(id, te, false)); RemoteObjectInvocationHandler obj = new RemoteObjectInvocationHandler(ref); Registry proxy = (Registry) Proxy.newProxyInstance(JRMPClient.class.getClassLoader(), new Class[] { Registry.class }, obj); return proxy; }
public Registry getObject ( String connection ) throws Exception { String host; int port; int sep = connection.indexOf(':'); if ( sep < 0 ) { port = new Random().nextInt(65535); host = connection; } else { host = connection.substring(0, sep); port = Integer.valueOf(connection.substring(sep + 1)); } ObjID id = new ObjID(new Random().nextInt()); // RMI registry TCPEndpoint te = new TCPEndpoint(host, port); UnicastRef ref = new UnicastRef(new LiveRef(id, te, false)); RemoteObjectInvocationHandler obj = new RemoteObjectInvocationHandler(ref); Registry proxy = (Registry) Proxy.newProxyInstance(JRMPClient.class.getClassLoader(), new Class[] { Registry.class }, obj); return proxy; }
/** * Returns a stub if the object to be serialized is a * {@link java.rmi.Remote} instance. * * @param obj * the object to be replaced if needed be. * @return if the argument was a {@link java.rmi.Remote} object locally * exported a stub for that object is returned, in case it is not a * {@link java.rmi.Remote} the object is returned * @throws IOException * if the I/O operation fails */ @Override protected final Object replaceObject(Object obj) throws IOException { if (obj instanceof Remote) { RemoteReferenceManager rrm = RemoteReferenceManager .getRemoteReferenceManager(); if (rrm.isExported((Remote) obj)) { writesRemote = true; return RemoteObject.toStub((Remote) obj); } if (obj instanceof RemoteStub) { writesRemote = true; return obj; } if (Proxy.isProxyClass(obj.getClass())) { InvocationHandler ih = Proxy.getInvocationHandler(obj); if (ih instanceof RemoteObjectInvocationHandler) { writesRemote = true; } } } return obj; }
public final void testInvoke004() throws RemoteException { EchoUnicast_Imp eui = new EchoUnicast_Imp() { public String echo(String msg) throws RemoteException { return msg; } }; RemoteRef ref = eui.getRef(); Echo proxy = (Echo) RemoteObject.toStub(eui); String toSend = "hola invoquer"; try { r = new RemoteObjectInvocationHandler(ref); assertEquals("The result is what we expect", toSend, r.invoke( proxy, Echo.class.getMethods()[0], new Object[] { toSend })); } catch (Throwable e) { fail("Failed with:" + e); } }
public final void testInvoke006() throws RemoteException { Echo_Imp eui = new Echo_Imp() { public String echo(String msg) throws RemoteException { return msg; } }; Remote proxy = UnicastRemoteObject.exportObject(eui, 1100); String toSend = "hola invoquer"; r = (RemoteObjectInvocationHandler) Proxy.getInvocationHandler(proxy); try { assertEquals("The result is what we expect", toSend, r.invoke( proxy, Echo.class.getMethods()[0], new Object[] { toSend })); } catch (Throwable e) { fail("Failed with:" + e); } }
public final void testInvoke008() throws RemoteException { EchoUnicast_Imp eui = new EchoUnicast_Imp() { public String echo(String msg) throws RemoteException { return msg; } }; RemoteRef ref = eui.getRef(); Echo proxy = (Echo) RemoteObject.toStub(eui); String toSend = "hola invoquer"; try { r = new RemoteObjectInvocationHandler(ref); assertEquals("The result is what we expected", toSend, r.invoke( proxy, Echo.class.getMethods()[0], new Object[] { toSend, toSend })); } catch (Throwable e) { fail("Failed with:" + e); } }
public final void testInvoke011() throws RemoteException { BigEcho_imp bei = new BigEcho_imp() { public void echo() throws RemoteException { throw new RemoteException("Always pass throw here"); } public void echo(Object... objs) throws RemoteException { if (objs == null) { throw new RemoteException("Objs Null"); } } }; RemoteRef ref = bei.getRef(); BigEcho proxy = (BigEcho) RemoteObject.toStub(bei); try { r = new RemoteObjectInvocationHandler(ref); assertNull(r.invoke(proxy, BigEcho.class.getMethods()[1], new Object[] { new Object[] { 1, 2, 3, 4 } })); } catch (Throwable e) { fail("Failed with:" + e); } }
public final void testInvoke012() throws RemoteException { BigEcho_imp bei = new BigEcho_imp() { public void echo() throws RemoteException { throw new RemoteException("Always pass throw here"); } public void echo(Object... objs) throws RemoteException { if (objs == null) { throw new RemoteException("Objs Null"); } } }; RemoteRef ref = bei.getRef(); BigEcho proxy = (BigEcho) RemoteObject.toStub(bei); try { r = new RemoteObjectInvocationHandler(ref); assertNull(r.invoke(proxy, BigEcho.class.getMethods()[1], new Object[] { new Object[] {} })); } catch (Throwable e) { fail("Failed with:" + e); } }
/** * Returns a proxy for the specified implClass. * * If both of the following criteria is satisfied, a dynamic proxy for * the specified implClass is returned (otherwise a RemoteStub instance * for the specified implClass is returned): * * a) either the property java.rmi.server.ignoreStubClasses is true or * a pregenerated stub class does not exist for the impl class, and * b) forceStubUse is false. * * If the above criteria are satisfied, this method constructs a * dynamic proxy instance (that implements the remote interfaces of * implClass) constructed with a RemoteObjectInvocationHandler instance * constructed with the clientRef. * * Otherwise, this method loads the pregenerated stub class (which * extends RemoteStub and implements the remote interfaces of * implClass) and constructs an instance of the pregenerated stub * class with the clientRef. * * @param implClass the class to obtain remote interfaces from * @param clientRef the remote ref to use in the invocation handler * @param forceStubUse if true, forces creation of a RemoteStub * @throws IllegalArgumentException if implClass implements illegal * remote interfaces * @throws StubNotFoundException if problem locating/creating stub or * creating the dynamic proxy instance **/ public static Remote createProxy(Class<?> implClass, RemoteRef clientRef, boolean forceStubUse) throws StubNotFoundException { Class<?> remoteClass; try { remoteClass = getRemoteClass(implClass); } catch (ClassNotFoundException ex ) { throw new StubNotFoundException( "object does not implement a remote interface: " + implClass.getName()); } if (forceStubUse || !(ignoreStubClasses || !stubClassExists(remoteClass))) { return createStub(remoteClass, clientRef); } final ClassLoader loader = implClass.getClassLoader(); final Class<?>[] interfaces = getRemoteInterfaces(implClass); final InvocationHandler handler = new RemoteObjectInvocationHandler(clientRef); /* REMIND: private remote interfaces? */ try { return AccessController.doPrivileged(new PrivilegedAction<Remote>() { public Remote run() { return (Remote) Proxy.newProxyInstance(loader, interfaces, handler); }}); } catch (IllegalArgumentException e) { throw new StubNotFoundException("unable to create proxy", e); } }
private static void checkStub(Remote stub, Class<?> stubClass) { // Check remote stub is from the expected class. // if (stub.getClass() != stubClass) { if (!Proxy.isProxyClass(stub.getClass())) { throw new SecurityException( "Expecting a " + stubClass.getName() + " stub!"); } else { InvocationHandler handler = Proxy.getInvocationHandler(stub); if (handler.getClass() != RemoteObjectInvocationHandler.class) throw new SecurityException( "Expecting a dynamic proxy instance with a " + RemoteObjectInvocationHandler.class.getName() + " invocation handler!"); else stub = (Remote) handler; } } // Check RemoteRef in stub is from the expected class // "sun.rmi.server.UnicastRef2". // RemoteRef ref = ((RemoteObject)stub).getRef(); if (ref.getClass() != UnicastRef2.class) throw new SecurityException( "Expecting a " + UnicastRef2.class.getName() + " remote reference in stub!"); // Check RMIClientSocketFactory in stub is from the expected class // "javax.rmi.ssl.SslRMIClientSocketFactory". // LiveRef liveRef = ((UnicastRef2)ref).getLiveRef(); RMIClientSocketFactory csf = liveRef.getClientSocketFactory(); if (csf == null || csf.getClass() != SslRMIClientSocketFactory.class) throw new SecurityException( "Expecting a " + SslRMIClientSocketFactory.class.getName() + " RMI client socket factory in stub!"); }
public static String getAddress(final Service svc) throws RemoteException { final RemoteObjectInvocationHandler h = (RemoteObjectInvocationHandler) getInvocationHandler(svc); final LiveRef ref = ((UnicastRef) h.getRef()).getLiveRef(); final Channel channel = ref.getChannel(); final TCPEndpoint endpoint = (TCPEndpoint) channel.getEndpoint(); return endpoint.getHost() + ":" + endpoint.getPort(); }
private static void checkStub(Remote stub, Class<? extends Remote> stubClass) { // Check remote stub is from the expected class. // if (stub.getClass() != stubClass) { if (!Proxy.isProxyClass(stub.getClass())) { throw new SecurityException( "Expecting a " + stubClass.getName() + " stub!"); } else { InvocationHandler handler = Proxy.getInvocationHandler(stub); if (handler.getClass() != RemoteObjectInvocationHandler.class) { throw new SecurityException( "Expecting a dynamic proxy instance with a " + RemoteObjectInvocationHandler.class.getName() + " invocation handler!"); } else { stub = (Remote) handler; } } } // Check RemoteRef in stub is from the expected class // "sun.rmi.server.UnicastRef2". // RemoteRef ref = ((RemoteObject)stub).getRef(); if (ref.getClass() != UnicastRef2.class) { throw new SecurityException( "Expecting a " + UnicastRef2.class.getName() + " remote reference in stub!"); } // Check RMIClientSocketFactory in stub is from the expected class // "javax.rmi.ssl.SslRMIClientSocketFactory". // LiveRef liveRef = ((UnicastRef2)ref).getLiveRef(); RMIClientSocketFactory csf = liveRef.getClientSocketFactory(); if (csf == null || csf.getClass() != SslRMIClientSocketFactory.class) { throw new SecurityException( "Expecting a " + SslRMIClientSocketFactory.class.getName() + " RMI client socket factory in stub!"); } }
/** * Returns a proxy for the specified implClass. * * If both of the following criteria is satisfied, a dynamic proxy for * the specified implClass is returned (otherwise a RemoteStub instance * for the specified implClass is returned): * * a) either the property java.rmi.server.ignoreStubClasses is true or * a pregenerated stub class does not exist for the impl class, and * b) forceStubUse is false. * * If the above criteria are satisfied, this method constructs a * dynamic proxy instance (that implements the remote interfaces of * implClass) constructed with a RemoteObjectInvocationHandler instance * constructed with the clientRef. * * Otherwise, this method loads the pregenerated stub class (which * extends RemoteStub and implements the remote interfaces of * implClass) and constructs an instance of the pregenerated stub * class with the clientRef. * * @param implClass the class to obtain remote interfaces from * @param clientRef the remote ref to use in the invocation handler * @param forceStubUse if true, forces creation of a RemoteStub * @throws IllegalArgumentException if implClass implements illegal * remote interfaces * @throws StubNotFoundException if problem locating/creating stub or * creating the dynamic proxy instance **/ public static Remote createProxy(Class implClass, RemoteRef clientRef, boolean forceStubUse) throws StubNotFoundException { Class remoteClass; try { remoteClass = getRemoteClass(implClass); } catch (ClassNotFoundException ex ) { throw new StubNotFoundException( "object does not implement a remote interface: " + implClass.getName()); } if (forceStubUse || !(ignoreStubClasses || !stubClassExists(remoteClass))) { return createStub(remoteClass, clientRef); } ClassLoader loader = implClass.getClassLoader(); Class[] interfaces = getRemoteInterfaces(implClass); InvocationHandler handler = new RemoteObjectInvocationHandler(clientRef); /* REMIND: private remote interfaces? */ try { return (Remote) Proxy.newProxyInstance(loader, interfaces, handler); } catch (IllegalArgumentException e) { throw new StubNotFoundException("unable to create proxy", e); } }
/** * Returns a proxy for the specified implClass. * * If both of the following criteria is satisfied, a dynamic proxy for * the specified implClass is returned (otherwise a RemoteStub instance * for the specified implClass is returned): * * a) either the property java.rmi.server.ignoreStubClasses is true or * a pregenerated stub class does not exist for the impl class, and * b) forceStubUse is false. * * If the above criteria are satisfied, this method constructs a * dynamic proxy instance (that implements the remote interfaces of * implClass) constructed with a RemoteObjectInvocationHandler instance * constructed with the clientRef. * * Otherwise, this method loads the pregenerated stub class (which * extends RemoteStub and implements the remote interfaces of * implClass) and constructs an instance of the pregenerated stub * class with the clientRef. * * @param implClass the class to obtain remote interfaces from * @param clientRef the remote ref to use in the invocation handler * @param forceStubUse if true, forces creation of a RemoteStub * @throws IllegalArgumentException if implClass implements illegal * remote interfaces * @throws StubNotFoundException if problem locating/creating stub or * creating the dynamic proxy instance **/ public static Remote createProxy(Class<?> implClass, RemoteRef clientRef, boolean forceStubUse) throws StubNotFoundException { Class<?> remoteClass; try { remoteClass = getRemoteClass(implClass); } catch (ClassNotFoundException ex ) { throw new StubNotFoundException( "object does not implement a remote interface: " + implClass.getName()); } if (forceStubUse || !(ignoreStubClasses || !stubClassExists(remoteClass))) { return createStub(remoteClass, clientRef); } ClassLoader loader = implClass.getClassLoader(); Class[] interfaces = getRemoteInterfaces(implClass); InvocationHandler handler = new RemoteObjectInvocationHandler(clientRef); /* REMIND: private remote interfaces? */ try { return (Remote) Proxy.newProxyInstance(loader, interfaces, handler); } catch (IllegalArgumentException e) { throw new StubNotFoundException("unable to create proxy", e); } }