corba/src/share/classes/com/sun/corba/se/spi/servicecontext/ServiceContexts.java
author ohair
Tue, 25 May 2010 15:52:11 -0700
changeset 5555 b2b5ed3f0d0d
parent 4 02bb8761fcce
child 22362 1eb21f1e04c1
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     2
 * Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
package com.sun.corba.se.spi.servicecontext;
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.InvocationTargetException ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.Modifier ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Field ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.Constructor ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
import java.util.*;
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
import org.omg.CORBA.OctetSeqHelper;
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
import org.omg.CORBA.SystemException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
import org.omg.CORBA.INTERNAL;
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
import org.omg.CORBA.CompletionStatus;
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
import org.omg.CORBA_2_3.portable.OutputStream ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
import org.omg.CORBA_2_3.portable.InputStream ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
import com.sun.org.omg.SendingContext.CodeBase;
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
import com.sun.corba.se.spi.orb.ORB ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
import com.sun.corba.se.spi.logging.CORBALogDomains;
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
import com.sun.corba.se.spi.servicecontext.ServiceContext ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
import com.sun.corba.se.spi.servicecontext.ServiceContextRegistry ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
import com.sun.corba.se.spi.servicecontext.ServiceContextData ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
import com.sun.corba.se.spi.servicecontext.UnknownServiceContext ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
import com.sun.corba.se.impl.encoding.CDRInputStream;
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
import com.sun.corba.se.impl.encoding.EncapsInputStream ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
import com.sun.corba.se.impl.orbutil.ORBUtility ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
import com.sun.corba.se.impl.util.Utility ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
public class ServiceContexts {
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
    private static boolean isDebugging( OutputStream os )
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
        ORB orb = (ORB)(os.orb()) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
        if (orb==null)
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
            return false ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
        return orb.serviceContextDebugFlag ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
    private static boolean isDebugging( InputStream is )
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
        ORB orb = (ORB)(is.orb()) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
        if (orb==null)
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
            return false ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
        return orb.serviceContextDebugFlag ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
    private void dprint( String msg )
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
        ORBUtility.dprint( this, msg ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
    public static void writeNullServiceContext( OutputStream os )
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
        if (isDebugging(os))
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
            ORBUtility.dprint( "ServiceContexts", "Writing null service context" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
        os.write_long( 0 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
     * Given the input stream, this fills our service
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
     * context map.  See the definition of scMap for
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
     * details.  Creates a HashMap.
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
     * Note that we don't actually unmarshal the
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
     * bytes of the service contexts here.  That is
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
     * done when they are actually requested via
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
     * get(int).
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
    private void createMapFromInputStream(InputStream is)
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
        orb = (ORB)(is.orb()) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
        if (orb.serviceContextDebugFlag)
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
            dprint( "Constructing ServiceContexts from input stream" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
        int numValid = is.read_long() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
        if (orb.serviceContextDebugFlag)
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
            dprint("Number of service contexts = " + numValid);
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
        for (int ctr = 0; ctr < numValid; ctr++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
            int scId = is.read_long();
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
            if (orb.serviceContextDebugFlag)
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
                dprint("Reading service context id " + scId);
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
            byte[] data = OctetSeqHelper.read(is);
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
            if (orb.serviceContextDebugFlag)
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
                dprint("Service context" + scId + " length: " + data.length);
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
            scMap.put(new Integer(scId), data);
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
    public ServiceContexts( ORB orb )
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
        this.orb = orb ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
        wrapper = ORBUtilSystemException.get( orb,
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
            CORBALogDomains.RPC_PROTOCOL ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
        addAlignmentOnWrite = false ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
        scMap = new HashMap();
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
        // Use the GIOP version of the ORB.  Should
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
        // be specified in ServiceContext.
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
        // See REVISIT below concerning giopVersion.
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
        giopVersion = orb.getORBData().getGIOPVersion();
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
        codeBase = null ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
     * Read the Service contexts from the input stream.
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
    public ServiceContexts(InputStream s)
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
        this( (ORB)(s.orb()) ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
        // We need to store this so that we can have access
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
        // to the CodeBase for unmarshaling possible
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
        // RMI-IIOP valuetype data within an encapsulation.
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
        // (Known case: UnknownExceptionInfo)
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
        codeBase = ((CDRInputStream)s).getCodeBase();
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
        createMapFromInputStream(s);
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
        // Fix for bug 4904723
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
        giopVersion = ((CDRInputStream)s).getGIOPVersion();
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
     * Find the ServiceContextData for a given scId and unmarshal
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
     * the bytes.
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
    private ServiceContext unmarshal(Integer scId, byte[] data) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
        ServiceContextRegistry scr = orb.getServiceContextRegistry();
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
        ServiceContextData scd = scr.findServiceContextData(scId.intValue());
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
        ServiceContext sc = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
        if (scd == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
            if (orb.serviceContextDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
                dprint("Could not find ServiceContextData for "
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
                       + scId
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
                       + " using UnknownServiceContext");
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
            sc = new UnknownServiceContext(scId.intValue(), data);
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
            if (orb.serviceContextDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
                dprint("Found " + scd);
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
            // REVISIT.  GIOP version should be specified as
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
            // part of a service context's definition, so should
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
            // be accessible from ServiceContextData via
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
            // its ServiceContext implementation class.
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
            //
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
            // Since we don't have that, yet, I'm using the GIOP
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
            // version of the input stream, presuming that someone
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
            // can't send a service context of a later GIOP
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
            // version than its stream version.
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
            //
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
            // Note:  As of Jan 2001, no standard OMG or Sun service contexts
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
            // ship wchar data or are defined as using anything but GIOP 1.0 CDR.
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
            EncapsInputStream eis
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
                = new EncapsInputStream(orb,
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
                                        data,
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
                                        data.length,
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
                                        giopVersion,
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
                                        codeBase);
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
            eis.consumeEndian();
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
            // Now the input stream passed to a ServiceContext
02bb8761fcce Initial load
duke
parents:
diff changeset
   209
            // constructor is already the encapsulation input
02bb8761fcce Initial load
duke
parents:
diff changeset
   210
            // stream with the endianness read off, so the
02bb8761fcce Initial load
duke
parents:
diff changeset
   211
            // service context should just unmarshal its own
02bb8761fcce Initial load
duke
parents:
diff changeset
   212
            // data.
02bb8761fcce Initial load
duke
parents:
diff changeset
   213
            sc = scd.makeServiceContext(eis, giopVersion);
02bb8761fcce Initial load
duke
parents:
diff changeset
   214
            if (sc == null)
02bb8761fcce Initial load
duke
parents:
diff changeset
   215
                throw wrapper.svcctxUnmarshalError(
02bb8761fcce Initial load
duke
parents:
diff changeset
   216
                    CompletionStatus.COMPLETED_MAYBE);
02bb8761fcce Initial load
duke
parents:
diff changeset
   217
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   218
02bb8761fcce Initial load
duke
parents:
diff changeset
   219
        return sc;
02bb8761fcce Initial load
duke
parents:
diff changeset
   220
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   221
02bb8761fcce Initial load
duke
parents:
diff changeset
   222
    public void addAlignmentPadding()
02bb8761fcce Initial load
duke
parents:
diff changeset
   223
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   224
        // Make service context 12 bytes longer by adding
02bb8761fcce Initial load
duke
parents:
diff changeset
   225
        // JAVAIDL_ALIGN_SERVICE_ID service context at end.
02bb8761fcce Initial load
duke
parents:
diff changeset
   226
        // The exact length
02bb8761fcce Initial load
duke
parents:
diff changeset
   227
        // must be >8 (minimum service context size) and
02bb8761fcce Initial load
duke
parents:
diff changeset
   228
        // =4 mod 8, so 12 is the minimum.
02bb8761fcce Initial load
duke
parents:
diff changeset
   229
        addAlignmentOnWrite = true ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   230
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   231
02bb8761fcce Initial load
duke
parents:
diff changeset
   232
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   233
     * Hopefully unused scid:  This should be changed to a proper
02bb8761fcce Initial load
duke
parents:
diff changeset
   234
     * VMCID aligned value.  REVISIT!
02bb8761fcce Initial load
duke
parents:
diff changeset
   235
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   236
    private static final int JAVAIDL_ALIGN_SERVICE_ID = 0xbe1345cd ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   237
02bb8761fcce Initial load
duke
parents:
diff changeset
   238
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   239
     * Write the service contexts to the output stream.
02bb8761fcce Initial load
duke
parents:
diff changeset
   240
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   241
     * If they haven't been unmarshaled, we don't have to
02bb8761fcce Initial load
duke
parents:
diff changeset
   242
     * unmarshal them.
02bb8761fcce Initial load
duke
parents:
diff changeset
   243
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   244
    public void write(OutputStream os, GIOPVersion gv)
02bb8761fcce Initial load
duke
parents:
diff changeset
   245
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   246
        if (isDebugging(os)) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   247
            dprint( "Writing service contexts to output stream" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   248
            Utility.printStackTrace() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   249
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   250
02bb8761fcce Initial load
duke
parents:
diff changeset
   251
        int numsc = scMap.size();
02bb8761fcce Initial load
duke
parents:
diff changeset
   252
02bb8761fcce Initial load
duke
parents:
diff changeset
   253
        if (addAlignmentOnWrite) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   254
            if (isDebugging(os))
02bb8761fcce Initial load
duke
parents:
diff changeset
   255
                dprint( "Adding alignment padding" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   256
02bb8761fcce Initial load
duke
parents:
diff changeset
   257
            numsc++ ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   258
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   259
02bb8761fcce Initial load
duke
parents:
diff changeset
   260
        if (isDebugging(os))
02bb8761fcce Initial load
duke
parents:
diff changeset
   261
            dprint( "Service context has " + numsc + " components"  ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   262
02bb8761fcce Initial load
duke
parents:
diff changeset
   263
        os.write_long( numsc ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   264
02bb8761fcce Initial load
duke
parents:
diff changeset
   265
        writeServiceContextsInOrder(os, gv);
02bb8761fcce Initial load
duke
parents:
diff changeset
   266
02bb8761fcce Initial load
duke
parents:
diff changeset
   267
        if (addAlignmentOnWrite) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   268
            if (isDebugging(os))
02bb8761fcce Initial load
duke
parents:
diff changeset
   269
                dprint( "Writing alignment padding" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   270
02bb8761fcce Initial load
duke
parents:
diff changeset
   271
            os.write_long( JAVAIDL_ALIGN_SERVICE_ID ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   272
            os.write_long( 4 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   273
            os.write_octet( (byte)0 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   274
            os.write_octet( (byte)0 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   275
            os.write_octet( (byte)0 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   276
            os.write_octet( (byte)0 ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   277
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   278
02bb8761fcce Initial load
duke
parents:
diff changeset
   279
        if (isDebugging(os))
02bb8761fcce Initial load
duke
parents:
diff changeset
   280
            dprint( "Service context writing complete" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   281
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   282
02bb8761fcce Initial load
duke
parents:
diff changeset
   283
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   284
     * Write the service contexts in scMap in a desired order.
02bb8761fcce Initial load
duke
parents:
diff changeset
   285
     * Right now, the only special case we have is UnknownExceptionInfo,
02bb8761fcce Initial load
duke
parents:
diff changeset
   286
     * so I'm merely writing it last if present.
02bb8761fcce Initial load
duke
parents:
diff changeset
   287
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   288
    private void writeServiceContextsInOrder(OutputStream os, GIOPVersion gv) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   289
02bb8761fcce Initial load
duke
parents:
diff changeset
   290
        // Temporarily remove this rather than check it per iteration
02bb8761fcce Initial load
duke
parents:
diff changeset
   291
        Integer ueInfoId
02bb8761fcce Initial load
duke
parents:
diff changeset
   292
            = new Integer(UEInfoServiceContext.SERVICE_CONTEXT_ID);
02bb8761fcce Initial load
duke
parents:
diff changeset
   293
02bb8761fcce Initial load
duke
parents:
diff changeset
   294
        Object unknownExceptionInfo = scMap.remove(ueInfoId);
02bb8761fcce Initial load
duke
parents:
diff changeset
   295
02bb8761fcce Initial load
duke
parents:
diff changeset
   296
        Iterator iter = scMap.keySet().iterator();
02bb8761fcce Initial load
duke
parents:
diff changeset
   297
02bb8761fcce Initial load
duke
parents:
diff changeset
   298
        while (iter.hasNext()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   299
            Integer id = (Integer)iter.next();
02bb8761fcce Initial load
duke
parents:
diff changeset
   300
02bb8761fcce Initial load
duke
parents:
diff changeset
   301
            writeMapEntry(os, id, scMap.get(id), gv);
02bb8761fcce Initial load
duke
parents:
diff changeset
   302
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   303
02bb8761fcce Initial load
duke
parents:
diff changeset
   304
        // Write the UnknownExceptionInfo service context last
02bb8761fcce Initial load
duke
parents:
diff changeset
   305
        // (so it will be after the CodeBase) and restore it in
02bb8761fcce Initial load
duke
parents:
diff changeset
   306
        // the map.
02bb8761fcce Initial load
duke
parents:
diff changeset
   307
        if (unknownExceptionInfo != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   308
            writeMapEntry(os, ueInfoId, unknownExceptionInfo, gv);
02bb8761fcce Initial load
duke
parents:
diff changeset
   309
02bb8761fcce Initial load
duke
parents:
diff changeset
   310
            scMap.put(ueInfoId, unknownExceptionInfo);
02bb8761fcce Initial load
duke
parents:
diff changeset
   311
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   312
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   313
02bb8761fcce Initial load
duke
parents:
diff changeset
   314
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   315
     * Write the given entry from the scMap to the OutputStream.
02bb8761fcce Initial load
duke
parents:
diff changeset
   316
     * See note on giopVersion.  The service context should
02bb8761fcce Initial load
duke
parents:
diff changeset
   317
     * know the GIOP version it is meant for.
02bb8761fcce Initial load
duke
parents:
diff changeset
   318
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   319
    private void writeMapEntry(OutputStream os, Integer id, Object scObj, GIOPVersion gv) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   320
02bb8761fcce Initial load
duke
parents:
diff changeset
   321
        // If it's still in byte[] form, we don't need to
02bb8761fcce Initial load
duke
parents:
diff changeset
   322
        // unmarshal it here, just copy the bytes into
02bb8761fcce Initial load
duke
parents:
diff changeset
   323
        // the new stream.
02bb8761fcce Initial load
duke
parents:
diff changeset
   324
02bb8761fcce Initial load
duke
parents:
diff changeset
   325
        if (scObj instanceof byte[]) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   326
            if (isDebugging(os))
02bb8761fcce Initial load
duke
parents:
diff changeset
   327
                dprint( "Writing service context bytes for id " + id);
02bb8761fcce Initial load
duke
parents:
diff changeset
   328
02bb8761fcce Initial load
duke
parents:
diff changeset
   329
            OctetSeqHelper.write(os, (byte[])scObj);
02bb8761fcce Initial load
duke
parents:
diff changeset
   330
02bb8761fcce Initial load
duke
parents:
diff changeset
   331
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   332
02bb8761fcce Initial load
duke
parents:
diff changeset
   333
            // We actually unmarshaled it into a ServiceContext
02bb8761fcce Initial load
duke
parents:
diff changeset
   334
            // at some point.
02bb8761fcce Initial load
duke
parents:
diff changeset
   335
            ServiceContext sc = (ServiceContext)scObj;
02bb8761fcce Initial load
duke
parents:
diff changeset
   336
02bb8761fcce Initial load
duke
parents:
diff changeset
   337
            if (isDebugging(os))
02bb8761fcce Initial load
duke
parents:
diff changeset
   338
                dprint( "Writing service context " + sc ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   339
02bb8761fcce Initial load
duke
parents:
diff changeset
   340
            sc.write(os, gv);
02bb8761fcce Initial load
duke
parents:
diff changeset
   341
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   342
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   343
02bb8761fcce Initial load
duke
parents:
diff changeset
   344
    /** Add a service context to the stream, if there is not already
02bb8761fcce Initial load
duke
parents:
diff changeset
   345
     * a service context in this object with the same id as sc.
02bb8761fcce Initial load
duke
parents:
diff changeset
   346
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   347
    public void put( ServiceContext sc )
02bb8761fcce Initial load
duke
parents:
diff changeset
   348
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   349
        Integer id = new Integer(sc.getId());
02bb8761fcce Initial load
duke
parents:
diff changeset
   350
        scMap.put(id, sc);
02bb8761fcce Initial load
duke
parents:
diff changeset
   351
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   352
02bb8761fcce Initial load
duke
parents:
diff changeset
   353
    public void delete( int scId ) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   354
        this.delete(new Integer(scId));
02bb8761fcce Initial load
duke
parents:
diff changeset
   355
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   356
02bb8761fcce Initial load
duke
parents:
diff changeset
   357
    public void delete(Integer id)
02bb8761fcce Initial load
duke
parents:
diff changeset
   358
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   359
        scMap.remove(id)  ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   360
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   361
02bb8761fcce Initial load
duke
parents:
diff changeset
   362
    public ServiceContext get(int scId) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   363
        return this.get(new Integer(scId));
02bb8761fcce Initial load
duke
parents:
diff changeset
   364
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   365
02bb8761fcce Initial load
duke
parents:
diff changeset
   366
    public ServiceContext get(Integer id)
02bb8761fcce Initial load
duke
parents:
diff changeset
   367
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   368
        Object result = scMap.get(id);
02bb8761fcce Initial load
duke
parents:
diff changeset
   369
        if (result == null)
02bb8761fcce Initial load
duke
parents:
diff changeset
   370
            return null ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   371
02bb8761fcce Initial load
duke
parents:
diff changeset
   372
        // Lazy unmarshaling on first use.
02bb8761fcce Initial load
duke
parents:
diff changeset
   373
        if (result instanceof byte[]) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   374
02bb8761fcce Initial load
duke
parents:
diff changeset
   375
            ServiceContext sc = unmarshal(id, (byte[])result);
02bb8761fcce Initial load
duke
parents:
diff changeset
   376
02bb8761fcce Initial load
duke
parents:
diff changeset
   377
            scMap.put(id, sc);
02bb8761fcce Initial load
duke
parents:
diff changeset
   378
02bb8761fcce Initial load
duke
parents:
diff changeset
   379
            return sc;
02bb8761fcce Initial load
duke
parents:
diff changeset
   380
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   381
            return (ServiceContext)result;
02bb8761fcce Initial load
duke
parents:
diff changeset
   382
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   383
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   384
02bb8761fcce Initial load
duke
parents:
diff changeset
   385
    private ORB orb ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   386
02bb8761fcce Initial load
duke
parents:
diff changeset
   387
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   388
     * Map of all ServiceContext objects in this container.
02bb8761fcce Initial load
duke
parents:
diff changeset
   389
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   390
     * Keys are java.lang.Integers for service context IDs.
02bb8761fcce Initial load
duke
parents:
diff changeset
   391
     * Values are either instances of ServiceContext or the
02bb8761fcce Initial load
duke
parents:
diff changeset
   392
     * unmarshaled byte arrays (unmarshaled on first use).
02bb8761fcce Initial load
duke
parents:
diff changeset
   393
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   394
     * This provides a mild optimization if we don't happen to
02bb8761fcce Initial load
duke
parents:
diff changeset
   395
     * use a given service context, but it's main advantage is
02bb8761fcce Initial load
duke
parents:
diff changeset
   396
     * that it allows us to change the order in which we
02bb8761fcce Initial load
duke
parents:
diff changeset
   397
     * unmarshal them.  We need to do the UnknownExceptionInfo service
02bb8761fcce Initial load
duke
parents:
diff changeset
   398
     * context after the SendingContextRunTime service context so that we can
02bb8761fcce Initial load
duke
parents:
diff changeset
   399
     * get the CodeBase if necessary.
02bb8761fcce Initial load
duke
parents:
diff changeset
   400
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   401
    private Map scMap;
02bb8761fcce Initial load
duke
parents:
diff changeset
   402
02bb8761fcce Initial load
duke
parents:
diff changeset
   403
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   404
     * If true, write out a special alignment service context to force the
02bb8761fcce Initial load
duke
parents:
diff changeset
   405
     * correct alignment on re-marshalling.
02bb8761fcce Initial load
duke
parents:
diff changeset
   406
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   407
    private boolean addAlignmentOnWrite ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   408
02bb8761fcce Initial load
duke
parents:
diff changeset
   409
    private CodeBase codeBase;
02bb8761fcce Initial load
duke
parents:
diff changeset
   410
    private GIOPVersion giopVersion;
02bb8761fcce Initial load
duke
parents:
diff changeset
   411
    private ORBUtilSystemException wrapper ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   412
}