1. /*
  2. * @(#)ServiceContext.java 1.31 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.spi.servicecontext;
  8. import org.omg.CORBA.SystemException;
  9. import org.omg.CORBA.INTERNAL;
  10. import org.omg.CORBA_2_3.portable.InputStream ;
  11. import org.omg.CORBA_2_3.portable.OutputStream ;
  12. import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
  13. import com.sun.corba.se.spi.orb.ORB ;
  14. import com.sun.corba.se.impl.encoding.CDRInputStream ;
  15. import com.sun.corba.se.impl.encoding.EncapsInputStream ;
  16. import com.sun.corba.se.impl.encoding.EncapsOutputStream ;
  17. import com.sun.corba.se.impl.orbutil.ORBUtility ;
  18. /** Base class for all ServiceContext classes.
  19. * There is a derived ServiceContext class for each service context that
  20. * the ORB supports. Each subclass encapsulates the representation of
  21. * the service context and provides any needed methods for manipulating
  22. * the service context. Each subclass must provide the following
  23. * members:
  24. * <p>
  25. * <ul>
  26. * </li>a public static final int SERVICE_CONTEXT_ID that gives the OMG
  27. * (or other) defined id for the service context. This is needed for the
  28. * registration mechanism defined in ServiceContexts. OMG defined
  29. * service context ids are taken from section 13.6.7 of ptc/98-12-04.</li>
  30. * <li>a public constructor that takes an InputStream as its argument.</li>
  31. * <li>Appropriate definitions of getId() and writeData(). getId() must
  32. * return SERVICE_CONTEXT_ID.</li>
  33. * </ul>
  34. * <p>
  35. * The subclass can be constructed either directly from the service context
  36. * representation, or by reading the representation from an input stream.
  37. * These cases are needed when the service context is created and written to
  38. * the request or reply, and when the service context is read from the
  39. * received request or reply.
  40. */
  41. public abstract class ServiceContext {
  42. /** Simple default constructor used when subclass is constructed
  43. * from its representation.
  44. */
  45. protected ServiceContext() { }
  46. private void dprint( String msg )
  47. {
  48. ORBUtility.dprint( this, msg ) ;
  49. }
  50. /** Stream constructor used when subclass is constructed from an
  51. * InputStream. This constructor must be called by super( stream )
  52. * in the subclass. After this constructor completes, the service
  53. * context representation can be read from in.
  54. * Note that the service context id has been consumed from the input
  55. * stream before this object is constructed.
  56. */
  57. protected ServiceContext(InputStream s, GIOPVersion gv) throws SystemException
  58. {
  59. in = s;
  60. }
  61. /** Returns Service context id. Must be overloaded in subclass.
  62. */
  63. public abstract int getId() ;
  64. /** Write the service context to an output stream. This method
  65. * must be used for writing the service context to a request or reply
  66. * header.
  67. */
  68. public void write(OutputStream s, GIOPVersion gv) throws SystemException
  69. {
  70. EncapsOutputStream os = new EncapsOutputStream( (ORB)(s.orb()), gv ) ;
  71. os.putEndian() ;
  72. writeData( os ) ;
  73. byte[] data = os.toByteArray() ;
  74. s.write_long(getId());
  75. s.write_long(data.length);
  76. s.write_octet_array(data, 0, data.length);
  77. }
  78. /** Writes the data used to represent the subclasses service context
  79. * into an encapsulation stream. Must be overloaded in subclass.
  80. */
  81. protected abstract void writeData( OutputStream os ) ;
  82. /** in is the stream containing the service context representation.
  83. * It is constructed by the stream constructor, and available for use
  84. * in the subclass stream constructor.
  85. */
  86. protected InputStream in = null ;
  87. public String toString()
  88. {
  89. return "ServiceContext[ id=" + getId() + " ]" ;
  90. }
  91. }