Skip to content

Commit d45153c

Browse files
committed
Fix LOGBACK-1604
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
1 parent 15a1823 commit d45153c

File tree

6 files changed

+56
-24
lines changed

6 files changed

+56
-24
lines changed

logback-classic/src/main/java/ch/qos/logback/classic/joran/action/InsertFromJNDIAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void begin(InterpretationContext ec, String name, Attributes attributes)
6565

6666
try {
6767
Context ctx = JNDIUtil.getInitialContext();
68-
envEntryValue = JNDIUtil.lookup(ctx, envEntryName);
68+
envEntryValue = JNDIUtil.lookupString(ctx, envEntryName);
6969
if (OptionHelper.isEmpty(envEntryValue)) {
7070
addError("[" + envEntryName + "] has null or empty value");
7171
} else {

logback-classic/src/main/java/ch/qos/logback/classic/selector/ContextJNDISelector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public LoggerContext getLoggerContext() {
8484
// We first try to find the name of our
8585
// environment's LoggerContext
8686
ctx = JNDIUtil.getInitialContext();
87-
contextName = (String) JNDIUtil.lookup(ctx, JNDI_CONTEXT_NAME);
87+
contextName = (String) JNDIUtil.lookupString(ctx, JNDI_CONTEXT_NAME);
8888
} catch (NamingException ne) {
8989
// We can't log here
9090
}
@@ -127,7 +127,7 @@ private URL findConfigFileURL(Context ctx, LoggerContext loggerContext) {
127127

128128
String jndiEntryForConfigResource = null;
129129
try {
130-
jndiEntryForConfigResource = JNDIUtil.lookup(ctx, JNDI_CONFIGURATION_RESOURCE);
130+
jndiEntryForConfigResource = JNDIUtil.lookupString(ctx, JNDI_CONFIGURATION_RESOURCE);
131131
} catch (NamingException ne) {
132132
}
133133
// Do we have a dedicated configuration file?

logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/ContextDetachingSCL.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void contextDestroyed(ServletContextEvent servletContextEvent) {
4040

4141
try {
4242
Context ctx = JNDIUtil.getInitialContext();
43-
loggerContextName = (String) JNDIUtil.lookup(ctx, JNDI_CONTEXT_NAME);
43+
loggerContextName = (String) JNDIUtil.lookupString(ctx, JNDI_CONTEXT_NAME);
4444
} catch (NamingException ne) {
4545
}
4646

logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private Session lookupSessionInJNDI() {
135135
addInfo("Looking up javax.mail.Session at JNDI location [" + jndiLocation + "]");
136136
try {
137137
Context initialContext = JNDIUtil.getInitialContext();
138-
Object obj = JNDIUtil.lookup(initialContext, jndiLocation);
138+
Object obj = JNDIUtil.lookupObject(initialContext, jndiLocation);
139139
return (Session) obj;
140140
} catch (Exception e) {
141141
addError("Failed to obtain javax.mail.Session from JNDI location [" + jndiLocation + "]");

logback-core/src/main/java/ch/qos/logback/core/util/JNDIUtil.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,43 @@
2323
* A simple utility class to create and use a JNDI Context.
2424
*
2525
* @author Ceki G&uuml;lc&uuml;
26-
* @author Michael Osipov
26+
* @author Michael Osipov
2727
* @author S&eacute;bastien Pennec
2828
*
2929
*/
3030

3131
public class JNDIUtil {
3232

33-
static final String RESTRICTION_MSG = "JNDI name must start with " + JNDI_JAVA_NAMESPACE + " but was ";
33+
static final String RESTRICTION_MSG = "JNDI name must start with " + JNDI_JAVA_NAMESPACE + " but was ";
3434

3535
public static Context getInitialContext() throws NamingException {
36-
return new InitialContext();
37-
}
36+
return new InitialContext();
37+
}
3838

39-
public static String lookup(Context ctx, String name) throws NamingException {
40-
if (ctx == null) {
41-
return null;
42-
}
39+
public static Object lookupObject(Context ctx, String name) throws NamingException {
40+
if (ctx == null) {
41+
return null;
42+
}
4343

44-
if (OptionHelper.isEmpty(name)) {
45-
return null;
46-
}
44+
if (OptionHelper.isEmpty(name)) {
45+
return null;
46+
}
4747

48-
if (!name.startsWith(JNDI_JAVA_NAMESPACE)) {
49-
throw new NamingException(RESTRICTION_MSG + name);
50-
}
48+
jndiNameSecurityCheck(name);
5149

5250
Object lookup = ctx.lookup(name);
53-
return lookup == null ? null : lookup.toString();
54-
}
51+
return lookup;
52+
}
53+
54+
private static void jndiNameSecurityCheck(String name) throws NamingException {
55+
if (!name.startsWith(JNDI_JAVA_NAMESPACE)) {
56+
throw new NamingException(RESTRICTION_MSG + name);
57+
}
58+
}
59+
60+
public static String lookupString(Context ctx, String name) throws NamingException {
61+
Object lookup = lookupObject(ctx, name);
62+
return (String) lookup;
63+
}
64+
5565
}

logback-core/src/test/java/ch/qos/logback/core/util/JNDIUtilTest.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,30 @@
55
import javax.naming.Context;
66
import javax.naming.NamingException;
77

8+
import org.junit.Before;
89
import org.junit.Test;
910

1011
public class JNDIUtilTest {
1112

13+
Context ctxt;
14+
15+
@Before
16+
public void setup() {
17+
try {
18+
ctxt = JNDIUtil.getInitialContext();
19+
} catch (NamingException e) {
20+
e.printStackTrace();
21+
}
22+
}
23+
1224
@Test
1325
public void ensureJavaNameSpace() throws NamingException {
14-
Context ctxt = JNDIUtil.getInitialContext();
1526

1627
try {
17-
JNDIUtil.lookup(ctxt, "ldap:...");
28+
JNDIUtil.lookupString(ctxt, "ldap:...");
1829
} catch (NamingException e) {
1930
String excaptionMsg = e.getMessage();
20-
if(excaptionMsg.startsWith(JNDIUtil.RESTRICTION_MSG))
31+
if (excaptionMsg.startsWith(JNDIUtil.RESTRICTION_MSG))
2132
return;
2233
else {
2334
fail("unexpected exception " + e);
@@ -27,4 +38,15 @@ public void ensureJavaNameSpace() throws NamingException {
2738
fail("Should aNot yet implemented");
2839
}
2940

41+
@Test
42+
public void testToStringCast() throws NamingException {
43+
String x = JNDIUtil.lookupString(ctxt, "java:comp:/inexistent");
44+
assertNull(x);
45+
}
46+
47+
public String castToString(Object input) {
48+
String a = (String) input;
49+
return a;
50+
}
51+
3052
}

0 commit comments

Comments
 (0)