messagebase.java

来自「JAVA 所有包」· Java 代码 · 共 949 行 · 第 1/3 页

JAVA
949
字号
/* * @(#)MessageBase.java	1.20 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.corba.se.impl.protocol.giopmsgheaders;import java.io.IOException;import java.lang.Class;import java.lang.reflect.Constructor;import java.nio.ByteBuffer;import java.util.Iterator;import org.omg.CORBA.CompletionStatus;import org.omg.CORBA.INTERNAL;import org.omg.CORBA.MARSHAL;import org.omg.CORBA.Principal;import org.omg.CORBA.SystemException;import org.omg.IOP.TaggedProfile;import com.sun.corba.se.pept.transport.ByteBufferPool;import com.sun.corba.se.spi.ior.ObjectKey;import com.sun.corba.se.spi.ior.ObjectId;import com.sun.corba.se.spi.ior.IOR;import com.sun.corba.se.spi.ior.ObjectKeyFactory;import com.sun.corba.se.spi.ior.iiop.IIOPProfile;import com.sun.corba.se.spi.ior.iiop.IIOPFactories;import com.sun.corba.se.spi.ior.iiop.IIOPProfileTemplate ;import com.sun.corba.se.spi.ior.iiop.GIOPVersion;import com.sun.corba.se.spi.ior.iiop.RequestPartitioningComponent;import com.sun.corba.se.spi.logging.CORBALogDomains ;import com.sun.corba.se.spi.orb.ORB;import com.sun.corba.se.spi.transport.CorbaConnection;import com.sun.corba.se.spi.transport.ReadTimeouts;import com.sun.corba.se.spi.servicecontext.ServiceContexts;import com.sun.corba.se.impl.encoding.ByteBufferWithInfo;import com.sun.corba.se.impl.encoding.CDRInputStream_1_0;import com.sun.corba.se.impl.logging.ORBUtilSystemException ;import com.sun.corba.se.impl.orbutil.ORBUtility;import com.sun.corba.se.impl.orbutil.ORBConstants;import com.sun.corba.se.impl.orbutil.ORBClassLoader;import com.sun.corba.se.impl.protocol.AddressingDispositionException;/** * This class acts as the base class for the various GIOP message types. This * also serves as a factory to create various message types. We currently * support GIOP 1.0, 1.1 and 1.2 message types. * * @author Ram Jeyaraman 05/14/2000 * @version 1.0 */public abstract class MessageBase implements Message{    // This is only used when the giopDebug flag is    // turned on.    public byte[] giopHeader;    private ByteBuffer byteBuffer;    private int threadPoolToUse;    // (encodingVersion == 0x00) implies CDR encoding,     // (encodingVersion >  0x00) implies Java serialization version.    byte encodingVersion = (byte) Message.CDR_ENC_VERSION;    private static ORBUtilSystemException wrapper = 	ORBUtilSystemException.get( CORBALogDomains.RPC_PROTOCOL ) ;    // Static methods    public static String typeToString(int type)    {	return typeToString((byte)type);    }    public static String typeToString(byte type)    {	String result = type + "/";	switch (type) {	case GIOPRequest         : result += "GIOPRequest";         break;	case GIOPReply           : result += "GIOPReply";           break;	case GIOPCancelRequest   : result += "GIOPCancelRequest";   break;	case GIOPLocateRequest   : result += "GIOPLocateRequest";   break;	case GIOPLocateReply     : result += "GIOPLocateReply";     break;	case GIOPCloseConnection : result += "GIOPCloseConnection"; break;	case GIOPMessageError    : result += "GIOPMessageError";    break;	case GIOPFragment        : result += "GIOPFragment";        break;	default                  : result += "Unknown";             break;	}	return result;    }    public static MessageBase readGIOPMessage(ORB orb, CorbaConnection connection)    {	MessageBase msg = readGIOPHeader(orb, connection);	msg = (MessageBase)readGIOPBody(orb, connection, (Message)msg);	return msg;    }    public static MessageBase readGIOPHeader(ORB orb, CorbaConnection connection)    {        MessageBase msg = null;        ReadTimeouts readTimeouts =	                   orb.getORBData().getTransportTCPReadTimeouts();	ByteBuffer buf = null;	try {	    buf = connection.read(GIOPMessageHeaderLength,			  0, GIOPMessageHeaderLength,			  readTimeouts.get_max_giop_header_time_to_wait());	} catch (IOException e) {	    throw wrapper.ioexceptionWhenReadingConnection(e);	}        if (orb.giopDebugFlag) {            // Since this is executed in debug mode only the overhead of            // using a View Buffer is not an issue. We'll also use a            // read-only View Buffer so we don't disturb the state of            // byteBuffer.             dprint(".readGIOPHeader: " + typeToString(buf.get(7)));	    dprint(".readGIOPHeader: GIOP header is: ");            ByteBuffer viewBuffer = buf.asReadOnlyBuffer();            viewBuffer.position(0).limit(GIOPMessageHeaderLength);	    ByteBufferWithInfo bbwi = new ByteBufferWithInfo(orb,viewBuffer);	    bbwi.buflen = GIOPMessageHeaderLength;	    CDRInputStream_1_0.printBuffer(bbwi);        }        // Sanity checks        /*         * check for magic corruption         * check for version incompatibility         * check if fragmentation is allowed based on mesg type.            . 1.0 fragmentation disallowed; FragmentMessage is non-existent.            . 1.1 only {Request, Reply} msgs maybe fragmented.            . 1.2 only {Request, Reply, LocateRequest, LocateReply} msgs              maybe fragmented.        */        int b1, b2, b3, b4;        b1 = (buf.get(0) << 24) & 0xFF000000;        b2 = (buf.get(1) << 16) & 0x00FF0000;        b3 = (buf.get(2) << 8)  & 0x0000FF00;        b4 = (buf.get(3) << 0)  & 0x000000FF;        int magic = (b1 | b2 | b3 | b4);        if (magic != GIOPBigMagic) {            // If Magic is incorrect, it is an error.            // ACTION : send MessageError and close the connection.	    throw wrapper.giopMagicError( CompletionStatus.COMPLETED_MAYBE);        }	// Extract the encoding version from the request GIOP Version,	// if it contains an encoding, and set GIOP version appropriately.	// For Java serialization, we use GIOP Version 1.2 message format.	byte requestEncodingVersion = Message.CDR_ENC_VERSION;	if ((buf.get(4) == 0x0D) &&	    (buf.get(5) <= Message.JAVA_ENC_VERSION) &&	    (buf.get(5) > Message.CDR_ENC_VERSION) &&	    orb.getORBData().isJavaSerializationEnabled()) {	    // Entering this block means the request is using Java encoding,	    // and the encoding version is <= this ORB's Java encoding version.	    requestEncodingVersion = buf.get(5);	    buf.put(4, (byte) 0x01);	    buf.put(5, (byte) 0x02);	}        GIOPVersion orbVersion = orb.getORBData().getGIOPVersion();        if (orb.giopDebugFlag) {            dprint(".readGIOPHeader: Message GIOP version: "                              + buf.get(4) + '.' + buf.get(5));            dprint(".readGIOPHeader: ORB Max GIOP Version: "                              + orbVersion);        }        if ( (buf.get(4) > orbVersion.getMajor()) ||             ( (buf.get(4) == orbVersion.getMajor()) && (buf.get(5) > orbVersion.getMinor()) )            ) {            // For requests, sending ORB should use the version info            // published in the IOR or may choose to use a <= version            // for requests. If the version is greater than published version,            // it is an error.            // For replies, the ORB should always receive a version it supports            // or less, but never greater (except for MessageError)            // ACTION : Send back a MessageError() with the the highest version            // the server ORB supports, and close the connection.            if ( buf.get(7) != GIOPMessageError ) {		throw wrapper.giopVersionError( CompletionStatus.COMPLETED_MAYBE);            }        }        AreFragmentsAllowed(buf.get(4), buf.get(5), buf.get(6), buf.get(7));        // create appropriate messages types        switch (buf.get(7)) {        case GIOPRequest:            if (orb.giopDebugFlag) {                dprint(".readGIOPHeader: creating RequestMessage");            }            //msg = new RequestMessage(orb.giopDebugFlag);            if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x00) ) { // 1.0                msg = new RequestMessage_1_0(orb);            } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x01) ) { // 1.1                msg = new RequestMessage_1_1(orb);            } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x02) ) { // 1.2                msg = new RequestMessage_1_2(orb);            } else {		throw wrapper.giopVersionError(		    CompletionStatus.COMPLETED_MAYBE);            }            break;        case GIOPLocateRequest:            if (orb.giopDebugFlag) {                dprint(".readGIOPHeader: creating LocateRequestMessage");            }            //msg = new LocateRequestMessage(orb.giopDebugFlag);            if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x00) ) { // 1.0                msg = new LocateRequestMessage_1_0(orb);            } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x01) ) { // 1.1                msg = new LocateRequestMessage_1_1(orb);            } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x02) ) { // 1.2                msg = new LocateRequestMessage_1_2(orb);            } else {		throw wrapper.giopVersionError(		    CompletionStatus.COMPLETED_MAYBE);            }            break;        case GIOPCancelRequest:            if (orb.giopDebugFlag) {                dprint(".readGIOPHeader: creating CancelRequestMessage");            }            //msg = new CancelRequestMessage(orb.giopDebugFlag);            if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x00) ) { // 1.0                msg = new CancelRequestMessage_1_0();            } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x01) ) { // 1.1                msg = new CancelRequestMessage_1_1();            } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x02) ) { // 1.2                msg = new CancelRequestMessage_1_2();            } else {		throw wrapper.giopVersionError(		    CompletionStatus.COMPLETED_MAYBE);            }            break;        case GIOPReply:            if (orb.giopDebugFlag) {                dprint(".readGIOPHeader: creating ReplyMessage");            }            //msg = new ReplyMessage(orb.giopDebugFlag);            if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x00) ) { // 1.0                msg = new ReplyMessage_1_0(orb);            } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x01) ) { // 1.1                msg = new ReplyMessage_1_1(orb);            } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x02) ) { // 1.2                msg = new ReplyMessage_1_2(orb);            } else {		throw wrapper.giopVersionError(		    CompletionStatus.COMPLETED_MAYBE);            }            break;        case GIOPLocateReply:            if (orb.giopDebugFlag) {                dprint(".readGIOPHeader: creating LocateReplyMessage");            }            //msg = new LocateReplyMessage(orb.giopDebugFlag);            if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x00) ) { // 1.0                msg = new LocateReplyMessage_1_0(orb);            } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x01) ) { // 1.1                msg = new LocateReplyMessage_1_1(orb);            } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x02) ) { // 1.2                msg = new LocateReplyMessage_1_2(orb);            } else {		throw wrapper.giopVersionError(		    CompletionStatus.COMPLETED_MAYBE);            }            break;        case GIOPCloseConnection:        case GIOPMessageError:            if (orb.giopDebugFlag) {                dprint(".readGIOPHeader: creating Message for CloseConnection or MessageError");            }            // REVISIT a MessageError  may contain the highest version server            // can support. In such a case, a new request may be made with the            // correct version or the connection be simply closed. Note the            // connection may have been closed by the server.            //msg = new Message(orb.giopDebugFlag);            if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x00) ) { // 1.0                msg = new Message_1_0();            } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x01) ) { // 1.1                msg = new Message_1_1();            } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x02) ) { // 1.2                msg = new Message_1_1();            } else {		throw wrapper.giopVersionError(		    CompletionStatus.COMPLETED_MAYBE);            }            break;        case GIOPFragment:            if (orb.giopDebugFlag) {                dprint(".readGIOPHeader: creating FragmentMessage");            }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?