author | shade |
Wed, 02 May 2018 19:26:44 +0200 | |
changeset 49951 | 65b13c206495 |
parent 47216 | 71c04702a3d5 |
child 53876 | 8bc3d3eeaa53 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
18565 | 2 |
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
||
27 |
package java.util.logging; |
|
28 |
||
29094
a4fd2b5e49f8
8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents:
25859
diff
changeset
|
29 |
import java.util.Objects; |
2 | 30 |
import java.io.UnsupportedEncodingException; |
22110
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
31 |
import java.security.AccessController; |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
32 |
import java.security.PrivilegedAction; |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
33 |
|
2 | 34 |
/** |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
35 |
* A {@code Handler} object takes log messages from a {@code Logger} and |
2 | 36 |
* exports them. It might for example, write them to a console |
37 |
* or write them to a file, or send them to a network logging service, |
|
38 |
* or forward them to an OS log, or whatever. |
|
39 |
* <p> |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
40 |
* A {@code Handler} can be disabled by doing a {@code setLevel(Level.OFF)} |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
41 |
* and can be re-enabled by doing a {@code setLevel} with an appropriate level. |
2 | 42 |
* <p> |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
43 |
* {@code Handler} classes typically use {@code LogManager} properties to set |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
44 |
* default values for the {@code Handler}'s {@code Filter}, {@code Formatter}, |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
45 |
* and {@code Level}. See the specific documentation for each concrete |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
46 |
* {@code Handler} class. |
2 | 47 |
* |
48 |
* |
|
49 |
* @since 1.4 |
|
50 |
*/ |
|
51 |
||
52 |
public abstract class Handler { |
|
53 |
private static final int offValue = Level.OFF.intValue(); |
|
19808
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
54 |
private final LogManager manager = LogManager.getLogManager(); |
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
55 |
|
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
56 |
// We're using volatile here to avoid synchronizing getters, which |
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
57 |
// would prevent other threads from calling isLoggable() |
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
58 |
// while publish() is executing. |
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
59 |
// On the other hand, setters will be synchronized to exclude concurrent |
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
60 |
// execution with more complex methods, such as StreamHandler.publish(). |
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
61 |
// We wouldn't want 'level' to be changed by another thread in the middle |
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
62 |
// of the execution of a 'publish' call. |
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
63 |
private volatile Filter filter; |
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
64 |
private volatile Formatter formatter; |
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
65 |
private volatile Level logLevel = Level.ALL; |
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
66 |
private volatile ErrorManager errorManager = new ErrorManager(); |
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
67 |
private volatile String encoding; |
2 | 68 |
|
69 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
70 |
* Default constructor. The resulting {@code Handler} has a log |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
71 |
* level of {@code Level.ALL}, no {@code Formatter}, and no |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
72 |
* {@code Filter}. A default {@code ErrorManager} instance is installed |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
73 |
* as the {@code ErrorManager}. |
2 | 74 |
*/ |
75 |
protected Handler() { |
|
76 |
} |
|
77 |
||
78 |
/** |
|
22110
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
79 |
* Package-private constructor for chaining from subclass constructors |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
80 |
* that wish to configure the handler with specific default and/or |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
81 |
* specified values. |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
82 |
* |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
83 |
* @param defaultLevel a default {@link Level} to configure if one is |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
84 |
* not found in LogManager configuration properties |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
85 |
* @param defaultFormatter a default {@link Formatter} to configure if one is |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
86 |
* not specified by {@code specifiedFormatter} parameter |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
87 |
* nor found in LogManager configuration properties |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
88 |
* @param specifiedFormatter if not null, this is the formatter to configure |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
89 |
*/ |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
90 |
Handler(Level defaultLevel, Formatter defaultFormatter, |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
91 |
Formatter specifiedFormatter) { |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
92 |
|
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
93 |
LogManager manager = LogManager.getLogManager(); |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
94 |
String cname = getClass().getName(); |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
95 |
|
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
96 |
final Level level = manager.getLevelProperty(cname + ".level", defaultLevel); |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
97 |
final Filter filter = manager.getFilterProperty(cname + ".filter", null); |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
98 |
final Formatter formatter = specifiedFormatter == null |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
99 |
? manager.getFormatterProperty(cname + ".formatter", defaultFormatter) |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
100 |
: specifiedFormatter; |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
101 |
final String encoding = manager.getStringProperty(cname + ".encoding", null); |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
102 |
|
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
103 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
104 |
@Override |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
105 |
public Void run() { |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
106 |
setLevel(level); |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
107 |
setFilter(filter); |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
108 |
setFormatter(formatter); |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
109 |
try { |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
110 |
setEncoding(encoding); |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
111 |
} catch (Exception ex) { |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
112 |
try { |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
113 |
setEncoding(null); |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
114 |
} catch (Exception ex2) { |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
115 |
// doing a setEncoding with null should always work. |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
116 |
// assert false; |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
117 |
} |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
118 |
} |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
119 |
return null; |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
120 |
} |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
121 |
}, null, LogManager.controlPermission); |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
122 |
} |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
123 |
|
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
124 |
/** |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
125 |
* Publish a {@code LogRecord}. |
2 | 126 |
* <p> |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
127 |
* The logging request was made initially to a {@code Logger} object, |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
128 |
* which initialized the {@code LogRecord} and forwarded it here. |
2 | 129 |
* <p> |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
130 |
* The {@code Handler} is responsible for formatting the message, when and |
2 | 131 |
* if necessary. The formatting should include localization. |
132 |
* |
|
133 |
* @param record description of the log event. A null record is |
|
134 |
* silently ignored and is not published |
|
135 |
*/ |
|
136 |
public abstract void publish(LogRecord record); |
|
137 |
||
138 |
/** |
|
139 |
* Flush any buffered output. |
|
140 |
*/ |
|
141 |
public abstract void flush(); |
|
142 |
||
143 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
144 |
* Close the {@code Handler} and free all associated resources. |
2 | 145 |
* <p> |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
146 |
* The close method will perform a {@code flush} and then close the |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
147 |
* {@code Handler}. After close has been called this {@code Handler} |
2 | 148 |
* should no longer be used. Method calls may either be silently |
149 |
* ignored or may throw runtime exceptions. |
|
150 |
* |
|
151 |
* @exception SecurityException if a security manager exists and if |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
152 |
* the caller does not have {@code LoggingPermission("control")}. |
2 | 153 |
*/ |
154 |
public abstract void close() throws SecurityException; |
|
155 |
||
156 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
157 |
* Set a {@code Formatter}. This {@code Formatter} will be used |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
158 |
* to format {@code LogRecords} for this {@code Handler}. |
2 | 159 |
* <p> |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
160 |
* Some {@code Handlers} may not use {@code Formatters}, in |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
161 |
* which case the {@code Formatter} will be remembered, but not used. |
24196
61c9885d76e2
8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents:
22110
diff
changeset
|
162 |
* |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
163 |
* @param newFormatter the {@code Formatter} to use (may not be null) |
2 | 164 |
* @exception SecurityException if a security manager exists and if |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
165 |
* the caller does not have {@code LoggingPermission("control")}. |
2 | 166 |
*/ |
19808
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
167 |
public synchronized void setFormatter(Formatter newFormatter) throws SecurityException { |
14216
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
5506
diff
changeset
|
168 |
checkPermission(); |
29094
a4fd2b5e49f8
8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents:
25859
diff
changeset
|
169 |
formatter = Objects.requireNonNull(newFormatter); |
2 | 170 |
} |
171 |
||
172 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
173 |
* Return the {@code Formatter} for this {@code Handler}. |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
174 |
* @return the {@code Formatter} (may be null). |
2 | 175 |
*/ |
176 |
public Formatter getFormatter() { |
|
177 |
return formatter; |
|
178 |
} |
|
179 |
||
180 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
181 |
* Set the character encoding used by this {@code Handler}. |
2 | 182 |
* <p> |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
183 |
* The encoding should be set before any {@code LogRecords} are written |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
184 |
* to the {@code Handler}. |
2 | 185 |
* |
186 |
* @param encoding The name of a supported character encoding. |
|
187 |
* May be null, to indicate the default platform encoding. |
|
188 |
* @exception SecurityException if a security manager exists and if |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
189 |
* the caller does not have {@code LoggingPermission("control")}. |
2 | 190 |
* @exception UnsupportedEncodingException if the named encoding is |
191 |
* not supported. |
|
192 |
*/ |
|
19808
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
193 |
public synchronized void setEncoding(String encoding) |
2 | 194 |
throws SecurityException, java.io.UnsupportedEncodingException { |
14216
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
5506
diff
changeset
|
195 |
checkPermission(); |
2 | 196 |
if (encoding != null) { |
197 |
try { |
|
198 |
if(!java.nio.charset.Charset.isSupported(encoding)) { |
|
199 |
throw new UnsupportedEncodingException(encoding); |
|
200 |
} |
|
201 |
} catch (java.nio.charset.IllegalCharsetNameException e) { |
|
202 |
throw new UnsupportedEncodingException(encoding); |
|
203 |
} |
|
204 |
} |
|
205 |
this.encoding = encoding; |
|
206 |
} |
|
207 |
||
208 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
209 |
* Return the character encoding for this {@code Handler}. |
2 | 210 |
* |
211 |
* @return The encoding name. May be null, which indicates the |
|
212 |
* default encoding should be used. |
|
213 |
*/ |
|
214 |
public String getEncoding() { |
|
215 |
return encoding; |
|
216 |
} |
|
217 |
||
218 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
219 |
* Set a {@code Filter} to control output on this {@code Handler}. |
2 | 220 |
* <P> |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
221 |
* For each call of {@code publish} the {@code Handler} will call |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
222 |
* this {@code Filter} (if it is non-null) to check if the |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
223 |
* {@code LogRecord} should be published or discarded. |
2 | 224 |
* |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
225 |
* @param newFilter a {@code Filter} object (may be null) |
2 | 226 |
* @exception SecurityException if a security manager exists and if |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
227 |
* the caller does not have {@code LoggingPermission("control")}. |
2 | 228 |
*/ |
19808
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
229 |
public synchronized void setFilter(Filter newFilter) throws SecurityException { |
14216
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
5506
diff
changeset
|
230 |
checkPermission(); |
2 | 231 |
filter = newFilter; |
232 |
} |
|
233 |
||
234 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
235 |
* Get the current {@code Filter} for this {@code Handler}. |
2 | 236 |
* |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
237 |
* @return a {@code Filter} object (may be null) |
2 | 238 |
*/ |
239 |
public Filter getFilter() { |
|
240 |
return filter; |
|
241 |
} |
|
242 |
||
243 |
/** |
|
244 |
* Define an ErrorManager for this Handler. |
|
245 |
* <p> |
|
246 |
* The ErrorManager's "error" method will be invoked if any |
|
247 |
* errors occur while using this Handler. |
|
248 |
* |
|
249 |
* @param em the new ErrorManager |
|
250 |
* @exception SecurityException if a security manager exists and if |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
251 |
* the caller does not have {@code LoggingPermission("control")}. |
2 | 252 |
*/ |
19808
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
253 |
public synchronized void setErrorManager(ErrorManager em) { |
14216
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
5506
diff
changeset
|
254 |
checkPermission(); |
2 | 255 |
if (em == null) { |
256 |
throw new NullPointerException(); |
|
257 |
} |
|
258 |
errorManager = em; |
|
259 |
} |
|
260 |
||
261 |
/** |
|
262 |
* Retrieves the ErrorManager for this Handler. |
|
263 |
* |
|
18565 | 264 |
* @return the ErrorManager for this Handler |
2 | 265 |
* @exception SecurityException if a security manager exists and if |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
266 |
* the caller does not have {@code LoggingPermission("control")}. |
2 | 267 |
*/ |
268 |
public ErrorManager getErrorManager() { |
|
14216
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
5506
diff
changeset
|
269 |
checkPermission(); |
2 | 270 |
return errorManager; |
271 |
} |
|
272 |
||
273 |
/** |
|
274 |
* Protected convenience method to report an error to this Handler's |
|
275 |
* ErrorManager. Note that this method retrieves and uses the ErrorManager |
|
276 |
* without doing a security check. It can therefore be used in |
|
277 |
* environments where the caller may be non-privileged. |
|
278 |
* |
|
279 |
* @param msg a descriptive string (may be null) |
|
280 |
* @param ex an exception (may be null) |
|
281 |
* @param code an error code defined in ErrorManager |
|
282 |
*/ |
|
283 |
protected void reportError(String msg, Exception ex, int code) { |
|
284 |
try { |
|
285 |
errorManager.error(msg, ex, code); |
|
286 |
} catch (Exception ex2) { |
|
287 |
System.err.println("Handler.reportError caught:"); |
|
288 |
ex2.printStackTrace(); |
|
289 |
} |
|
290 |
} |
|
291 |
||
292 |
/** |
|
293 |
* Set the log level specifying which message levels will be |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
294 |
* logged by this {@code Handler}. Message levels lower than this |
2 | 295 |
* value will be discarded. |
296 |
* <p> |
|
297 |
* The intention is to allow developers to turn on voluminous |
|
298 |
* logging, but to limit the messages that are sent to certain |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
299 |
* {@code Handlers}. |
2 | 300 |
* |
301 |
* @param newLevel the new value for the log level |
|
302 |
* @exception SecurityException if a security manager exists and if |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
303 |
* the caller does not have {@code LoggingPermission("control")}. |
2 | 304 |
*/ |
305 |
public synchronized void setLevel(Level newLevel) throws SecurityException { |
|
306 |
if (newLevel == null) { |
|
307 |
throw new NullPointerException(); |
|
308 |
} |
|
14216
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
5506
diff
changeset
|
309 |
checkPermission(); |
2 | 310 |
logLevel = newLevel; |
311 |
} |
|
312 |
||
313 |
/** |
|
314 |
* Get the log level specifying which messages will be |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
315 |
* logged by this {@code Handler}. Message levels lower |
2 | 316 |
* than this level will be discarded. |
317 |
* @return the level of messages being logged. |
|
318 |
*/ |
|
19808
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
319 |
public Level getLevel() { |
2 | 320 |
return logLevel; |
321 |
} |
|
322 |
||
323 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
324 |
* Check if this {@code Handler} would actually log a given {@code LogRecord}. |
2 | 325 |
* <p> |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
326 |
* This method checks if the {@code LogRecord} has an appropriate |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
327 |
* {@code Level} and whether it satisfies any {@code Filter}. It also |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
328 |
* may make other {@code Handler} specific checks that might prevent a |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
329 |
* handler from logging the {@code LogRecord}. It will return false if |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
330 |
* the {@code LogRecord} is null. |
24196
61c9885d76e2
8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents:
22110
diff
changeset
|
331 |
* |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
332 |
* @param record a {@code LogRecord} |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
29094
diff
changeset
|
333 |
* @return true if the {@code LogRecord} would be logged. |
2 | 334 |
* |
335 |
*/ |
|
336 |
public boolean isLoggable(LogRecord record) { |
|
19808
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
337 |
final int levelValue = getLevel().intValue(); |
2 | 338 |
if (record.getLevel().intValue() < levelValue || levelValue == offValue) { |
339 |
return false; |
|
340 |
} |
|
19808
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
18565
diff
changeset
|
341 |
final Filter filter = getFilter(); |
2 | 342 |
if (filter == null) { |
343 |
return true; |
|
344 |
} |
|
345 |
return filter.isLoggable(record); |
|
346 |
} |
|
347 |
||
348 |
// Package-private support method for security checks. |
|
22110
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
349 |
// We check that the caller has appropriate security privileges |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
350 |
// to update Handler state and if not throw a SecurityException. |
14216
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
5506
diff
changeset
|
351 |
void checkPermission() throws SecurityException { |
22110
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
352 |
manager.checkPermission(); |
2 | 353 |
} |
354 |
} |