1 25 26 package org.objectweb.easybeans.naming.context; 27 28 import java.util.Hashtable ; 29 30 import javax.naming.Binding ; 31 import javax.naming.CompositeName ; 32 import javax.naming.Context ; 33 import javax.naming.InitialContext ; 34 import javax.naming.InvalidNameException ; 35 import javax.naming.LinkRef ; 36 import javax.naming.Name ; 37 import javax.naming.NameAlreadyBoundException ; 38 import javax.naming.NameClassPair ; 39 import javax.naming.NameNotFoundException ; 40 import javax.naming.NameParser ; 41 import javax.naming.NamingEnumeration ; 42 import javax.naming.NamingException ; 43 import javax.naming.NotContextException ; 44 import javax.naming.OperationNotSupportedException ; 45 import javax.naming.RefAddr ; 46 import javax.naming.Reference ; 47 48 import org.objectweb.easybeans.log.JLog; 49 import org.objectweb.easybeans.log.JLogFactory; 50 51 55 public class ContextImpl implements Context { 56 57 60 private static JLog logger = JLogFactory.getLog(ContextImpl.class); 61 62 65 private Hashtable environment = null; 66 67 70 private Hashtable <String , Object > bindings = new Hashtable <String , Object >(); 71 72 75 private static NameParser myParser = new JavaNameParser(); 76 77 80 private String compId; 81 82 87 public ContextImpl(final String id, final Hashtable <Object , Object > env) { 88 if (env != null) { 89 environment = (Hashtable ) (env.clone()); 91 } else { 92 environment = new Hashtable <Object , Object >(); 93 } 94 compId = id; 95 } 96 97 101 @SuppressWarnings ("unchecked") 102 public ContextImpl(final String id) { 103 this(id, new Hashtable ()); 104 } 105 106 112 public Object lookup(final Name name) throws NamingException { 113 return lookup(name.toString()); 115 } 116 117 123 public Object lookup(final String name) throws NamingException { 124 logger.debug("lookup {0}", name); 125 126 Name n = new CompositeName (name); 127 if (n.size() < 1) { 128 logger.debug("Empty name"); 130 return this; 131 } 132 133 if (n.size() > 1) { 134 String suffix = n.getSuffix(1).toString(); 136 Context subctx = lookupCtx(n.get(0)); 138 return subctx.lookup(suffix); 139 } 140 Object ret = bindings.get(name); 142 if (ret == null) { 143 logger.debug(" {0} not found.", name); 144 throw new NameNotFoundException (name); 145 } 146 if (ret instanceof LinkRef ) { 147 151 InitialContext ictx = new InitialContext (); 152 RefAddr ra = ((Reference ) ret).get(0); 153 try { 154 ret = ictx.lookup((String ) ra.getContent()); 155 } catch (Exception e) { 156 NamingException ne = new NamingException (e.getMessage()); 157 ne.setRootCause(e); 158 logger.error("unexpected exception {0}", e.getMessage(), e); 159 throw ne; 160 } 161 } else if (ret instanceof Reference ) { 162 try { 164 Object o = javax.naming.spi.NamingManager.getObjectInstance(ret, n, this, environment); 165 ret = o; 166 } catch (NamingException e) { 167 throw e; 168 } catch (Exception e) { 169 NamingException ne = new NamingException (e.getMessage()); 170 ne.setRootCause(e); 171 throw ne; 172 } 173 if (ret == null) { 174 logger.error("Can not build an object with the reference {0}", name); 175 throw new NamingException ("Can not build an object with the reference '" + name + "'"); 176 } 177 } 178 return ret; 179 180 } 181 182 189 public void bind(final Name name, final Object obj) throws NamingException { 190 bind(name.toString(), obj); 192 } 193 194 201 public void bind(final String name, final Object obj) throws NamingException { 202 logger.debug("bind {0}", name); 203 204 Name n = new CompositeName (name); 205 if (n.size() < 1) { 206 logger.error("CompNamingContext bind empty name ?"); 207 208 throw new InvalidNameException ("CompNamingContext cannot bind empty name"); 209 } 210 211 if (n.size() == 1) { 212 if (bindings.get(name) != null) { 214 logger.error("CompNamingContext: trying to overbind"); 215 throw new NameAlreadyBoundException ("CompNamingContext: Use rebind to bind over a name"); 216 } 217 bindings.put(name, obj); 218 } else { 219 String suffix = n.getSuffix(1).toString(); 221 Context subctx; 223 try { 224 subctx = lookupCtx(n.get(0)); 225 } catch (NameNotFoundException e) { 226 subctx = createSubcontext(n.get(0)); 227 } 228 subctx.bind(suffix, obj); 229 } 230 } 231 232 238 public void rebind(final Name name, final Object obj) throws NamingException { 239 rebind(name.toString(), obj); 241 } 242 243 250 public void rebind(final String name, final Object obj) throws NamingException { 251 252 logger.debug("rebind {0}", name); 253 254 Name n = new CompositeName (name); 255 if (n.size() < 1) { 256 logger.error("CompNamingContext rebind empty name ?"); 257 throw new InvalidNameException ("CompNamingContext cannot rebind empty name"); 258 } 259 260 if (n.size() == 1) { 261 bindings.put(name, obj); 263 } else { 264 String suffix = n.getSuffix(1).toString(); 266 Context subctx; 268 try { 269 subctx = lookupCtx(n.get(0)); 270 } catch (NameNotFoundException e) { 271 subctx = createSubcontext(n.get(0)); 272 } 273 subctx.rebind(suffix, obj); 274 } 275 } 276 277 283 public void unbind(final Name name) throws NamingException { 284 unbind(name.toString()); 286 } 287 288 295 public void unbind(final String name) throws NamingException { 296 297 logger.debug("unbind {0}", name); 298 299 Name n = new CompositeName (name); 300 if (n.size() < 1) { 301 logger.error("CompNamingContext rebind empty name ?"); 302 throw new InvalidNameException ("CompNamingContext cannot unbind empty name"); 303 } 304 305 if (n.size() == 1) { 306 if (bindings.get(name) == null) { 308 logger.error("CompNamingContext nothing to unbind"); 309 throw new NameNotFoundException (name); 310 } 311 bindings.remove(name); 312 } else { 313 String suffix = n.getSuffix(1).toString(); 315 Context subctx = lookupCtx(n.get(0)); 317 subctx.unbind(suffix); 318 } 319 } 320 321 328 public void rename(final Name oldName, final Name newName) throws NamingException { 329 rename(oldName.toString(), newName.toString()); 331 } 332 333 340 public void rename(final String oldName, final String newName) throws NamingException { 341 logger.debug("CompNamingContext rename {0} in {1}", oldName, newName); 342 Object obj = lookup(oldName); 343 rebind(newName, obj); 344 unbind(oldName); 345 } 346 347 357 public NamingEnumeration <NameClassPair > list(final Name name) throws NamingException { 358 return list(name.toString()); 360 } 361 362 371 public NamingEnumeration <NameClassPair > list(final String name) throws NamingException { 372 logger.debug("list {0}", name); 373 374 if (name.length() == 0) { 375 return new NamingEnumerationImpl(bindings); 377 } 378 Object obj = lookup(name); 379 if (obj instanceof Context ) { 380 return ((Context ) obj).list(""); 381 } 382 throw new NotContextException (name); 383 } 384 385 395 public NamingEnumeration <Binding > listBindings(final Name name) throws NamingException { 396 return listBindings(name.toString()); 398 } 399 400 408 public NamingEnumeration <Binding > listBindings(final String name) throws NamingException { 409 logger.debug("listBindings {0}", name); 410 411 if (name.length() == 0) { 412 return new BindingsImpl(bindings); 414 } 415 Object obj = lookup(name); 416 if (obj instanceof Context ) { 417 return ((Context ) obj).listBindings(""); 418 } 419 logger.error("CompNamingContext: can only list a Context"); 420 throw new NotContextException (name); 421 422 } 423 424 430 public void destroySubcontext(final Name name) throws NamingException { 431 destroySubcontext(name.toString()); 433 } 434 435 441 public void destroySubcontext(final String name) throws NamingException { 442 logger.error("CompNamingContext try to destroySubcontext {0}", name); 443 throw new OperationNotSupportedException ("CompNamingContext: destroySubcontext"); 444 } 445 446 454 public Context createSubcontext(final Name name) throws NamingException { 455 return createSubcontext(name.toString()); 457 } 458 459 466 @SuppressWarnings ("unchecked") 467 public Context createSubcontext(final String name) throws NamingException { 468 logger.debug("createSubcontext {0}", name); 469 470 Name n = new CompositeName (name); 471 if (n.size() < 1) { 472 logger.error("CompNamingContext createSubcontext with empty name ?"); 473 throw new InvalidNameException ("CompNamingContext cannot create empty Subcontext"); 474 } 475 476 Context ctx = null; if (n.size() == 1) { 478 ctx = new ContextImpl(compId, environment); 480 bindings.put(name, ctx); 481 } else { 482 String suffix = n.getSuffix(1).toString(); 485 Context subctx; 486 String newName = n.get(0); 487 try { 488 subctx = lookupCtx(newName); 489 } catch (NameNotFoundException e) { 490 subctx = createSubcontext(newName); 491 } 492 ctx = subctx.createSubcontext(suffix); 493 } 494 return ctx; 495 } 496 497 506 public Object lookupLink(final Name name) throws NamingException { 507 return lookupLink(name.toString()); 509 } 510 511 520 public Object lookupLink(final String name) throws NamingException { 521 logger.debug("lookupLink {0}", name); 522 523 logger.error("CompNamingContext lookupLink not implemented yet!"); 525 526 return lookup(name); 527 } 528 529 536 public NameParser getNameParser(final Name name) throws NamingException { 537 return myParser; 538 } 539 540 547 public NameParser getNameParser(final String name) throws NamingException { 548 return myParser; 549 } 550 551 558 public Name composeName(final Name name, final Name prefix) throws NamingException { 559 logger.error("CompNamingContext composeName not implemented yet!"); 560 561 throw new OperationNotSupportedException ("CompNamingContext composeName"); 562 } 563 564 572 public String composeName(final String name, final String prefix) throws NamingException { 573 logger.error("CompNamingContext composeName {0} {1}", name, prefix); 574 575 throw new OperationNotSupportedException ("CompNamingContext composeName"); 576 } 577 578 588 @SuppressWarnings ("unchecked") 589 public Object addToEnvironment(final String propName, final Object propVal) throws NamingException { 590 return environment.put(propName, propVal); 591 } 592 593 601 public Object removeFromEnvironment(final String propName) throws NamingException { 602 603 logger.debug("removeFromEnvironment {0}", propName); 604 605 if (environment == null) { 606 return null; 607 } 608 return environment.remove(propName); 609 } 610 611 616 public Hashtable <?, ?> getEnvironment() throws NamingException { 617 return environment; 618 } 619 620 624 public void close() throws NamingException { 625 environment = null; 626 } 627 628 632 public String getNameInNamespace() { 633 return compId; 635 } 636 637 641 649 private Context lookupCtx(final String name) throws NamingException { 650 Object obj = bindings.get(name); 651 if (obj == null) { 652 throw new NameNotFoundException (); 653 } 654 if (obj instanceof ContextImpl) { 655 return (Context ) obj; 656 } 657 throw new NameAlreadyBoundException (name); 658 } 659 660 } 661 | Popular Tags |