/*
 * Copyright (c) 1999, 2003, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package com.sun.corba.se.impl.iiop;

import com.sun.corba.se.impl.protocol.Request;
import com.sun.corba.se.impl.core.ClientRequest;
import com.sun.corba.se.impl.core.ServiceContext;
import com.sun.corba.se.impl.core.ServiceContexts;
import com.sun.corba.se.impl.core.ClientResponse;
import com.sun.corba.se.impl.core.ServerRequest;
import com.sun.corba.se.impl.core.ServerResponse;
import com.sun.corba.se.impl.corba.IOR;
import com.sun.corba.se.impl.corba.GIOPVersion;
import com.sun.corba.se.impl.protocol.giopmsgheaders.MessageBase;
import com.sun.corba.se.impl.protocol.giopmsgheaders.RequestMessage;
import com.sun.corba.se.impl.orbutil.ORBConstants;
import com.sun.corba.se.impl.core.ORBVersion;
import com.sun.corba.se.impl.core.ORB;
import com.sun.corba.se.impl.orbutil.ORBUtility;
import com.sun.corba.se.impl.ior.ObjectKeyFactory ;
import com.sun.corba.se.impl.ior.ObjectKey ;
import com.sun.corba.se.impl.ior.ObjectKeyTemplate ;
import com.sun.corba.se.impl.ior.IIOPProfile;

public class LocalClientRequestImpl extends IIOPOutputStream 
    implements ClientRequest 
{
    public LocalClientRequestImpl( GIOPVersion gv, 
	ORB orb, IOR ior, short addrDisposition, 
        String operationName, boolean oneway, ServiceContexts svc, 
	int requestId, byte streamFormatVersion)
    {
	super(gv, 
              orb, 
              null, 
              BufferManagerFactory.newBufferManagerWrite(BufferManagerFactory.GROW),
              streamFormatVersion);

	this.isOneway = oneway;
	boolean responseExpected = !isOneway;

        IIOPProfile iop = ior.getProfile();
	ObjectKey okey = iop.getObjectKey();
	ObjectKeyTemplate oktemp = okey.getTemplate() ;
	ORBVersion version = oktemp.getORBVersion() ;
	orb.setORBVersion( version ) ;

        this.request = MessageBase.createRequest(orb, gv, requestId,
	    responseExpected, ior, addrDisposition, operationName, svc, null);
	setMessage(request);
	request.write(this);

	// mark beginning of msg body for possible later use
	bodyBegin = getSize();
    }

    public int getRequestId() {
	return request.getRequestId();
    }
    
    public boolean isOneWay() {
	return isOneway;
    }

    public ServiceContexts getServiceContexts() {
	return request.getServiceContexts();
    }

    public String getOperationName() {
	return request.getOperation();
    }

    public ObjectKey getObjectKey() {
	return request.getObjectKey();
    }

    public ServerRequest getServerRequest()
    {
	// Set the size of the marshalled data in the message header.
	getMessage().setSize( getByteBuffer(), getSize() ) ;

	// Construct a new ServerRequest out of the buffer in this ClientRequest
	LocalServerRequestImpl serverRequest = new LocalServerRequestImpl(
	    (ORB)orb(), toByteArray(), request ) ;

	// Skip over all of the GIOP header information.  This positions
	// the offset in the buffer so that the skeleton can correctly read
	// the marshalled arguments.
	serverRequest.setIndex( bodyBegin ) ;

	return serverRequest ;
    }
    
    public ClientResponse invoke() 
    {	
	ORB myORB = (ORB)orb() ;

	ServerResponse serverResponse = myORB.process( getServerRequest() ) ;

	LocalServerResponseImpl lsr = (LocalServerResponseImpl)serverResponse ;

	return lsr.getClientResponse() ;
    }

    /**
     * Check to see if the request is local.
     */
    public boolean isLocal(){
        return true;
    }

    private RequestMessage request;
    private int bodyBegin;
    private boolean isOneway;
}
