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