KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > core > NamingLocator


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 JavaDoc;
19
20 import javax.naming.Context JavaDoc;
21 import javax.naming.InitialContext JavaDoc;
22 import javax.naming.NamingException JavaDoc;
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.4.2.1 2005/12/21 22:25:01 tomdz Exp $
32  */

33
34 public class NamingLocator
35 {
36     private static Logger log = LoggerFactory.getLogger(NamingLocator.class);
37     private static Context JavaDoc ctx = null;
38     private static Properties JavaDoc prop;
39
40     /**
41      * Returns the naming context.
42      */

43     public static Context JavaDoc getContext()
44     {
45         if (ctx == null)
46         {
47             try
48             {
49                 setContext(null);
50             }
51             catch (Exception JavaDoc 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 JavaDoc lookup(String JavaDoc jndiName)
67     {
68         if(log.isDebugEnabled()) log.debug("lookup("+jndiName+") was called");
69         try
70         {
71             return getContext().lookup(jndiName);
72         }
73         catch (NamingException JavaDoc 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 JavaDoc 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 JavaDoc properties) throws NamingException JavaDoc
109     {
110         log.info("Instantiate naming context, properties: " + properties);
111         if(properties != null)
112         {
113             ctx = new InitialContext JavaDoc(properties);
114         }
115         else
116         {
117             ctx = new InitialContext JavaDoc();
118         }
119         prop = properties;
120     }
121 }
122
Popular Tags