1 package org.apache.ojb.broker.core; 2 3 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 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 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 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 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 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 } 122 | Popular Tags |