1 25 package org.objectweb.carol.jndi.spi; 26 27 import java.rmi.Remote ; 28 import java.util.Hashtable ; 29 30 import javax.naming.CompositeName ; 31 import javax.naming.Context ; 32 import javax.naming.InvalidNameException ; 33 import javax.naming.Name ; 34 import javax.naming.NameParser ; 35 import javax.naming.NamingEnumeration ; 36 import javax.naming.NamingException ; 37 38 import org.objectweb.carol.jndi.wrapping.JNDIRemoteResource; 39 import org.objectweb.carol.rmi.exception.NamingExceptionHelper; 40 import org.objectweb.carol.util.configuration.ConfigurationRepository; 41 42 49 public abstract class AbsContext implements Context { 50 51 54 private Context wrappedContext = null; 55 56 59 private Hashtable exportedObjects = new Hashtable (); 60 61 66 protected AbsContext(Context ctx) { 67 this.wrappedContext = ctx; 68 } 69 70 78 protected abstract Object unwrapObject(Object o, Name name) throws NamingException ; 79 80 91 protected abstract Object wrapObject(Object o, Name name, boolean replace) throws NamingException ; 92 93 102 protected Object defaultUnwrapObject(Object o, Name name) throws NamingException { 103 try { 104 if (o instanceof JNDIRemoteResource) { 105 return ((JNDIRemoteResource) o).getResource(); 106 } else { 107 return o; 108 } 109 } catch (Exception e) { 110 throw NamingExceptionHelper.create("Cannot unwrap object '" + o + "' with name '" + name + "' :" 111 + e.getMessage(), e); 112 } 113 } 114 115 119 protected int getObjectPort() { 120 return 0; 121 122 } 123 124 130 public Object lookup(Name name) throws NamingException { 131 if (name.isEmpty()) { 132 return this; 133 } 134 return unwrapObject(wrappedContext.lookup(encode(name)), name); 135 } 136 137 143 public Object lookup(String name) throws NamingException { 144 return lookup(new CompositeName (name)); 145 } 146 147 153 public void bind(Name name, Object obj) throws NamingException { 154 if (name.isEmpty()) { 155 throw new NamingException ("Cannot bind empty name"); 156 } 157 wrappedContext.bind(encode(name), wrapObject(obj, name, false)); 158 } 159 160 166 public void bind(String name, Object obj) throws NamingException { 167 bind(new CompositeName (name), obj); 168 } 169 170 178 public void rebind(Name name, Object obj) throws NamingException { 179 if (name.isEmpty()) { 180 throw new NamingException ("Cannot rebind empty name"); 181 } 182 wrappedContext.rebind(encode(name), wrapObject(obj, name, true)); 183 } 184 185 191 public void rebind(String name, Object obj) throws NamingException { 192 rebind(new CompositeName (name), obj); 193 } 194 195 202 public void unbind(Name name) throws NamingException { 203 if (name.isEmpty()) { 204 throw new NamingException ("Cannot unbind empty name"); 205 } 206 try { 207 wrappedContext.unbind(encode(name)); 208 if (exportedObjects.containsKey(name)) { 209 ConfigurationRepository.getCurrentConfiguration().getProtocol().getPortableRemoteObject().unexportObject( 210 (Remote ) exportedObjects.remove(name)); 211 } 212 } catch (Exception e) { 213 throw NamingExceptionHelper.create("Cannot unbind name '" + name + "' : " + e.getMessage(), e); 214 } 215 } 216 217 222 public void unbind(String name) throws NamingException { 223 unbind(new CompositeName (name)); 224 } 225 226 234 public void rename(Name oldName, Name newName) throws NamingException { 235 if (exportedObjects.containsKey(oldName)) { 236 exportedObjects.put(newName, exportedObjects.remove(oldName)); 237 } 238 wrappedContext.rename(encode(oldName), encode(newName)); 239 } 240 241 248 public void rename(String oldName, String newName) throws NamingException { 249 rename(new CompositeName (oldName), new CompositeName (newName)); 250 } 251 252 262 public NamingEnumeration list(Name name) throws NamingException { 263 return new WrappedEnumeration(wrappedContext.list(encode(name))); 264 } 265 266 275 public NamingEnumeration list(String name) throws NamingException { 276 return list(new CompositeName (name)); 277 } 278 279 287 public NamingEnumeration listBindings(Name name) throws NamingException { 288 return new WrappedEnumeration(wrappedContext.listBindings(encode(name))); 289 } 290 291 299 public NamingEnumeration listBindings(String name) throws NamingException { 300 return listBindings(new CompositeName (name)); 301 } 302 303 310 public void destroySubcontext(Name name) throws NamingException { 311 wrappedContext.destroySubcontext(encode(name)); 312 } 313 314 319 public void destroySubcontext(String name) throws NamingException { 320 destroySubcontext(new CompositeName (name)); 321 } 322 323 329 public Context createSubcontext(Name name) throws NamingException { 330 return wrappedContext.createSubcontext(encode(name)); 331 } 332 333 339 public Context createSubcontext(String name) throws NamingException { 340 return createSubcontext(new CompositeName (name)); 341 } 342 343 351 public Object lookupLink(Name name) throws NamingException { 352 return wrappedContext.lookupLink(encode(name)); 353 } 354 355 363 public Object lookupLink(String name) throws NamingException { 364 return lookupLink(new CompositeName (name)); 365 } 366 367 374 public NameParser getNameParser(Name name) throws NamingException { 375 return wrappedContext.getNameParser(encode(name)); 376 } 377 378 385 public NameParser getNameParser(String name) throws NamingException { 386 return getNameParser(new CompositeName (name)); 387 } 388 389 396 public String composeName(String name, String prefix) throws NamingException { 397 return name; 398 } 399 400 407 public Name composeName(Name name, Name prefix) throws NamingException { 408 return (Name ) name.clone(); 409 } 410 411 422 public Object addToEnvironment(String propName, Object propVal) throws NamingException { 423 return wrappedContext.addToEnvironment(propName, propVal); 424 } 425 426 435 public Object removeFromEnvironment(String propName) throws NamingException { 436 return wrappedContext.removeFromEnvironment(propName); 437 } 438 439 445 public Hashtable getEnvironment() throws NamingException { 446 return wrappedContext.getEnvironment(); 447 } 448 449 455 public void close() throws NamingException { 456 } 458 459 464 public String getNameInNamespace() throws NamingException { 465 return wrappedContext.getNameInNamespace(); 466 } 467 468 474 protected Object addToExported(Name name, Object o) { 475 return exportedObjects.put(name, o); 476 } 477 478 481 protected Context getWrappedContext() { 482 return wrappedContext; 483 } 484 485 491 protected Name encode(Name initialName) { 492 String name = initialName.toString(); 493 494 if (name.length() < 1) { 496 return initialName; 497 } 498 StringBuffer newname = new StringBuffer (name); 500 int i = 0; 501 while (i < newname.length()) { 502 char c = newname.charAt(i); 503 if (c == '/' || c == '\\') { 504 newname.insert(i, '\\'); 505 i++; 506 } 507 i++; 508 } 509 if (newname.charAt(0) == '"' || newname.charAt(0) == '\'') { 511 newname.insert(0, '\\'); 512 } 513 514 try { 516 return new CompositeName (newname.toString()); 517 } catch (InvalidNameException e) { 518 return initialName; 519 } 520 } 521 522 527 protected String decode(String name) { 528 StringBuffer newname = new StringBuffer (name); 529 if (newname.length() >= 2 && (newname.charAt(0) == '"' || newname.charAt(0) == '\'') 531 && newname.charAt(0) == newname.charAt(newname.length() - 1)) { 532 newname.deleteCharAt(0); 533 newname.deleteCharAt(newname.length() - 1); 534 } 535 if (name.indexOf('\\') < 0) { 537 return newname.toString(); 538 } 539 int i = 0; 541 while (i < newname.length()) { 542 if (newname.charAt(i) == '\\') { 543 newname.deleteCharAt(i); 544 i++; 545 continue; 546 } 547 i++; 548 } 549 550 return newname.toString(); 551 } 552 553 557 protected class WrappedEnumeration implements NamingEnumeration { 558 559 562 private NamingEnumeration wrappedEnumeration; 563 564 568 WrappedEnumeration(NamingEnumeration wrappedEnumeration) { 569 this.wrappedEnumeration = wrappedEnumeration; 570 } 571 572 578 public boolean hasMoreElements() { 579 return wrappedEnumeration.hasMoreElements(); 580 } 581 582 592 public boolean hasMore() throws NamingException { 593 return hasMoreElements(); 594 } 595 596 601 public Object nextElement() { 602 javax.naming.NameClassPair ncp; 603 ncp = (javax.naming.NameClassPair ) wrappedEnumeration.nextElement(); 604 ncp.setName(decode(ncp.getName())); 605 return ncp; 606 } 607 608 613 public Object next() { 614 return nextElement(); 615 } 616 617 622 public void close() throws NamingException { 623 wrappedEnumeration = null; 624 } 625 } 626 } 627 | Popular Tags |