Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.
/ lanai Public archive

Commit c4df791

Browse files
committedJun 20, 2020
8247907: XMLDsig logging does not work
Reviewed-by: mullan
1 parent 25b1e5a commit c4df791

File tree

3 files changed

+137
-12
lines changed

3 files changed

+137
-12
lines changed
 

‎src/java.base/share/lib/security/default.policy

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ grant codeBase "jrt:/java.sql.rowset" {
7878

7979

8080
grant codeBase "jrt:/java.xml.crypto" {
81+
permission java.lang.RuntimePermission
82+
"getStackWalkerWithClassReference";
8183
permission java.lang.RuntimePermission
8284
"accessClassInPackage.sun.security.util";
8385
permission java.util.PropertyPermission "*", "read";

‎src/java.xml.crypto/share/classes/com/sun/org/slf4j/internal/Logger.java

+77-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,56 +24,121 @@
2424
*/
2525
package com.sun.org.slf4j.internal;
2626

27+
import java.security.AccessController;
28+
import java.security.PrivilegedAction;
29+
import java.util.logging.Level;
30+
2731
// Bridge to java.util.logging.
2832
public class Logger {
2933

34+
/**
35+
* StackWalker to find out the caller of this class so that it can be
36+
* shown in the log output. The multiple private log0() methods below
37+
* skip exactly 2 frames -- one log0() itself, the other one of the
38+
* public debug()/warn()/error()/trace() methods in this class --
39+
* to find the caller.
40+
*/
41+
private static final StackWalker WALKER;
42+
static {
43+
final PrivilegedAction<StackWalker> action =
44+
() -> StackWalker.getInstance(StackWalker.Option
45+
.RETAIN_CLASS_REFERENCE);
46+
WALKER = AccessController.doPrivileged(action);
47+
}
48+
3049
private final java.util.logging.Logger impl;
3150

3251
public Logger(String name) {
3352
impl = java.util.logging.Logger.getLogger(name);
3453
}
3554

3655
public boolean isDebugEnabled() {
37-
return impl.isLoggable(java.util.logging.Level.FINE);
56+
return impl.isLoggable(Level.FINE);
3857
}
3958

4059
public boolean isTraceEnabled() {
41-
return impl.isLoggable(java.util.logging.Level.FINE);
60+
return impl.isLoggable(Level.FINE);
4261
}
4362

4463
public void debug(String s) {
45-
impl.log(java.util.logging.Level.FINE, s);
64+
log0(Level.FINE, s);
4665
}
4766

4867
public void debug(String s, Throwable e) {
49-
impl.log(java.util.logging.Level.FINE, s, e);
68+
log0(Level.FINE, s, e);
5069
}
5170

5271
public void debug(String s, Object... o) {
53-
impl.log(java.util.logging.Level.FINE, s, o);
72+
log0(Level.FINE, s, o);
5473
}
5574

5675
public void trace(String s) {
57-
impl.log(java.util.logging.Level.FINE, s);
76+
log0(Level.FINE, s);
5877
}
5978

6079
public void error(String s) {
61-
impl.log(java.util.logging.Level.SEVERE, s);
80+
log0(Level.SEVERE, s);
6281
}
6382

6483
public void error(String s, Throwable e) {
65-
impl.log(java.util.logging.Level.SEVERE, s, e);
84+
log0(Level.SEVERE, s, e);
6685
}
6786

6887
public void error(String s, Object... o) {
69-
impl.log(java.util.logging.Level.SEVERE, s, o);
88+
log0(Level.SEVERE, s, o);
7089
}
7190

7291
public void warn(String s) {
73-
impl.log(java.util.logging.Level.WARNING, s);
92+
log0(Level.WARNING, s);
7493
}
7594

7695
public void warn(String s, Throwable e) {
77-
impl.log(java.util.logging.Level.WARNING, s, e);
96+
log0(Level.WARNING, s, e);
97+
}
98+
99+
private void log0(Level level, String s) {
100+
if (impl.isLoggable(level)) {
101+
var sf = WALKER.walk(f -> f.skip(2).findFirst()).get();
102+
impl.logp(Level.FINE, sf.getClassName(), sf.getMethodName(), s);
103+
}
104+
}
105+
106+
public void log0(Level level, String s, Throwable e) {
107+
if (impl.isLoggable(level)) {
108+
var sf = WALKER.walk(f -> f.skip(2).findFirst()).get();
109+
impl.logp(Level.FINE, sf.getClassName(), sf.getMethodName(), s, e);
110+
}
111+
}
112+
113+
public void log0(Level level, String s, Object... o) {
114+
if (impl.isLoggable(level)) {
115+
var sf = WALKER.walk(f -> f.skip(2).findFirst()).get();
116+
impl.logp(Level.FINE, sf.getClassName(), sf.getMethodName(),
117+
addIndex(s), o);
118+
}
119+
}
120+
121+
/**
122+
* Translate the log4j message format "Hello {}, {}" to the
123+
* java.util.logging format "Hello {0}, {1}".
124+
*/
125+
private static String addIndex(String s) {
126+
int start = 0;
127+
int index = 0;
128+
StringBuilder sb = new StringBuilder();
129+
while (true) {
130+
int pos = s.indexOf("{}", start);
131+
if (pos < 0) {
132+
break;
133+
}
134+
sb.append(s, start, pos + 1).append(index++);
135+
start = pos + 1;
136+
}
137+
if (index == 0) {
138+
return s;
139+
} else {
140+
sb.append(s, start, s.length());
141+
return sb.toString();
142+
}
78143
}
79144
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2020, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import jdk.test.lib.hexdump.HexPrinter;
25+
26+
import java.io.ByteArrayOutputStream;
27+
import java.util.logging.*;
28+
29+
/**
30+
* @test
31+
* @bug 8247907
32+
* @library /test/lib
33+
* @modules java.xml.crypto/com.sun.org.slf4j.internal
34+
*/
35+
public class LogParameters {
36+
public static void main(String[] args) {
37+
38+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
39+
Logger.getLogger(String.class.getName()).setLevel(Level.ALL);
40+
Handler h = new StreamHandler(bout, new SimpleFormatter());
41+
h.setLevel(Level.ALL);
42+
Logger.getLogger(String.class.getName()).addHandler(h);
43+
44+
com.sun.org.slf4j.internal.Logger log =
45+
com.sun.org.slf4j.internal.LoggerFactory.getLogger(String.class);
46+
log.debug("I have {} {}s.", 10, "apple");
47+
48+
h.flush();
49+
50+
byte[] data = bout.toByteArray();
51+
String s = new String(data);
52+
if (!s.contains("LogParameters main")
53+
|| !s.contains("FINE: I have 10 apples.")) {
54+
HexPrinter.simple().format(data);
55+
throw new RuntimeException("Unexpected log output: " + s);
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)
This repository has been archived.