1 23 24 package com.rift.coad.lib.naming.cos; 26 27 import java.io.Serializable ; 29 import java.rmi.Remote ; 30 import java.util.Hashtable ; 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import javax.naming.Name ; 34 import javax.naming.NameParser ; 35 import javax.naming.NamingEnumeration ; 36 import javax.naming.NamingException ; 37 import javax.naming.OperationNotSupportedException ; 38 import javax.naming.Reference ; 39 import javax.naming.Referenceable ; 40 import javax.rmi.PortableRemoteObject ; 41 import org.omg.CORBA.Any ; 42 import org.omg.CORBA.TypeCode ; 43 import org.omg.CORBA.portable.ObjectImpl ; 44 import org.omg.PortableServer.POA ; 45 import org.omg.DynamicAny.DynAny ; 46 import org.omg.DynamicAny.DynAnyFactory ; 47 import org.omg.DynamicAny.DynAnyFactoryHelper ; 48 import org.omg.CosNaming.NamingContext ; 49 import org.omg.CosNaming.NamingContextHelper ; 50 import org.omg.PortableServer.Servant ; 51 import org.omg.PortableServer.ForwardRequest ; 52 import org.omg.PortableServer.IdAssignmentPolicyValue ; 53 import org.omg.PortableServer.LifespanPolicyValue ; 54 import org.omg.PortableServer.RequestProcessingPolicyValue ; 55 import javax.rmi.CORBA.Util ; 56 57 import org.apache.log4j.Logger; 59 60 import org.objectweb.carol.jndi.wrapping.JNDIRemoteResource; 66 import org.objectweb.carol.jndi.wrapping.JNDIReferenceWrapper; 67 import org.objectweb.carol.jndi.wrapping.JNDIResourceWrapper; 68 69 import com.rift.coad.lib.thread.CoadunationThreadGroup; 71 import com.rift.coad.lib.configuration.Configuration; 72 import com.rift.coad.lib.configuration.ConfigurationFactory; 73 import com.rift.coad.lib.naming.NamingConstants; 74 import com.rift.coad.lib.naming.OrbManager; 75 import com.rift.coad.lib.thread.BasicThread; 76 import com.rift.coad.lib.thread.ThreadStateMonitor; 77 import com.rift.coad.lib.naming.NamingDirector; 78 79 84 public class CosContext implements Context { 85 86 90 public class InstanceBinder extends BasicThread { 91 92 private ThreadStateMonitor stateMonitor = new 94 ThreadStateMonitor(60 * 1000); 95 96 101 public InstanceBinder() throws Exception { 102 103 } 104 105 106 110 public void process() { 111 while(!stateMonitor.isTerminated()) { 112 try { 113 log.info("Making a connection to the name server context"); 114 NamingContext namingContext = NamingContextHelper.narrow( 115 orbManager.getORB().string_to_object(instanceCosURL)); 116 log.info("Binding to the primary context [" + masterCosURL 117 + "]"); 118 Context context = getInitialContext(instanceURL); 119 120 log.info("Loop through the instance url and add [" + 121 instanceURL.toString() + "]"); 122 for (int index = 0; index < instanceURL.size() - 1; index++){ 123 try { 124 context = (Context )context.lookup( 125 instanceURL.get(index)); 126 } catch (javax.naming.NameNotFoundException ex) { 127 context = context.createSubcontext( 128 instanceURL.get(index)); 129 } 130 } 131 log.info("Add entry to the context"); 132 context.rebind(instanceURL.get(instanceURL.size() -1), 133 namingContext); 134 log.info("Registered with master Cos Naming service"); 135 break; 136 } catch (Exception ex) { 137 log.error("Failed to bind to the mater cos name server : " + 138 ex.getMessage(),ex); 139 } 140 stateMonitor.monitor(); 141 } 142 } 143 144 145 148 public void terminate() { 149 stateMonitor.terminate(true); 150 } 151 } 152 153 154 protected Logger log = 156 Logger.getLogger(CosContext.class.getName()); 157 158 private final static String SUN_COS_CONTEXT_FACTORY = 160 "com.sun.jndi.cosnaming.CNCtxFactory"; 161 private final static String PRIMARY = "primary"; 162 private final static String INSTANCE_COS_URL = "instance_cos_url"; 163 private final static String MASTER_COS_URL = "master_cos_url"; 164 private final static String USER = "cos_user"; 165 166 private Hashtable env = null; 168 private CoadunationThreadGroup threadGroup = null; 169 private OrbManager orbManager = null; 170 private Name base = null; 171 private Name instanceURL = null; 172 private String instanceCosURL = null; 173 private Context instanceContext = null; 174 private boolean primary = false; 175 private String masterCosURL = null; 176 private Context masterContext = null; 177 private InstanceBinder instanceBinder = null; 178 179 180 188 public CosContext(Hashtable env, CoadunationThreadGroup threadGroup, 189 OrbManager orbManager,String instanceId) throws NamingException { 190 try { 191 this.env = env; 192 this.threadGroup = threadGroup.createThreadGroup(); 193 this.orbManager = orbManager; 194 Configuration config = ConfigurationFactory.getInstance(). 195 getConfig(this.getClass()); 196 197 instanceCosURL = config.getString(INSTANCE_COS_URL); 198 if (config.getBoolean(PRIMARY)) { 199 instanceURL = new NamingParser().parse(config.getString( 200 NamingDirector.PRIMARY_URL)); 201 base = instanceURL; 202 primary = true; 203 log.info("Running as primary"); 204 } else { 205 instanceURL = new NamingParser().parse(config.getString( 206 NamingDirector.PRIMARY_URL)) 207 .add(NamingConstants.SUBCONTEXT) 208 .add(instanceId); 209 210 base = new NamingParser().parse(""); 211 masterCosURL = config.getString(MASTER_COS_URL); 212 213 instanceBinder = new InstanceBinder(); 215 threadGroup.addThread(instanceBinder,config.getString(USER)); 216 instanceBinder.start(); 217 log.info("Running as secondary"); 218 219 } 220 221 } catch (Exception ex) { 222 log.error("Failed to create a new URL context : " + 223 ex.getMessage()); 224 throw new NamingException ("Failed to create a new URL context : " + 225 ex.getMessage()); 226 } 227 } 228 229 236 public Object addToEnvironment(String propName, Object propVal) throws 237 NamingException { 238 return null; 239 } 240 241 242 249 public void bind(Name name, Object obj) throws NamingException { 250 if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) { 251 throw new NamingException ( 252 "Not allowed to bind to the base context. " + 253 "Must use relative binding"); 254 } 255 Context context = getInitialContext(name); 256 try { 257 Name composedName = composeName(name, base); 258 for (int index = 0; index < (composedName.size() -1); index++) { 259 try { 260 context = (Context )context.lookup(composedName.get(index)); 261 } catch (javax.naming.NameNotFoundException ex) { 262 context = context.createSubcontext(composedName.get(index)); 263 } 264 } 265 log.info("Binding object [" + composedName.toString() + "] [" + 266 name.get(0) + "][" + composedName.get( 267 composedName.size() -1) + "] object [" + 268 obj.getClass().getName() + "]"); 269 context.bind(composedName.get(composedName.size() -1), 270 wrapBindingValue(obj)); 271 } catch (NamingException ex) { 272 resetContext(context); 273 log.error("Failed to bind the object to the context :" + 274 ex.getMessage(),ex); 275 throw ex; 276 } catch (Exception ex) { 277 resetContext(context); 278 log.error("Failed to bind the object to the context :" + 279 ex.getMessage(),ex); 280 throw new NamingException ( 281 "Failed to bind the object to the context :" + 282 ex.getMessage()); 283 } 284 } 285 286 287 290 public void bind(String name, Object obj) throws NamingException { 291 bind(new NamingParser().parse(name),obj); 292 } 293 294 295 298 public void close() throws NamingException { 299 if (instanceContext != null) { 300 instanceContext.close(); 301 } 302 if (masterContext != null) { 303 masterContext.close(); 304 } 305 } 306 307 308 316 public Name composeName(Name name, Name prefix) throws NamingException { 317 Name tmp = (Name )prefix.clone(); 318 tmp.addAll(name); 319 return tmp; 320 } 321 322 323 330 public String composeName(String name, String prefix) throws 331 NamingException { 332 return composeName(new NamingParser().parse(name), 333 new NamingParser().parse(prefix)).toString(); 334 } 335 336 337 344 public Context createSubcontext(Name name) throws NamingException { 345 if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) { 346 throw new NamingException ( 347 "Must use relative bindings."); 348 } 349 Context context = getInitialContext(name); 350 try { 351 Name composedName = composeName(name, base); 352 for (int index = 0; index < (composedName.size()); index++) { 353 try { 354 context = (Context )context.lookup(composedName.get(index)); 355 } catch (javax.naming.NameNotFoundException ex) { 356 context = context.createSubcontext(composedName.get(index)); 357 } 358 } 359 return context; 360 } catch (NamingException ex) { 361 resetContext(context); 362 log.error("Failed to create the sub context :" + 363 ex.getMessage(),ex); 364 throw ex; 365 } catch (Exception ex) { 366 resetContext(context); 367 log.error("Failed to create the sub context :" + 368 ex.getMessage(),ex); 369 throw new NamingException ( 370 "Failed to create the sub context :" + 371 ex.getMessage()); 372 } 373 } 374 375 376 383 public Context createSubcontext(String name) throws NamingException { 384 return createSubcontext(new NamingParser().parse(name)); 385 } 386 387 388 394 public void destroySubcontext(Name name) throws NamingException { 395 if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) { 396 throw new NamingException ( 397 "Must use relative bindings."); 398 } 399 Context context = getInitialContext(name); 400 try { 401 Name composedName = composeName(name, base); 402 context.destroySubcontext(composedName); 403 } catch (NamingException ex) { 404 resetContext(context); 405 log.error("Failed to destroy the sub context :" + 406 ex.getMessage(),ex); 407 throw ex; 408 } catch (Exception ex) { 409 resetContext(context); 410 log.error("Failed to destroy the sub context :" + 411 ex.getMessage(),ex); 412 throw new NamingException ( 413 "Failed to destroy the sub context :" + 414 ex.getMessage()); 415 } 416 } 417 418 419 424 public void destroySubcontext(String name) throws NamingException { 425 destroySubcontext(new NamingParser().parse(name)); 426 } 427 428 429 435 public Hashtable getEnvironment() throws NamingException { 436 return null; 437 } 438 439 440 443 public String getNameInNamespace() throws NamingException { 444 return this.instanceURL.toString(); 445 } 446 447 448 455 public NameParser getNameParser(Name name) throws NamingException { 456 return new NamingParser(); 457 } 458 459 460 463 public NameParser getNameParser(String name) throws NamingException { 464 return new NamingParser(); 465 } 466 467 468 472 public NamingEnumeration list(Name name) throws NamingException { 473 throw new OperationNotSupportedException ( 474 "This operation is currently not supported"); 475 } 476 477 478 486 public NamingEnumeration list(String name) throws NamingException { 487 throw new OperationNotSupportedException ( 488 "This operation is currently not supported"); 489 } 490 491 492 500 public NamingEnumeration listBindings(Name name) throws NamingException { 501 throw new OperationNotSupportedException ( 502 "This operation is currently not supported"); 503 } 504 505 506 514 public NamingEnumeration listBindings(String name) throws NamingException { 515 throw new OperationNotSupportedException ( 516 "This operation is currently not supported"); 517 } 518 519 520 527 public Object lookup(Name name) throws NamingException { 528 Context context = getInitialContext(name); 529 String currentName = null; 530 try { 531 Name composedName = null; 532 if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) { 533 composedName = name; 534 } else { 535 composedName = composeName(name, base); 536 } 537 for (int index =0; index < (composedName.size() - 1); index++ ) { 538 currentName = composedName.get(index); 539 context = (Context )context.lookup(currentName); 540 } 541 currentName = composedName.get(composedName.size() - 1); 542 return unwrapBindingValue( 543 context.lookup(currentName)); 544 } catch (NamingException ex) { 545 resetContext(context); 546 log.error("Failed to lookup the object [" + currentName + "] :" + 547 ex.getMessage(),ex); 548 throw ex; 549 } catch (Exception ex) { 550 resetContext(context); 551 log.error("Failed to lookup the object [" + currentName + "] :" + 552 ex.getMessage(),ex); 553 throw new NamingException ( 554 "Failed to lookup the object [" + currentName + "] :" + 555 ex.getMessage()); 556 } 557 } 558 559 560 567 public Object lookup(String name) throws NamingException { 568 return lookup(new NamingParser().parse(name)); 569 } 570 571 572 580 public Object lookupLink(Name name) throws NamingException { 581 throw new OperationNotSupportedException ( 582 "This method is not currently supported"); 583 } 584 585 586 594 public Object lookupLink(String name) throws NamingException { 595 return lookupLink(new NamingParser().parse(name)); 596 } 597 598 599 606 public void rebind(Name name, Object obj) throws NamingException { 607 if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) { 608 throw new NamingException ( 609 "Not allowed to bind to the base context. " + 610 "Must use relative binding"); 611 } 612 Context context = getInitialContext(name); 613 try { 614 Name composedName = composeName(name, base); 615 for (int index = 0; index < (composedName.size() -1); index++) { 616 try { 617 context = (Context )context.lookup(composedName.get(index)); 618 } catch (javax.naming.NameNotFoundException ex) { 619 context = context.createSubcontext(composedName.get(index)); 620 } 621 } 622 log.info("Rebinding object [" + composedName.toString() + "] [" + 623 name.get(0) + "][" + composedName.get( 624 composedName.size() -1) + "] object [" + 625 obj.getClass().getName() + "]"); 626 context.rebind(composedName.get(composedName.size() -1), 627 wrapBindingValue(obj)); 628 } catch (NamingException ex) { 629 resetContext(context); 630 log.error("Failed to rebind the object to the context :" + 631 ex.getMessage(),ex); 632 throw ex; 633 } catch (Exception ex) { 634 resetContext(context); 635 log.error("Failed to rebind the object to the context :" + 636 ex.getMessage(),ex); 637 throw new NamingException ( 638 "Failed to rebind the object to the context :" + 639 ex.getMessage()); 640 } 641 } 642 643 644 651 public void rebind(String name, Object obj) throws NamingException { 652 rebind(new NamingParser().parse(name),obj); 653 } 654 655 656 662 public Object removeFromEnvironment(String propName) throws NamingException { 663 return null; 664 } 665 666 667 675 public void rename(Name oldName, Name newName) throws NamingException { 676 if (oldName.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX) || 677 newName.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) { 678 throw new NamingException ( 679 "Must use relative binding"); 680 } 681 Context context = getInitialContext(oldName); 682 try { 683 Object value = lookup(oldName); 684 bind(newName,value); 685 unbind(oldName); 686 } catch (NamingException ex) { 687 resetContext(context); 688 log.error("Failed to rename the object in the context :" + 689 ex.getMessage(),ex); 690 throw ex; 691 } catch (Exception ex) { 692 resetContext(context); 693 log.error("Failed to rename the object in the context :" + 694 ex.getMessage(),ex); 695 throw new NamingException ( 696 "Failed to rename the object in the context :" + 697 ex.getMessage()); 698 } 699 } 700 701 702 710 public void rename(String oldName, String newName) throws NamingException { 711 rename(new NamingParser().parse(oldName), 712 new NamingParser().parse(newName)); 713 } 714 715 716 722 public void unbind(Name name) throws NamingException { 723 if (name.get(0).equals(NamingConstants.JNDI_NETWORK_PREFIX)) { 724 throw new NamingException ( 725 "Not allowed to use the base context. " + 726 "Must use relative binding"); 727 } 728 Context context = getInitialContext(name); 729 try { 730 Name composedName = composeName(name, base); 731 context.unbind(composedName); 732 } catch (NamingException ex) { 733 resetContext(context); 734 log.error("Failed to unbind the object to the context :" + 735 ex.getMessage(),ex); 736 throw ex; 737 } catch (Exception ex) { 738 resetContext(context); 739 log.error("Failed to unbind the object to the context :" + 740 ex.getMessage(),ex); 741 throw new NamingException ( 742 "Failed to unbind the object to the context :" + 743 ex.getMessage()); 744 } 745 } 746 747 748 754 public void unbind(String name) throws NamingException { 755 unbind(new NamingParser().parse(name)); 756 } 757 758 759 763 public void terminate() { 764 threadGroup.terminate(); 765 try { 766 close(); 767 } catch (Exception ex) { 768 log.error("Failed to close the contexts : " + ex.getMessage(),ex); 769 } 770 if (instanceBinder != null) { 771 instanceBinder.terminate(); 772 } 773 } 774 775 776 783 private Context getInitialContext(Name name) throws NamingException { 784 try { 785 if (primary || !(name.get(0).equals(this.instanceURL.get(0)))) { 786 synchronized(this) { 787 if (instanceContext != null) { 788 return instanceContext; 789 } 790 Hashtable env = new Hashtable (); 791 env.put(Context.INITIAL_CONTEXT_FACTORY, 792 SUN_COS_CONTEXT_FACTORY); 793 env.put(Context.PROVIDER_URL, 794 instanceCosURL); 795 env.put("java.naming.corba.orb",orbManager.getORB()); 796 return instanceContext = new InitialContext (env); 797 } 798 } 799 synchronized(this) { 800 if (masterContext != null) { 801 return masterContext; 802 } 803 Hashtable env = new Hashtable (); 804 env.put(Context.INITIAL_CONTEXT_FACTORY, 805 SUN_COS_CONTEXT_FACTORY); 806 env.put(Context.PROVIDER_URL, 807 masterCosURL); 808 env.put("java.naming.corba.orb",orbManager.getORB()); 809 log.info("Retrieve the master context url"); 810 return masterContext = new InitialContext (env); 811 } 812 } catch (Exception ex) { 813 log.error("Failed to retrieve an initial context for the name [" + 814 name.toString() + "] because : " + ex.getMessage(),ex); 815 throw new NamingException ("Failed to retrieve an initial context " + 816 "for the name [" + name.toString() + "] because : " + 817 ex.getMessage()); 818 } 819 } 820 821 822 827 private void resetContext(Context context) { 828 if (context == instanceContext) { 829 instanceContext = null; 830 } 831 if (context == masterContext) { 832 masterContext = null; 833 } 834 } 835 836 837 845 public Object wrapBindingValue(Object value) throws NamingException { 846 if (value instanceof Remote ) { 847 return value; 848 } else if (!(value instanceof Serializable )) { 849 throw new NamingException ("binding value is not instance of " + 850 "Remote or Serializable cannot be stored in Cos Naming"); 851 } 852 JNDIResourceWrapper resourceWrapper = null; 853 if (value instanceof Referenceable ) { 854 resourceWrapper = new JNDIResourceWrapper((Serializable )(( 855 Referenceable )value). 856 getReference()); 857 } else { 858 resourceWrapper = new JNDIResourceWrapper((Serializable )value); 859 } 860 try { 861 PortableRemoteObject.exportObject(resourceWrapper); 862 } catch (Exception ex) { 863 log.error("Failed to export wrapper :" + ex.getMessage(),ex); 864 throw new NamingException ("Failed to export wrapper :" + 865 ex.getMessage()); 866 } 867 return resourceWrapper; 868 } 869 870 871 880 public Object unwrapBindingValue(Object value) throws 881 NamingException { 882 try { 883 ObjectImpl objectImpl = (ObjectImpl )PortableRemoteObject.narrow( 884 value,ObjectImpl .class); 885 String [] ids = objectImpl._ids(); 886 String itf = ids[0]; 887 888 if (itf.indexOf(":org.objectweb.carol.jndi.wrapping." + 889 "JNDIRemoteResource:") != -1) { 890 JNDIRemoteResource jndiRemoteResource = (JNDIRemoteResource) 891 PortableRemoteObject.narrow(value,JNDIRemoteResource.class); 892 return jndiRemoteResource.getResource(); 893 } 894 895 return value; 896 } catch (Exception ex) { 897 log.error("Failed to wrapp the object :" + ex.getMessage(),ex); 898 throw new NamingException ("Failed to wrapp the object :" + 899 ex.getMessage()); 900 } 901 } 902 } 903 | Popular Tags |