1 25 26 package org.objectweb.carol.jndi.enc.java; 27 28 import java.util.Hashtable ; 29 30 import javax.naming.Context ; 31 import javax.naming.Name ; 32 import javax.naming.NameNotFoundException ; 33 import javax.naming.NameParser ; 34 import javax.naming.NamingEnumeration ; 35 import javax.naming.NamingException ; 36 37 import org.objectweb.carol.util.configuration.TraceCarol; 38 39 48 public class JavaURLContext implements Context { 49 50 51 52 55 private static final String URL_PREFIX = "java:"; 56 57 60 private Hashtable myEnv = null; 61 62 65 private static Hashtable clBindings = new Hashtable (); 66 67 70 private static ThreadLocal threadContext = new ThreadLocal (); 71 72 76 public JavaURLContext(Hashtable env) { 77 78 if (env != null) { 79 myEnv = (Hashtable ) (env.clone()); 81 } 82 } 83 84 90 private String getRelativeName(String name) throws NamingException { 91 92 if (!name.startsWith(URL_PREFIX)) { 94 TraceCarol.error("relative name! :" + name); 95 throw new NameNotFoundException ("Invalid name:" + name); 96 } 97 if (name.endsWith("/")) { 98 name = name.substring(URL_PREFIX.length(), name.length() - 1); 99 } else { 100 name = name.substring(URL_PREFIX.length()); 101 } 102 103 return name; 104 } 105 106 112 private Name getRelativeName(Name name) throws NamingException { 113 if (name.get(0).equals(URL_PREFIX)) { 114 return (name.getSuffix(1)); 115 } else { 116 TraceCarol.error("relative name! :" + name); 117 throw new NameNotFoundException ("Invalid name:" + name); 118 } 119 } 120 121 127 public Object lookup(Name name) throws NamingException { 128 return findContext().lookup(getRelativeName(name)); 129 } 130 131 137 public Object lookup(String name) throws NamingException { 138 return findContext().lookup(getRelativeName(name)); 139 } 140 141 147 public void bind(Name name, Object obj) throws NamingException { 148 findContext().bind(getRelativeName(name), obj); 149 } 150 151 159 public void bind(String name, Object obj) throws NamingException { 160 findContext().bind(getRelativeName(name), obj); 161 } 162 163 174 public void rebind(Name name, Object obj) throws NamingException { 175 findContext().rebind(getRelativeName(name), obj); 176 } 177 178 185 public void rebind(String name, Object obj) throws NamingException { 186 findContext().rebind(getRelativeName(name), obj); 187 } 188 189 201 public void unbind(Name name) throws NamingException { 202 findContext().unbind(getRelativeName(name)); 203 } 204 205 210 public void unbind(String name) throws NamingException { 211 findContext().unbind(getRelativeName(name)); 212 } 213 214 221 public void rename(Name oldName, Name newName) throws NamingException { 222 findContext().rename(getRelativeName(oldName), getRelativeName(newName)); 223 } 224 225 232 public void rename(String oldName, String newName) throws NamingException { 233 findContext().rename(getRelativeName(oldName), getRelativeName(newName)); 234 } 235 236 247 public NamingEnumeration list(Name name) throws NamingException { 248 return findContext().list(getRelativeName(name)); 249 } 250 251 260 public NamingEnumeration list(String name) throws NamingException { 261 return findContext().list(getRelativeName(name)); 262 } 263 264 274 public NamingEnumeration listBindings(Name name) throws NamingException { 275 return findContext().listBindings(getRelativeName(name)); 276 } 277 278 286 public NamingEnumeration listBindings(String name) throws NamingException { 287 return findContext().listBindings(getRelativeName(name)); 288 } 289 290 308 public void destroySubcontext(Name name) throws NamingException { 309 findContext().destroySubcontext(getRelativeName(name)); 310 } 311 312 318 public void destroySubcontext(String name) throws NamingException { 319 findContext().destroySubcontext(getRelativeName(name)); 320 } 321 322 331 public Context createSubcontext(Name name) throws NamingException { 332 return findContext().createSubcontext(getRelativeName(name)); 333 } 334 335 342 public Context createSubcontext(String name) throws NamingException { 343 return findContext().createSubcontext(getRelativeName(name)); 344 } 345 346 355 public Object lookupLink(Name name) throws NamingException { 356 return findContext().lookupLink(getRelativeName(name)); 357 } 358 359 367 public Object lookupLink(String name) throws NamingException { 368 return findContext().lookupLink(getRelativeName(name)); 369 } 370 371 383 public NameParser getNameParser(Name name) throws NamingException { 384 return findContext().getNameParser(getRelativeName(name)); 385 } 386 387 395 public NameParser getNameParser(String name) throws NamingException { 396 return findContext().getNameParser(getRelativeName(name)); 397 } 398 399 406 public Name composeName(Name name, Name prefix) throws NamingException { 407 prefix = (Name ) name.clone(); 408 return prefix.addAll(name); 409 } 410 411 418 public String composeName(String name, String prefix) throws NamingException { 419 return prefix + "/" + name; 420 } 421 422 433 public Object addToEnvironment(String propName, Object propVal) throws NamingException { 434 return findContext().addToEnvironment(propName, propVal); 435 } 436 437 446 public Object removeFromEnvironment(String propName) throws NamingException { 447 return findContext().removeFromEnvironment(propName); 448 } 449 450 459 public Hashtable getEnvironment() throws NamingException { 460 return findContext().getEnvironment(); 461 } 462 463 472 public void close() throws NamingException { 473 findContext().close(); 474 } 475 476 481 public String getNameInNamespace() throws NamingException { 482 return URL_PREFIX; 483 } 484 485 488 public Context findContext() { 489 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 490 491 Context ctx = null; 493 if ((cl != null) && (cl.getParent() != null)) { 494 ctx = (Context ) clBindings.get(cl.getParent()); 495 if (ctx != null) { 496 return ctx; 497 } else { 498 ctx = buildNewContext(cl.getParent().toString()); 500 clBindings.put(cl.getParent(), ctx); 502 } 503 } 504 505 if (ctx == null) { 506 ctx = (Context ) threadContext.get(); 507 if (ctx == null) { 508 ctx = buildNewContext(threadContext.toString()); 510 } 511 } 512 513 return ctx; 514 } 515 516 521 private Context buildNewContext(String name) { 522 return new CompNamingContext(name, (Hashtable ) myEnv.clone()); 523 } 524 525 } | Popular Tags |