1 16 17 package org.apache.naming.modules.memory; 18 19 import java.util.Hashtable ; 20 21 import javax.naming.Context ; 22 import javax.naming.InitialContext ; 23 import javax.naming.LinkRef ; 24 import javax.naming.Name ; 25 import javax.naming.NameNotFoundException ; 26 import javax.naming.NamingEnumeration ; 27 import javax.naming.NamingException ; 28 import javax.naming.NotContextException ; 29 import javax.naming.Reference ; 30 import javax.naming.Referenceable ; 31 import javax.naming.directory.Attributes ; 32 import javax.naming.directory.DirContext ; 33 import javax.naming.spi.NamingManager ; 34 35 import org.apache.naming.core.BaseDirContext; 36 import org.apache.naming.core.NamingContextEnumeration; 37 import org.apache.naming.core.NamingEntry; 38 import org.apache.tomcat.util.res.StringManager; 39 40 48 public class MemoryNamingContext extends BaseDirContext { 49 50 public MemoryNamingContext() 51 throws NamingException  52 { 53 super(); 54 } 55 56 59 public MemoryNamingContext(Hashtable env) 60 throws NamingException  61 { 62 super( env ); 63 this.bindings = new Hashtable (); 64 } 65 66 68 71 protected static StringManager sm = 72 StringManager.getManager("org.apache.naming.res"); 73 74 77 protected Hashtable bindings; 78 79 public void setBindings( Hashtable bindings ) { 80 this.bindings = bindings; 81 } 82 83 85 99 public void unbind(Name name, boolean isContext) 100 throws NamingException  101 { 102 checkWritable(name); 103 104 while ((!name.isEmpty()) && (name.get(0).length() == 0)) 105 name = name.getSuffix(1); 106 107 if (name.isEmpty()) 108 throw new NamingException  109 (sm.getString("namingContext.invalidName")); 110 111 NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); 112 113 if (entry == null) { 114 throw new NameNotFoundException  115 (sm.getString("namingContext.nameNotBound", name.get(0))); 116 } 117 118 if (name.size() > 1) { 119 if (entry.type == NamingEntry.CONTEXT) { 120 ((Context ) entry.value).unbind(name.getSuffix(1)); 121 } else { 122 throw new NamingException  123 (sm.getString("namingContext.contextExpected")); 124 } 125 } else { 126 if (entry.type == NamingEntry.CONTEXT) { 127 ((Context ) entry.value).close(); 128 bindings.remove(name.get(0)); 129 } else { 130 if( isContext ) { 131 throw new NotContextException  132 (sm.getString("namingContext.contextExpected")); 133 } else { 134 bindings.remove(name.get(0)); 135 } 136 } 137 } 138 } 139 140 153 public NamingEnumeration list(Name name) 154 throws NamingException { 155 while ((!name.isEmpty()) && (name.get(0).length() == 0)) 157 name = name.getSuffix(1); 158 159 if (name.isEmpty()) { 160 return new NamingContextEnumeration(bindings.elements(), this, false); 161 } 162 163 NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); 164 165 if (entry == null) { 166 throw new NameNotFoundException  167 (sm.getString("namingContext.nameNotBound", name.get(0))); 168 } 169 170 if (entry.type != NamingEntry.CONTEXT) { 171 throw new NamingException  172 (sm.getString("namingContext.contextExpected")); 173 } 174 return ((Context ) entry.value).list(name.getSuffix(1)); 175 } 176 177 private Name removeEmptyPrefix(Name name ) { 178 while ((!name.isEmpty()) && (name.get(0).length() == 0)) 179 name = name.getSuffix(1); 180 return name; 181 } 182 183 196 public NamingEnumeration listBindings(Name name) 197 throws NamingException { 198 while ((!name.isEmpty()) && (name.get(0).length() == 0)) 200 name = name.getSuffix(1); 201 202 if (name.isEmpty()) { 203 return new NamingContextEnumeration(bindings.elements(), this, true); 204 } 205 206 NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); 207 208 if (entry == null) { 209 throw new NameNotFoundException  210 (sm.getString("namingContext.nameNotBound", name.get(0))); 211 } 212 213 if (entry.type != NamingEntry.CONTEXT) { 214 throw new NamingException  215 (sm.getString("namingContext.contextExpected")); 216 } 217 return ((Context ) entry.value).listBindings(name.getSuffix(1)); 218 } 219 220 233 public DirContext createSubcontext(Name name, Attributes attrs) 234 throws NamingException  235 { 236 checkWritable(name); 237 238 DirContext newContext = new MemoryNamingContext(env); 239 bind(name, newContext); 240 return newContext; 241 } 242 243 private NamingEntry findNamingEntry(Name name, boolean resolveLinks) 245 throws NamingException  246 { 247 if (name.isEmpty()) { 248 return null; 253 } 254 255 NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); 256 257 if (entry == null) { 258 throw new NameNotFoundException  259 (sm.getString("namingContext.nameNotBound", name.get(0))); 260 } 261 262 if (name.size() > 1) { 263 if (entry.type != NamingEntry.CONTEXT) { 266 throw new NamingException  267 (sm.getString("namingContext.contextExpected")); 268 } 269 return entry; 270 } else { 271 return entry; 272 } 273 } 274 275 public Object lookup(Name name, boolean resolveLinks) 276 throws NamingException  277 { 278 while ((!name.isEmpty()) && (name.get(0).length() == 0)) 280 name = name.getSuffix(1); 281 282 NamingEntry entry=findNamingEntry( name, resolveLinks ); 283 284 if( entry.type == NamingEntry.CONTEXT ) { 285 return ((BaseDirContext) entry.value).lookup(name.getSuffix(1), resolveLinks); 286 } 287 288 if ((resolveLinks) && (entry.type == NamingEntry.LINK_REF)) { 289 String link = ((LinkRef ) entry.value).getLinkName(); 290 if (link.startsWith(".")) { 291 return lookup(link.substring(1)); 293 } else { 294 return (new InitialContext (env)).lookup(link); 295 } 296 } else if (entry.type == NamingEntry.REFERENCE) { 297 try { 298 Object obj = NamingManager.getObjectInstance 299 (entry.value, name, this, env); 300 if (obj != null) { 301 entry.value = obj; 302 entry.type = NamingEntry.ENTRY; 303 } 304 return obj; 305 } catch (NamingException e) { 306 throw e; 307 } catch (Exception e) { 308 throw new NamingException (e.getMessage()); 309 } 310 } else { 311 return entry.value; 312 } 313 } 314 315 328 public void bind(Name name, Object obj, Attributes attrs, 329 boolean rebind) 330 throws NamingException  331 { 332 checkWritable(name); 333 334 while ((!name.isEmpty()) && (name.get(0).length() == 0)) 335 name = name.getSuffix(1); 336 337 if (name.isEmpty()) 338 throw new NamingException  339 (sm.getString("namingContext.invalidName")); 340 341 NamingEntry entry = (NamingEntry) bindings.get(name.get(0)); 342 343 if (name.size() > 1) { 344 if (entry == null) { 345 throw new NameNotFoundException  346 (sm.getString("namingContext.nameNotBound", name.get(0))); 347 } 348 if (entry.type == NamingEntry.CONTEXT) { 349 if (rebind) { 350 ((Context ) entry.value).rebind(name.getSuffix(1), obj); 351 } else { 352 ((Context ) entry.value).bind(name.getSuffix(1), obj); 353 } 354 } else { 355 throw new NamingException  356 (sm.getString("namingContext.contextExpected")); 357 } 358 } else { 359 if ((!rebind) && (entry != null)) { 360 throw new NamingException  361 (sm.getString("namingContext.alreadyBound", name.get(0))); 362 } else { 363 Object toBind = 366 NamingManager.getStateToBind(obj, name, this, env); 367 if (toBind instanceof Context ) { 368 entry = new NamingEntry(name.get(0), toBind, attrs, 369 NamingEntry.CONTEXT); 370 } else if (toBind instanceof LinkRef ) { 371 entry = new NamingEntry(name.get(0), toBind, attrs, 372 NamingEntry.LINK_REF); 373 } else if (toBind instanceof Reference ) { 374 entry = new NamingEntry(name.get(0), toBind, attrs, 375 NamingEntry.REFERENCE); 376 } else if (toBind instanceof Referenceable ) { 377 toBind = ((Referenceable ) toBind).getReference(); 378 entry = new NamingEntry(name.get(0), toBind, attrs, 379 NamingEntry.REFERENCE); 380 } else { 381 entry = new NamingEntry(name.get(0), toBind, attrs, 382 NamingEntry.ENTRY); 383 } 384 bindings.put(name.get(0), entry); 385 } 386 } 387 } 388 } 389 390
| Popular Tags
|