1. /*
  2. * @(#)ServiceContext.java 1.28 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.internal.core;
  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.internal.iiop.CDRInputStream ;
  13. import com.sun.corba.se.internal.corba.EncapsInputStream ;
  14. import com.sun.corba.se.internal.corba.EncapsOutputStream ;
  15. import com.sun.corba.se.internal.orbutil.ORBUtility ;
  16. /** Base class for all ServiceContext classes.
  17. * There is a derived ServiceContext class for each service context that
  18. * the ORB supports. Each subclass encapsulates the representation of
  19. * the service context and provides any needed methods for manipulating
  20. * the service context. Each subclass must provide the following
  21. * members:
  22. * <p>
  23. * <ul>
  24. * </li>a public static final int SERVICE_CONTEXT_ID that gives the OMG
  25. * (or other) defined id for the service context. This is needed for the
  26. * registration mechanism defined in ServiceContexts. OMG defined
  27. * service context ids are taken from section 13.6.7 of ptc/98-12-04.</li>
  28. * <li>a public constructor that takes an InputStream as its argument.</li>
  29. * <li>Appropriate definitions of getId() and writeData(). getId() must
  30. * return SERVICE_CONTEXT_ID.</li>
  31. * </ul>
  32. * <p>
  33. * The subclass can be constructed either directly from the service context
  34. * representation, or by reading the representation from an input stream.
  35. * These cases are needed when the service context is created and written to
  36. * the request or reply, and when the service context is read from the
  37. * received request or reply.
  38. */
  39. public abstract class ServiceContext {
  40. /** Simple default constructor used when subclass is constructed
  41. * from its representation.
  42. */
  43. protected ServiceContext() { }
  44. private void dprint( String msg )
  45. {
  46. ORBUtility.dprint( this, msg ) ;
  47. }
  48. /** Stream constructor used when subclass is constructed from an
  49. * InputStream. This constructor must be called by super( stream )
  50. * in the subclass. After this constructor completes, the service
  51. * context representation can be read from in.
  52. * Note that the service context id has been consumed from the input
  53. * stream before this object is constructed.
  54. */
  55. protected ServiceContext(InputStream s, GIOPVersion gv) throws SystemException
  56. {
  57. com.sun.corba.se.internal.corba.ORB orb
  58. = (com.sun.corba.se.internal.corba.ORB)(((com.sun.corba.se.internal.iiop.CDRInputStream)s).orb()) ;
  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(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. }