1 16 17 package org.springframework.jndi; 18 19 import java.util.Properties ; 20 21 import javax.naming.Context ; 22 import javax.naming.InitialContext ; 23 import javax.naming.NameNotFoundException ; 24 import javax.naming.NamingException ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 39 public class JndiTemplate { 40 41 protected final Log logger = LogFactory.getLog(getClass()); 42 43 private Properties environment; 44 45 46 49 public JndiTemplate() { 50 } 51 52 55 public JndiTemplate(Properties environment) { 56 this.environment = environment; 57 } 58 59 60 63 public void setEnvironment(Properties environment) { 64 this.environment = environment; 65 } 66 67 70 public Properties getEnvironment() { 71 return this.environment; 72 } 73 74 75 82 public Object execute(JndiCallback contextCallback) throws NamingException { 83 Context ctx = createInitialContext(); 84 try { 85 return contextCallback.doInContext(ctx); 86 } 87 finally { 88 try { 89 ctx.close(); 90 } 91 catch (NamingException ex) { 92 logger.debug("Could not close JNDI InitialContext", ex); 93 } 94 } 95 } 96 97 104 protected Context createInitialContext() throws NamingException { 105 return new InitialContext (getEnvironment()); 106 } 107 108 109 117 public Object lookup(final String name) throws NamingException { 118 if (logger.isDebugEnabled()) { 119 logger.debug("Looking up JNDI object with name [" + name + "]"); 120 } 121 return execute(new JndiCallback() { 122 public Object doInContext(Context ctx) throws NamingException { 123 Object located = ctx.lookup(name); 124 if (located == null) { 125 throw new NameNotFoundException ( 126 "JNDI object with [" + name + "] not found: JNDI implementation returned null"); 127 } 128 return located; 129 } 130 }); 131 } 132 133 145 public Object lookup(String name, Class requiredType) throws NamingException { 146 Object jndiObject = lookup(name); 147 if (requiredType != null && !requiredType.isInstance(jndiObject)) { 148 throw new TypeMismatchNamingException( 149 "Object [" + jndiObject + "] available at JNDI location [" + 150 name + "] is not assignable to [" + requiredType.getName() + "]"); 151 } 152 return jndiObject; 153 } 154 155 161 public void bind(final String name, final Object object) throws NamingException { 162 if (logger.isDebugEnabled()) { 163 logger.debug("Binding JNDI object with name [" + name + "]"); 164 } 165 execute(new JndiCallback() { 166 public Object doInContext(Context ctx) throws NamingException { 167 ctx.bind(name, object); 168 return null; 169 } 170 }); 171 } 172 173 180 public void rebind(final String name, final Object object) throws NamingException { 181 if (logger.isDebugEnabled()) { 182 logger.debug("Rebinding JNDI object with name [" + name + "]"); 183 } 184 execute(new JndiCallback() { 185 public Object doInContext(Context ctx) throws NamingException { 186 ctx.rebind(name, object); 187 return null; 188 } 189 }); 190 } 191 192 197 public void unbind(final String name) throws NamingException { 198 if (logger.isDebugEnabled()) { 199 logger.debug("Unbinding JNDI object with name [" + name + "]"); 200 } 201 execute(new JndiCallback() { 202 public Object doInContext(Context ctx) throws NamingException { 203 ctx.unbind(name); 204 return null; 205 } 206 }); 207 } 208 209 } 210 | Popular Tags |