J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | * @bug 4507539 |
| 26 | * @summary support using dynamic proxies as RMI stubs |
| 27 | * @author Ann Wollrath |
| 28 | * |
| 29 | * @build UseDynamicProxies UseDynamicProxies_Stub |
| 30 | * @run main/othervm/policy=security.policy/timeout=240 UseDynamicProxies true |
| 31 | * @run main/othervm/policy=security.policy/timeout=240 UseDynamicProxies |
| 32 | * false |
| 33 | */ |
| 34 | |
| 35 | import java.io.IOException; |
| 36 | import java.lang.reflect.Proxy; |
| 37 | import java.rmi.Remote; |
| 38 | import java.rmi.server.RemoteObjectInvocationHandler; |
| 39 | import java.rmi.server.RemoteStub; |
| 40 | import java.rmi.server.UnicastRemoteObject; |
| 41 | |
| 42 | public class UseDynamicProxies implements RemoteInterface { |
| 43 | |
| 44 | |
| 45 | public Object passObject(Object obj) { |
| 46 | return obj; |
| 47 | } |
| 48 | |
| 49 | public int passInt(int x) { |
| 50 | return x; |
| 51 | } |
| 52 | |
| 53 | public String passString(String string) { |
| 54 | return string; |
| 55 | } |
| 56 | |
| 57 | public static void main(String[] args) throws Exception { |
| 58 | |
| 59 | RemoteInterface server = null; |
| 60 | RemoteInterface proxy = null; |
| 61 | |
| 62 | try { |
| 63 | System.setProperty("java.rmi.server.ignoreStubClasses", args[0]); |
| 64 | boolean ignoreStubClasses = Boolean.parseBoolean(args[0]); |
| 65 | |
| 66 | if (System.getSecurityManager() == null) { |
| 67 | System.setSecurityManager(new SecurityManager()); |
| 68 | } |
| 69 | |
| 70 | System.err.println("export object"); |
| 71 | server = new UseDynamicProxies(); |
| 72 | proxy = |
| 73 | (RemoteInterface) UnicastRemoteObject.exportObject(server, 0); |
| 74 | |
| 75 | System.err.println("proxy = " + proxy); |
| 76 | if (ignoreStubClasses) { |
| 77 | if (!Proxy.isProxyClass(proxy.getClass())) { |
| 78 | throw new RuntimeException( |
| 79 | "server proxy is not a dynamic proxy"); |
| 80 | } |
| 81 | if (!(Proxy.getInvocationHandler(proxy) instanceof |
| 82 | RemoteObjectInvocationHandler)) |
| 83 | { |
| 84 | throw new RuntimeException("invalid invocation handler"); |
| 85 | } |
| 86 | |
| 87 | } else if (!(proxy instanceof RemoteStub)) { |
| 88 | throw new RuntimeException( |
| 89 | "server proxy is not a RemoteStub"); |
| 90 | } |
| 91 | |
| 92 | System.err.println("invoke methods"); |
| 93 | Object obj = proxy.passObject(proxy); |
| 94 | if (!proxy.equals(obj)) { |
| 95 | throw new RuntimeException("returned proxy not equal"); |
| 96 | } |
| 97 | |
| 98 | int x = proxy.passInt(53); |
| 99 | if (x != 53) { |
| 100 | throw new RuntimeException("returned int not equal"); |
| 101 | } |
| 102 | |
| 103 | String string = proxy.passString("test"); |
| 104 | if (!string.equals("test")) { |
| 105 | throw new RuntimeException("returned string not equal"); |
| 106 | } |
| 107 | |
| 108 | System.err.println("TEST PASSED"); |
| 109 | |
| 110 | } finally { |
| 111 | if (proxy != null) { |
| 112 | UnicastRemoteObject.unexportObject(server, true); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
| 119 | interface RemoteInterface extends Remote { |
| 120 | Object passObject(Object obj) throws IOException; |
| 121 | int passInt(int x) throws IOException; |
| 122 | String passString(String string) throws IOException; |
| 123 | } |