author | dfuchs |
Thu, 06 Apr 2017 14:38:15 +0100 | |
changeset 44538 | a603c475d649 |
parent 32037 | ab4526f4ac10 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
22110
diff
changeset
|
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 |
||
29 |
import java.io.*; |
|
22110
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
30 |
import java.security.AccessController; |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
31 |
import java.security.PrivilegedAction; |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
32 |
import java.util.Objects; |
2 | 33 |
|
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:
25859
diff
changeset
|
35 |
* Stream based logging {@code Handler}. |
2 | 36 |
* <p> |
37 |
* This is primarily intended as a base class or support class to |
|
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:
25859
diff
changeset
|
38 |
* be used in implementing other logging {@code Handlers}. |
2 | 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:
25859
diff
changeset
|
40 |
* {@code LogRecords} are published to a given {@code java.io.OutputStream}. |
2 | 41 |
* <p> |
42 |
* <b>Configuration:</b> |
|
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:
25859
diff
changeset
|
43 |
* By default each {@code StreamHandler} is initialized using the following |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
25859
diff
changeset
|
44 |
* {@code LogManager} configuration properties where {@code <handler-name>} |
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
45 |
* refers to the fully-qualified class name of the handler. |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
46 |
* If properties are not defined |
2 | 47 |
* (or have invalid values) then the specified default values are used. |
48 |
* <ul> |
|
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
49 |
* <li> <handler-name>.level |
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:
25859
diff
changeset
|
50 |
* specifies the default level for the {@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:
25859
diff
changeset
|
51 |
* (defaults to {@code Level.INFO}). </li> |
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
52 |
* <li> <handler-name>.filter |
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:
25859
diff
changeset
|
53 |
* specifies the name of a {@code Filter} class to use |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
25859
diff
changeset
|
54 |
* (defaults to no {@code Filter}). </li> |
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
55 |
* <li> <handler-name>.formatter |
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:
25859
diff
changeset
|
56 |
* specifies the name of a {@code Formatter} class to use |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
25859
diff
changeset
|
57 |
* (defaults to {@code java.util.logging.SimpleFormatter}). </li> |
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
58 |
* <li> <handler-name>.encoding |
2 | 59 |
* the name of the character set encoding to use (defaults to |
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
60 |
* the default platform encoding). </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
61 |
* </ul> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
62 |
* <p> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
63 |
* For example, the properties for {@code StreamHandler} would be: |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
64 |
* <ul> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
65 |
* <li> java.util.logging.StreamHandler.level=INFO </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
66 |
* <li> java.util.logging.StreamHandler.formatter=java.util.logging.SimpleFormatter </li> |
2 | 67 |
* </ul> |
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
68 |
* <p> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
69 |
* For a custom handler, e.g. com.foo.MyHandler, the properties would be: |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
70 |
* <ul> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
71 |
* <li> com.foo.MyHandler.level=INFO </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
72 |
* <li> com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
73 |
* </ul> |
24196
61c9885d76e2
8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents:
23010
diff
changeset
|
74 |
* |
2 | 75 |
* @since 1.4 |
76 |
*/ |
|
77 |
||
78 |
public class StreamHandler extends Handler { |
|
79 |
private OutputStream output; |
|
80 |
private boolean doneHeader; |
|
19808
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
14333
diff
changeset
|
81 |
private volatile Writer writer; |
2 | 82 |
|
83 |
/** |
|
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:
25859
diff
changeset
|
84 |
* Create a {@code StreamHandler}, with no current output stream. |
2 | 85 |
*/ |
86 |
public StreamHandler() { |
|
22110
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
87 |
// configure with specific defaults for StreamHandler |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
88 |
super(Level.INFO, new SimpleFormatter(), null); |
2 | 89 |
} |
90 |
||
91 |
/** |
|
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:
25859
diff
changeset
|
92 |
* Create a {@code StreamHandler} with a given {@code Formatter} |
2 | 93 |
* and output stream. |
24196
61c9885d76e2
8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents:
23010
diff
changeset
|
94 |
* |
2 | 95 |
* @param out the target output stream |
96 |
* @param formatter Formatter to be used to format output |
|
97 |
*/ |
|
98 |
public StreamHandler(OutputStream out, Formatter formatter) { |
|
22110
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
99 |
// configure with default level but use specified formatter |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
100 |
super(Level.INFO, null, Objects.requireNonNull(formatter)); |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
101 |
|
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
102 |
setOutputStreamPrivileged(out); |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
103 |
} |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
104 |
|
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
105 |
/** |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
106 |
* @see Handler#Handler(Level, Formatter, Formatter) |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
107 |
*/ |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
108 |
StreamHandler(Level defaultLevel, |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
109 |
Formatter defaultFormatter, |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
110 |
Formatter specifiedFormatter) { |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
111 |
super(defaultLevel, defaultFormatter, specifiedFormatter); |
2 | 112 |
} |
113 |
||
114 |
/** |
|
115 |
* Change the output stream. |
|
116 |
* <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:
25859
diff
changeset
|
117 |
* If there is a current output stream then the {@code Formatter}'s |
2 | 118 |
* tail string is written and the stream is flushed and closed. |
119 |
* Then the output stream is replaced with the new output stream. |
|
120 |
* |
|
121 |
* @param out New output stream. May not be null. |
|
122 |
* @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:
25859
diff
changeset
|
123 |
* the caller does not have {@code LoggingPermission("control")}. |
2 | 124 |
*/ |
125 |
protected synchronized void setOutputStream(OutputStream out) throws SecurityException { |
|
126 |
if (out == null) { |
|
127 |
throw new NullPointerException(); |
|
128 |
} |
|
129 |
flushAndClose(); |
|
130 |
output = out; |
|
131 |
doneHeader = false; |
|
132 |
String encoding = getEncoding(); |
|
133 |
if (encoding == null) { |
|
134 |
writer = new OutputStreamWriter(output); |
|
135 |
} else { |
|
136 |
try { |
|
137 |
writer = new OutputStreamWriter(output, encoding); |
|
138 |
} catch (UnsupportedEncodingException ex) { |
|
139 |
// This shouldn't happen. The setEncoding method |
|
140 |
// should have validated that the encoding is OK. |
|
141 |
throw new Error("Unexpected exception " + ex); |
|
142 |
} |
|
143 |
} |
|
144 |
} |
|
145 |
||
146 |
/** |
|
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:
25859
diff
changeset
|
147 |
* Set (or change) the character encoding used by this {@code Handler}. |
2 | 148 |
* <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:
25859
diff
changeset
|
149 |
* 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:
25859
diff
changeset
|
150 |
* to the {@code Handler}. |
2 | 151 |
* |
152 |
* @param encoding The name of a supported character encoding. |
|
153 |
* May be null, to indicate the default platform encoding. |
|
154 |
* @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:
25859
diff
changeset
|
155 |
* the caller does not have {@code LoggingPermission("control")}. |
2 | 156 |
* @exception UnsupportedEncodingException if the named encoding is |
157 |
* not supported. |
|
158 |
*/ |
|
19808
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
14333
diff
changeset
|
159 |
@Override |
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
14333
diff
changeset
|
160 |
public synchronized void setEncoding(String encoding) |
2 | 161 |
throws SecurityException, java.io.UnsupportedEncodingException { |
162 |
super.setEncoding(encoding); |
|
163 |
if (output == null) { |
|
164 |
return; |
|
165 |
} |
|
166 |
// Replace the current writer with a writer for the new encoding. |
|
167 |
flush(); |
|
168 |
if (encoding == null) { |
|
169 |
writer = new OutputStreamWriter(output); |
|
170 |
} else { |
|
171 |
writer = new OutputStreamWriter(output, encoding); |
|
172 |
} |
|
173 |
} |
|
174 |
||
175 |
/** |
|
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:
25859
diff
changeset
|
176 |
* Format and publish a {@code LogRecord}. |
2 | 177 |
* <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:
25859
diff
changeset
|
178 |
* The {@code StreamHandler} first checks if there is an {@code OutputStream} |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
25859
diff
changeset
|
179 |
* and if the given {@code LogRecord} has at least the required log level. |
2 | 180 |
* If not it silently returns. If so, it calls any associated |
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:
25859
diff
changeset
|
181 |
* {@code Filter} to check if the record should be published. If so, |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
25859
diff
changeset
|
182 |
* it calls its {@code Formatter} to format the record and then writes |
2 | 183 |
* the result to the current output stream. |
184 |
* <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:
25859
diff
changeset
|
185 |
* If this is the first {@code LogRecord} to be written to a given |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
25859
diff
changeset
|
186 |
* {@code OutputStream}, the {@code Formatter}'s "head" string is |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
25859
diff
changeset
|
187 |
* written to the stream before the {@code LogRecord} is written. |
2 | 188 |
* |
189 |
* @param record description of the log event. A null record is |
|
190 |
* silently ignored and is not published |
|
191 |
*/ |
|
19808
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
14333
diff
changeset
|
192 |
@Override |
2 | 193 |
public synchronized void publish(LogRecord record) { |
194 |
if (!isLoggable(record)) { |
|
195 |
return; |
|
196 |
} |
|
197 |
String msg; |
|
198 |
try { |
|
199 |
msg = getFormatter().format(record); |
|
200 |
} catch (Exception ex) { |
|
201 |
// We don't want to throw an exception here, but we |
|
202 |
// report the exception to any registered ErrorManager. |
|
203 |
reportError(null, ex, ErrorManager.FORMAT_FAILURE); |
|
204 |
return; |
|
205 |
} |
|
206 |
||
207 |
try { |
|
208 |
if (!doneHeader) { |
|
209 |
writer.write(getFormatter().getHead(this)); |
|
210 |
doneHeader = true; |
|
211 |
} |
|
212 |
writer.write(msg); |
|
213 |
} catch (Exception ex) { |
|
214 |
// We don't want to throw an exception here, but we |
|
215 |
// report the exception to any registered ErrorManager. |
|
216 |
reportError(null, ex, ErrorManager.WRITE_FAILURE); |
|
217 |
} |
|
218 |
} |
|
219 |
||
220 |
||
221 |
/** |
|
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:
25859
diff
changeset
|
222 |
* Check if this {@code Handler} would actually log a given {@code LogRecord}. |
2 | 223 |
* <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:
25859
diff
changeset
|
224 |
* This method checks if the {@code LogRecord} has an appropriate level and |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
25859
diff
changeset
|
225 |
* whether it satisfies any {@code Filter}. It will also return false if |
3853 | 226 |
* no output stream has been assigned yet or the LogRecord is null. |
24196
61c9885d76e2
8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents:
23010
diff
changeset
|
227 |
* |
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:
25859
diff
changeset
|
228 |
* @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:
25859
diff
changeset
|
229 |
* @return true if the {@code LogRecord} would be logged. |
2 | 230 |
* |
231 |
*/ |
|
19808
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
14333
diff
changeset
|
232 |
@Override |
2 | 233 |
public boolean isLoggable(LogRecord record) { |
234 |
if (writer == null || record == null) { |
|
235 |
return false; |
|
236 |
} |
|
237 |
return super.isLoggable(record); |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* Flush any buffered messages. |
|
242 |
*/ |
|
19808
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
14333
diff
changeset
|
243 |
@Override |
2 | 244 |
public synchronized void flush() { |
245 |
if (writer != null) { |
|
246 |
try { |
|
247 |
writer.flush(); |
|
248 |
} catch (Exception ex) { |
|
249 |
// We don't want to throw an exception here, but we |
|
250 |
// report the exception to any registered ErrorManager. |
|
251 |
reportError(null, ex, ErrorManager.FLUSH_FAILURE); |
|
252 |
} |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
private synchronized void flushAndClose() throws SecurityException { |
|
14216
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
5506
diff
changeset
|
257 |
checkPermission(); |
2 | 258 |
if (writer != null) { |
259 |
try { |
|
260 |
if (!doneHeader) { |
|
261 |
writer.write(getFormatter().getHead(this)); |
|
262 |
doneHeader = true; |
|
263 |
} |
|
264 |
writer.write(getFormatter().getTail(this)); |
|
265 |
writer.flush(); |
|
266 |
writer.close(); |
|
267 |
} catch (Exception ex) { |
|
268 |
// We don't want to throw an exception here, but we |
|
269 |
// report the exception to any registered ErrorManager. |
|
270 |
reportError(null, ex, ErrorManager.CLOSE_FAILURE); |
|
271 |
} |
|
272 |
writer = null; |
|
273 |
output = null; |
|
274 |
} |
|
275 |
} |
|
276 |
||
277 |
/** |
|
278 |
* Close the current output stream. |
|
279 |
* <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:
25859
diff
changeset
|
280 |
* The {@code Formatter}'s "tail" string is written to the stream before it |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
25859
diff
changeset
|
281 |
* is closed. In addition, if the {@code Formatter}'s "head" string has not |
2 | 282 |
* yet been written to the stream, it will be written before the |
283 |
* "tail" string. |
|
284 |
* |
|
285 |
* @exception SecurityException if a security manager exists and if |
|
286 |
* the caller does not have LoggingPermission("control"). |
|
287 |
*/ |
|
19808
39cb79123ab2
6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents:
14333
diff
changeset
|
288 |
@Override |
2 | 289 |
public synchronized void close() throws SecurityException { |
290 |
flushAndClose(); |
|
291 |
} |
|
22110
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
292 |
|
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
293 |
// Package-private support for setting OutputStream |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
294 |
// with elevated privilege. |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
295 |
final void setOutputStreamPrivileged(final OutputStream out) { |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
296 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
297 |
@Override |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
298 |
public Void run() { |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
299 |
setOutputStream(out); |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
300 |
return null; |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
301 |
} |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
302 |
}, null, LogManager.controlPermission); |
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
19808
diff
changeset
|
303 |
} |
2 | 304 |
} |