View Javadoc

1   /*
2    * ----------------------------------------------------------------------
3    * Copyright (C) 2009 Enrique Lara (k957@68k.org)
4    *
5    * TinLizard is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public License
7    * as published by the Free Software Foundation; either version 3.0
8    * of the License, or (at your option) any later version.
9    *
10   * TinLizard is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with TinLizard. If not, see http://www.gnu.org/licenses/.
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 }