1 /*
2 * Copyright (c) 2015, 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 */
38 import java.util.ResourceBundle;
39 import java.util.concurrent.ArrayBlockingQueue;
40 import java.util.concurrent.ConcurrentHashMap;
41 import java.util.concurrent.atomic.AtomicBoolean;
42 import java.util.concurrent.atomic.AtomicLong;
43 import java.util.function.Supplier;
44 import java.lang.System.LoggerFinder;
45 import java.lang.System.Logger;
46 import java.lang.System.Logger.Level;
47 import java.security.AccessControlException;
48 import java.util.stream.Stream;
49 import sun.util.logging.PlatformLogger;
50
51 /**
52 * @test
53 * @bug 8140364
54 * @summary JDK implementation specific unit test for JDK internal API.
55 * Tests a naive implementation of System.Logger, and in particular
56 * the default mapping provided by PlatformLogger.
57 * @modules java.base/sun.util.logging
58 * @build CustomSystemClassLoader BasePlatformLoggerTest
59 * @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader BasePlatformLoggerTest NOSECURITY
60 * @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader BasePlatformLoggerTest NOPERMISSIONS
61 * @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader BasePlatformLoggerTest WITHPERMISSIONS
62 * @author danielfuchs
63 */
64 public class BasePlatformLoggerTest {
65
66 public static final RuntimePermission LOGGERFINDER_PERMISSION =
67 new RuntimePermission("loggerFinder");
68
69 final static AtomicLong sequencer = new AtomicLong();
70 final static boolean VERBOSE = false;
71 static final ThreadLocal<AtomicBoolean> allowControl = new ThreadLocal<AtomicBoolean>() {
72 @Override
73 protected AtomicBoolean initialValue() {
74 return new AtomicBoolean(false);
75 }
76 };
77 static final ThreadLocal<AtomicBoolean> allowAccess = new ThreadLocal<AtomicBoolean>() {
78 @Override
79 protected AtomicBoolean initialValue() {
80 return new AtomicBoolean(false);
81 }
82 };
83 static final ThreadLocal<AtomicBoolean> allowAll = new ThreadLocal<AtomicBoolean>() {
84 @Override
85 protected AtomicBoolean initialValue() {
86 return new AtomicBoolean(false);
87 }
88 };
89
90 static final Class<?> providerClass;
91 static {
92 try {
93 providerClass = ClassLoader.getSystemClassLoader().loadClass("BasePlatformLoggerTest$BaseLoggerFinder");
94 } catch (ClassNotFoundException ex) {
95 throw new ExceptionInInitializerError(ex);
96 }
97 }
98
99 static final PlatformLogger.Level[] julLevels = {
100 PlatformLogger.Level.ALL,
101 PlatformLogger.Level.FINEST,
102 PlatformLogger.Level.FINER,
103 PlatformLogger.Level.FINE,
104 PlatformLogger.Level.CONFIG,
105 PlatformLogger.Level.INFO,
106 PlatformLogger.Level.WARNING,
107 PlatformLogger.Level.SEVERE,
108 PlatformLogger.Level.OFF,
109 };
110
111 static final Level[] mappedLevels = {
112 Level.ALL, // ALL
113 Level.TRACE, // FINEST
313 }
314
315 void log(LogEvent event) {
316 eventQueue.add(event);
317 }
318
319 @Override
320 public void log(Level level, Supplier<String> msgSupplier) {
321 log(LogEvent.of(isLoggable(level), name, level, null, msgSupplier));
322 }
323
324 @Override
325 public void log(Level level, Supplier<String> msgSupplier, Throwable thrown) {
326 log(LogEvent.of(isLoggable(level), name, level, thrown, msgSupplier));
327 }
328 }
329
330 public Logger getLogger(String name, Module caller);
331 }
332
333 public static class BaseLoggerFinder extends LoggerFinder implements TestLoggerFinder {
334 @Override
335 public Logger getLogger(String name, Module caller) {
336 SecurityManager sm = System.getSecurityManager();
337 if (sm != null) {
338 sm.checkPermission(LOGGERFINDER_PERMISSION);
339 }
340 PrivilegedAction<ClassLoader> pa = () -> caller.getClassLoader();
341 ClassLoader callerLoader = AccessController.doPrivileged(pa);
342 if (callerLoader == null) {
343 return system.computeIfAbsent(name, (n) -> new LoggerImpl(n));
344 } else {
345 return user.computeIfAbsent(name, (n) -> new LoggerImpl(n));
346 }
347 }
348 }
349
350 static PlatformLogger getPlatformLogger(String name) {
351 boolean old = allowAccess.get().get();
352 allowAccess.get().set(true);
353 try {
354 return PlatformLogger.getLogger(name);
355 } finally {
356 allowAccess.get().set(old);
357 }
358 }
359
360 static enum TestCases {NOSECURITY, NOPERMISSIONS, WITHPERMISSIONS};
361
362 static void setSecurityManager() {
363 if (System.getSecurityManager() == null) {
364 Policy.setPolicy(new SimplePolicy(allowControl, allowAccess, allowAll));
365 System.setSecurityManager(new SecurityManager());
366 }
367 }
368
369 public static void main(String[] args) {
|
1 /*
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.
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 */
38 import java.util.ResourceBundle;
39 import java.util.concurrent.ArrayBlockingQueue;
40 import java.util.concurrent.ConcurrentHashMap;
41 import java.util.concurrent.atomic.AtomicBoolean;
42 import java.util.concurrent.atomic.AtomicLong;
43 import java.util.function.Supplier;
44 import java.lang.System.LoggerFinder;
45 import java.lang.System.Logger;
46 import java.lang.System.Logger.Level;
47 import java.security.AccessControlException;
48 import java.util.stream.Stream;
49 import sun.util.logging.PlatformLogger;
50
51 /**
52 * @test
53 * @bug 8140364
54 * @summary JDK implementation specific unit test for JDK internal API.
55 * Tests a naive implementation of System.Logger, and in particular
56 * the default mapping provided by PlatformLogger.
57 * @modules java.base/sun.util.logging
58 * @build CustomSystemClassLoader BaseLoggerFinder BasePlatformLoggerTest
59 * @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader BasePlatformLoggerTest NOSECURITY
60 * @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader BasePlatformLoggerTest NOPERMISSIONS
61 * @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader BasePlatformLoggerTest WITHPERMISSIONS
62 * @author danielfuchs
63 */
64 public class BasePlatformLoggerTest {
65
66 public static final RuntimePermission LOGGERFINDER_PERMISSION =
67 new RuntimePermission("loggerFinder");
68
69 final static AtomicLong sequencer = new AtomicLong();
70 final static boolean VERBOSE = false;
71 static final ThreadLocal<AtomicBoolean> allowControl = new ThreadLocal<AtomicBoolean>() {
72 @Override
73 protected AtomicBoolean initialValue() {
74 return new AtomicBoolean(false);
75 }
76 };
77 static final ThreadLocal<AtomicBoolean> allowAccess = new ThreadLocal<AtomicBoolean>() {
78 @Override
79 protected AtomicBoolean initialValue() {
80 return new AtomicBoolean(false);
81 }
82 };
83 static final ThreadLocal<AtomicBoolean> allowAll = new ThreadLocal<AtomicBoolean>() {
84 @Override
85 protected AtomicBoolean initialValue() {
86 return new AtomicBoolean(false);
87 }
88 };
89
90 static final Class<?> providerClass;
91 static {
92 try {
93 providerClass = ClassLoader.getSystemClassLoader().loadClass("BaseLoggerFinder");
94 } catch (ClassNotFoundException ex) {
95 throw new ExceptionInInitializerError(ex);
96 }
97 }
98
99 static final PlatformLogger.Level[] julLevels = {
100 PlatformLogger.Level.ALL,
101 PlatformLogger.Level.FINEST,
102 PlatformLogger.Level.FINER,
103 PlatformLogger.Level.FINE,
104 PlatformLogger.Level.CONFIG,
105 PlatformLogger.Level.INFO,
106 PlatformLogger.Level.WARNING,
107 PlatformLogger.Level.SEVERE,
108 PlatformLogger.Level.OFF,
109 };
110
111 static final Level[] mappedLevels = {
112 Level.ALL, // ALL
113 Level.TRACE, // FINEST
313 }
314
315 void log(LogEvent event) {
316 eventQueue.add(event);
317 }
318
319 @Override
320 public void log(Level level, Supplier<String> msgSupplier) {
321 log(LogEvent.of(isLoggable(level), name, level, null, msgSupplier));
322 }
323
324 @Override
325 public void log(Level level, Supplier<String> msgSupplier, Throwable thrown) {
326 log(LogEvent.of(isLoggable(level), name, level, thrown, msgSupplier));
327 }
328 }
329
330 public Logger getLogger(String name, Module caller);
331 }
332
333 static PlatformLogger getPlatformLogger(String name) {
334 boolean old = allowAccess.get().get();
335 allowAccess.get().set(true);
336 try {
337 return PlatformLogger.getLogger(name);
338 } finally {
339 allowAccess.get().set(old);
340 }
341 }
342
343 static enum TestCases {NOSECURITY, NOPERMISSIONS, WITHPERMISSIONS};
344
345 static void setSecurityManager() {
346 if (System.getSecurityManager() == null) {
347 Policy.setPolicy(new SimplePolicy(allowControl, allowAccess, allowAll));
348 System.setSecurityManager(new SecurityManager());
349 }
350 }
351
352 public static void main(String[] args) {
|