/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.remoting;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Map;
import java.lang.reflect.Proxy;

import org.jboss.remoting.loading.ClassBytes;

/**
 * RemoteMethodInvocation is a serialized object that contains the remote method, arguments, signature and
 * associated data necessary for a remote ServerInvoker to transport an invocation.
 *
 * @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
 * @version $Revision: 1.7.4.1 $
 */
public class RemoteMethodInvocation implements Serializable
{
   private String sessionId;
   private ClassBytes param;
   private ClassBytes classbytes[];
   private String subsystem;
   private Map payload;
   private InvokerLocator locator;


   public RemoteMethodInvocation (String sessionId, String subsystem, Object arg, Map payload, InvokerLocator locator)
      throws IOException
   {
      this.sessionId = sessionId;
      this.subsystem = subsystem;
      this.payload = payload;
      this.locator = locator;
      if (arg!=null)
      {
         ByteArrayOutputStream bos=new ByteArrayOutputStream();

         byte data[]=null;
         String cn = null;
         ObjectOutputStream oos=new ObjectOutputStream(bos);
         oos.writeObject(arg);
         oos.flush();
         data=bos.toByteArray();
         if (Proxy.isProxyClass(arg.getClass()))
         {
            // just get the first interface in the list
            cn = arg.getClass().getInterfaces()[0].getName();
         }
         else
         {
            cn = arg.getClass().getName();
         }
         // note: we want to use the actual class name, not the signature
         // since the resolveClass needs the real class not the signature
         param = new org.jboss.remoting.loading.ClassBytes(cn,data);
      }
   }

   public String getSubsystem ()
   {
      return subsystem;
   }
   public void setSubsystem (String sub)
   {
      this.subsystem = sub;
   }
   public String toString ()
   {
      return org.jboss.remoting.loading.ClassUtil.getShortClassName(this.getClass()) +" [sessionId:"+sessionId+",subsystem:"+subsystem+",locator:"+locator+"]";
   }
   public String getSessionId ()
   {
      return sessionId;
   }
   public ClassBytes getParameter ()
   {
      return param;
   }
   public Map getPayload ()
   {
      return this.payload;
   }
   public void setPayload (Map payload)
   {
      this.payload = payload;
   }
   public void setClassBytes (ClassBytes bytes[])
   {
      this.classbytes = bytes;
   }
   /**
    * return the locator (client) that invoked this method (on server)
    *
    * @return
    */
   public InvokerLocator getLocator ()
   {
      return locator;
   }

   public ClassBytes[] getClassBytes ()
   {
      return this.classbytes;
   }

   public byte [] serialize ()
      throws IOException
   {
      return org.jboss.remoting.loading.ClassUtil.serialize(this);
   }
}
