1. /*
  2. * @(#)IIOPProfileImpl.java 1.32 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.ior.iiop;
  8. import java.util.List ;
  9. import java.util.Iterator ;
  10. import org.omg.CORBA.SystemException ;
  11. import org.omg.CORBA_2_3.portable.OutputStream ;
  12. import org.omg.CORBA_2_3.portable.InputStream ;
  13. import org.omg.IOP.TAG_ALTERNATE_IIOP_ADDRESS ;
  14. import org.omg.IOP.TAG_INTERNET_IOP;
  15. import org.omg.IOP.TAG_JAVA_CODEBASE;
  16. import com.sun.corba.se.spi.protocol.RequestDispatcherRegistry ;
  17. import com.sun.corba.se.spi.oa.ObjectAdapter ;
  18. import com.sun.corba.se.spi.oa.ObjectAdapterFactory ;
  19. import com.sun.corba.se.spi.ior.ObjectId ;
  20. import com.sun.corba.se.spi.ior.ObjectAdapterId ;
  21. import com.sun.corba.se.spi.ior.TaggedProfile ;
  22. import com.sun.corba.se.spi.ior.TaggedProfileTemplate ;
  23. import com.sun.corba.se.spi.ior.ObjectKey ;
  24. import com.sun.corba.se.spi.ior.ObjectKeyTemplate ;
  25. import com.sun.corba.se.spi.ior.TaggedComponent ;
  26. import com.sun.corba.se.spi.ior.IdentifiableBase ;
  27. import com.sun.corba.se.spi.ior.IORFactories ;
  28. import com.sun.corba.se.spi.ior.ObjectKeyFactory ;
  29. import com.sun.corba.se.spi.ior.iiop.IIOPAddress ;
  30. import com.sun.corba.se.spi.ior.iiop.IIOPProfile ;
  31. import com.sun.corba.se.spi.ior.iiop.IIOPProfileTemplate ;
  32. import com.sun.corba.se.spi.ior.iiop.IIOPFactories ;
  33. import com.sun.corba.se.spi.ior.iiop.GIOPVersion ;
  34. import com.sun.corba.se.spi.ior.iiop.JavaCodebaseComponent ;
  35. import com.sun.corba.se.spi.orb.ORB ;
  36. import com.sun.corba.se.spi.orb.ORBVersion ;
  37. import com.sun.corba.se.spi.logging.CORBALogDomains ;
  38. import com.sun.corba.se.impl.ior.EncapsulationUtility ;
  39. import com.sun.corba.se.impl.encoding.EncapsInputStream ;
  40. import com.sun.corba.se.impl.encoding.EncapsOutputStream ;
  41. import com.sun.corba.se.impl.util.JDKBridge;
  42. import com.sun.corba.se.impl.logging.IORSystemException;
  43. /**
  44. * @author
  45. */
  46. public class IIOPProfileImpl extends IdentifiableBase implements IIOPProfile
  47. {
  48. private ORB orb ;
  49. private IORSystemException wrapper ;
  50. private ObjectId oid;
  51. private IIOPProfileTemplate proftemp;
  52. private ObjectKeyTemplate oktemp ;
  53. // Cached lookups
  54. protected String codebase = null ;
  55. protected boolean cachedCodebase = false;
  56. private boolean checkedIsLocal = false ;
  57. private boolean cachedIsLocal = false ;
  58. // initialize-on-demand holder
  59. private static class LocalCodeBaseSingletonHolder {
  60. public static JavaCodebaseComponent comp ;
  61. static {
  62. String localCodebase = JDKBridge.getLocalCodebase() ;
  63. if (localCodebase == null)
  64. comp = null ;
  65. else
  66. comp = IIOPFactories.makeJavaCodebaseComponent(
  67. localCodebase ) ;
  68. }
  69. }
  70. private GIOPVersion giopVersion = null;
  71. public boolean equals( Object obj )
  72. {
  73. if (!(obj instanceof IIOPProfileImpl))
  74. return false ;
  75. IIOPProfileImpl other = (IIOPProfileImpl)obj ;
  76. return oid.equals( other.oid ) && proftemp.equals( other.proftemp ) &&
  77. oktemp.equals( other.oktemp ) ;
  78. }
  79. public int hashCode()
  80. {
  81. return oid.hashCode() ^ proftemp.hashCode() ^ oktemp.hashCode() ;
  82. }
  83. public ObjectId getObjectId()
  84. {
  85. return oid ;
  86. }
  87. public TaggedProfileTemplate getTaggedProfileTemplate()
  88. {
  89. return proftemp ;
  90. }
  91. public ObjectKeyTemplate getObjectKeyTemplate()
  92. {
  93. return oktemp ;
  94. }
  95. private IIOPProfileImpl( ORB orb )
  96. {
  97. this.orb = orb ;
  98. wrapper = IORSystemException.get( orb,
  99. CORBALogDomains.OA_IOR ) ;
  100. }
  101. public IIOPProfileImpl( ORB orb, ObjectKeyTemplate oktemp, ObjectId oid,
  102. IIOPProfileTemplate proftemp )
  103. {
  104. this( orb ) ;
  105. this.oktemp = oktemp ;
  106. this.oid = oid ;
  107. this.proftemp = proftemp ;
  108. }
  109. public IIOPProfileImpl( InputStream is )
  110. {
  111. this( (ORB)(is.orb()) ) ;
  112. init( is ) ;
  113. }
  114. public IIOPProfileImpl( ORB orb, org.omg.IOP.TaggedProfile profile)
  115. {
  116. this( orb ) ;
  117. if (profile == null || profile.tag != TAG_INTERNET_IOP.value ||
  118. profile.profile_data == null) {
  119. throw wrapper.invalidTaggedProfile() ;
  120. }
  121. EncapsInputStream istr = new EncapsInputStream((ORB)orb, profile.profile_data,
  122. profile.profile_data.length);
  123. istr.consumeEndian();
  124. init( istr ) ;
  125. }
  126. private void init( InputStream istr )
  127. {
  128. // First, read all of the IIOP IOR data
  129. GIOPVersion version = new GIOPVersion() ;
  130. version.read( istr ) ;
  131. IIOPAddress primary = new IIOPAddressImpl( istr ) ;
  132. byte[] key = EncapsulationUtility.readOctets( istr ) ;
  133. ObjectKey okey = orb.getObjectKeyFactory().create( key ) ;
  134. oktemp = okey.getTemplate() ;
  135. oid = okey.getId() ;
  136. proftemp = IIOPFactories.makeIIOPProfileTemplate( orb,
  137. version, primary ) ;
  138. // Handle any tagged components (if applicable)
  139. if (version.getMinor() > 0)
  140. EncapsulationUtility.readIdentifiableSequence( proftemp,
  141. orb.getTaggedComponentFactoryFinder(), istr ) ;
  142. // If there is no codebase in this IOR and there IS a
  143. // java.rmi.server.codebase property set, we need to
  144. // update the IOR with the local codebase. Note that
  145. // there is only one instance of the local codebase, but it
  146. // can be safely shared in multiple IORs since it is immutable.
  147. if (uncachedGetCodeBase() == null) {
  148. JavaCodebaseComponent jcc = LocalCodeBaseSingletonHolder.comp ;
  149. if (jcc != null) {
  150. if (version.getMinor() > 0)
  151. proftemp.add( jcc ) ;
  152. codebase = jcc.getURLs() ;
  153. }
  154. // Whether codebase is null or not, we have it,
  155. // and so getCodebase ned never call uncachedGetCodebase.
  156. cachedCodebase = true;
  157. }
  158. }
  159. public void writeContents(OutputStream os)
  160. {
  161. proftemp.write( oktemp, oid, os ) ;
  162. }
  163. public int getId()
  164. {
  165. return proftemp.getId() ;
  166. }
  167. public boolean isEquivalent( TaggedProfile prof )
  168. {
  169. if (!(prof instanceof IIOPProfile))
  170. return false ;
  171. IIOPProfile other = (IIOPProfile)prof ;
  172. return oid.equals( other.getObjectId() ) &&
  173. proftemp.isEquivalent( other.getTaggedProfileTemplate() ) &&
  174. oktemp.equals( other.getObjectKeyTemplate() ) ;
  175. }
  176. public ObjectKey getObjectKey()
  177. {
  178. ObjectKey result = IORFactories.makeObjectKey( oktemp, oid ) ;
  179. return result ;
  180. }
  181. public org.omg.IOP.TaggedProfile getIOPProfile()
  182. {
  183. EncapsOutputStream os = new EncapsOutputStream( orb ) ;
  184. os.write_long( getId() ) ;
  185. write( os ) ;
  186. InputStream is = (InputStream)(os.create_input_stream()) ;
  187. return org.omg.IOP.TaggedProfileHelper.read( is ) ;
  188. }
  189. private String uncachedGetCodeBase() {
  190. Iterator iter = proftemp.iteratorById( TAG_JAVA_CODEBASE.value ) ;
  191. if (iter.hasNext()) {
  192. JavaCodebaseComponent jcbc = (JavaCodebaseComponent)(iter.next()) ;
  193. return jcbc.getURLs() ;
  194. }
  195. return null ;
  196. }
  197. public synchronized String getCodebase() {
  198. if (!cachedCodebase) {
  199. cachedCodebase = true ;
  200. codebase = uncachedGetCodeBase() ;
  201. }
  202. return codebase ;
  203. }
  204. /**
  205. * @return the ORBVersion associated with the object key in the IOR.
  206. */
  207. public ORBVersion getORBVersion() {
  208. return oktemp.getORBVersion();
  209. }
  210. public synchronized boolean isLocal()
  211. {
  212. if (!checkedIsLocal) {
  213. checkedIsLocal = true ;
  214. String host = proftemp.getPrimaryAddress().getHost() ;
  215. cachedIsLocal = orb.isLocalHost(host) &&
  216. orb.isLocalServerId(oktemp.getSubcontractId(),
  217. oktemp.getServerId()) &&
  218. orb.getLegacyServerSocketManager()
  219. .legacyIsLocalServerPort(
  220. proftemp.getPrimaryAddress().getPort());
  221. }
  222. return cachedIsLocal ;
  223. }
  224. /** Return the servant for this IOR, if it is local AND if the OA that
  225. * implements this objref supports direct access to servants outside of an
  226. * invocation.
  227. * XXX revisit: do we want this at all? If we do, it might move to the
  228. * ObjectKeyTemplate instead.
  229. */
  230. public java.lang.Object getServant()
  231. {
  232. if (!isLocal())
  233. return null ;
  234. RequestDispatcherRegistry scr = orb.getRequestDispatcherRegistry() ;
  235. ObjectAdapterFactory oaf = scr.getObjectAdapterFactory(
  236. oktemp.getSubcontractId() ) ;
  237. ObjectAdapterId oaid = oktemp.getObjectAdapterId() ;
  238. ObjectAdapter oa = null ;
  239. try {
  240. oa = oaf.find( oaid ) ;
  241. } catch (SystemException exc) {
  242. // Could not find the OA, so just return null.
  243. // This usually happens when POAs are being deleted,
  244. // and the POA always return null for getLocalServant anyway.
  245. wrapper.getLocalServantFailure( exc, oaid.toString() ) ;
  246. return null ;
  247. }
  248. byte[] boid = oid.getId() ;
  249. java.lang.Object servant = oa.getLocalServant( boid ) ;
  250. return servant ;
  251. }
  252. /**
  253. * Return GIOPVersion for this IOR.
  254. * Requests created against this IOR will be of the
  255. * return Version.
  256. */
  257. public synchronized GIOPVersion getGIOPVersion()
  258. {
  259. return proftemp.getGIOPVersion() ;
  260. }
  261. public void makeImmutable()
  262. {
  263. proftemp.makeImmutable() ;
  264. }
  265. }