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 {
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      */
isAvailable()70     public static boolean isAvailable() {
71         return proxy != null;
72     }
73 
ensureAvailable()74     private static void ensureAvailable() {
75         if (proxy == null)
76             throw new AssertionError("Should not here");
77     }
78 
getLoggerNames()79     public static java.util.List<String> getLoggerNames() {
80         ensureAvailable();
81         return proxy.getLoggerNames();
82     }
getLoggerLevel(String loggerName)83     public static String getLoggerLevel(String loggerName) {
84         ensureAvailable();
85         return proxy.getLoggerLevel(loggerName);
86     }
87 
setLoggerLevel(String loggerName, String levelName)88     public static void setLoggerLevel(String loggerName, String levelName) {
89         ensureAvailable();
90         proxy.setLoggerLevel(loggerName, levelName);
91     }
92 
getParentLoggerName(String loggerName)93     public static String getParentLoggerName(String loggerName) {
94         ensureAvailable();
95         return proxy.getParentLoggerName(loggerName);
96     }
97 
getLogger(String name)98     public static Object getLogger(String name) {
99         ensureAvailable();
100         return proxy.getLogger(name);
101     }
102 
getLevel(Object logger)103     public static Object getLevel(Object logger) {
104         ensureAvailable();
105         return proxy.getLevel(logger);
106     }
107 
setLevel(Object logger, Object newLevel)108     public static void setLevel(Object logger, Object newLevel) {
109         ensureAvailable();
110         proxy.setLevel(logger, newLevel);
111     }
112 
isLoggable(Object logger, Object level)113     public static boolean isLoggable(Object logger, Object level) {
114         ensureAvailable();
115         return proxy.isLoggable(logger,level);
116     }
117 
log(Object logger, Object level, String msg)118     public static void log(Object logger, Object level, String msg) {
119         ensureAvailable();
120         proxy.log(logger, level, msg);
121     }
122 
log(Object logger, Object level, String msg, Throwable t)123     public static void log(Object logger, Object level, String msg, Throwable t) {
124         ensureAvailable();
125         proxy.log(logger, level, msg, t);
126     }
127 
log(Object logger, Object level, String msg, Object... params)128     public static void log(Object logger, Object level, String msg, Object... params) {
129         ensureAvailable();
130         proxy.log(logger, level, msg, params);
131     }
132 
parseLevel(String levelName)133     public static Object parseLevel(String levelName) {
134         ensureAvailable();
135         return proxy.parseLevel(levelName);
136     }
137 
getLevelName(Object level)138     public static String getLevelName(Object level) {
139         ensureAvailable();
140         return proxy.getLevelName(level);
141     }
142 
getLevelValue(Object level)143     public static int getLevelValue(Object level) {
144         ensureAvailable();
145         return proxy.getLevelValue(level);
146     }
147 
148     private static final String DEFAULT_FORMAT =
149         "%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%6$s%n";
150 
151     private static final String FORMAT_PROP_KEY = "java.util.logging.SimpleFormatter.format";
getSimpleFormat()152     public static String getSimpleFormat() {
153         return getSimpleFormat(true);
154     }
155 
156     // useProxy if true will cause initialization of
157     // java.util.logging and read its configuration
getSimpleFormat(boolean useProxy)158     static String getSimpleFormat(boolean useProxy) {
159         String format =
160             AccessController.doPrivileged(
161                 new PrivilegedAction<String>() {
162                     public String run() {
163                         return System.getProperty(FORMAT_PROP_KEY);
164                     }
165                 });
166 
167         if (useProxy && proxy != null && format == null) {
168             format = proxy.getProperty(FORMAT_PROP_KEY);
169         }
170 
171         if (format != null) {
172             try {
173                 // validate the user-defined format string
174                 String.format(format, new Date(), "", "", "", "", "");
175             } catch (IllegalArgumentException e) {
176                 // illegal syntax; fall back to the default format
177                 format = DEFAULT_FORMAT;
178             }
179         } else {
180             format = DEFAULT_FORMAT;
181         }
182         return format;
183     }
184 
185 }
186