1. /*
  2. * @(#)ObjectKeyFactoryImpl.java 1.22 03/12/19
  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;
  8. import java.io.IOException ;
  9. import org.omg.CORBA.MARSHAL ;
  10. import org.omg.CORBA.OctetSeqHolder ;
  11. import org.omg.CORBA_2_3.portable.InputStream ;
  12. import com.sun.corba.se.spi.ior.ObjectId ;
  13. import com.sun.corba.se.spi.ior.ObjectKey ;
  14. import com.sun.corba.se.spi.ior.ObjectKeyFactory ;
  15. import com.sun.corba.se.spi.ior.ObjectKeyTemplate ;
  16. import com.sun.corba.se.spi.orb.ORB ;
  17. import com.sun.corba.se.spi.logging.CORBALogDomains ;
  18. import com.sun.corba.se.impl.orbutil.ORBConstants ;
  19. import com.sun.corba.se.impl.ior.JIDLObjectKeyTemplate ;
  20. import com.sun.corba.se.impl.ior.POAObjectKeyTemplate ;
  21. import com.sun.corba.se.impl.ior.WireObjectKeyTemplate ;
  22. import com.sun.corba.se.impl.ior.ObjectIdImpl ;
  23. import com.sun.corba.se.impl.ior.ObjectKeyImpl ;
  24. import com.sun.corba.se.impl.logging.IORSystemException ;
  25. import com.sun.corba.se.impl.encoding.EncapsInputStream ;
  26. /** Based on the magic and scid, return the appropriate
  27. * ObjectKeyTemplate. Expects to be called with a valid
  28. * magic. If scid is not valid, null should be returned.
  29. */
  30. interface Handler {
  31. ObjectKeyTemplate handle( int magic, int scid,
  32. InputStream is, OctetSeqHolder osh ) ;
  33. }
  34. /** Singleton used to manufacture ObjectKey and ObjectKeyTemplate
  35. * instances.
  36. * @author Ken Cavanaugh
  37. */
  38. public class ObjectKeyFactoryImpl implements ObjectKeyFactory
  39. {
  40. public static final int MAGIC_BASE = 0xAFABCAFE ;
  41. // Magic used in our object keys for JDK 1.2, 1.3, RMI-IIOP OP,
  42. // J2EE 1.0-1.2.1.
  43. public static final int JAVAMAGIC_OLD = MAGIC_BASE ;
  44. // Magic used only in JDK 1.3.1. No format changes in object keys.
  45. public static final int JAVAMAGIC_NEW = MAGIC_BASE + 1 ;
  46. // New magic used in our object keys for JDK 1.4, J2EE 1.3 and later.
  47. // Format changes: all object keys have version string; POA key format
  48. // is changed.
  49. public static final int JAVAMAGIC_NEWER = MAGIC_BASE + 2 ;
  50. public static final int MAX_MAGIC = JAVAMAGIC_NEWER ;
  51. // Beginning in JDK 1.3.1_01, we introduced changes which required
  52. // the ability to distinguish between JDK 1.3.1 FCS and the patch
  53. // versions. See OldJIDLObjectKeyTemplate.
  54. public static final byte JDK1_3_1_01_PATCH_LEVEL = 1;
  55. private final ORB orb ;
  56. private IORSystemException wrapper ;
  57. public ObjectKeyFactoryImpl( ORB orb )
  58. {
  59. this.orb = orb ;
  60. wrapper = IORSystemException.get( orb,
  61. CORBALogDomains.OA_IOR ) ;
  62. }
  63. // XXX The handlers still need to be made pluggable.
  64. //
  65. // I think this can be done as follows:
  66. // 1. Move the Handler interface into the SPI as ObjectKeyHandler.
  67. // 2. Add two methods to ObjectAdapterFactory:
  68. // ObjectKeyHandler getHandlerForObjectKey( ) ;
  69. // ObjectKeyHandler getHandlerForObjectKeyTemplate( ) ;
  70. // 3. Move the implementation of the fullKey handler and the
  71. // oktempOnly handler into TOAFactory and POAFactory.
  72. // 4. Move the ObjectKey impl classes into the impl/oa packages.
  73. // 5. Create an internal interface
  74. // interface HandlerFinder {
  75. // ObjectKeyHandler get( int scid ) ;
  76. // }
  77. // and modify create(InputStream,Handler,OctetSeqHolder)
  78. // to take a HandlerFinder instead of a Handler.
  79. // 6. Modify create( byte[] ) and createTemplate( InputStream )
  80. // to create an instance of HandlerFinder: something like:
  81. // new HandlerFinder() {
  82. // ObjectKeyHandler get( int scid )
  83. // {
  84. // return orb.getRequestDispatcherRegistry().
  85. // getObjectAdapterFactory( scid ).getHandlerForObjectKey() ;
  86. // }
  87. // and similarly for getHandlerForObjectKeyTemplate.
  88. /** This handler reads the full object key, both the oktemp
  89. * and the ID.
  90. */
  91. private Handler fullKey = new Handler() {
  92. public ObjectKeyTemplate handle( int magic, int scid,
  93. InputStream is, OctetSeqHolder osh ) {
  94. ObjectKeyTemplate oktemp = null ;
  95. if ((scid >= ORBConstants.FIRST_POA_SCID) &&
  96. (scid <= ORBConstants.MAX_POA_SCID)) {
  97. if (magic >= JAVAMAGIC_NEWER)
  98. oktemp = new POAObjectKeyTemplate( orb, magic, scid, is, osh ) ;
  99. else
  100. oktemp = new OldPOAObjectKeyTemplate( orb, magic, scid, is, osh ) ;
  101. } else if ((scid >= 0) && (scid < ORBConstants.FIRST_POA_SCID)) {
  102. if (magic >= JAVAMAGIC_NEWER)
  103. oktemp = new JIDLObjectKeyTemplate( orb, magic, scid, is, osh ) ;
  104. else
  105. oktemp = new OldJIDLObjectKeyTemplate( orb, magic, scid, is, osh );
  106. }
  107. return oktemp ;
  108. }
  109. } ;
  110. /** This handler reads only the oktemp.
  111. */
  112. private Handler oktempOnly = new Handler() {
  113. public ObjectKeyTemplate handle( int magic, int scid,
  114. InputStream is, OctetSeqHolder osh ) {
  115. ObjectKeyTemplate oktemp = null ;
  116. if ((scid >= ORBConstants.FIRST_POA_SCID) &&
  117. (scid <= ORBConstants.MAX_POA_SCID)) {
  118. if (magic >= JAVAMAGIC_NEWER)
  119. oktemp = new POAObjectKeyTemplate( orb, magic, scid, is ) ;
  120. else
  121. oktemp = new OldPOAObjectKeyTemplate( orb, magic, scid, is ) ;
  122. } else if ((scid >= 0) && (scid < ORBConstants.FIRST_POA_SCID)) {
  123. if (magic >= JAVAMAGIC_NEWER)
  124. oktemp = new JIDLObjectKeyTemplate( orb, magic, scid, is ) ;
  125. else
  126. oktemp = new OldJIDLObjectKeyTemplate( orb, magic, scid, is ) ;
  127. }
  128. return oktemp ;
  129. }
  130. } ;
  131. /** Returns true iff magic is in the range of valid magic numbers
  132. * for our ORB.
  133. */
  134. private boolean validMagic( int magic )
  135. {
  136. return (magic >= MAGIC_BASE) && (magic <= MAX_MAGIC) ;
  137. }
  138. /** Creates an ObjectKeyTemplate from the InputStream. Most of the
  139. * decoding is done inside the handler.
  140. */
  141. private ObjectKeyTemplate create( InputStream is, Handler handler,
  142. OctetSeqHolder osh )
  143. {
  144. ObjectKeyTemplate oktemp = null ;
  145. try {
  146. is.mark(0) ;
  147. int magic = is.read_long() ;
  148. if (validMagic( magic )) {
  149. int scid = is.read_long() ;
  150. oktemp = handler.handle( magic, scid, is, osh ) ;
  151. }
  152. } catch (MARSHAL mexc) {
  153. // XXX log this error
  154. // ignore this: error handled below because oktemp == null
  155. }
  156. if (oktemp == null)
  157. // If we did not successfully construct a oktemp, reset the
  158. // stream so that WireObjectKeyTemplate can correctly construct the
  159. // object key.
  160. try {
  161. is.reset() ;
  162. } catch (IOException exc) {
  163. // XXX log this error
  164. // ignore this
  165. }
  166. return oktemp ;
  167. }
  168. public ObjectKey create( byte[] key )
  169. {
  170. OctetSeqHolder osh = new OctetSeqHolder() ;
  171. EncapsInputStream is = new EncapsInputStream( orb, key, key.length ) ;
  172. ObjectKeyTemplate oktemp = create( is, fullKey, osh ) ;
  173. if (oktemp == null)
  174. oktemp = new WireObjectKeyTemplate( is, osh ) ;
  175. ObjectId oid = new ObjectIdImpl( osh.value ) ;
  176. return new ObjectKeyImpl( oktemp, oid ) ;
  177. }
  178. public ObjectKeyTemplate createTemplate( InputStream is )
  179. {
  180. ObjectKeyTemplate oktemp = create( is, oktempOnly, null ) ;
  181. if (oktemp == null)
  182. oktemp = new WireObjectKeyTemplate( orb ) ;
  183. return oktemp ;
  184. }
  185. }