1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.util;
15
16 import static ch.qos.logback.core.CoreConstants.JNDI_JAVA_NAMESPACE;
17
18 import java.util.Hashtable;
19
20 import javax.naming.Context;
21 import javax.naming.InitialContext;
22 import javax.naming.NamingException;
23
24
25
26
27
28
29
30
31
32
33 public class JNDIUtil {
34
35 static final String RESTRICTION_MSG = "JNDI name must start with " + JNDI_JAVA_NAMESPACE + " but was ";
36
37 public static Context getInitialContext() throws NamingException {
38 return new InitialContext();
39 }
40
41 public static Context getInitialContext(Hashtable<?, ?> props) throws NamingException {
42 return new InitialContext(props);
43 }
44
45 public static Object lookupObject(Context ctx, String name) throws NamingException {
46 if (ctx == null) {
47 return null;
48 }
49
50 if (OptionHelper.isNullOrEmptyOrAllSpaces(name)) {
51 return null;
52 }
53
54 jndiNameSecurityCheck(name);
55
56 Object lookup = ctx.lookup(name);
57 return lookup;
58 }
59
60 public static void jndiNameSecurityCheck(String name) throws NamingException {
61 if (!name.startsWith(JNDI_JAVA_NAMESPACE)) {
62 throw new NamingException(RESTRICTION_MSG + name);
63 }
64 }
65
66 public static String lookupString(Context ctx, String name) throws NamingException {
67 Object lookup = lookupObject(ctx, name);
68 return (String) lookup;
69 }
70
71 }