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.4.2.1 2005/12/21 22:25:01 tomdz Exp $
032: */
033:
034: public class NamingLocator {
035: private static Logger log = LoggerFactory
036: .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: if (ctx == null) {
045: try {
046: setContext(null);
047: } catch (Exception e) {
048: log.error("Cannot instantiate the InitialContext", e);
049: throw new OJBRuntimeException(e);
050: }
051: }
052: return ctx;
053: }
054:
055: /**
056: * Lookup an object instance from JNDI context.
057: *
058: * @param jndiName JNDI lookup name
059: * @return Matching object or <em>null</em> if none found.
060: */
061: public static Object lookup(String jndiName) {
062: if (log.isDebugEnabled())
063: log.debug("lookup(" + jndiName + ") was called");
064: try {
065: return getContext().lookup(jndiName);
066: } catch (NamingException e) {
067: throw new OJBRuntimeException("Lookup failed for: "
068: + jndiName, e);
069: } catch (OJBRuntimeException e) {
070: throw e;
071: }
072: }
073:
074: /**
075: * Refresh the used {@link InitialContext} instance.
076: */
077: public static void refresh() {
078: try {
079: setContext(prop);
080: } catch (NamingException e) {
081: log.error("Unable to refresh the naming context");
082: throw new OJBRuntimeException(
083: "Refresh of context failed, used properties: "
084: + (prop != null ? prop.toString() : "none"),
085: e);
086: }
087: }
088:
089: /**
090: * Set the used {@link InitialContext}. If properties argument is <em>null</em>, the default
091: * initial context was used.
092: *
093: * @param properties The properties used for context instantiation - the properties are:
094: * {@link Context#INITIAL_CONTEXT_FACTORY}, {@link Context#PROVIDER_URL}, {@link Context#URL_PKG_PREFIXES}
095: * @throws NamingException
096: */
097: public static synchronized void setContext(Properties properties)
098: throws NamingException {
099: log.info("Instantiate naming context, properties: "
100: + properties);
101: if (properties != null) {
102: ctx = new InitialContext(properties);
103: } else {
104: ctx = new InitialContext();
105: }
106: prop = properties;
107: }
108: }
|