blob: f94ff8696a966326b498cf99da5e38d0886a1781 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999-2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 4242317
26 * @summary When a class that can be found in the CLASSPATH of the rmiregistry
27 * tool is marshalled via RMI, it should be annotated with the value of the
28 * java.rmi.server.codebase property, not the list of "file:" URLs for the
29 * actual elements of the CLASSPATH.
30 * @author Peter Jones
31 *
32 * @library ../../testlibrary
33 * @build ClassPathCodebase Dummy
34 * @run main/othervm/policy=security.policy ClassPathCodebase
35 */
36
37import java.io.*;
38import java.net.*;
39import java.rmi.*;
40import java.rmi.server.*;
41import java.rmi.registry.*;
42import java.util.Arrays;
43
44public class ClassPathCodebase {
45
46 /** wait 10 seconds for the registry process to be ready to call */
47 private final static long REGISTRY_WAIT = 15000;
48
49 private final static String dummyClassName = "Dummy";
50
51 private final static String dummyBinding = "DummyObject";
52
53 private final static String importCodebase = "codebase_IMPORT_";
54 private final static String exportCodebase = "codebase_EXPORT_";
55
56 public static void main(String[] args) {
57
58 System.err.println("\nRegression test for bug 4242317\n");
59
60 TestLibrary.suggestSecurityManager("java.lang.SecurityManager");
61
62 Process rmiregistry = null;
63
64 try {
65 /*
66 * Install a dummy class in two codebases: one that will be in
67 * the rmiregistry's CLASSPATH (the "import" codebase) and one
68 * that will be in the rmiregistry's "java.rmi.server.codebase"
69 * property (the "export" codebase).
70 */
71 URL importCodebaseURL = TestLibrary.installClassInCodebase(
72 dummyClassName, importCodebase, false);
73 URL exportCodebaseURL = TestLibrary.installClassInCodebase(
74 dummyClassName, exportCodebase, true);
75
76 /*
77 * Spawn an rmiregistry in the "import" codebase directory.
78 */
79 File rmiregistryDir =
80 new File(System.getProperty("user.dir", "."), importCodebase);
81
82 String rmiregistryCommand =
83 System.getProperty("java.home") + File.separator +
84 "bin" + File.separator + "rmiregistry";
85
86 String cmdarray[] = new String[] {
87 rmiregistryCommand,
88 "-J-Denv.class.path=.",
89 "-J-Djava.rmi.server.codebase=" + exportCodebaseURL,
90 Integer.toString(TestLibrary.REGISTRY_PORT) };
91
92 System.err.println("\nCommand used to spawn rmiregistry process:");
93 System.err.println("\t" + Arrays.asList(cmdarray).toString());
94
95 rmiregistry = Runtime.getRuntime().exec(cmdarray, null, rmiregistryDir);
96
97 // pipe rmiregistry output to our output, for debugging failures
98 StreamPipe.plugTogether(rmiregistry.getInputStream(), System.err);
99 StreamPipe.plugTogether(rmiregistry.getErrorStream(), System.err);
100
101 /*
102 * Wait for the registry to initialize and be ready to call.
103 */
104 Thread.sleep(REGISTRY_WAIT);
105 System.err.println();
106
107 /*
108 * Create an instance of the dummy class, finding it from the
109 * "import" codebase.
110 */
111 ClassLoader loader = URLClassLoader.newInstance(
112 new URL[] { importCodebaseURL });
113 Class dummyClass = Class.forName(dummyClassName, false, loader);
114 Remote dummyObject = (Remote) dummyClass.newInstance();
115
116 /*
117 * Find the registry that we created and bind the
118 * dummy object to it.
119 */
120 Registry registry = LocateRegistry.getRegistry(
121 "localhost", TestLibrary.REGISTRY_PORT);
122
123 try {
124 registry.bind(dummyBinding, dummyObject);
125 System.err.println("Bound dummy object in registry");
126 } catch (java.rmi.ConnectException e) {
127 System.err.println("Error: rmiregistry not started in time");
128 throw e;
129 } catch (ServerException e) {
130 if (e.detail instanceof UnmarshalException &&
131 ((UnmarshalException) e.detail).detail instanceof
132 ClassNotFoundException)
133 {
134 System.err.println(
135 "Error: another registry running on port " +
136 TestLibrary.REGISTRY_PORT + "?");
137 }
138 throw e;
139 }
140
141 /*
142 * Look up the dummy object from our registry and make sure
143 * that its class was annotated with the "export" codebase.
144 */
145 Remote dummyLookup = registry.lookup(dummyBinding);
146 System.err.println(
147 "Looked up dummy object from registry: " + dummyLookup);
148 Class dummyLookupClass = dummyLookup.getClass();
149 String dummyLookupAnnotation =
150 RMIClassLoader.getClassAnnotation(dummyLookupClass);
151 System.err.println(
152 "Class annotation from registry: " + dummyLookupAnnotation);
153
154 System.err.println();
155 if (dummyLookupAnnotation.indexOf(exportCodebase) >= 0) {
156 System.err.println("TEST PASSED");
157 } else if (dummyLookupAnnotation.indexOf(importCodebase) >= 0) {
158 throw new RuntimeException(
159 "rmiregistry annotated with CLASSPATH element URL");
160 } else {
161 throw new RuntimeException(
162 "rmiregistry used unexpected annotation: \"" +
163 dummyLookupAnnotation + "\"");
164 }
165
166 } catch (Exception e) {
167 e.printStackTrace();
168 throw new RuntimeException("TEST FAILED: " + e.toString());
169 } finally {
170 if (rmiregistry != null) {
171 rmiregistry.destroy();
172 }
173 }
174 }
175}