1 25 package org.objectweb.easybeans.naming.pkg.java; 26 27 import java.util.Hashtable ; 28 29 import javax.naming.Binding ; 30 import javax.naming.Context ; 31 import javax.naming.Name ; 32 import javax.naming.NameClassPair ; 33 import javax.naming.NameNotFoundException ; 34 import javax.naming.NameParser ; 35 import javax.naming.NamingEnumeration ; 36 import javax.naming.NamingException ; 37 38 import org.objectweb.easybeans.log.JLog; 39 import org.objectweb.easybeans.log.JLogFactory; 40 import org.objectweb.easybeans.naming.NamingManager; 41 42 46 public class JavaURLContext implements Context { 47 48 51 private static final String JAVA_PREFIX = "java:"; 52 53 56 private static NamingManager namingManager = null; 57 58 61 private static JLog logger = JLogFactory.getLog(JavaURLContext.class); 62 63 67 public JavaURLContext() throws NamingException { 68 if (namingManager == null) { 69 namingManager = NamingManager.getInstance(); 70 } 71 } 72 73 79 private String getRelativeName(final String name) throws NamingException { 80 String newName = name; 81 if (!name.startsWith(JAVA_PREFIX)) { 83 logger.error("relative name! {0}" + name); 84 throw new NameNotFoundException ("Invalid name:" + name); 85 } 86 if (name.endsWith("/")) { 87 newName = name.substring(JAVA_PREFIX.length() + 1); 88 } else { 89 newName = name.substring(JAVA_PREFIX.length()); 90 } 91 92 return newName; 93 } 94 95 101 private Name getRelativeName(final Name name) throws NamingException { 102 if (name.get(0).equals(JAVA_PREFIX)) { 103 return (name.getSuffix(1)); 104 } 105 throw new NameNotFoundException ("Invalid name:" + name); 106 } 107 108 114 public Object lookup(final Name name) throws NamingException { 115 return findContext().lookup(getRelativeName(name)); 116 } 117 118 124 public Object lookup(final String name) throws NamingException { 125 return findContext().lookup(getRelativeName(name)); 126 } 127 128 134 public void bind(final Name name, final Object obj) throws NamingException { 135 findContext().bind(getRelativeName(name), obj); 136 } 137 138 146 public void bind(final String name, final Object obj) throws NamingException { 147 findContext().bind(getRelativeName(name), obj); 148 } 149 150 161 public void rebind(final Name name, final Object obj) throws NamingException { 162 findContext().rebind(getRelativeName(name), obj); 163 } 164 165 172 public void rebind(final String name, final Object obj) throws NamingException { 173 findContext().rebind(getRelativeName(name), obj); 174 } 175 176 188 public void unbind(final Name name) throws NamingException { 189 findContext().unbind(getRelativeName(name)); 190 } 191 192 197 public void unbind(final String name) throws NamingException { 198 findContext().unbind(getRelativeName(name)); 199 } 200 201 208 public void rename(final Name oldName, final Name newName) throws NamingException { 209 findContext().rename(getRelativeName(oldName), getRelativeName(newName)); 210 } 211 212 219 public void rename(final String oldName, final String newName) throws NamingException { 220 findContext().rename(getRelativeName(oldName), getRelativeName(newName)); 221 } 222 223 234 public NamingEnumeration <NameClassPair > list(final Name name) throws NamingException { 235 return findContext().list(getRelativeName(name)); 236 } 237 238 247 public NamingEnumeration <NameClassPair > list(final String name) throws NamingException { 248 return findContext().list(getRelativeName(name)); 249 } 250 251 261 public NamingEnumeration <Binding > listBindings(final Name name) throws NamingException { 262 return findContext().listBindings(getRelativeName(name)); 263 } 264 265 273 public NamingEnumeration <Binding > listBindings(final String name) throws NamingException { 274 return findContext().listBindings(getRelativeName(name)); 275 } 276 277 295 public void destroySubcontext(final Name name) throws NamingException { 296 findContext().destroySubcontext(getRelativeName(name)); 297 } 298 299 305 public void destroySubcontext(final String name) throws NamingException { 306 findContext().destroySubcontext(getRelativeName(name)); 307 } 308 309 318 public Context createSubcontext(final Name name) throws NamingException { 319 return findContext().createSubcontext(getRelativeName(name)); 320 } 321 322 329 public Context createSubcontext(final String name) throws NamingException { 330 return findContext().createSubcontext(getRelativeName(name)); 331 } 332 333 342 public Object lookupLink(final Name name) throws NamingException { 343 return findContext().lookupLink(getRelativeName(name)); 344 } 345 346 354 public Object lookupLink(final String name) throws NamingException { 355 return findContext().lookupLink(getRelativeName(name)); 356 } 357 358 370 public NameParser getNameParser(final Name name) throws NamingException { 371 return findContext().getNameParser(getRelativeName(name)); 372 } 373 374 382 public NameParser getNameParser(final String name) throws NamingException { 383 return findContext().getNameParser(getRelativeName(name)); 384 } 385 386 393 public Name composeName(final Name name, final Name prefix) throws NamingException { 394 Name newPrefix = (Name ) name.clone(); 395 return newPrefix.addAll(name); 396 } 397 398 405 public String composeName(final String name, final String prefix) throws NamingException { 406 return prefix + "/" + name; 407 } 408 409 420 public Object addToEnvironment(final String propName, final Object propVal) throws NamingException { 421 return findContext().addToEnvironment(propName, propVal); 422 } 423 424 433 public Object removeFromEnvironment(final String propName) throws NamingException { 434 return findContext().removeFromEnvironment(propName); 435 } 436 437 446 public Hashtable <?, ?> getEnvironment() throws NamingException { 447 return findContext().getEnvironment(); 448 } 449 450 459 public void close() throws NamingException { 460 findContext().close(); 461 } 462 463 468 public String getNameInNamespace() throws NamingException { 469 return JAVA_PREFIX; 470 } 471 472 476 public Context findContext() throws NamingException { 477 return namingManager.getComponentContext(); 478 } 479 480 481 } 482 | Popular Tags |