ServiceContext.java revision 672:2bb058ce572e
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* <ul> 46* <li>a public static final int SERVICE_CONTEXT_ID that gives the OMG 47* (or other) defined id for the service context. This is needed for the 48* registration mechanism defined in ServiceContexts. OMG defined 49* service context ids are taken from section 13.6.7 of ptc/98-12-04.</li> 50* <li>a public constructor that takes an InputStream as its argument.</li> 51* <li>Appropriate definitions of getId() and writeData(). getId() must 52* return SERVICE_CONTEXT_ID.</li> 53* </ul> 54* <p> 55* The subclass can be constructed either directly from the service context 56* representation, or by reading the representation from an input stream. 57* These cases are needed when the service context is created and written to 58* the request or reply, and when the service context is read from the 59* received request or reply. 60*/ 61public abstract class ServiceContext { 62 /** Simple default constructor used when subclass is constructed 63 * from its representation. 64 */ 65 protected ServiceContext() { } 66 67 private void dprint( String msg ) 68 { 69 ORBUtility.dprint( this, msg ) ; 70 } 71 72 /** Stream constructor used when subclass is constructed from an 73 * InputStream. This constructor must be called by super( stream ) 74 * in the subclass. After this constructor completes, the service 75 * context representation can be read from in. 76 * Note that the service context id has been consumed from the input 77 * stream before this object is constructed. 78 */ 79 protected ServiceContext(InputStream s, GIOPVersion gv) throws SystemException 80 { 81 in = s; 82 } 83 84 /** Returns Service context id. Must be overloaded in subclass. 85 */ 86 public abstract int getId() ; 87 88 /** Write the service context to an output stream. This method 89 * must be used for writing the service context to a request or reply 90 * header. 91 */ 92 public void write(OutputStream s, GIOPVersion gv) throws SystemException 93 { 94 EncapsOutputStream os = 95 sun.corba.OutputStreamFactory.newEncapsOutputStream((ORB)(s.orb()), gv); 96 os.putEndian() ; 97 writeData( os ) ; 98 byte[] data = os.toByteArray() ; 99 100 s.write_long(getId()); 101 s.write_long(data.length); 102 s.write_octet_array(data, 0, data.length); 103 } 104 105 /** Writes the data used to represent the subclasses service context 106 * into an encapsulation stream. Must be overloaded in subclass. 107 */ 108 protected abstract void writeData( OutputStream os ) ; 109 110 /** in is the stream containing the service context representation. 111 * It is constructed by the stream constructor, and available for use 112 * in the subclass stream constructor. 113 */ 114 protected InputStream in = null ; 115 116 public String toString() 117 { 118 return "ServiceContext[ id=" + getId() + " ]" ; 119 } 120} 121