jdk/test/java/rmi/server/useCustomRef/UseCustomRef.java
author smarks
Wed, 12 Dec 2012 09:53:01 -0800
changeset 14778 5947768d173d
parent 13256 5886d7607acd
child 30820 0d4717a011d3
permissions -rw-r--r--
8004748: clean up @build tags in RMI tests Reviewed-by: alanb, darcy, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
     2
 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4155894
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary remote references can't be downloaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * test currently needs RMISecurityManager because of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * 4180392
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @library ../../testlibrary
14778
5947768d173d 8004748: clean up @build tags in RMI tests
smarks
parents: 13256
diff changeset
    34
 * @build TestLibrary Ping UseCustomRef_Stub UseCustomRef_Skel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @run main/othervm/policy=security.policy/secure=java.rmi.RMISecurityManager/timeout=120 UseCustomRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This test was failing to run because the synthetic access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * control context used by the application class loader to find and define
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * CustomServerRef does not have accessClassInPackage.sun.rmi.server runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * permission necessary to load its superclass sun.rmi.server.UnicastServerRef,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * even though this test's code is granted that permission in its policy file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * That bug number is 4256530
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.rmi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.rmi.server.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.rmi.registry.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.rmi.transport.LiveRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class UseCustomRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        extends RemoteServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        implements Ping
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public UseCustomRef() throws RemoteException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        exportObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public void exportObject() throws RemoteException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        ref = new CustomServerRef(new LiveRef(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        ((ServerRef) ref).exportObject(this, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public RemoteRef getRef() { return ref; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public void ping() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public void receiveAndPing(Ping p) throws RemoteException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        p.ping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        Ping obj = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        Registry registry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
             * create registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            System.err.println("creating Registry...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    85
            registry = TestLibrary.createRegistryOnUnusedPort();
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    86
            int port = TestLibrary.getRegistryPort(registry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
             * create object with custom ref and bind in registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            System.err.println("creating UseCustomRef...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            UseCustomRef cr = new UseCustomRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            RemoteRef ref = cr.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (!(ref instanceof CustomServerRef)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                TestLibrary.bomb("test failed: reference not " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                "instanceof CustomServerRef");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 5506
diff changeset
    98
            String name = "//:" + port + "/UseCustomRef";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            //      String name = "UseCustomRef";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            System.err.println("binding object in registry...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            Naming.rebind(name, cr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
             * look up object and invoke its ping method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            System.err.println("ping object...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            obj = (Ping) Naming.lookup(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            obj.ping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
             * pass object with custom ref in remote call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            System.err.println("pass object in remote call...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            obj.receiveAndPing(cr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
             * write remote object with custom ref to output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            System.err.println("writing remote object to stream...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            ObjectOutputStream out = new ObjectOutputStream(bout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            out.writeObject(cr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
             * read back remote object from output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            System.err.println("reading remote object from stream...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            ObjectInputStream in = new ObjectInputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                new ByteArrayInputStream(bout.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            cr = (UseCustomRef) in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
             * re-export object and ping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            System.err.println("re-export object read...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            cr.exportObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            System.err.println("look up object again...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            Naming.rebind(name, cr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            System.err.println("ping object read...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            obj = (Ping) Naming.lookup(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            obj.ping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            System.err.println("TEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            Naming.unbind(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            cr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            TestLibrary.bomb("test failed with exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            TestLibrary.unexport(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            TestLibrary.unexport(registry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            registry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            obj = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public static class CustomServerRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        extends sun.rmi.server.UnicastServerRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        public CustomServerRef() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        public CustomServerRef(LiveRef ref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            super(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        /*****
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        public CustomServerRef(int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                               RMIClientSocketFactory csf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                               RMIServerSocketFactory ssf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            super (new LiveRef(port, csf, ssf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        *****/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        public String getRefClass(ObjectOutput out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        protected void unmarshalCustomCallData(ObjectInput in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            System.err.println("unmarshalling call data...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            String s = (String) (in.readObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            System.err.println(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        protected RemoteRef getClientRef() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return new CustomRef(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public static class CustomRef extends sun.rmi.server.UnicastRef {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        public CustomRef() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        public CustomRef(sun.rmi.transport.LiveRef ref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            super(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        protected void marshalCustomCallData(ObjectOutput out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            // this custom data ensures that a custom server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // ref has written the relevant information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            System.err.println("marshalling call data...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            out.writeObject("hello there.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        public String getRefClass(ObjectOutput out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
}