1 23 package fr.dyade.aaa.jndi2.scn; 24 25 import java.util.Hashtable ; 26 import javax.naming.Context ; 27 import javax.naming.Name ; 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 import javax.naming.spi.InitialContextFactory ; 34 import javax.naming.spi.ResolveResult ; 35 36 import fr.dyade.aaa.jndi2.client.NamingContextFactory; 37 38 import org.objectweb.util.monolog.api.BasicLevel; 39 import org.objectweb.util.monolog.api.Logger; 40 41 47 public class scnURLContext implements Context { 48 49 public final static Logger logger; 50 51 static { 52 logger = fr.dyade.aaa.util.Debug.getLogger("fr.dyade.aaa.jndi2.scn"); 53 } 54 55 static private final String URL_PREFIX = "scn:comp/"; 56 static private final String ENV_PREFIX = "env"; 57 58 private Hashtable myEnv; 59 private InitialContextFactory namingFactory; 60 61 64 public scnURLContext(Hashtable env) throws NamingException { 65 if (logger.isLoggable(BasicLevel.DEBUG)) 66 logger.log(BasicLevel.DEBUG, 67 "scnURLContext.<init>(" + env + ')'); 68 if (env != null) { 69 myEnv = (Hashtable )env.clone(); 71 72 String factoryClassName = (String )myEnv.get("scn.naming.factory.class"); 73 if (factoryClassName == null) { 74 namingFactory = new NamingContextFactory(); 75 } else { 76 try { 77 Class factoryClass = Class.forName(factoryClassName); 78 namingFactory = 79 (InitialContextFactory )factoryClass.newInstance(); 80 } catch (Exception exc) { 81 NamingException ne = new NamingException (); 82 ne.setRootCause(exc); 83 throw ne; 84 } 85 } 86 } 87 } 88 89 92 private String getRelativeName(String name) throws NamingException { 93 if (!name.startsWith(URL_PREFIX)) { 95 throw new NameNotFoundException ("Invalid name:" + name); 96 } 97 98 name = name.substring(URL_PREFIX.length()); 99 return name; 100 } 101 102 106 private ResolveResult findContextFor(String name) throws NamingException { 107 String rname = getRelativeName(name); 108 Context context = null; 109 110 if (rname.equals("")) { 111 context = new scnURLContext(myEnv); 113 } else { 114 context = namingFactory.getInitialContext(myEnv); 116 } 117 118 if (context == null) { 120 throw new NameNotFoundException ("No context for this component"); 121 } 122 123 ResolveResult r = new ResolveResult (context, rname); 125 return r; 126 } 127 128 132 139 public Object lookup(Name name) throws NamingException { 140 return lookup(name.toString()); 142 } 143 144 151 public Object lookup(String name) throws NamingException { 152 if (logger.isLoggable(BasicLevel.DEBUG)) 153 logger.log(BasicLevel.DEBUG, 154 "scnURLContext.lookup(" + name + ')'); 155 if (name.equals("")) { 157 return new scnURLContext(myEnv); 158 } 159 160 ResolveResult r = findContextFor(name); 162 Context ctx = (Context ) r.getResolvedObj(); 163 String rname = r.getRemainingName().toString(); 164 165 166 Object o = ctx.lookup(rname); 167 return o; 168 } 172 173 188 public void bind(Name name, Object obj) throws NamingException { 189 bind(name.toString(), obj); 191 } 192 193 207 public void bind(String name, Object obj) throws NamingException { 208 ResolveResult r = findContextFor(name); 210 Context ctx = (Context ) r.getResolvedObj(); 211 String rname = r.getRemainingName().toString(); 212 213 ctx.bind(rname, obj); 215 } 216 217 236 public void rebind(Name name, Object obj) throws NamingException { 237 rebind(name.toString(), obj); 239 } 240 241 253 public void rebind(String name, Object obj) throws NamingException { 254 ResolveResult r = findContextFor(name); 256 Context ctx = (Context ) r.getResolvedObj(); 257 String rname = r.getRemainingName().toString(); 258 259 ctx.rebind(rname, obj); 261 } 262 263 284 public void unbind(Name name) throws NamingException { 285 unbind(name.toString()); 287 } 288 289 298 public void unbind(String name) throws NamingException { 299 ResolveResult r = findContextFor(name); 301 Context ctx = (Context ) r.getResolvedObj(); 302 String rname = r.getRemainingName().toString(); 303 304 ctx.unbind(rname); 306 } 307 308 318 public void rename(Name oldName, Name newName) throws NamingException { 319 rename(oldName.toString(), newName.toString()); 321 } 322 323 333 public void rename(String oldName, String newName) throws NamingException { 334 throw new OperationNotSupportedException ("Rename not supported in scn:comp in " + this.getClass().getName()); 335 } 336 337 356 public NamingEnumeration list(Name name) throws NamingException { 357 return list(name.toString()); 359 } 360 361 373 public NamingEnumeration list(String name) throws NamingException { 374 ResolveResult r = findContextFor(name); 376 Context ctx = (Context ) r.getResolvedObj(); 377 String rname = r.getRemainingName().toString(); 378 379 return ctx.list(rname); 381 } 382 383 402 public NamingEnumeration listBindings(Name name) throws NamingException { 403 return listBindings(name.toString()); 405 } 406 407 419 public NamingEnumeration listBindings(String name) throws NamingException { 420 ResolveResult r = findContextFor(name); 422 Context ctx = (Context ) r.getResolvedObj(); 423 String rname = r.getRemainingName().toString(); 424 425 return ctx.listBindings(rname); 427 } 428 429 462 public void destroySubcontext(Name name) throws NamingException { 463 destroySubcontext(name.toString()); 465 } 466 467 479 public void destroySubcontext(String name) throws NamingException { 480 throw new OperationNotSupportedException ("destroySubcontext not supported in scn:comp in " + this.getClass().getName()); 481 } 482 483 503 public Context createSubcontext(Name name) throws NamingException { 504 return createSubcontext(name.toString()); 506 } 507 508 522 public Context createSubcontext(String name) throws NamingException { 523 ResolveResult r = findContextFor(name); 525 Context ctx = (Context ) r.getResolvedObj(); 526 String rname = r.getRemainingName().toString(); 527 528 return ctx.createSubcontext(rname); 530 } 531 532 546 public Object lookupLink(Name name) throws NamingException { 547 return lookupLink(name.toString()); 549 } 550 551 562 public Object lookupLink(String name) throws NamingException { 563 if (name.equals("")) { 565 return new scnURLContext(myEnv); 566 } 567 568 ResolveResult r = findContextFor(name); 570 Context ctx = (Context ) r.getResolvedObj(); 571 String rname = r.getRemainingName().toString(); 572 573 return ctx.lookupLink(rname); 575 } 576 577 596 public NameParser getNameParser(Name name) throws NamingException { 597 return null; 598 } 599 600 610 public NameParser getNameParser(String name) throws NamingException { 611 return null; 612 } 613 614 627 public Name composeName(Name name, Name prefix) throws NamingException { 628 throw new OperationNotSupportedException ("scnURLContext: composeName not supported"); 629 } 630 631 642 public String composeName(String name, String prefix) throws NamingException { 643 throw new OperationNotSupportedException ("composeName not supported in scn:comp in " + this.getClass().getName()); 644 } 645 646 662 public Object addToEnvironment(String propName, Object propVal) throws NamingException { 663 if (myEnv == null) { 664 myEnv = new Hashtable (); 665 } 666 return myEnv.put(propName, propVal); 667 } 668 669 683 public Object removeFromEnvironment(String propName) throws NamingException { 684 if (myEnv == null) { 685 return null; 686 } 687 return myEnv.remove(propName); 688 } 689 690 705 public Hashtable getEnvironment() throws NamingException { 706 if (myEnv == null) { 707 myEnv = new Hashtable (); 708 } 709 return myEnv; 710 } 711 712 723 public void close() throws NamingException { 724 myEnv = null; 725 } 726 727 736 public String getNameInNamespace() throws NamingException { 737 throw new OperationNotSupportedException ("getNameInNamespace not implemented in scn:comp in " + this.getClass().getName()); 738 } 739 } 740 741 | Popular Tags |