| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| NamingLocator |
|
| 4.25;4.25 |
| 1 | package org.apache.ojb.broker.core; | |
| 2 | ||
| 3 | /* Copyright 2003-2005 The Apache Software Foundation | |
| 4 | * | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 6 | * you may not use this file except in compliance with the License. | |
| 7 | * You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | */ | |
| 17 | ||
| 18 | import java.util.Properties; | |
| 19 | ||
| 20 | import javax.naming.Context; | |
| 21 | import javax.naming.InitialContext; | |
| 22 | import javax.naming.NamingException; | |
| 23 | import org.apache.ojb.broker.OJBRuntimeException; | |
| 24 | import org.apache.ojb.broker.util.logging.Logger; | |
| 25 | import org.apache.ojb.broker.util.logging.LoggerFactory; | |
| 26 | ||
| 27 | /** | |
| 28 | * Encapsulates a reference to the JNDI Naming context. | |
| 29 | * | |
| 30 | * @author <a href="mailto:thma@apache.org">Thomas Mahler<a> | |
| 31 | * @version $Id: NamingLocator.java,v 1.1 2007-08-24 22:17:35 ewestfal Exp $ | |
| 32 | */ | |
| 33 | ||
| 34 | public class NamingLocator | |
| 35 | { | |
| 36 | private static Logger log = LoggerFactory.getLogger(NamingLocator.class); | |
| 37 | private static Context ctx = null; | |
| 38 | private static Properties prop; | |
| 39 | ||
| 40 | /** | |
| 41 | * Returns the naming context. | |
| 42 | */ | |
| 43 | public static Context getContext() | |
| 44 | { | |
| 45 | if (ctx == null) | |
| 46 | { | |
| 47 | try | |
| 48 | { | |
| 49 | setContext(null); | |
| 50 | } | |
| 51 | catch (Exception e) | |
| 52 | { | |
| 53 | log.error("Cannot instantiate the InitialContext", e); | |
| 54 | throw new OJBRuntimeException(e); | |
| 55 | } | |
| 56 | } | |
| 57 | return ctx; | |
| 58 | } | |
| 59 | ||
| 60 | /** | |
| 61 | * Lookup an object instance from JNDI context. | |
| 62 | * | |
| 63 | * @param jndiName JNDI lookup name | |
| 64 | * @return Matching object or <em>null</em> if none found. | |
| 65 | */ | |
| 66 | public static Object lookup(String jndiName) | |
| 67 | { | |
| 68 | if(log.isDebugEnabled()) log.debug("lookup("+jndiName+") was called"); | |
| 69 | try | |
| 70 | { | |
| 71 | return getContext().lookup(jndiName); | |
| 72 | } | |
| 73 | catch (NamingException e) | |
| 74 | { | |
| 75 | throw new OJBRuntimeException("Lookup failed for: " + jndiName, e); | |
| 76 | } | |
| 77 | catch(OJBRuntimeException e) | |
| 78 | { | |
| 79 | throw e; | |
| 80 | } | |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * Refresh the used {@link InitialContext} instance. | |
| 85 | */ | |
| 86 | public static void refresh() | |
| 87 | { | |
| 88 | try | |
| 89 | { | |
| 90 | setContext(prop); | |
| 91 | } | |
| 92 | catch (NamingException e) | |
| 93 | { | |
| 94 | log.error("Unable to refresh the naming context"); | |
| 95 | throw new OJBRuntimeException("Refresh of context failed, used properties: " | |
| 96 | + (prop != null ? prop.toString() : "none"), e); | |
| 97 | } | |
| 98 | } | |
| 99 | ||
| 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 | } |