1/*
2 * Copyright (c) 2003, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25 * @bug 4507539
26 * @summary support using dynamic proxies as RMI stubs
27 * @author Ann Wollrath
28 *
29 * @build ToStub ToStub_Stub
30 * @run main/othervm/policy=security.policy/timeout=240 ToStub
31 */
32
33import java.io.IOException;
34import java.lang.reflect.Proxy;
35import java.rmi.NoSuchObjectException;
36import java.rmi.Remote;
37import java.rmi.server.RemoteObjectInvocationHandler;
38import java.rmi.server.RemoteStub;
39import java.rmi.server.UnicastRemoteObject;
40import java.rmi.server.RemoteObject;
41
42public class ToStub implements RemoteInterface {
43
44
45    public Object passObject(Object obj) {
46        return obj;
47    }
48
49
50    public static void main(String[] args) throws Exception {
51
52        RemoteInterface server1 = null;
53        RemoteInterface server2 = null;
54        RemoteInterface stub = null;
55        RemoteInterface proxy = null;
56
57        try {
58            System.setProperty("java.rmi.server.ignoreStubClasses", "true");
59
60            if (System.getSecurityManager() == null) {
61                System.setSecurityManager(new SecurityManager());
62            }
63
64            System.err.println("export objects");
65            server1 = new ToStub();
66            server2 = new ToStub();
67            stub = (RemoteInterface) UnicastRemoteObject.exportObject(server1);
68            proxy = (RemoteInterface)
69                UnicastRemoteObject.exportObject(server2, 0);
70
71            System.err.println("test toStub");
72            if (stub != RemoteObject.toStub(server1)) {
73                throw new RuntimeException(
74                    "toStub returned incorrect value for server1");
75            }
76
77            if (!Proxy.isProxyClass(proxy.getClass())) {
78                throw new RuntimeException("proxy is not a dynamic proxy");
79            }
80
81            if (proxy != RemoteObject.toStub(server2)) {
82                throw new RuntimeException(
83                    "toStub returned incorrect value for server2");
84            }
85
86            try {
87                RemoteObject.toStub(new ToStub());
88                throw new RuntimeException(
89                    "stub returned for exported object!");
90            } catch (NoSuchObjectException nsoe) {
91            }
92
93            System.err.println("invoke methods");
94            Object obj = stub.passObject(stub);
95            if (!stub.equals(obj)) {
96                throw new RuntimeException("returned stub not equal");
97            }
98
99            obj = proxy.passObject(proxy);
100            if (!proxy.equals(obj)) {
101                throw new RuntimeException("returned proxy not equal");
102            }
103
104            System.err.println("TEST PASSED");
105
106        } finally {
107            if (stub != null) {
108                UnicastRemoteObject.unexportObject(server1, true);
109            }
110
111            if (proxy != null) {
112                UnicastRemoteObject.unexportObject(server2, true);
113            }
114        }
115    }
116}
117
118interface RemoteInterface extends Remote {
119    Object passObject(Object obj) throws IOException;
120}
121