History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/util/logging/LoggingSupport.java (1 of 18352 files). Show all

Unified · Split

java/pkg/sun/util/logging/LoggingSupport.java+180−0
@@ -0,0 +1,180 @@
1+/*
2+ * Copyright (c) 2009, 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. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle in the LICENSE file that accompanied this code.
10+ *
11+ * This code is distributed in the hope that it will be useful, but WITHOUT
12+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+ * version 2 for more details (a copy is included in the LICENSE file that
15+ * accompanied this code).
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+
26+
27+package sun.util.logging;
28+
29+import java.lang.reflect.Field;
30+import java.security.AccessController;
31+import java.security.PrivilegedAction;
32+import java.util.Date;
33+
34+/**
35+ * Internal API to support JRE implementation to detect if the java.util.logging
36+ * support is available but with no dependency on the java.util.logging
37+ * classes. This LoggingSupport class provides several static methods to
38+ * access the java.util.logging functionality that requires the caller
39+ * to ensure that the logging support is {@linkplain #isAvailable available}
40+ * before invoking it.
41+ *
42+ * @see sun.util.logging.PlatformLogger if you want to log messages even
43+ * if the logging support is not available
44+ */
45+public class LoggingSupport {
46+ private LoggingSupport() { }
47+
48+ private static final LoggingProxy proxy =
49+ AccessController.doPrivileged(new PrivilegedAction<LoggingProxy>() {
50+ public LoggingProxy run() {
51+ try {
52+ // create a LoggingProxyImpl instance when
53+ // java.util.logging classes exist
54+ Class<?> c = Class.forName("java.util.logging.LoggingProxyImpl", true, null);
55+ Field f = c.getDeclaredField("INSTANCE");
56+ f.setAccessible(true);
57+ return (LoggingProxy) f.get(null);
58+ } catch (ClassNotFoundException cnf) {
59+ return null;
60+ } catch (NoSuchFieldException e) {
61+ throw new AssertionError(e);
62+ } catch (IllegalAccessException e) {
63+ throw new AssertionError(e);
64+ }
65+ }});
66+
67+ /**
68+ * Returns true if java.util.logging support is available.
69+ */
70+ public static boolean isAvailable() {
71+ return proxy != null;
72+ }
73+
74+ private static void ensureAvailable() {
75+ if (proxy == null)
76+ throw new AssertionError("Should not here");
77+ }
78+
79+ public static java.util.List<String> getLoggerNames() {
80+ ensureAvailable();
81+ return proxy.getLoggerNames();
82+ }
83+ public static String getLoggerLevel(String loggerName) {
84+ ensureAvailable();
85+ return proxy.getLoggerLevel(loggerName);
86+ }
87+
88+ public static void setLoggerLevel(String loggerName, String levelName) {
89+ ensureAvailable();
90+ proxy.setLoggerLevel(loggerName, levelName);
91+ }
92+
93+ public static String getParentLoggerName(String loggerName) {
94+ ensureAvailable();
95+ return proxy.getParentLoggerName(loggerName);
96+ }
97+
98+ public static Object getLogger(String name) {
99+ ensureAvailable();
100+ return proxy.getLogger(name);
101+ }
102+
103+ public static Object getLevel(Object logger) {
104+ ensureAvailable();
105+ return proxy.getLevel(logger);
106+ }
107+
108+ public static void setLevel(Object logger, Object newLevel) {
109+ ensureAvailable();
110+ proxy.setLevel(logger, newLevel);
111+ }
112+
113+ public static boolean isLoggable(Object logger, Object level) {
114+ ensureAvailable();
115+ return proxy.isLoggable(logger,level);
116+ }
117+
118+ public static void log(Object logger, Object level, String msg) {
119+ ensureAvailable();
120+ proxy.log(logger, level, msg);
121+ }
122+
123+ public static void log(Object logger, Object level, String msg, Throwable t) {
124+ ensureAvailable();
125+ proxy.log(logger, level, msg, t);
126+ }
127+
128+ public static void log(Object logger, Object level, String msg, Object... params) {
129+ ensureAvailable();
130+ proxy.log(logger, level, msg, params);
131+ }
132+
133+ public static Object parseLevel(String levelName) {
134+ ensureAvailable();
135+ return proxy.parseLevel(levelName);
136+ }
137+
138+ public static String getLevelName(Object level) {
139+ ensureAvailable();
140+ return proxy.getLevelName(level);
141+ }
142+
143+ private static final String DEFAULT_FORMAT =
144+ "%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%6$s%n";
145+
146+ private static final String FORMAT_PROP_KEY = "java.util.logging.SimpleFormatter.format";
147+ public static String getSimpleFormat() {
148+ return getSimpleFormat(true);
149+ }
150+
151+ // useProxy if true will cause initialization of
152+ // java.util.logging and read its configuration
153+ static String getSimpleFormat(boolean useProxy) {
154+ String format =
155+ AccessController.doPrivileged(
156+ new PrivilegedAction<String>() {
157+ public String run() {
158+ return System.getProperty(FORMAT_PROP_KEY);
159+ }
160+ });
161+
162+ if (useProxy && proxy != null && format == null) {
163+ format = proxy.getProperty(FORMAT_PROP_KEY);
164+ }
165+
166+ if (format != null) {
167+ try {
168+ // validate the user-defined format string
169+ String.format(format, new Date(), "", "", "", "", "");
170+ } catch (IllegalArgumentException e) {
171+ // illegal syntax; fall back to the default format
172+ format = DEFAULT_FORMAT;
173+ }
174+ } else {
175+ format = DEFAULT_FORMAT;
176+ }
177+ return format;
178+ }
179+
180+}