jdk/test/java/rmi/server/RMIClassLoader/useGetURLs/UseGetURLs.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 14778 5947768d173d
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2002, 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 4137605
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary When the RMIClassLoader.getClassAnnotation() is called with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * class loaded from any URLClassLoader instance (not just those created for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * internal use by the RMI runtime), then it should return a String containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * a space-separated list of the class loader's path of URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @author Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @library ../../../testlibrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @build TestLibrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @build UseGetURLs Dummy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @run main/othervm/policy=security.policy/timeout=120 UseGetURLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.rmi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.rmi.server.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class UseGetURLs {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        System.err.println("\nRegression test for bug 4137605\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        System.err.println("Security manager: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                           System.getSecurityManager().getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
         * Install dummy class in first codebase to be loaded from an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
         * arbitrary URLClassLoader; the second codebase is used to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
         * the desired annotation more interesting (more than one element).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        URL codebase1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        URL codebase2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            codebase1 = TestLibrary.installClassInCodebase("Dummy",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                                           "codebase1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            File cb2file =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                new File(TestLibrary.getProperty("user.dir", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                         "codebase2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            codebase2 = cb2file.toURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            TestLibrary.bomb("failed to install test classes", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
             * Create an arbitary URLClassLoader for the two codebases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            ClassLoader loader = URLClassLoader.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                new URL[] { codebase1, codebase2 });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            System.err.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                "URLs for class loader: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                Arrays.asList(((URLClassLoader) loader).getURLs()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            System.err.println("Expecting annotation: \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                codebase1 + " " + codebase2 + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            System.err.println("First URL:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            dumpURL(codebase1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            System.err.println("Second URL:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            dumpURL(codebase2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
             * Load dummy class from the loader, get the annotation string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
             * and verify that it is correct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            Class cl = loader.loadClass("Dummy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            String annotation = RMIClassLoader.getClassAnnotation(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            System.err.println("Received annotation:  \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                annotation + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (annotation == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                throw new RuntimeException("annotation was null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            URL[] urls = pathToURLs(annotation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            System.err.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                "URLs from annotation: " + Arrays.asList(urls));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (urls.length != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    "wrong number of elements in annotation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (!urls[0].equals(codebase1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                System.err.println("First URL in annotation is incorrect:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                dumpURL(urls[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    "first URL in annotation is incorrect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (!urls[1].equals(codebase2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                System.err.println("Second URL in annotation is incorrect:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                dumpURL(urls[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    "second URL in annotation is incorrect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            System.err.println("TEST PASSED: annotation matched codebase");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            TestLibrary.bomb(e.getMessage(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private static URL[] pathToURLs(String path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        throws MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        StringTokenizer st = new StringTokenizer(path); // divide by spaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        URL[] urls = new URL[st.countTokens()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        for (int i = 0; st.hasMoreTokens(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            urls[i] = new URL(st.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static void dumpURL(URL u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        System.err.println("\tprotocol:  " + u.getProtocol());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        System.err.println("\tauthority: " + u.getAuthority());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        System.err.println("\tuser info: " + u.getUserInfo());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        System.err.println("\thost:      " + u.getHost());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        System.err.println("\tport:      " + u.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        System.err.println("\tpath:      " + u.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        System.err.println("\tfile:      " + u.getFile());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        System.err.println("\tquery:     " + u.getQuery());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        System.err.println("\tref:       " + u.getRef());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}