1. /*
  2. * @(#)CorbaContactInfoListImpl.java 1.35 04/06/21
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.impl.transport;
  8. import java.util.ArrayList;
  9. import java.util.Iterator;
  10. import java.util.List;
  11. import com.sun.corba.se.pept.transport.ContactInfo;
  12. import com.sun.corba.se.spi.ior.IOR ;
  13. import com.sun.corba.se.spi.ior.iiop.IIOPProfile ;
  14. import com.sun.corba.se.spi.ior.iiop.IIOPProfileTemplate ;
  15. import com.sun.corba.se.spi.oa.ObjectAdapterFactory;
  16. import com.sun.corba.se.spi.orb.ORB;
  17. import com.sun.corba.se.spi.protocol.LocalClientRequestDispatcher;
  18. import com.sun.corba.se.spi.protocol.LocalClientRequestDispatcherFactory;
  19. import com.sun.corba.se.spi.transport.CorbaContactInfoList ;
  20. import com.sun.corba.se.spi.transport.SocketInfo;
  21. import com.sun.corba.se.impl.orbutil.ORBConstants;
  22. import com.sun.corba.se.impl.protocol.NotLocalLocalCRDImpl;
  23. /**
  24. * @author Harold Carr
  25. */
  26. public class CorbaContactInfoListImpl
  27. implements
  28. CorbaContactInfoList
  29. {
  30. protected ORB orb;
  31. protected LocalClientRequestDispatcher LocalClientRequestDispatcher;
  32. protected IOR targetIOR;
  33. protected IOR effectiveTargetIOR;
  34. protected List effectiveTargetIORContactInfoList;
  35. protected boolean isCachedHashValue = false;
  36. protected int cachedHashValue;
  37. protected ContactInfo primaryContactInfo;
  38. // XREVISIT - is this used?
  39. public CorbaContactInfoListImpl(ORB orb)
  40. {
  41. this.orb = orb;
  42. }
  43. public CorbaContactInfoListImpl(ORB orb, IOR targetIOR)
  44. {
  45. this(orb);
  46. setTargetIOR(targetIOR);
  47. }
  48. ////////////////////////////////////////////////////
  49. //
  50. // pept.transport.ContactInfoList
  51. //
  52. public synchronized Iterator iterator()
  53. {
  54. createContactInfoList();
  55. return new CorbaContactInfoListIteratorImpl(
  56. orb, this, primaryContactInfo,
  57. effectiveTargetIORContactInfoList);
  58. }
  59. ////////////////////////////////////////////////////
  60. //
  61. // spi.transport.CorbaContactInfoList
  62. //
  63. public synchronized void setTargetIOR(IOR targetIOR)
  64. {
  65. this.targetIOR = targetIOR;
  66. setEffectiveTargetIOR(targetIOR);
  67. this.isCachedHashValue = false; // Very important.
  68. }
  69. public synchronized IOR getTargetIOR()
  70. {
  71. return targetIOR;
  72. }
  73. public synchronized void setEffectiveTargetIOR(IOR effectiveTargetIOR)
  74. {
  75. this.effectiveTargetIOR = effectiveTargetIOR;
  76. effectiveTargetIORContactInfoList = null;
  77. if (primaryContactInfo != null &&
  78. orb.getORBData().getIIOPPrimaryToContactInfo() != null)
  79. {
  80. orb.getORBData().getIIOPPrimaryToContactInfo()
  81. .reset(primaryContactInfo);
  82. }
  83. primaryContactInfo = null;
  84. setLocalSubcontract();
  85. }
  86. public synchronized IOR getEffectiveTargetIOR()
  87. {
  88. return effectiveTargetIOR;
  89. }
  90. public synchronized LocalClientRequestDispatcher getLocalClientRequestDispatcher()
  91. {
  92. return LocalClientRequestDispatcher;
  93. }
  94. ////////////////////////////////////////////////////
  95. //
  96. // org.omg.CORBA.portable.Delegate
  97. //
  98. // REVISIT - hashCode(org.omg.CORBA.Object self)
  99. ////////////////////////////////////////////////////
  100. //
  101. // java.lang.Object
  102. //
  103. public synchronized int hashCode()
  104. {
  105. if (! isCachedHashValue) {
  106. cachedHashValue = targetIOR.stringify().hashCode();
  107. isCachedHashValue = true;
  108. }
  109. return cachedHashValue;
  110. }
  111. ////////////////////////////////////////////////////
  112. //
  113. // Implementation
  114. //
  115. protected void createContactInfoList()
  116. {
  117. if (effectiveTargetIORContactInfoList != null) {
  118. return;
  119. }
  120. effectiveTargetIORContactInfoList = new ArrayList();
  121. IIOPProfile iiopProfile = effectiveTargetIOR.getProfile();
  122. String hostname =
  123. ((IIOPProfileTemplate)iiopProfile.getTaggedProfileTemplate())
  124. .getPrimaryAddress().getHost().toLowerCase();
  125. int port =
  126. ((IIOPProfileTemplate)iiopProfile.getTaggedProfileTemplate())
  127. .getPrimaryAddress().getPort();
  128. // For use by "sticky manager" if one is registered.
  129. primaryContactInfo =
  130. createContactInfo(SocketInfo.IIOP_CLEAR_TEXT, hostname, port);
  131. if (iiopProfile.isLocal()) {
  132. // NOTE: IMPORTANT:
  133. // Only do local. The APP Server interceptors check
  134. // effectiveTarget.isLocal - which is determined via
  135. // the IOR - so if we added other addresses then
  136. // transactions and interceptors would not execute.
  137. ContactInfo contactInfo = new SharedCDRContactInfoImpl(
  138. orb, this, effectiveTargetIOR,
  139. orb.getORBData().getGIOPAddressDisposition());
  140. effectiveTargetIORContactInfoList.add(contactInfo);
  141. } else {
  142. addRemoteContactInfos(effectiveTargetIOR,
  143. effectiveTargetIORContactInfoList);
  144. }
  145. }
  146. protected void addRemoteContactInfos(
  147. IOR effectiveTargetIOR,
  148. List effectiveTargetIORContactInfoList)
  149. {
  150. ContactInfo contactInfo;
  151. List socketInfos = orb.getORBData()
  152. .getIORToSocketInfo().getSocketInfo(effectiveTargetIOR);
  153. Iterator iterator = socketInfos.iterator();
  154. while (iterator.hasNext()) {
  155. SocketInfo socketInfo = (SocketInfo) iterator.next();
  156. String type = socketInfo.getType();
  157. String host = socketInfo.getHost().toLowerCase();
  158. int port = socketInfo.getPort();
  159. contactInfo = createContactInfo(type, host, port);
  160. effectiveTargetIORContactInfoList.add(contactInfo);
  161. }
  162. }
  163. protected ContactInfo createContactInfo(String type,
  164. String hostname, int port)
  165. {
  166. return new SocketOrChannelContactInfoImpl(
  167. orb, this,
  168. // XREVISIT - See Base Line 62
  169. effectiveTargetIOR,
  170. orb.getORBData().getGIOPAddressDisposition(),
  171. type, hostname, port);
  172. }
  173. /**
  174. * setLocalSubcontract sets cached information that is set whenever
  175. * the effectiveTargetIOR changes.
  176. *
  177. * Note: this must be maintained accurately whether or not the ORB
  178. * allows local optimization, because ServantManagers in the POA
  179. * ALWAYS use local optimization ONLY (they do not have a remote case).
  180. */
  181. protected void setLocalSubcontract()
  182. {
  183. if (!effectiveTargetIOR.getProfile().isLocal()) {
  184. LocalClientRequestDispatcher = new NotLocalLocalCRDImpl();
  185. return;
  186. }
  187. // XXX Note that this always uses the first IIOP profile to get the
  188. // scid. What about multi-profile IORs? This should perhaps be
  189. // tied to the current ContactInfo in some way, together with an
  190. // implementation of ClientDelegate that generally prefers co-located
  191. // ContactInfo. This may in fact mean that we should do this at
  192. // the ContactInfo level, rather than the IOR/profile level.
  193. int scid = effectiveTargetIOR.getProfile().getObjectKeyTemplate().
  194. getSubcontractId() ;
  195. LocalClientRequestDispatcherFactory lcsf = orb.getRequestDispatcherRegistry().getLocalClientRequestDispatcherFactory( scid ) ;
  196. LocalClientRequestDispatcher = lcsf.create( scid, effectiveTargetIOR ) ;
  197. }
  198. }
  199. // End of file.