16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package jdk.internal.logger;
26
27 import java.io.FilePermission;
28 import java.security.AccessController;
29 import java.security.Permission;
30 import java.security.PrivilegedAction;
31 import java.util.Iterator;
32 import java.util.Locale;
33 import java.util.ServiceConfigurationError;
34 import java.util.ServiceLoader;
35 import sun.security.util.SecurityConstants;
36
37 /**
38 * Helper class used to load the {@link java.lang.System.LoggerFinder}.
39 */
40 public final class LoggerFinderLoader {
41 private static volatile System.LoggerFinder service;
42 private static final Object lock = new int[0];
43 static final Permission CLASSLOADER_PERMISSION =
44 SecurityConstants.GET_CLASSLOADER_PERMISSION;
45 static final Permission READ_PERMISSION =
46 new FilePermission("<<ALL FILES>>",
47 SecurityConstants.FILE_READ_ACTION);
48 public static final RuntimePermission LOGGERFINDER_PERMISSION =
49 new RuntimePermission("loggerFinder");
50
51 // This is used to control how the LoggerFinderLoader handles
52 // errors when instantiating the LoggerFinder provider.
53 // ERROR => throws ServiceConfigurationError
54 // WARNING => Do not fail, use plain default (simple logger) implementation,
55 // prints warning on console. (this is the default)
62 private LoggerFinderLoader() {
63 throw new InternalError("LoggerFinderLoader cannot be instantiated");
64 }
65
66
67 // Return the loaded LoggerFinder, or load it if not already loaded.
68 private static System.LoggerFinder service() {
69 if (service != null) return service;
70 synchronized(lock) {
71 if (service != null) return service;
72 service = loadLoggerFinder();
73 }
74 // Since the LoggerFinder is already loaded - we can stop using
75 // temporary loggers.
76 BootstrapLogger.redirectTemporaryLoggers();
77 return service;
78 }
79
80 // Get configuration error policy
81 private static ErrorPolicy configurationErrorPolicy() {
82 final PrivilegedAction<String> getConfigurationErrorPolicy =
83 () -> System.getProperty("jdk.logger.finder.error");
84 String errorPolicy = AccessController.doPrivileged(getConfigurationErrorPolicy);
85 if (errorPolicy == null || errorPolicy.isEmpty()) {
86 return ErrorPolicy.WARNING;
87 }
88 try {
89 return ErrorPolicy.valueOf(errorPolicy.toUpperCase(Locale.ROOT));
90 } catch (IllegalArgumentException x) {
91 return ErrorPolicy.WARNING;
92 }
93 }
94
95 // Whether multiple provider should be considered as an error.
96 // This is further submitted to the configuration error policy.
97 private static boolean ensureSingletonProvider() {
98 final PrivilegedAction<Boolean> ensureSingletonProvider =
99 () -> Boolean.getBoolean("jdk.logger.finder.singleton");
100 return AccessController.doPrivileged(ensureSingletonProvider);
101 }
102
103 private static Iterator<System.LoggerFinder> findLoggerFinderProviders() {
104 final Iterator<System.LoggerFinder> iterator;
105 if (System.getSecurityManager() == null) {
106 iterator = ServiceLoader.load(System.LoggerFinder.class,
107 ClassLoader.getSystemClassLoader()).iterator();
108 } else {
109 final PrivilegedAction<Iterator<System.LoggerFinder>> pa =
110 () -> ServiceLoader.load(System.LoggerFinder.class,
111 ClassLoader.getSystemClassLoader()).iterator();
112 iterator = AccessController.doPrivileged(pa, null,
113 LOGGERFINDER_PERMISSION, CLASSLOADER_PERMISSION,
114 READ_PERMISSION);
115 }
116 return iterator;
117 }
118
119 // Loads the LoggerFinder using ServiceLoader. If no LoggerFinder
120 // is found returns the default (possibly JUL based) implementation
|
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package jdk.internal.logger;
26
27 import java.io.FilePermission;
28 import java.security.AccessController;
29 import java.security.Permission;
30 import java.security.PrivilegedAction;
31 import java.util.Iterator;
32 import java.util.Locale;
33 import java.util.ServiceConfigurationError;
34 import java.util.ServiceLoader;
35 import sun.security.util.SecurityConstants;
36 import sun.security.action.GetPropertyAction;
37
38 /**
39 * Helper class used to load the {@link java.lang.System.LoggerFinder}.
40 */
41 public final class LoggerFinderLoader {
42 private static volatile System.LoggerFinder service;
43 private static final Object lock = new int[0];
44 static final Permission CLASSLOADER_PERMISSION =
45 SecurityConstants.GET_CLASSLOADER_PERMISSION;
46 static final Permission READ_PERMISSION =
47 new FilePermission("<<ALL FILES>>",
48 SecurityConstants.FILE_READ_ACTION);
49 public static final RuntimePermission LOGGERFINDER_PERMISSION =
50 new RuntimePermission("loggerFinder");
51
52 // This is used to control how the LoggerFinderLoader handles
53 // errors when instantiating the LoggerFinder provider.
54 // ERROR => throws ServiceConfigurationError
55 // WARNING => Do not fail, use plain default (simple logger) implementation,
56 // prints warning on console. (this is the default)
63 private LoggerFinderLoader() {
64 throw new InternalError("LoggerFinderLoader cannot be instantiated");
65 }
66
67
68 // Return the loaded LoggerFinder, or load it if not already loaded.
69 private static System.LoggerFinder service() {
70 if (service != null) return service;
71 synchronized(lock) {
72 if (service != null) return service;
73 service = loadLoggerFinder();
74 }
75 // Since the LoggerFinder is already loaded - we can stop using
76 // temporary loggers.
77 BootstrapLogger.redirectTemporaryLoggers();
78 return service;
79 }
80
81 // Get configuration error policy
82 private static ErrorPolicy configurationErrorPolicy() {
83 String errorPolicy =
84 GetPropertyAction.getProperty("jdk.logger.finder.error");
85 if (errorPolicy == null || errorPolicy.isEmpty()) {
86 return ErrorPolicy.WARNING;
87 }
88 try {
89 return ErrorPolicy.valueOf(errorPolicy.toUpperCase(Locale.ROOT));
90 } catch (IllegalArgumentException x) {
91 return ErrorPolicy.WARNING;
92 }
93 }
94
95 // Whether multiple provider should be considered as an error.
96 // This is further submitted to the configuration error policy.
97 private static boolean ensureSingletonProvider() {
98 return Boolean.parseBoolean(
99 GetPropertyAction.getProperty("jdk.logger.finder.singleton"));
100 }
101
102 private static Iterator<System.LoggerFinder> findLoggerFinderProviders() {
103 final Iterator<System.LoggerFinder> iterator;
104 if (System.getSecurityManager() == null) {
105 iterator = ServiceLoader.load(System.LoggerFinder.class,
106 ClassLoader.getSystemClassLoader()).iterator();
107 } else {
108 final PrivilegedAction<Iterator<System.LoggerFinder>> pa =
109 () -> ServiceLoader.load(System.LoggerFinder.class,
110 ClassLoader.getSystemClassLoader()).iterator();
111 iterator = AccessController.doPrivileged(pa, null,
112 LOGGERFINDER_PERMISSION, CLASSLOADER_PERMISSION,
113 READ_PERMISSION);
114 }
115 return iterator;
116 }
117
118 // Loads the LoggerFinder using ServiceLoader. If no LoggerFinder
119 // is found returns the default (possibly JUL based) implementation
|