else if ("cancelMessage".equals(methodName)) { oos.writeByte(CANCEL_MESSAGE);
else if ("cancelDelivery".equals(methodName)) { oos.writeByte(CANCEL_DELIVERY);
   public void write(Object obj, OutputStream out) throws IOException   {      // sanity check      if (!(out instanceof JBossObjectOutputStream))      {         log.error("out is a " + out.getClass());         throw new IllegalStateException("OutputStream must be a JBossObjectOutputStream");      }                 JBossObjectOutputStream oos = (JBossObjectOutputStream)out;            handleVersion(obj, oos);      if (obj instanceof InvocationRequest)      {                  if (trace) { log.trace("writing InvocationRequest"); }                  InvocationRequest req = (InvocationRequest)obj;         Object param;                  if (req.getParameter() instanceof MessagingMarshallable)         {            param = ((MessagingMarshallable)req.getParameter()).getLoad();         }         else         {            param = req.getParameter();         }         if (trace) { log.trace("param is " + param); }                  if (param instanceof MethodInvocation)         {                        MethodInvocation mi = (MethodInvocation)param;                        String methodName = mi.getMethod().getName();                        if (trace) { log.trace("methodInvocation (" + methodName + "())"); }                        if (methodName.equals("send"))            {                              oos.writeByte(SEND);                              writeHeader(mi, oos);                              JBossMessage m = (JBossMessage)mi.getArguments()[0];                                                            oos.writeByte(m.getType());                              m.writeExternal(oos);                              oos.flush();                                if (trace) { log.trace("wrote send()"); }            }            else if ("activate".equals(methodName))            {               oos.writeByte(ACTIVATE);                              writeHeader(mi, oos);                                                          oos.flush();                              if (trace) { log.trace("wrote activate()"); }            }            else if ("deactivate".equals(methodName))            {               oos.writeByte(DEACTIVATE);                              writeHeader(mi, oos);                                                           oos.flush();                              if (trace) { log.trace("wrote deactivate()"); }            }            else if ("getMessageNow".equals(methodName))            {               oos.writeByte(GETMESSAGENOW);                              writeHeader(mi, oos);                                  boolean wait = ((Boolean)mi.getArguments()[0]).booleanValue();                              oos.writeBoolean(wait);                              oos.flush();                              if (trace) { log.trace("wrote getMessageNow()"); }            }            else if ("acknowledge".equals(methodName))            {               oos.writeByte(ACKNOWLEDGE);                              writeHeader(mi, oos);                                                            oos.flush();                              if (trace) { log.trace("wrote acknowledge()"); }            }            else if ("sendTransaction".equals(methodName))            {               oos.writeByte(SEND_TRANSACTION);                              writeHeader(mi, oos);                                  TransactionRequest request = (TransactionRequest)mi.getArguments()[0];                              request.writeExternal(oos);                              oos.flush();                              if (trace) { log.trace("wrote getMessageNow()"); }            }            else if ("getIdBlock".equals(methodName))            {               oos.writeByte(GET_ID_BLOCK);                              writeHeader(mi, oos);                                  int size = ((Integer)mi.getArguments()[0]).intValue();                              oos.writeInt(size);                              oos.flush();                              if (trace) { log.trace("wrote getIdBlock()"); }            }            else if ("cancelMessage".equals(methodName))            {               oos.writeByte(CANCEL_MESSAGE);                              writeHeader(mi, oos);                              long id = ((Long)mi.getArguments()[0]).longValue();                              oos.writeLong(id);                              oos.flush();                              if (trace) { log.trace("wrote cancelMessage()"); }                          }            else if ("cancelMessages".equals(methodName))            {               oos.writeByte(CANCEL_MESSAGES);                              writeHeader(mi, oos);                              List ids = (List)mi.getArguments()[0];                              oos.writeInt(ids.size());                              Iterator iter = ids.iterator();                              while (iter.hasNext())               {                  Long l = (Long)iter.next();                  oos.writeLong(l.longValue());               }                                             oos.flush();                              if (trace) { log.trace("wrote cancelMessage()"); }                          }            else            {                oos.write(SERIALIZED);                              //Delegate to serialization to handle the wire format               serializableMarshaller.write(obj, oos);                              if (trace) { log.trace("wrote using standard serialization"); }            }         }         else if (param instanceof DeliveryRunnable)         {            //Message delivery callback                        if (trace) { log.trace("DeliveryRunnable"); }                           DeliveryRunnable dr = (DeliveryRunnable)param;                        oos.writeByte(CALLBACK);                                int consumerID = dr.getConsumerID();                        MessageProxy del = dr.getMessageProxy();                        oos.writeInt(consumerID);                        oos.writeByte(del.getMessage().getType());                        oos.writeInt(del.getDeliveryCount());                        del.getMessage().writeExternal(oos);                        oos.flush();                            if (trace) { log.trace("wrote DeliveryRunnable"); }         }                     else         {                       //Internal invocation                        oos.write(SERIALIZED);                        //Delegate to serialization to handle the wire format            serializableMarshaller.write(obj, oos);                        if (trace) { log.trace("wrote using standard serialization"); }         }      }      else if (obj instanceof InvocationResponse)      {         if (trace) { log.trace("writing InvocationResponse"); }                  InvocationResponse resp = (InvocationResponse)obj;                  Object res;                  if (resp.getResult() instanceof MessagingMarshallable)         {            res = ((MessagingMarshallable)resp.getResult()).getLoad();         }         else         {            res = resp.getResult();         }                  if (trace) { log.trace("result is " + res); }                  if (res == null && !resp.isException())         {            oos.write(NULL_RESPONSE);                        oos.flush();                                    if (trace) { log.trace("wrote null response"); }         }         else if (res instanceof MessageProxy)         {            //Return value from getMessageNow            oos.write(MESSAGE_RESPONSE);                        MessageProxy del = (MessageProxy)res;                        oos.writeByte(del.getMessage().getType());                        oos.writeInt(del.getDeliveryCount());                        del.getMessage().writeExternal(oos);                        oos.flush();                        if (trace) { log.trace("wrote message response"); }         }         else if (res instanceof IdBlock)         {            //Return value from getMessageNow            oos.write(ID_BLOCK_RESPONSE);                        IdBlock block = (IdBlock)res;                        block.writeExternal(oos);                        oos.flush();                        if (trace) { log.trace("wrote message response"); }         }         else if (res instanceof Long)         {            //Return value from deactivate            oos.write(DEACTIVATE_RESPONSE);                        Long l = (Long)res;                        oos.writeLong(l.longValue());                        oos.flush();                           if (trace) { log.trace("wrote deactivate response"); }         }         else         {                  oos.write(SERIALIZED);                          //Delegate to serialization to handle the wire format            serializableMarshaller.write(obj, out);                        if (trace) { log.trace("wrote using standard serialization"); }         }      }      else      {         throw new IllegalStateException("Invalid object " + obj);      }      }