1 24 25 package com.rift.coad.lib.naming.cos; 27 28 import java.util.Hashtable ; 30 import java.util.HashMap ; 31 import java.util.Map ; 32 import javax.naming.Context ; 33 import javax.naming.CompositeName ; 34 import javax.naming.Name ; 35 import javax.naming.NameParser ; 36 import javax.naming.NamingEnumeration ; 37 import javax.naming.NamingException ; 38 import javax.naming.NameAlreadyBoundException ; 39 import javax.naming.NameNotFoundException ; 40 import javax.naming.Reference ; 41 import javax.naming.OperationNotSupportedException ; 42 import javax.naming.spi.ObjectFactory ; 43 44 import org.apache.log4j.Logger; 46 47 import com.rift.coad.lib.naming.*; 49 import com.rift.coad.lib.thread.CoadunationThreadGroup; 50 51 57 public class MasterContext implements Context { 58 59 protected Logger log = 61 Logger.getLogger(MasterContext.class.getName()); 62 63 private Hashtable env = null; 65 private OrbManager orbManager = null; 66 private CosContext cosContext = null; 67 private MemoryContext masterMemoryContext = null; 68 private Map classLoaderMemoryContexts = new HashMap (); 69 70 71 81 public MasterContext(Hashtable env, CoadunationThreadGroup threadGroup, 82 OrbManager orbManager, String instanceId) throws 83 com.rift.coad.lib.naming.NamingException { 84 try { 85 this.env = (Hashtable )env.clone(); 86 this.orbManager = orbManager; 87 this.cosContext = new CosContext(env,threadGroup,orbManager, 88 instanceId); 89 this.masterMemoryContext = new MemoryContext(env,new NamingParser(). 90 parse("")); 91 } catch (javax.naming.NamingException ex) { 92 log.error("Failed to instanciate the Master Context because : " + 93 ex.getMessage(),ex); 94 throw new com.rift.coad.lib.naming.NamingException("Failed to " + 95 "instanciate the Master Context because : " + 96 ex.getMessage(),ex); 97 } 98 } 99 100 101 108 public Object addToEnvironment(String propName, Object propVal) throws 109 NamingException { 110 synchronized(env) { 111 Object origValue = null; 112 if (env.containsKey(propName)) { 113 origValue = env.get(propName); 114 } 115 env.put(propName,propVal); 116 return origValue; 117 } 118 } 119 120 121 128 public void bind(Name name, Object obj) throws NamingException { 129 Context context = getContext(name); 130 context.bind(name,obj); 131 } 132 133 134 141 public void bind(String name, Object obj) throws NamingException { 142 Context context = getContext(name); 143 context.bind(name,obj); 144 } 145 146 147 150 public void close() throws NamingException { 151 152 } 153 154 155 163 public Name composeName(Name name, Name prefix) throws NamingException { 164 Name newName = (Name )prefix.clone(); 165 newName.addAll(name); 166 return newName; 167 } 168 169 170 177 public String composeName(String name, String prefix) throws 178 NamingException { 179 return composeName(new NamingParser().parse(name),new NamingParser(). 180 parse(prefix)).toString(); 181 } 182 183 184 191 public Context createSubcontext(Name name) throws NamingException { 192 Context context = getContext(name); 193 return context.createSubcontext(name); 194 } 195 196 197 204 public Context createSubcontext(String name) throws NamingException { 205 Context context = getContext(name); 206 return context.createSubcontext(name); 207 } 208 209 210 216 public void destroySubcontext(Name name) throws NamingException { 217 Context context = getContext(name); 218 context.destroySubcontext(name); 219 } 220 221 222 227 public void destroySubcontext(String name) throws NamingException { 228 Context context = getContext(name); 229 context.destroySubcontext(name); 230 } 231 232 233 239 public Hashtable getEnvironment() throws NamingException { 240 return env; 241 } 242 243 244 247 public String getNameInNamespace() throws NamingException { 248 return new NamingParser().parse("").toString(); 249 } 250 251 252 259 public NameParser getNameParser(Name name) throws NamingException { 260 Context context = getContext(name); 261 return context.getNameParser(name); 262 } 263 264 265 268 public NameParser getNameParser(String name) throws NamingException { 269 Context context = getContext(name); 270 return context.getNameParser(name); 271 } 272 273 274 278 public NamingEnumeration list(Name name) throws NamingException { 279 Context context = getContext(name); 280 return context.list(name); 281 } 282 283 284 292 public NamingEnumeration list(String name) throws NamingException { 293 Context context = getContext(name); 294 return context.list(name); 295 } 296 297 298 306 public NamingEnumeration listBindings(Name name) throws NamingException { 307 Context context = getContext(name); 308 return context.listBindings(name); 309 } 310 311 312 320 public NamingEnumeration listBindings(String name) throws NamingException { 321 Context context = getContext(name); 322 return context.listBindings(name); 323 } 324 325 326 333 public Object lookup(Name name) throws NamingException { 334 Context context = getContext(name); 335 try { 336 return processResult(context.lookup(name),name); 337 } catch (javax.naming.NameNotFoundException ex) { 338 if (context != masterMemoryContext) { 339 return processResult(masterMemoryContext.lookup(name),name); 340 } 341 throw ex; 342 } 343 } 344 345 346 353 public Object lookup(String name) throws NamingException { 354 Context context = getContext(name); 355 try { 356 return processResult(context.lookup(name),name); 357 } catch (javax.naming.NameNotFoundException ex) { 358 if (context != masterMemoryContext) { 359 return processResult(masterMemoryContext.lookup(name),name); 360 } 361 throw ex; 362 } 363 } 364 365 366 374 public Object lookupLink(Name name) throws NamingException { 375 Context context = getContext(name); 376 try { 377 return processResult(context.lookupLink(name),name); 378 } catch (javax.naming.NameNotFoundException ex) { 379 if (context != masterMemoryContext) { 380 return processResult(masterMemoryContext.lookupLink(name),name); 381 } 382 throw ex; 383 } 384 } 385 386 387 395 public Object lookupLink(String name) throws NamingException { 396 Context context = getContext(name); 397 try { 398 return processResult(context.lookupLink(name),name); 399 } catch (javax.naming.NameNotFoundException ex) { 400 if (context != masterMemoryContext) { 401 return processResult(masterMemoryContext.lookupLink(name),name); 402 } 403 throw ex; 404 } 405 } 406 407 408 415 public void rebind(Name name, Object obj) throws NamingException { 416 Context context = getContext(name); 417 context.rebind(name,obj); 418 } 419 420 421 428 public void rebind(String name, Object obj) throws NamingException { 429 Context context = getContext(name); 430 context.rebind(name,obj); 431 } 432 433 434 440 public Object removeFromEnvironment(String propName) throws NamingException { 441 synchronized(env) { 442 Object original = null; 443 if (env.containsKey(propName)) { 444 original = env.get(propName); 445 env.remove(propName); 446 } 447 return original; 448 } 449 } 450 451 452 460 public void rename(Name oldName, Name newName) throws NamingException { 461 Context oldContext = getContext(oldName); 462 Context newContext = getContext(newName); 463 Object ref = oldContext.lookup(oldName); 464 newContext.bind(newName,ref); 465 oldContext.unbind(oldName); 466 } 467 468 469 477 public void rename(String oldName, String newName) throws NamingException { 478 Context oldContext = getContext(oldName); 479 Context newContext = getContext(newName); 480 Object ref = oldContext.lookup(oldName); 481 newContext.bind(newName,ref); 482 oldContext.unbind(oldName); 483 } 484 485 486 492 public void unbind(Name name) throws NamingException { 493 Context context = getContext(name); 494 context.unbind(name); 495 } 496 497 498 504 public void unbind(String name) throws NamingException { 505 Context context = getContext(name); 506 context.unbind(name); 507 } 508 509 510 515 public void initContext() throws com.rift.coad.lib.naming.NamingException { 516 try { 517 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 518 synchronized(classLoaderMemoryContexts) { 519 classLoaderMemoryContexts.put(loader,new MemoryContext(env, 520 new NamingParser().parse(""))); 521 } 522 } catch (Exception ex) { 523 log.error("Failed to init the context for a class loader : " + 524 ex.getMessage(),ex); 525 throw new com.rift.coad.lib.naming.NamingException( 526 "Failed to init the context for a class loader : " + 527 ex.getMessage(),ex); 528 } 529 } 530 531 532 535 public void releaseContext() { 536 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 537 synchronized(classLoaderMemoryContexts) { 538 classLoaderMemoryContexts.remove(loader); 539 } 540 } 541 542 543 547 public void terminate() { 548 cosContext.terminate(); 549 } 550 551 552 556 private Context getContext(String name) throws NamingException { 557 return getContext(new NamingParser().parse(name)); 558 } 559 560 561 565 private Context getContext(Name name) { 566 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 567 if (!name.get(0).equals(NamingConstants.JAVA_JNDI_PREFIX)) 570 { 571 return cosContext; 572 } 573 synchronized(classLoaderMemoryContexts) { 575 if (classLoaderMemoryContexts.containsKey(loader)) { 576 return (Context )classLoaderMemoryContexts.get(loader); 577 } 578 } 579 return masterMemoryContext; 581 } 582 583 584 590 private Object processResult(Object result,String name) throws NamingException { 591 return processResult(result, new NamingParser().parse(name)); 592 } 593 594 600 private Object processResult(Object result,Name name) throws NamingException { 601 try { 602 if (result instanceof Reference ) { 603 Reference ref = (Reference )result; 604 ObjectFactory objFactory = (ObjectFactory )(Class.forName( 605 ref.getFactoryClassName())).newInstance(); 606 return objFactory.getObjectInstance(ref,name,this, 607 this.getEnvironment()); 608 } 609 return result; 610 } catch (Exception ex) { 611 log.error("Failed to process the result : " + 612 ex.getMessage(),ex); 613 throw new NamingException ("Failed to process the result : " + 614 ex.getMessage()); 615 } 616 } 617 } 618 | Popular Tags |