001    /*
002     * ----------------------------------------------------------------------
003     * Copyright (C) 2009 Enrique Lara (k957@68k.org)
004     *
005     * TinLizard is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU Lesser General Public License
007     * as published by the Free Software Foundation; either version 3.0
008     * of the License, or (at your option) any later version.
009     *
010     * TinLizard is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013     * GNU Lesser General Public License for more details.
014     *
015     * You should have received a copy of the GNU Lesser General Public License
016     * along with TinLizard. If not, see http://www.gnu.org/licenses/.
017     * ----------------------------------------------------------------------
018     */
019    package tinlizard.util;
020    
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    import javax.naming.Binding;
025    import javax.naming.Context;
026    import javax.naming.InitialContext;
027    import javax.naming.NameNotFoundException;
028    import javax.naming.NamingEnumeration;
029    import javax.naming.NamingException;
030    
031    import org.apache.log4j.Logger;
032    
033    /**
034     * Utility class to access all JNDI configuration information.
035     */
036    public final class JndiUtil {
037        private static final String JAVA_JNDI_CTX = "java:comp/env";
038        private static final Logger LOG = Logger.getLogger(JndiUtil.class);
039        public static final String HOME = "TINLIZARD_HOME";
040        public static final String LOADTESTDATA = "TINLIZARD_LOADTESTDATA";
041    
042        private JndiUtil() {
043        }
044    
045        public static Boolean getBoolean(final String name) {
046            Boolean rval = false;
047    
048            try {
049                InitialContext ctx = new InitialContext();
050                Context env = (Context) ctx.lookup(JAVA_JNDI_CTX);
051    
052                String value = null;
053    
054                if (env != null) {
055                    value = (String) env.lookup(name);
056                }
057    
058                if ((value == null) || (value.trim().length() < 1)) {
059                    value = (String) ctx.lookup(name);
060                }
061    
062                if ((value != null) && (value.trim().length() > 0)) {
063                    return Boolean.valueOf(value.trim().toString());
064                }
065            } catch (NameNotFoundException e) {
066                if (LOG.isDebugEnabled()) {
067                    LOG.debug(e);
068                }
069    
070                return false;
071            } catch (NamingException e) {
072                throw new RuntimeException(Messages.error_0551(name), e);
073            }
074    
075            return rval;
076        }
077    
078        public static Map<String, Object> getContextMap() {
079            Map<String, Object> map = null;
080    
081            Context ctx;
082    
083            try {
084                map = new HashMap<String, Object>();
085    
086                ctx = new InitialContext();
087    
088                NamingEnumeration<Binding> e = ctx.listBindings(JAVA_JNDI_CTX);
089    
090                while (e.hasMoreElements()) {
091                    Binding binding = e.next();
092                    String name = binding.getName();
093                    int idx;
094    
095                    if ((idx = name.indexOf("TINLIZARD_")) != -1) {
096                        name = name.substring(idx);
097    
098                        if (LOG.isDebugEnabled()) {
099                            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    }