1 /* |
1 /* |
2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
61 * Tests the behavior of LoggerFinderLoader with respect to the |
61 * Tests the behavior of LoggerFinderLoader with respect to the |
62 * value of the internal diagnosability switches. Also test the |
62 * value of the internal diagnosability switches. Also test the |
63 * DefaultLoggerFinder and SimpleConsoleLogger implementation. |
63 * DefaultLoggerFinder and SimpleConsoleLogger implementation. |
64 * @modules java.base/sun.util.logging |
64 * @modules java.base/sun.util.logging |
65 * java.base/jdk.internal.logger |
65 * java.base/jdk.internal.logger |
66 * @build AccessSystemLogger LoggerFinderLoaderTest CustomSystemClassLoader |
66 * @build AccessSystemLogger LoggerFinderLoaderTest CustomSystemClassLoader BaseLoggerFinder BaseLoggerFinder2 |
67 * @run driver AccessSystemLogger |
67 * @run driver AccessSystemLogger |
68 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader LoggerFinderLoaderTest NOSECURITY |
68 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader LoggerFinderLoaderTest NOSECURITY |
69 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader LoggerFinderLoaderTest NOPERMISSIONS |
69 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader LoggerFinderLoaderTest NOPERMISSIONS |
70 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader LoggerFinderLoaderTest WITHPERMISSIONS |
70 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader LoggerFinderLoaderTest WITHPERMISSIONS |
71 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true LoggerFinderLoaderTest NOSECURITY |
71 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader -Dtest.fails=true LoggerFinderLoaderTest NOSECURITY |
116 final static AccessSystemLogger accessSystemLogger = new AccessSystemLogger(); |
116 final static AccessSystemLogger accessSystemLogger = new AccessSystemLogger(); |
117 static final Class<?>[] providerClass; |
117 static final Class<?>[] providerClass; |
118 static { |
118 static { |
119 try { |
119 try { |
120 providerClass = new Class<?>[] { |
120 providerClass = new Class<?>[] { |
121 ClassLoader.getSystemClassLoader().loadClass("LoggerFinderLoaderTest$BaseLoggerFinder"), |
121 ClassLoader.getSystemClassLoader().loadClass("BaseLoggerFinder"), |
122 ClassLoader.getSystemClassLoader().loadClass("LoggerFinderLoaderTest$BaseLoggerFinder2") |
122 ClassLoader.getSystemClassLoader().loadClass("BaseLoggerFinder2") |
123 }; |
123 }; |
124 } catch (ClassNotFoundException ex) { |
124 } catch (ClassNotFoundException ex) { |
125 throw new ExceptionInInitializerError(ex); |
125 throw new ExceptionInInitializerError(ex); |
126 } |
126 } |
127 } |
127 } |
167 |
167 |
168 } |
168 } |
169 |
169 |
170 public Logger getLogger(String name, Module caller); |
170 public Logger getLogger(String name, Module caller); |
171 public Logger getLocalizedLogger(String name, ResourceBundle bundle, Module caller); |
171 public Logger getLocalizedLogger(String name, ResourceBundle bundle, Module caller); |
172 } |
|
173 |
|
174 public static class BaseLoggerFinder extends LoggerFinder implements TestLoggerFinder { |
|
175 |
|
176 static final RuntimePermission LOGGERFINDER_PERMISSION = |
|
177 new RuntimePermission("loggerFinder"); |
|
178 public BaseLoggerFinder() { |
|
179 if (fails.get()) { |
|
180 throw new RuntimeException("Simulate exception while loading provider"); |
|
181 } |
|
182 } |
|
183 |
|
184 System.Logger createSimpleLogger(String name) { |
|
185 PrivilegedAction<System.Logger> pa = () -> SimpleConsoleLogger.makeSimpleLogger(name); |
|
186 return AccessController.doPrivileged(pa); |
|
187 } |
|
188 |
|
189 |
|
190 @Override |
|
191 public Logger getLogger(String name, Module caller) { |
|
192 SecurityManager sm = System.getSecurityManager(); |
|
193 if (sm != null) { |
|
194 sm.checkPermission(LOGGERFINDER_PERMISSION); |
|
195 } |
|
196 PrivilegedAction<ClassLoader> pa = () -> caller.getClassLoader(); |
|
197 ClassLoader callerLoader = AccessController.doPrivileged(pa); |
|
198 if (callerLoader == null) { |
|
199 return system.computeIfAbsent(name, (n) -> new LoggerImpl(n, createSimpleLogger(name))); |
|
200 } else { |
|
201 return user.computeIfAbsent(name, (n) -> new LoggerImpl(n, createSimpleLogger(name))); |
|
202 } |
|
203 } |
|
204 } |
|
205 |
|
206 public static class BaseLoggerFinder2 extends LoggerFinder implements TestLoggerFinder { |
|
207 |
|
208 static final RuntimePermission LOGGERFINDER_PERMISSION = |
|
209 new RuntimePermission("loggerFinder"); |
|
210 public BaseLoggerFinder2() { |
|
211 throw new ServiceConfigurationError("Should not come here"); |
|
212 } |
|
213 @Override |
|
214 public Logger getLogger(String name, Module caller) { |
|
215 throw new ServiceConfigurationError("Should not come here"); |
|
216 } |
|
217 } |
172 } |
218 |
173 |
219 public static class MyBundle extends ResourceBundle { |
174 public static class MyBundle extends ResourceBundle { |
220 |
175 |
221 final ConcurrentHashMap<String,String> map = new ConcurrentHashMap<>(); |
176 final ConcurrentHashMap<String,String> map = new ConcurrentHashMap<>(); |
268 } |
223 } |
269 if (!warning.contains("WARNING: Exception raised trying to instantiate LoggerFinder")) { |
224 if (!warning.contains("WARNING: Exception raised trying to instantiate LoggerFinder")) { |
270 throw new RuntimeException("Expected message not found. Error stream contained: " + warning); |
225 throw new RuntimeException("Expected message not found. Error stream contained: " + warning); |
271 } |
226 } |
272 if (TestLoggerFinder.fails.get()) { |
227 if (TestLoggerFinder.fails.get()) { |
273 if (!warning.contains("java.util.ServiceConfigurationError: java.lang.System$LoggerFinder: Provider LoggerFinderLoaderTest$BaseLoggerFinder could not be instantiated")) { |
228 if (!warning.contains("java.util.ServiceConfigurationError: java.lang.System$LoggerFinder: Provider BaseLoggerFinder could not be instantiated")) { |
274 throw new RuntimeException("Expected message not found. Error stream contained: " + warning); |
229 throw new RuntimeException("Expected message not found. Error stream contained: " + warning); |
275 } |
230 } |
276 } else if (singleton) { |
231 } else if (singleton) { |
277 if (!warning.contains("java.util.ServiceConfigurationError: More than on LoggerFinder implementation")) { |
232 if (!warning.contains("java.util.ServiceConfigurationError: More than on LoggerFinder implementation")) { |
278 throw new RuntimeException("Expected message not found. Error stream contained: " + warning); |
233 throw new RuntimeException("Expected message not found. Error stream contained: " + warning); |
423 if (!TestLoggerFinder.fails.get()) { |
378 if (!TestLoggerFinder.fails.get()) { |
424 ServiceLoader<LoggerFinder> serviceLoader = |
379 ServiceLoader<LoggerFinder> serviceLoader = |
425 ServiceLoader.load(LoggerFinder.class, ClassLoader.getSystemClassLoader()); |
380 ServiceLoader.load(LoggerFinder.class, ClassLoader.getSystemClassLoader()); |
426 Iterator<LoggerFinder> iterator = serviceLoader.iterator(); |
381 Iterator<LoggerFinder> iterator = serviceLoader.iterator(); |
427 Object firstProvider = iterator.next(); |
382 Object firstProvider = iterator.next(); |
428 if (!firstProvider.getClass().getName().equals("LoggerFinderLoaderTest$BaseLoggerFinder")) { |
383 if (!firstProvider.getClass().getName().equals("BaseLoggerFinder")) { |
429 throw new RuntimeException("Unexpected provider: " + firstProvider.getClass().getName()); |
384 throw new RuntimeException("Unexpected provider: " + firstProvider.getClass().getName()); |
430 } |
385 } |
431 if (!iterator.hasNext()) { |
386 if (!iterator.hasNext()) { |
432 throw new RuntimeException("Expected two providers"); |
387 throw new RuntimeException("Expected two providers"); |
433 } |
388 } |