1 25 26 package org.objectweb.easybeans.injection; 27 28 import javax.naming.Context ; 29 import javax.naming.InitialContext ; 30 import javax.naming.LinkRef ; 31 import javax.naming.NamingException ; 32 33 import org.objectweb.easybeans.log.JLog; 34 import org.objectweb.easybeans.log.JLogFactory; 35 36 40 public final class JNDIBinderHelper { 41 42 45 public enum JndiType {JAVA_COMP_ENV} 46 47 50 private static final String JAVA_COMP = "java:comp/"; 51 52 55 private static final String JAVA_COMP_ENV = JAVA_COMP + "env"; 56 57 60 private static JLog logger = JLogFactory.getLog(JNDIBinderHelper.class); 61 62 63 64 67 private JNDIBinderHelper() { 68 69 } 70 71 76 public static Context getContext(final String name) { 77 InitialContext ictx; 78 try { 79 ictx = new InitialContext (); 80 } catch (NamingException e) { 81 logger.error("Cannot instantiate an initial context", e); 82 return null; 83 } 84 85 Object o = null; 86 try { 87 o = ictx.lookup(name); 88 } catch (NamingException e) { 89 logger.error("Cannot find the JNDI name {0}", name, e); 90 } 91 if (o == null) { 92 logger.error("No object was found for JNDI name {0}", name); 93 } 94 Context ctx = null; 95 if (o instanceof Context ) { 96 ctx = (Context ) o; 97 } else { 98 logger.error("Object not instance of context. Object = {0}", o); 99 } 100 return ctx; 101 } 102 103 108 public static void bindLinkRefEnvJndiName(final String encName, final String jndiName) { 109 try { 110 getContext(JAVA_COMP_ENV).rebind(encName, new LinkRef (jndiName)); 111 } catch (NamingException e) { 112 logger.error("Cannot do a LinkRef between jndiName {0} with ENC name {1}", jndiName, encName, e); 113 } 114 } 115 116 121 public static void bindEnvJndiName(final String name, final Object object) { 122 try { 123 getContext(JAVA_COMP_ENV).rebind(name, object); 124 } catch (NamingException e) { 125 logger.error("Cannot bind object {0} with name {1}", object, name, e); 126 } 127 } 128 129 130 } 131 | Popular Tags |