ServiceContext.java revision 608:7e06bf1dcb09
1/* 2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.corba.se.spi.servicecontext; 27 28import org.omg.CORBA.SystemException; 29import org.omg.CORBA.INTERNAL; 30import org.omg.CORBA_2_3.portable.InputStream ; 31import org.omg.CORBA_2_3.portable.OutputStream ; 32import com.sun.corba.se.spi.ior.iiop.GIOPVersion; 33import com.sun.corba.se.spi.orb.ORB ; 34import com.sun.corba.se.impl.encoding.CDRInputStream ; 35import com.sun.corba.se.impl.encoding.EncapsInputStream ; 36import com.sun.corba.se.impl.encoding.EncapsOutputStream ; 37import com.sun.corba.se.impl.orbutil.ORBUtility ; 38 39/** Base class for all ServiceContext classes. 40* There is a derived ServiceContext class for each service context that 41* the ORB supports. Each subclass encapsulates the representation of 42* the service context and provides any needed methods for manipulating 43* the service context. Each subclass must provide the following 44* members: 45* <p> 46* <ul> 47* </li>a public static final int SERVICE_CONTEXT_ID that gives the OMG 48* (or other) defined id for the service context. This is needed for the 49* registration mechanism defined in ServiceContexts. OMG defined 50* service context ids are taken from section 13.6.7 of ptc/98-12-04.</li> 51* <li>a public constructor that takes an InputStream as its argument.</li> 52* <li>Appropriate definitions of getId() and writeData(). getId() must 53* return SERVICE_CONTEXT_ID.</li> 54* </ul> 55* <p> 56* The subclass can be constructed either directly from the service context 57* representation, or by reading the representation from an input stream. 58* These cases are needed when the service context is created and written to 59* the request or reply, and when the service context is read from the 60* received request or reply. 61*/ 62public abstract class ServiceContext { 63 /** Simple default constructor used when subclass is constructed 64 * from its representation. 65 */ 66 protected ServiceContext() { } 67 68 private void dprint( String msg ) 69 { 70 ORBUtility.dprint( this, msg ) ; 71 } 72 73 /** Stream constructor used when subclass is constructed from an 74 * InputStream. This constructor must be called by super( stream ) 75 * in the subclass. After this constructor completes, the service 76 * context representation can be read from in. 77 * Note that the service context id has been consumed from the input 78 * stream before this object is constructed. 79 */ 80 protected ServiceContext(InputStream s, GIOPVersion gv) throws SystemException 81 { 82 in = s; 83 } 84 85 /** Returns Service context id. Must be overloaded in subclass. 86 */ 87 public abstract int getId() ; 88 89 /** Write the service context to an output stream. This method 90 * must be used for writing the service context to a request or reply 91 * header. 92 */ 93 public void write(OutputStream s, GIOPVersion gv) throws SystemException 94 { 95 EncapsOutputStream os = 96 sun.corba.OutputStreamFactory.newEncapsOutputStream((ORB)(s.orb()), gv); 97 os.putEndian() ; 98 writeData( os ) ; 99 byte[] data = os.toByteArray() ; 100 101 s.write_long(getId()); 102 s.write_long(data.length); 103 s.write_octet_array(data, 0, data.length); 104 } 105 106 /** Writes the data used to represent the subclasses service context 107 * into an encapsulation stream. Must be overloaded in subclass. 108 */ 109 protected abstract void writeData( OutputStream os ) ; 110 111 /** in is the stream containing the service context representation. 112 * It is constructed by the stream constructor, and available for use 113 * in the subclass stream constructor. 114 */ 115 protected InputStream in = null ; 116 117 public String toString() 118 { 119 return "ServiceContext[ id=" + getId() + " ]" ; 120 } 121} 122