public static void main(String[] args) { Ping obj = null; Registry registry = null; try { /* * create registry */ TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager"); System.err.println("creating Registry..."); registry = TestLibrary.createRegistryOnUnusedPort(); int port = TestLibrary.getRegistryPort(registry); /* * create object with custom ref and bind in registry */ System.err.println("creating UseCustomRef..."); UseCustomRef cr = new UseCustomRef(); RemoteRef ref = cr.getRef(); if (!(ref instanceof CustomServerRef)) { TestLibrary.bomb("test failed: reference not " + "instanceof CustomServerRef"); } String name = "//:" + port + "/UseCustomRef"; // String name = "UseCustomRef"; System.err.println("binding object in registry..."); Naming.rebind(name, cr); /* * look up object and invoke its ping method */ System.err.println("ping object..."); obj = (Ping) Naming.lookup(name); obj.ping(); /* * pass object with custom ref in remote call */ System.err.println("pass object in remote call..."); obj.receiveAndPing(cr); /* * write remote object with custom ref to output stream */ System.err.println("writing remote object to stream..."); ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bout); out.writeObject(cr); out.flush(); out.close(); /* * read back remote object from output stream */ System.err.println("reading remote object from stream..."); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray())); cr = (UseCustomRef) in.readObject(); /* * re-export object and ping */ System.err.println("re-export object read..."); cr.exportObject(); System.err.println("look up object again..."); Naming.rebind(name, cr); System.err.println("ping object read..."); obj = (Ping) Naming.lookup(name); obj.ping(); System.err.println("TEST PASSED"); Naming.unbind(name); cr = null; } catch (Exception e) { TestLibrary.bomb("test failed with exception: ", e); } finally { TestLibrary.unexport(obj); TestLibrary.unexport(registry); registry = null; obj = null; } }