1 6 7 package org.jfox.jndi; 8 9 import java.io.IOException ; 10 import java.io.Serializable ; 11 import java.lang.reflect.Proxy ; 12 import java.rmi.MarshalledObject ; 13 import java.util.Hashtable ; 14 import java.util.List ; 15 import javax.naming.Binding ; 16 import javax.naming.CompoundName ; 17 import javax.naming.Context ; 18 import javax.naming.ContextNotEmptyException ; 19 import javax.naming.InitialContext ; 20 import javax.naming.InvalidNameException ; 21 import javax.naming.LinkRef ; 22 import javax.naming.Name ; 23 import javax.naming.NameAlreadyBoundException ; 24 import javax.naming.NameClassPair ; 25 import javax.naming.NameNotFoundException ; 26 import javax.naming.NameParser ; 27 import javax.naming.NamingEnumeration ; 28 import javax.naming.NamingException ; 29 import javax.naming.NotContextException ; 30 import javax.naming.OperationNotSupportedException ; 31 import javax.naming.Reference ; 32 import javax.naming.Referenceable ; 33 import javax.naming.directory.Attributes ; 34 import javax.naming.directory.DirContext ; 35 import javax.naming.directory.InvalidAttributesException ; 36 import javax.naming.spi.NamingManager ; 37 38 import org.jfox.ioc.connector.ConnectorHelper; 39 import org.jfox.ioc.connector.ConnectorRemote; 40 import org.jfox.ioc.connector.ObjectId; 41 import org.jfox.ioc.util.ObjectUUID; 42 import org.jfox.jndi.enc.EnterpriseContextFactory; 43 44 47 48 public class ContextImpl implements Context , Serializable { 49 52 private Hashtable env = new Hashtable (); 53 54 57 private Name prefix; 58 59 private NameParserImpl parser = NameParserImpl.getInstance(); 60 61 private JNDIContainer container; 63 64 public ContextImpl(Name prefix, Hashtable env) throws NamingException { 65 init(prefix, env); 66 } 67 68 public ContextImpl(String prefix, Hashtable env) throws NamingException { 69 Name _prefix = null; 70 if(prefix == null || prefix.trim().length() == 0) { 71 _prefix = parser.parse("/"); 72 } 73 else { 74 _prefix = parser.parse(prefix.trim()); 75 } 76 77 init(_prefix, env); 78 79 } 80 81 private void init(Name prefix, Hashtable env) throws NamingException { 82 if(env == null) { 83 env = JNDIProperties.getDefaultEnvironment(); 84 } 85 this.env = (Hashtable ) env.clone(); 86 87 String privoderURL = (String ) env.get(Context.PROVIDER_URL); 88 ConnectorRemote remote = ConnectorHelper.lookupConnector(privoderURL); 89 container = (JNDIContainer) Proxy.newProxyInstance(this.getClass().getClassLoader(), 90 new Class []{JNDIContainer.class}, 91 new JNDIConnectorInvoker(new ObjectId(ObjectUUID.randomUUID()), remote)); 92 93 if(prefix == null || prefix.toString().trim().length() == 0) { 94 this.prefix = parser.parse("/"); 95 } 96 else { 97 this.prefix = prefix; 98 } 99 100 if(this.prefix.toString().equals("/") && !container.isBound(prefix)) { 102 this.bind(prefix, this); 103 } 104 105 } 106 107 108 121 public Object lookup(Name name) throws NamingException { 122 if(name.isEmpty()) { return new ContextImpl(this.getNameInNamespace(), this.getEnvironment()); 124 } 125 126 if(EnterpriseContextFactory.isEnterpriseContext(name)) { 128 Context ctx = (Context ) lookup(EnterpriseContextFactory.ENC_JNDI_NAME); 129 return ctx.lookup(EnterpriseContextFactory.getEnterpriseContextSuffix(name)); 130 } 131 132 try { 133 Name absoluteName = getAbsoluteName(name); 134 Object obj = container.lookup(absoluteName); 135 Object ret = obj; 136 if(obj instanceof MarshalledObject ) { 137 ret = ((MarshalledObject ) obj).get(); 138 } 139 else if(obj instanceof Reference ) { if(obj instanceof LinkRef ) { String ref = ((LinkRef ) obj).getLinkName(); 143 return new InitialContext (env).lookup(ref); 144 } 145 else { ret = NamingManager.getObjectInstance(obj, absoluteName, this, env); 147 } 148 } 149 150 return ret; 151 } 152 catch(NamingException e) { 153 throw e; 154 } 155 catch(Exception ex) { 156 ex.printStackTrace(); 157 throw new NamingException (ex.toString()); 158 } 159 160 } 161 162 170 public Object lookup(String name) throws NamingException { 171 return lookup(parser.parse(name)); 172 } 173 174 189 public void bind(Name name, Object obj) throws NamingException { 190 if(name.isEmpty()) { 191 throw new InvalidNameException (name.toString()); 192 } 193 194 String classname = null; 195 if(obj instanceof Referenceable ) { obj = ((Referenceable ) obj).getReference(); 197 } 198 if(obj instanceof Reference ) { 199 classname = ((Reference ) obj).getClassName(); 200 } 201 else { 202 classname = obj.getClass().getName(); 203 205 try { 206 obj = new MarshalledObject (obj); 208 } 209 catch(IOException e) { 210 throw new NamingException (e.toString()); 211 } 212 213 } 215 container.bind(this, getAbsoluteName(name), obj, classname); 216 217 } 218 219 229 public void bind(String name, Object obj) throws NamingException { 230 bind(parser.parse(name), obj); 231 } 232 233 253 public void rebind(Name name, Object obj) throws NamingException { 254 if(name.isEmpty() || name.toString().trim().equals("/")) { throw new InvalidNameException (name.toString()); 256 } 257 258 String classname = null; 259 if(obj instanceof Referenceable ) { obj = ((Referenceable ) obj).getReference(); 261 } 262 if(obj instanceof Reference ) { 263 classname = ((Reference ) obj).getClassName(); 264 } 265 else { 266 classname = obj.getClass().getName(); 267 try { 268 if(!(obj instanceof MarshalledObject )) { 269 obj = new MarshalledObject (obj); 270 } 271 } 272 catch(IOException e) { 273 throw new NamingException (e.toString()); 274 } 275 } 276 277 try { 278 container.rebind(this, getAbsoluteName(name), obj, classname); 280 } 281 catch(Exception e) { 282 e.printStackTrace(); 283 throw new NamingException (e.toString()); 284 } 285 286 } 287 288 297 public void rebind(String name, Object obj) throws NamingException { 298 rebind(parser.parse(name), obj); 299 } 300 301 321 public void unbind(Name name) throws NamingException { 322 if(name.isEmpty() || name.toString().trim().equals("/")) { 323 throw new InvalidNameException (name.toString()); 324 } 325 try { 326 Name absoluteName = getAbsoluteName(name); 327 container.unbind(this, absoluteName); 328 } 329 catch(Exception ex) { 330 throw new NamingException (ex.toString()); 331 } 332 333 } 334 335 343 public void unbind(String name) throws NamingException { 344 unbind(parser.parse(name)); 345 } 346 347 362 public void rename(Name oldName, Name newName) throws NamingException { 363 if(oldName.isEmpty() || oldName.toString().trim().equals("/") || 364 newName.isEmpty() || newName.toString().trim().equals("/")) { 365 throw new InvalidNameException ("oldName: " + oldName.toString() + ", newName: " + newName); 366 } 367 try { 368 Object obj = container.lookup(oldName); 369 bind(newName, obj); 370 } 371 catch(Exception e) { 372 throw new NamingException (e.toString()); 373 } 374 375 unbind(oldName); 376 } 377 378 388 public void rename(String oldName, String newName) throws NamingException { 389 rename(parser.parse(oldName), parser.parse(newName)); 390 } 391 392 409 public NamingEnumeration list(Name name) throws NamingException { 410 try { 411 List list = container.list(this, getAbsoluteName(name)); 412 return new NamingEnumerationImpl(list); 413 } 414 catch(Exception ex) { 415 throw new NamingException (ex.toString()); 416 } 417 } 418 419 430 public NamingEnumeration list(String name) throws NamingException { 431 return list(parser.parse(name)); 432 } 433 434 451 public NamingEnumeration listBindings(Name name) throws NamingException { 452 try { 453 List list = container.listBindings(this, getAbsoluteName(name)); 454 return new NamingEnumerationImpl(list); 455 } 456 catch(Exception ex) { 457 throw new NamingException (ex.toString()); 458 } 459 460 } 461 462 473 public NamingEnumeration listBindings(String name) throws NamingException { 474 return listBindings(parser.parse(name)); 475 } 476 477 508 public void destroySubcontext(Name name) throws NamingException { 509 if(name.isEmpty() || name.toString().trim().equals("/")) { throw new InvalidNameException (name.toString()); 511 } 512 try { 513 container.destroySubcontext(this, getAbsoluteName(name)); 514 } 515 catch(Exception e) { 516 throw new NamingException (e.toString()); 517 } 518 519 } 520 521 532 public void destroySubcontext(String name) throws NamingException { 533 destroySubcontext(parser.parse(name)); 534 } 535 536 552 public Context createSubcontext(Name name) throws NamingException { 553 if(name.isEmpty() || name.toString().trim().equals("/")) { 554 throw new InvalidNameException (name.toString()); 555 } 556 try { 557 name = getAbsoluteName(name); 558 return container.createSubcontext(this, name); 559 } 560 catch(Exception ex) { 561 throw new NamingException (ex.toString()); 562 } 563 564 } 565 566 577 public Context createSubcontext(String name) throws NamingException { 578 return createSubcontext(parser.parse(name)); 579 } 580 581 593 public Object lookupLink(Name name) throws NamingException { 594 return lookup(name); 595 } 596 597 607 public Object lookupLink(String name) throws NamingException { 608 return lookupLink(parser.parse(name)); 609 } 610 611 628 public NameParser getNameParser(Name name) throws NamingException { 629 return parser; 630 } 631 632 641 public NameParser getNameParser(String name) throws NamingException { 642 return parser; 643 } 644 645 676 public Name composeName(Name name, Name prefix) throws NamingException { 677 Name newName = (Name ) (prefix.clone()); 678 newName.addAll(name); 679 return newName; 680 } 681 682 692 public String composeName(String name, String prefix) throws NamingException { 693 Name result = composeName(parser.parse(name), parser.parse(prefix)); 694 return result.toString(); 695 } 696 697 710 public Object addToEnvironment(String propName, Object propVal) 711 throws NamingException { 712 713 return env.put(propName, propVal); 714 } 715 716 728 public Object removeFromEnvironment(String propName) 729 throws NamingException { 730 return env.remove(propName); 731 } 732 733 747 public Hashtable getEnvironment() throws NamingException { 748 return JNDIProperties.cloneEnvironment(env); 749 } 750 751 762 public void close() throws NamingException { 763 try { 764 container.closeSubcontext(this, prefix); 765 } 766 catch(Exception e) { 767 throw new NamingException (e.toString()); 768 } 769 } 770 771 789 public String getNameInNamespace() throws NamingException { 790 return prefix.toString(); 791 } 792 793 private Name getAbsoluteName(Name name) throws NamingException { 794 if(name.startsWith(prefix)) return name; 796 797 if(name.toString().indexOf("://") > 0) { 799 name = name.getSuffix(3); 800 } 801 else if(name.toString().indexOf(":/") > 0) { 803 name = name.getSuffix(1); 804 } 805 806 return composeName(name, prefix); 807 } 808 809 } | Popular Tags |