001    package org.apache.ojb.broker.core;
002    
003    /* Copyright 2003-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import java.util.Properties;
019    
020    import javax.naming.Context;
021    import javax.naming.InitialContext;
022    import javax.naming.NamingException;
023    import org.apache.ojb.broker.OJBRuntimeException;
024    import org.apache.ojb.broker.util.logging.Logger;
025    import org.apache.ojb.broker.util.logging.LoggerFactory;
026    
027    /**
028     * Encapsulates a reference to the JNDI Naming context.
029     *
030     * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
031     * @version $Id: NamingLocator.java,v 1.1 2007-08-24 22:17:35 ewestfal Exp $
032     */
033    
034    public class NamingLocator
035    {
036        private static Logger log = LoggerFactory.getLogger(NamingLocator.class);
037        private static Context ctx = null;
038        private static Properties prop;
039    
040        /**
041         * Returns the naming context.
042         */
043        public static Context getContext()
044        {
045            if (ctx == null)
046            {
047                try
048                {
049                    setContext(null);
050                }
051                catch (Exception e)
052                {
053                    log.error("Cannot instantiate the InitialContext", e);
054                    throw new OJBRuntimeException(e);
055                }
056            }
057            return ctx;
058        }
059    
060        /**
061         * Lookup an object instance from JNDI context.
062         *
063         * @param jndiName JNDI lookup name
064         * @return Matching object or <em>null</em> if none found.
065         */
066        public static Object lookup(String jndiName)
067        {
068            if(log.isDebugEnabled()) log.debug("lookup("+jndiName+") was called");
069            try
070            {
071                return getContext().lookup(jndiName);
072            }
073            catch (NamingException e)
074            {
075                throw new OJBRuntimeException("Lookup failed for: " + jndiName, e);
076            }
077            catch(OJBRuntimeException e)
078            {
079                throw e;
080            }
081        }
082    
083        /**
084         * Refresh the used {@link InitialContext} instance.
085         */
086        public static void refresh()
087        {
088            try
089            {
090                setContext(prop);
091            }
092            catch (NamingException e)
093            {
094                log.error("Unable to refresh the naming context");
095                throw new OJBRuntimeException("Refresh of context failed, used properties: "
096                        + (prop != null ? prop.toString() : "none"), e);
097            }
098        }
099    
100        /**
101         * Set the used {@link InitialContext}. If properties argument is <em>null</em>, the default
102         * initial context was used.
103         *
104         * @param properties The properties used for context instantiation - the properties are:
105         *  {@link Context#INITIAL_CONTEXT_FACTORY}, {@link Context#PROVIDER_URL}, {@link Context#URL_PKG_PREFIXES}
106         * @throws NamingException
107         */
108        public static synchronized void setContext(Properties properties) throws NamingException
109        {
110            log.info("Instantiate naming context, properties: " + properties);
111            if(properties != null)
112            {
113                ctx = new InitialContext(properties);
114            }
115            else
116            {
117                ctx = new InitialContext();
118            }
119            prop = properties;
120        }
121    }