KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > spi > servicecontext > ServiceContext


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
8 package com.sun.corba.se.spi.servicecontext;
9
10 import org.omg.CORBA.SystemException JavaDoc;
11 import org.omg.CORBA.INTERNAL JavaDoc;
12 import org.omg.CORBA_2_3.portable.InputStream JavaDoc ;
13 import org.omg.CORBA_2_3.portable.OutputStream JavaDoc ;
14 import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
15 import com.sun.corba.se.spi.orb.ORB ;
16 import com.sun.corba.se.impl.encoding.CDRInputStream ;
17 import com.sun.corba.se.impl.encoding.EncapsInputStream ;
18 import com.sun.corba.se.impl.encoding.EncapsOutputStream ;
19 import com.sun.corba.se.impl.orbutil.ORBUtility ;
20
21 /** Base class for all ServiceContext classes.
22 * There is a derived ServiceContext class for each service context that
23 * the ORB supports. Each subclass encapsulates the representation of
24 * the service context and provides any needed methods for manipulating
25 * the service context. Each subclass must provide the following
26 * members:
27 * <p>
28 * <ul>
29 * </li>a public static final int SERVICE_CONTEXT_ID that gives the OMG
30 * (or other) defined id for the service context. This is needed for the
31 * registration mechanism defined in ServiceContexts. OMG defined
32 * service context ids are taken from section 13.6.7 of ptc/98-12-04.</li>
33 * <li>a public constructor that takes an InputStream as its argument.</li>
34 * <li>Appropriate definitions of getId() and writeData(). getId() must
35 * return SERVICE_CONTEXT_ID.</li>
36 * </ul>
37 * <p>
38 * The subclass can be constructed either directly from the service context
39 * representation, or by reading the representation from an input stream.
40 * These cases are needed when the service context is created and written to
41 * the request or reply, and when the service context is read from the
42 * received request or reply.
43 */

44 public abstract class ServiceContext {
45     /** Simple default constructor used when subclass is constructed
46      * from its representation.
47      */

48     protected ServiceContext() { }
49
50     private void dprint( String JavaDoc msg )
51     {
52     ORBUtility.dprint( this, msg ) ;
53     }
54     
55     /** Stream constructor used when subclass is constructed from an
56      * InputStream. This constructor must be called by super( stream )
57      * in the subclass. After this constructor completes, the service
58      * context representation can be read from in.
59      * Note that the service context id has been consumed from the input
60      * stream before this object is constructed.
61      */

62     protected ServiceContext(InputStream JavaDoc s, GIOPVersion gv) throws SystemException JavaDoc
63     {
64         in = s;
65     }
66
67     /** Returns Service context id. Must be overloaded in subclass.
68      */

69     public abstract int getId() ;
70
71     /** Write the service context to an output stream. This method
72      * must be used for writing the service context to a request or reply
73      * header.
74      */

75     public void write(OutputStream JavaDoc s, GIOPVersion gv) throws SystemException JavaDoc
76     {
77         EncapsOutputStream os = new EncapsOutputStream( (ORB)(s.orb()), gv ) ;
78         os.putEndian() ;
79         writeData( os ) ;
80         byte[] data = os.toByteArray() ;
81
82         s.write_long(getId());
83         s.write_long(data.length);
84         s.write_octet_array(data, 0, data.length);
85     }
86
87     /** Writes the data used to represent the subclasses service context
88      * into an encapsulation stream. Must be overloaded in subclass.
89      */

90     protected abstract void writeData( OutputStream JavaDoc os ) ;
91
92     /** in is the stream containing the service context representation.
93      * It is constructed by the stream constructor, and available for use
94      * in the subclass stream constructor.
95      */

96     protected InputStream JavaDoc in = null ;
97
98     public String JavaDoc toString()
99     {
100     return "ServiceContext[ id=" + getId() + " ]" ;
101     }
102 }
103
Popular Tags