jdk/test/java/rmi/server/useCustomRef/UseCustomRef.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @build UseCustomRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @build Ping
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @build UseCustomRef_Stub
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @build UseCustomRef_Skel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @run main/othervm/policy=security.policy/secure=java.rmi.RMISecurityManager/timeout=120 UseCustomRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This test was failing to run because the synthetic access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * control context used by the application class loader to find and define
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * CustomServerRef does not have accessClassInPackage.sun.rmi.server runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * permission necessary to load its superclass sun.rmi.server.UnicastServerRef,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * even though this test's code is granted that permission in its policy file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * That bug number is 4256530
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.rmi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.rmi.server.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.rmi.registry.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.rmi.transport.LiveRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class UseCustomRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        extends RemoteServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        implements Ping
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public UseCustomRef() throws RemoteException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        exportObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public void exportObject() throws RemoteException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        ref = new CustomServerRef(new LiveRef(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        ((ServerRef) ref).exportObject(this, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public RemoteRef getRef() { return ref; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public void ping() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public void receiveAndPing(Ping p) throws RemoteException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        p.ping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        Ping obj = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        Registry registry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
             * create registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            System.err.println("creating Registry...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            registry = LocateRegistry.createRegistry(TestLibrary.REGISTRY_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
             * create object with custom ref and bind in registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            System.err.println("creating UseCustomRef...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            UseCustomRef cr = new UseCustomRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            RemoteRef ref = cr.getRef();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (!(ref instanceof CustomServerRef)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                TestLibrary.bomb("test failed: reference not " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                "instanceof CustomServerRef");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            String name = "//:" + TestLibrary.REGISTRY_PORT + "/UseCustomRef";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            //      String name = "UseCustomRef";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            System.err.println("binding object in registry...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            Naming.rebind(name, cr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
             * look up object and invoke its ping method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            System.err.println("ping object...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            obj = (Ping) Naming.lookup(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            obj.ping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
             * pass object with custom ref in remote call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            System.err.println("pass object in remote call...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            obj.receiveAndPing(cr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
             * write remote object with custom ref to output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            System.err.println("writing remote object to stream...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            ObjectOutputStream out = new ObjectOutputStream(bout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            out.writeObject(cr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
             * read back remote object from output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            System.err.println("reading remote object from stream...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            ObjectInputStream in = new ObjectInputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                new ByteArrayInputStream(bout.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            cr = (UseCustomRef) in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
             * re-export object and ping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            System.err.println("re-export object read...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            cr.exportObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            System.err.println("look up object again...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            Naming.rebind(name, cr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            System.err.println("ping object read...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            obj = (Ping) Naming.lookup(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            obj.ping();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            System.err.println("TEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            Naming.unbind(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            cr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            TestLibrary.bomb("test failed with exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            TestLibrary.unexport(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            TestLibrary.unexport(registry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            registry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            obj = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public static class CustomServerRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        extends sun.rmi.server.UnicastServerRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        public CustomServerRef() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        public CustomServerRef(LiveRef ref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            super(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        /*****
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        public CustomServerRef(int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                               RMIClientSocketFactory csf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                               RMIServerSocketFactory ssf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            super (new LiveRef(port, csf, ssf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        *****/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        public String getRefClass(ObjectOutput out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        protected void unmarshalCustomCallData(ObjectInput in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            System.err.println("unmarshalling call data...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            String s = (String) (in.readObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            System.err.println(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        protected RemoteRef getClientRef() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return new CustomRef(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public static class CustomRef extends sun.rmi.server.UnicastRef {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        public CustomRef() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        public CustomRef(sun.rmi.transport.LiveRef ref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            super(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        protected void marshalCustomCallData(ObjectOutput out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            // this custom data ensures that a custom server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            // ref has written the relevant information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            System.err.println("marshalling call data...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            out.writeObject("hello there.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        public String getRefClass(ObjectOutput out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
}