1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package tinlizard.util;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import javax.naming.Binding;
25 import javax.naming.Context;
26 import javax.naming.InitialContext;
27 import javax.naming.NameNotFoundException;
28 import javax.naming.NamingEnumeration;
29 import javax.naming.NamingException;
30
31 import org.apache.log4j.Logger;
32
33 /***
34 * Utility class to access all JNDI configuration information.
35 */
36 public final class JndiUtil {
37 private static final String JAVA_JNDI_CTX = "java:comp/env";
38 private static final Logger LOG = Logger.getLogger(JndiUtil.class);
39 public static final String HOME = "TINLIZARD_HOME";
40 public static final String LOADTESTDATA = "TINLIZARD_LOADTESTDATA";
41
42 private JndiUtil() {
43 }
44
45 public static Boolean getBoolean(final String name) {
46 Boolean rval = false;
47
48 try {
49 InitialContext ctx = new InitialContext();
50 Context env = (Context) ctx.lookup(JAVA_JNDI_CTX);
51
52 String value = null;
53
54 if (env != null) {
55 value = (String) env.lookup(name);
56 }
57
58 if ((value == null) || (value.trim().length() < 1)) {
59 value = (String) ctx.lookup(name);
60 }
61
62 if ((value != null) && (value.trim().length() > 0)) {
63 return Boolean.valueOf(value.trim().toString());
64 }
65 } catch (NameNotFoundException e) {
66 if (LOG.isDebugEnabled()) {
67 LOG.debug(e);
68 }
69
70 return false;
71 } catch (NamingException e) {
72 throw new RuntimeException(Messages.error_0551(name), e);
73 }
74
75 return rval;
76 }
77
78 public static Map<String, Object> getContextMap() {
79 Map<String, Object> map = null;
80
81 Context ctx;
82
83 try {
84 map = new HashMap<String, Object>();
85
86 ctx = new InitialContext();
87
88 NamingEnumeration<Binding> e = ctx.listBindings(JAVA_JNDI_CTX);
89
90 while (e.hasMoreElements()) {
91 Binding binding = e.next();
92 String name = binding.getName();
93 int idx;
94
95 if ((idx = name.indexOf("TINLIZARD_")) != -1) {
96 name = name.substring(idx);
97
98 if (LOG.isDebugEnabled()) {
99 LOG.debug("Adding JNDI entry:" + name);
100 }
101
102 Object value = binding.getObject();
103
104 if (value == null) {
105 value = ctx.lookup(name);
106 }
107
108 map.put(name, value);
109 } else if (LOG.isDebugEnabled()) {
110 LOG.debug("Skipping JNDI entry:" + name);
111 }
112 }
113
114 if (LOG.isDebugEnabled()) {
115 LOG.debug(map);
116 }
117 } catch (NamingException e) {
118 throw new RuntimeException(Messages.error_0550(), e);
119 }
120
121 return map;
122 }
123 }