001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/framework/jndi/JndiUtils.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    package org.deegree.framework.jndi;
037    
038    // J2EE 1.3
039    
040    import javax.naming.Context;
041    import javax.naming.InitialContext;
042    import javax.naming.NamingEnumeration;
043    import javax.naming.NamingException;
044    import javax.rmi.PortableRemoteObject;
045    
046    /**
047     * Utility class for retrieving home interfaces and environment values.
048     *
049     * @author <a href="mailto:tfr@users.sourceforge.net">Torsten Friebe</A>
050     * @author last edited by: $Author: mschneider $
051     *
052     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
053     */
054    public final class JndiUtils {
055    
056        /**
057         * Default constructor is hidden to protect this class from unwanted instantiation.
058         */
059        private JndiUtils() {
060            // nothing to do
061        }
062    
063        /**
064         * Lookup using <b>no</b> caching functionality.
065         *
066         * @param name
067         *            the environment name
068         * @param classForNarrow
069         *            the class to narrow
070         * @return if an entry exists, an instance of the given class with a value retrieved from the JNDI tree otherwise
071         *         <code>null</code>.
072         * @throws NamingException
073         */
074        public static Object lookup( String name, Class<?> classForNarrow )
075                                throws NamingException {
076            return PortableRemoteObject.narrow( JndiUtils.getInitialContext().lookup( name ), classForNarrow );
077        }
078    
079        /**
080         * Lookup at Enterprise Java Beans environment value. For home interfaces use EJBRemoteFactory instead.
081         *
082         * @param name
083         *            the environment name
084         * @param classForNarrow
085         *            the class to narrow
086         * @return if an entry exists, an instance of the given class with a value retrieved from the JNDI tree otherwise
087         *         <code>null</code>.
088         * @throws NamingException
089         *             if the given lookup name is not in the JNDI tree
090         */
091        public static Object lookupEnv( String name, Class<?> classForNarrow )
092                                throws NamingException {
093            return lookup( "java:/comp/env/" + name, classForNarrow );
094        }
095    
096        /**
097         * Returns a list of naming entries for the given root node.
098         *
099         * @param rootNode
100         * @return list of naming entries
101         * @throws NamingException
102         *             when the given root node doesn't exists
103         */
104        public static NamingEnumeration<?> getNamingList( String rootNode )
105                                throws NamingException {
106            return JndiUtils.getInitialContext().listBindings( rootNode );
107        }
108    
109        /**
110         * Returns the initial context for this application.
111         *
112         * @return the inital context for this appication. This depends on the system environment.
113         *
114         * @throws NamingException
115         *             if the context is not available
116         */
117        private static Context getInitialContext()
118                                throws NamingException {
119            return ( new InitialContext() );
120        }
121    }