1 28 package org.objectweb.carol.jndi.registry; 29 30 import java.rmi.AlreadyBoundException ; 31 import java.rmi.NotBoundException ; 32 import java.rmi.Remote ; 33 import java.rmi.RemoteException ; 34 import java.rmi.registry.Registry ; 35 import java.util.Hashtable ; 36 import java.util.Properties ; 37 38 import javax.naming.Binding ; 39 import javax.naming.CompositeName ; 40 import javax.naming.CompoundName ; 41 import javax.naming.Context ; 42 import javax.naming.Name ; 43 import javax.naming.NameAlreadyBoundException ; 44 import javax.naming.NameNotFoundException ; 45 import javax.naming.NameParser ; 46 import javax.naming.NamingEnumeration ; 47 import javax.naming.NamingException ; 48 49 import org.objectweb.carol.jndi.ns.JRMPRegistry; 50 import org.objectweb.carol.rmi.exception.NamingExceptionHelper; 51 52 57 public class RegistryWrapperContext implements Context { 58 59 62 private Registry registry; 63 64 67 private static Hashtable environment = null; 68 69 72 private static final NameParser NAME_PARSER = new SimpleNameParser(); 73 74 78 public RegistryWrapperContext(Hashtable env) { 79 registry = JRMPRegistry.getRegistry(); 80 environment = env; 81 environment.put(Context.INITIAL_CONTEXT_FACTORY, "org.objectweb.carol.jndi.spi.JRMPContextWrapperFactory"); 82 } 83 84 90 public Object lookup(Name name) throws NamingException { 91 if (name.isEmpty()) { 92 return this; 93 } 94 Remote obj; 95 try { 96 obj = registry.lookup(name.get(0)); 97 } catch (NotBoundException e) { 98 NameNotFoundException nnfe = new NameNotFoundException (name.get(0)); 99 nnfe.setRootCause(e); 100 throw nnfe; 101 } catch (Exception e) { 102 throw NamingExceptionHelper.create("Cannot lookup name '" + name + "' : " + e.getMessage(), e); 103 } 104 return obj; 105 } 106 107 113 public Object lookup(String name) throws NamingException { 114 return lookup(new CompositeName (name)); 115 } 116 117 123 public void bind(Name name, Object obj) throws NamingException { 124 if (name.isEmpty()) { 125 throw new NamingException ("Cannot bind empty name"); 126 } 127 128 if (!(obj instanceof Remote )) { 129 throw new NamingException ( 130 "Can only bind object which implements Remote interface. This is not the case for object '" + obj 131 + "' with name '" + name + "'."); 132 } 133 134 try { 135 registry.bind(name.get(0), (Remote ) obj); 136 } catch (AlreadyBoundException e) { 137 NamingException ne = new NameAlreadyBoundException (name.get(0)); 138 ne.setRootCause(e); 139 throw ne; 140 } catch (Exception e) { 141 NamingException ne = new NamingException (); 142 ne.setRootCause(e); 143 throw ne; 144 } 145 } 146 147 153 public void bind(String name, Object obj) throws NamingException { 154 bind(new CompositeName (name), obj); 155 } 156 157 165 public void rebind(Name name, Object obj) throws NamingException { 166 if (name.isEmpty()) { 167 throw new NamingException ("Cannot rebind empty name"); 168 } 169 if (!(obj instanceof Remote )) { 170 throw new NamingException ( 171 "Can only rebind object which implements Remote interface. This is not the case for object '" + obj 172 + "' with name '" + name + "'."); 173 } 174 175 try { 176 registry.rebind(name.get(0), (Remote ) obj); 177 } catch (Exception e) { 178 NamingException ne = new NamingException (); 179 ne.setRootCause(e); 180 throw ne; 181 } 182 } 183 184 190 public void rebind(String name, Object obj) throws NamingException { 191 rebind(new CompositeName (name), obj); 192 } 193 194 201 public void unbind(Name name) throws NamingException { 202 if (name.isEmpty()) { 203 throw new NamingException ("Cannot unbind empty name"); 204 } 205 try { 206 registry.unbind(name.get(0)); 207 } catch (Exception e) { 208 NamingException ne = new NamingException (); 209 ne.setRootCause(e); 210 throw ne; 211 } 212 } 213 214 219 public void unbind(String name) throws NamingException { 220 unbind(new CompositeName (name)); 221 } 222 223 231 public void rename(Name oldName, Name newName) throws NamingException { 232 bind(newName, lookup(oldName)); 233 unbind(oldName); 234 } 235 236 243 public void rename(String oldName, String newName) throws NamingException { 244 rename(new CompositeName (oldName), new CompositeName (newName)); 245 } 246 247 257 public NamingEnumeration list(Name name) throws NamingException { 258 if (!name.isEmpty()) { 259 throw new NamingException ("Cannot list with a given empty name"); 260 } 261 try { 262 String [] names = registry.list(); 263 return new LocalEnumeration(this, names); 264 } catch (Exception e) { 265 NamingException ne = new NamingException (); 266 ne.setRootCause(e); 267 throw ne; 268 } 269 } 270 271 280 public NamingEnumeration list(String name) throws NamingException { 281 return list(new CompositeName (name)); 282 } 283 284 292 public NamingEnumeration listBindings(Name name) throws NamingException { 293 if (!name.isEmpty()) { 294 throw new NamingException ("can not list"); 295 } 296 try { 297 String [] names = registry.list(); 298 return new LocalEnumeration(this, names); 299 } catch (RemoteException e) { 300 NamingException ne = new NamingException (); 301 ne.setRootCause(e); 302 throw ne; 303 } 304 } 305 306 314 public NamingEnumeration listBindings(String name) throws NamingException { 315 return listBindings(new CompositeName (name)); 316 } 317 318 325 public void destroySubcontext(Name name) throws NamingException { 326 throw new NamingException ("destroySubcontext() method not implemented"); 327 } 328 329 334 public void destroySubcontext(String name) throws NamingException { 335 destroySubcontext(new CompositeName (name)); 336 } 337 338 344 public Context createSubcontext(Name name) throws NamingException { 345 throw new NamingException ("createSubcontext() method not implemented"); 346 } 347 348 354 public Context createSubcontext(String name) throws NamingException { 355 return createSubcontext(new CompositeName (name)); 356 } 357 358 366 public Object lookupLink(Name name) throws NamingException { 367 return lookup(name); 368 } 369 370 378 public Object lookupLink(String name) throws NamingException { 379 return lookup(name); 380 } 381 382 389 public NameParser getNameParser(Name name) throws NamingException { 390 return NAME_PARSER; 391 } 392 393 400 public NameParser getNameParser(String name) throws NamingException { 401 return NAME_PARSER; 402 } 403 404 411 public Name composeName(Name name, Name prefix) throws NamingException { 412 Name result = (Name ) prefix.clone(); 413 return result.addAll(name); 414 } 415 416 423 public String composeName(String name, String prefix) throws NamingException { 424 return composeName(new CompositeName (name), new CompositeName (prefix)).toString(); 425 } 426 427 438 public Object addToEnvironment(String propName, Object propVal) throws NamingException { 439 return environment.put(propName, propVal); 440 } 441 442 451 public Object removeFromEnvironment(String propName) throws NamingException { 452 return environment.remove(propName); 453 } 454 455 461 public Hashtable getEnvironment() throws NamingException { 462 return (Hashtable ) environment.clone(); 463 } 464 465 470 public void close() { 471 } 472 473 477 public String getNameInNamespace() { 478 return ""; 479 } 480 481 } 482 483 486 487 class SimpleNameParser implements NameParser { 488 489 492 private static final Properties SYNTAX = new Properties (); 493 494 501 public Name parse(String name) throws NamingException { 502 return (new CompoundName (name, SYNTAX)); 503 } 504 } 505 506 509 510 class LocalEnumeration implements NamingEnumeration { 511 512 515 private Context localContext; 516 517 520 private final String [] names; 521 522 525 private int nextName; 526 527 532 public LocalEnumeration(Context ctx, String [] names) { 533 this.localContext = ctx; 534 this.names = names; 535 nextName = 0; 536 } 537 538 542 public boolean hasMore() { 543 return (nextName < names.length); 544 } 545 546 550 public Object next() throws NamingException { 551 if (!hasMore()) { 552 throw (new java.util.NoSuchElementException ()); 553 } 554 String name = names[nextName++]; 555 Name cname = (new CompositeName ()).add(name); 556 557 Object obj = localContext.lookup(cname); 558 return (new Binding (cname.toString(), obj)); 559 } 560 561 565 public boolean hasMoreElements() { 566 return hasMore(); 567 } 568 569 572 public Object nextElement() { 573 try { 574 return next(); 575 } catch (NamingException e) { 576 throw new java.util.NoSuchElementException (e.toString()); 577 } 578 } 579 580 583 public void close() { 584 } 585 586 } | Popular Tags |