1 16 17 package org.springframework.mock.jndi; 18 19 import java.util.HashMap ; 20 import java.util.Hashtable ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import javax.naming.Binding ; 25 import javax.naming.Context ; 26 import javax.naming.Name ; 27 import javax.naming.NameClassPair ; 28 import javax.naming.NameNotFoundException ; 29 import javax.naming.NameParser ; 30 import javax.naming.NamingEnumeration ; 31 import javax.naming.NamingException ; 32 import javax.naming.OperationNotSupportedException ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 import org.springframework.util.StringUtils; 38 39 54 public class SimpleNamingContext implements Context { 55 56 private final Log logger = LogFactory.getLog(getClass()); 57 58 private final String root; 59 60 private final Hashtable boundObjects; 61 62 private final Hashtable environment = new Hashtable (); 63 64 65 68 public SimpleNamingContext() { 69 this(""); 70 } 71 72 75 public SimpleNamingContext(String root) { 76 this.root = root; 77 this.boundObjects = new Hashtable (); 78 } 79 80 84 public SimpleNamingContext(String root, Hashtable boundObjects, Hashtable environment) { 85 this.root = root; 86 this.boundObjects = boundObjects; 87 if (environment != null) { 88 this.environment.putAll(environment); 89 } 90 } 91 92 93 95 public NamingEnumeration list(String root) throws NamingException { 96 if (logger.isDebugEnabled()) { 97 logger.debug("Listing name/class pairs under [" + root + "]"); 98 } 99 return new NameClassPairEnumeration(this, root); 100 } 101 102 public NamingEnumeration listBindings(String root) throws NamingException { 103 if (logger.isDebugEnabled()) { 104 logger.debug("Listing bindings under [" + root + "]"); 105 } 106 return new BindingEnumeration(this, root); 107 } 108 109 115 public Object lookup(String pname) throws NameNotFoundException { 116 String name = this.root + pname; 117 if (logger.isDebugEnabled()) { 118 logger.debug("Static JNDI lookup: [" + name + "]"); 119 } 120 if ("".equals(name)) { 121 return new SimpleNamingContext(this.root, this.boundObjects, this.environment); 122 } 123 Object found = this.boundObjects.get(name); 124 if (found == null) { 125 if (!name.endsWith("/")) { 126 name = name + "/"; 127 } 128 for (Iterator it = this.boundObjects.keySet().iterator(); it.hasNext();) { 129 String boundName = (String ) it.next(); 130 if (boundName.startsWith(name)) { 131 return new SimpleNamingContext(name, this.boundObjects, this.environment); 132 } 133 } 134 throw new NameNotFoundException ( 135 "Name [" + this.root + pname + "] not bound; " + this.boundObjects.size() + " bindings: [" + 136 StringUtils.collectionToDelimitedString(this.boundObjects.keySet(), ",") + "]"); 137 } 138 return found; 139 } 140 141 public Object lookupLink(String name) throws NameNotFoundException { 142 return lookup(name); 143 } 144 145 152 public void bind(String name, Object obj) { 153 if (logger.isInfoEnabled()) { 154 logger.info("Static JNDI binding: [" + this.root + name + "] = [" + obj + "]"); 155 } 156 this.boundObjects.put(this.root + name, obj); 157 } 158 159 public void unbind(String name) { 160 if (logger.isInfoEnabled()) { 161 logger.info("Static JNDI remove: [" + this.root + name + "]"); 162 } 163 this.boundObjects.remove(this.root + name); 164 } 165 166 public void rebind(String name, Object obj) { 167 bind(name, obj); 168 } 169 170 public void rename(String oldName, String newName) throws NameNotFoundException { 171 Object obj = lookup(oldName); 172 unbind(oldName); 173 bind(newName, obj); 174 } 175 176 public Context createSubcontext(String name) { 177 Context subcontext = new SimpleNamingContext(this.root + name, this.boundObjects, this.environment); 178 bind(name, subcontext); 179 return subcontext; 180 } 181 182 public void destroySubcontext(String name) { 183 unbind(name); 184 } 185 186 public String composeName(String name, String prefix) { 187 return prefix + name; 188 } 189 190 public Hashtable getEnvironment() { 191 return environment; 192 } 193 194 public Object addToEnvironment(String propName, Object propVal) { 195 return environment.put(propName, propVal); 196 } 197 198 public Object removeFromEnvironment(String propName) { 199 return environment.remove(propName); 200 } 201 202 public void close() { 203 } 204 205 206 208 public NamingEnumeration list(Name name) throws NamingException { 209 throw new OperationNotSupportedException ("SimpleNamingContext does not support [javax.naming.Name]"); 210 } 211 212 public NamingEnumeration listBindings(Name name) throws NamingException { 213 throw new OperationNotSupportedException ("SimpleNamingContext does not support [javax.naming.Name]"); 214 } 215 216 public Object lookup(Name name) throws NamingException { 217 throw new OperationNotSupportedException ("SimpleNamingContext does not support [javax.naming.Name]"); 218 } 219 220 public Object lookupLink(Name name) throws NamingException { 221 throw new OperationNotSupportedException ("SimpleNamingContext does not support [javax.naming.Name]"); 222 } 223 224 public void bind(Name name, Object obj) throws NamingException { 225 throw new OperationNotSupportedException ("SimpleNamingContext does not support [javax.naming.Name]"); 226 } 227 228 public void unbind(Name name) throws NamingException { 229 throw new OperationNotSupportedException ("SimpleNamingContext does not support [javax.naming.Name]"); 230 } 231 232 public void rebind(Name name, Object obj) throws NamingException { 233 throw new OperationNotSupportedException ("SimpleNamingContext does not support [javax.naming.Name]"); 234 } 235 236 public void rename(Name oldName, Name newName) throws NamingException { 237 throw new OperationNotSupportedException ("SimpleNamingContext does not support [javax.naming.Name]"); 238 } 239 240 public Context createSubcontext(Name name) throws NamingException { 241 throw new OperationNotSupportedException ("SimpleNamingContext does not support [javax.naming.Name]"); 242 } 243 244 public void destroySubcontext(Name name) throws NamingException { 245 throw new OperationNotSupportedException ("SimpleNamingContext does not support [javax.naming.Name]"); 246 } 247 248 public String getNameInNamespace() throws NamingException { 249 throw new OperationNotSupportedException ("SimpleNamingContext does not support [javax.naming.Name]"); 250 } 251 252 public NameParser getNameParser(Name name) throws NamingException { 253 throw new OperationNotSupportedException ("SimpleNamingContext does not support [javax.naming.Name]"); 254 } 255 256 public NameParser getNameParser(String name) throws NamingException { 257 throw new OperationNotSupportedException ("SimpleNamingContext does not support [javax.naming.Name]"); 258 } 259 260 public Name composeName(Name name, Name prefix) throws NamingException { 261 throw new OperationNotSupportedException ("SimpleNamingContext does not support [javax.naming.Name]"); 262 } 263 264 265 private static abstract class AbstractNamingEnumeration implements NamingEnumeration { 266 267 private Iterator iterator; 268 269 private AbstractNamingEnumeration(SimpleNamingContext context, String proot) throws NamingException { 270 if (!"".equals(proot) && !proot.endsWith("/")) { 271 proot = proot + "/"; 272 } 273 String root = context.root + proot; 274 Map contents = new HashMap (); 275 Iterator it = context.boundObjects.keySet().iterator(); 276 while (it.hasNext()) { 277 String boundName = (String ) it.next(); 278 if (boundName.startsWith(root)) { 279 int startIndex = root.length(); 280 int endIndex = boundName.indexOf('/', startIndex); 281 String strippedName = 282 (endIndex != -1 ? boundName.substring(startIndex, endIndex) : boundName.substring(startIndex)); 283 if (!contents.containsKey(strippedName)) { 284 try { 285 contents.put(strippedName, createObject(strippedName, context.lookup(proot + strippedName))); 286 } 287 catch (NameNotFoundException ex) { 288 } 290 } 291 } 292 } 293 if (contents.size() == 0) { 294 throw new NamingException ("Invalid root: [" + context.root + proot + "]"); 295 } 296 this.iterator = contents.values().iterator(); 297 } 298 299 protected abstract Object createObject(String strippedName, Object obj); 300 301 public boolean hasMore() { 302 return iterator.hasNext(); 303 } 304 305 public Object next() { 306 return iterator.next(); 307 } 308 309 public boolean hasMoreElements() { 310 return iterator.hasNext(); 311 } 312 313 public Object nextElement() { 314 return iterator.next(); 315 } 316 317 public void close() { 318 } 319 } 320 321 322 private static class NameClassPairEnumeration extends AbstractNamingEnumeration { 323 324 private NameClassPairEnumeration(SimpleNamingContext context, String root) throws NamingException { 325 super(context, root); 326 } 327 328 protected Object createObject(String strippedName, Object obj) { 329 return new NameClassPair (strippedName, obj.getClass().getName()); 330 } 331 } 332 333 334 private static class BindingEnumeration extends AbstractNamingEnumeration { 335 336 private BindingEnumeration(SimpleNamingContext context, String root) throws NamingException { 337 super(context, root); 338 } 339 340 protected Object createObject(String strippedName, Object obj) { 341 return new Binding (strippedName, obj); 342 } 343 } 344 345 } 346 | Popular Tags |