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.Queue;
39 import java.util.ResourceBundle;
40 import java.util.concurrent.ArrayBlockingQueue;
41 import java.util.concurrent.ConcurrentHashMap;
42 import java.util.concurrent.atomic.AtomicBoolean;
43 import java.util.concurrent.atomic.AtomicLong;
44 import java.util.function.Supplier;
45 import sun.util.logging.PlatformLogger;
46 import java.lang.System.LoggerFinder;
47 import java.lang.System.Logger;
48 import java.lang.System.Logger.Level;
49 import java.util.stream.Stream;
50
51 /**
52 * @test
53 * @bug 8140364
54 * @summary JDK implementation specific unit test for JDK internal artifacts.
55 * Tests a naive implementation of System.Logger, and in particular
56 * the default mapping provided by PlatformLogger.Bridge.
57 * @modules java.base/sun.util.logging java.base/jdk.internal.logger
58 * @build CustomSystemClassLoader BaseLoggerBridgeTest
59 * @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader BaseLoggerBridgeTest NOSECURITY
60 * @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader BaseLoggerBridgeTest NOPERMISSIONS
61 * @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader BaseLoggerBridgeTest WITHPERMISSIONS
62 * @author danielfuchs
63 */
64 public class BaseLoggerBridgeTest {
65
66 static final RuntimePermission LOGGERFINDER_PERMISSION =
67 new RuntimePermission("loggerFinder");
68 final static AtomicLong sequencer = new AtomicLong();
69 final static boolean VERBOSE = false;
70 // whether the implementation of Logger try to do a best
71 // effort for logp... Our base logger finder stub doesn't
72 // support logp, and thus the logp() implementation comes from
73 // LoggerWrapper - which does a best effort.
74 static final boolean BEST_EFFORT_FOR_LOGP = true;
75 static final ThreadLocal<AtomicBoolean> allowControl = new ThreadLocal<AtomicBoolean>() {
76 @Override
77 protected AtomicBoolean initialValue() {
78 return new AtomicBoolean(false);
79 }
80 };
81 static final ThreadLocal<AtomicBoolean> allowAccess = new ThreadLocal<AtomicBoolean>() {
82 @Override
83 protected AtomicBoolean initialValue() {
84 return new AtomicBoolean(false);
85 }
86 };
87 static final ThreadLocal<AtomicBoolean> allowAll = new ThreadLocal<AtomicBoolean>() {
88 @Override
89 protected AtomicBoolean initialValue() {
90 return new AtomicBoolean(false);
91 }
92 };
93
94 static final Class<?> providerClass;
95 static {
96 try {
97 providerClass = ClassLoader.getSystemClassLoader().loadClass("BaseLoggerBridgeTest$BaseLoggerFinder");
98 } catch (ClassNotFoundException ex) {
99 throw new ExceptionInInitializerError(ex);
100 }
101 }
102
103 static final sun.util.logging.PlatformLogger.Level[] julLevels = {
104 sun.util.logging.PlatformLogger.Level.ALL,
105 sun.util.logging.PlatformLogger.Level.FINEST,
106 sun.util.logging.PlatformLogger.Level.FINER,
107 sun.util.logging.PlatformLogger.Level.FINE,
108 sun.util.logging.PlatformLogger.Level.CONFIG,
109 sun.util.logging.PlatformLogger.Level.INFO,
110 sun.util.logging.PlatformLogger.Level.WARNING,
111 sun.util.logging.PlatformLogger.Level.SEVERE,
112 sun.util.logging.PlatformLogger.Level.OFF,
113 };
114
115 static final Level[] mappedLevels = {
116 Level.ALL, // ALL
117 Level.TRACE, // FINEST
319
320 @Override
321 public void log(Level level, ResourceBundle bundle, String format, Object... params) {
322 log(LogEvent.of(isLoggable(level), name, level, bundle, format, params));
323 }
324
325 void log(LogEvent event) {
326 eventQueue.add(event);
327 }
328
329 @Override
330 public void log(Level level, Supplier<String> msgSupplier) {
331 log(LogEvent.of(isLoggable(level), name, level, null, msgSupplier));
332 }
333
334 @Override
335 public void log(Level level, Supplier<String> msgSupplier, Throwable thrown) {
336 log(LogEvent.of(isLoggable(level), name, level, thrown, msgSupplier));
337 }
338
339
340
341 }
342
343 public Logger getLogger(String name, Module caller);
344 public Logger getLocalizedLogger(String name, ResourceBundle bundle, Module caller);
345 }
346
347 public static class BaseLoggerFinder extends LoggerFinder implements TestLoggerFinder {
348 static final RuntimePermission LOGGERFINDER_PERMISSION =
349 new RuntimePermission("loggerFinder");
350 @Override
351 public Logger getLogger(String name, Module caller) {
352 SecurityManager sm = System.getSecurityManager();
353 if (sm != null) {
354 sm.checkPermission(LOGGERFINDER_PERMISSION);
355 }
356 PrivilegedAction<ClassLoader> pa = () -> caller.getClassLoader();
357 ClassLoader callerLoader = AccessController.doPrivileged(pa);
358 if (callerLoader == null) {
359 return system.computeIfAbsent(name, (n) -> new LoggerImpl(n));
360 } else {
361 return user.computeIfAbsent(name, (n) -> new LoggerImpl(n));
362 }
363 }
364 }
365
366 static PlatformLogger.Bridge convert(Logger logger) {
367 boolean old = allowAll.get().get();
368 allowAccess.get().set(true);
369 try {
370 return PlatformLogger.Bridge.convert(logger);
371 } finally {
372 allowAccess.get().set(old);
373 }
374 }
375
376 static Logger getLogger(String name, Module caller) {
377 boolean old = allowAll.get().get();
378 allowAccess.get().set(true);
379 try {
380 return jdk.internal.logger.LazyLoggers.getLogger(name, caller);
381 } finally {
382 allowAccess.get().set(old);
383 }
384 }
385
|
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.Queue;
39 import java.util.ResourceBundle;
40 import java.util.concurrent.ArrayBlockingQueue;
41 import java.util.concurrent.ConcurrentHashMap;
42 import java.util.concurrent.atomic.AtomicBoolean;
43 import java.util.concurrent.atomic.AtomicLong;
44 import java.util.function.Supplier;
45 import sun.util.logging.PlatformLogger;
46 import java.lang.System.LoggerFinder;
47 import java.lang.System.Logger;
48 import java.lang.System.Logger.Level;
49 import java.util.stream.Stream;
50
51 /**
52 * @test
53 * @bug 8140364
54 * @summary JDK implementation specific unit test for JDK internal artifacts.
55 * Tests a naive implementation of System.Logger, and in particular
56 * the default mapping provided by PlatformLogger.Bridge.
57 * @modules java.base/sun.util.logging java.base/jdk.internal.logger
58 * @build CustomSystemClassLoader BaseLoggerFinder BaseLoggerBridgeTest
59 * @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader BaseLoggerBridgeTest NOSECURITY
60 * @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader BaseLoggerBridgeTest NOPERMISSIONS
61 * @run main/othervm -Djava.system.class.loader=CustomSystemClassLoader BaseLoggerBridgeTest WITHPERMISSIONS
62 * @author danielfuchs
63 */
64 public class BaseLoggerBridgeTest {
65
66 static final RuntimePermission LOGGERFINDER_PERMISSION =
67 new RuntimePermission("loggerFinder");
68 final static AtomicLong sequencer = new AtomicLong();
69 final static boolean VERBOSE = false;
70 // whether the implementation of Logger try to do a best
71 // effort for logp... Our base logger finder stub doesn't
72 // support logp, and thus the logp() implementation comes from
73 // LoggerWrapper - which does a best effort.
74 static final boolean BEST_EFFORT_FOR_LOGP = true;
75 static final ThreadLocal<AtomicBoolean> allowControl = new ThreadLocal<AtomicBoolean>() {
76 @Override
77 protected AtomicBoolean initialValue() {
78 return new AtomicBoolean(false);
79 }
80 };
81 static final ThreadLocal<AtomicBoolean> allowAccess = new ThreadLocal<AtomicBoolean>() {
82 @Override
83 protected AtomicBoolean initialValue() {
84 return new AtomicBoolean(false);
85 }
86 };
87 static final ThreadLocal<AtomicBoolean> allowAll = new ThreadLocal<AtomicBoolean>() {
88 @Override
89 protected AtomicBoolean initialValue() {
90 return new AtomicBoolean(false);
91 }
92 };
93
94 static final Class<?> providerClass;
95 static {
96 try {
97 providerClass = ClassLoader.getSystemClassLoader().loadClass("BaseLoggerFinder");
98 } catch (ClassNotFoundException ex) {
99 throw new ExceptionInInitializerError(ex);
100 }
101 }
102
103 static final sun.util.logging.PlatformLogger.Level[] julLevels = {
104 sun.util.logging.PlatformLogger.Level.ALL,
105 sun.util.logging.PlatformLogger.Level.FINEST,
106 sun.util.logging.PlatformLogger.Level.FINER,
107 sun.util.logging.PlatformLogger.Level.FINE,
108 sun.util.logging.PlatformLogger.Level.CONFIG,
109 sun.util.logging.PlatformLogger.Level.INFO,
110 sun.util.logging.PlatformLogger.Level.WARNING,
111 sun.util.logging.PlatformLogger.Level.SEVERE,
112 sun.util.logging.PlatformLogger.Level.OFF,
113 };
114
115 static final Level[] mappedLevels = {
116 Level.ALL, // ALL
117 Level.TRACE, // FINEST
319
320 @Override
321 public void log(Level level, ResourceBundle bundle, String format, Object... params) {
322 log(LogEvent.of(isLoggable(level), name, level, bundle, format, params));
323 }
324
325 void log(LogEvent event) {
326 eventQueue.add(event);
327 }
328
329 @Override
330 public void log(Level level, Supplier<String> msgSupplier) {
331 log(LogEvent.of(isLoggable(level), name, level, null, msgSupplier));
332 }
333
334 @Override
335 public void log(Level level, Supplier<String> msgSupplier, Throwable thrown) {
336 log(LogEvent.of(isLoggable(level), name, level, thrown, msgSupplier));
337 }
338
339 }
340
341 public Logger getLogger(String name, Module caller);
342 public Logger getLocalizedLogger(String name, ResourceBundle bundle, Module caller);
343 }
344
345 static PlatformLogger.Bridge convert(Logger logger) {
346 boolean old = allowAll.get().get();
347 allowAccess.get().set(true);
348 try {
349 return PlatformLogger.Bridge.convert(logger);
350 } finally {
351 allowAccess.get().set(old);
352 }
353 }
354
355 static Logger getLogger(String name, Module caller) {
356 boolean old = allowAll.get().get();
357 allowAccess.get().set(true);
358 try {
359 return jdk.internal.logger.LazyLoggers.getLogger(name, caller);
360 } finally {
361 allowAccess.get().set(old);
362 }
363 }
364
|