protected void tightMarshalCachedObject2(
     OpenWireFormat wireFormat, DataStructure o, DataOutput dataOut, BooleanStream bs)
     throws IOException {
   if (wireFormat.isCacheEnabled()) {
     Short index = wireFormat.getMarshallCacheIndex(o);
     if (bs.readBoolean()) {
       dataOut.writeShort(index.shortValue());
       wireFormat.tightMarshalNestedObject2(o, dataOut, bs);
     } else {
       dataOut.writeShort(index.shortValue());
     }
   } else {
     wireFormat.tightMarshalNestedObject2(o, dataOut, bs);
   }
 }
 protected int tightMarshalThrowable1(OpenWireFormat wireFormat, Throwable o, BooleanStream bs)
     throws IOException {
   if (o == null) {
     bs.writeBoolean(false);
     return 0;
   } else {
     int rc = 0;
     bs.writeBoolean(true);
     rc += tightMarshalString1(o.getClass().getName(), bs);
     rc += tightMarshalString1(o.getMessage(), bs);
     if (wireFormat.isStackTraceEnabled()) {
       rc += 2;
       StackTraceElement[] stackTrace = o.getStackTrace();
       for (int i = 0; i < stackTrace.length; i++) {
         StackTraceElement element = stackTrace[i];
         rc += tightMarshalString1(element.getClassName(), bs);
         rc += tightMarshalString1(element.getMethodName(), bs);
         rc += tightMarshalString1(element.getFileName(), bs);
         rc += 4;
       }
       rc += tightMarshalThrowable1(wireFormat, o.getCause(), bs);
     }
     return rc;
   }
 }
 protected DataStructure looseUnmarsalCachedObject(OpenWireFormat wireFormat, DataInput dataIn)
     throws IOException {
   if (wireFormat.isCacheEnabled()) {
     if (dataIn.readBoolean()) {
       short index = dataIn.readShort();
       DataStructure object = wireFormat.looseUnmarshalNestedObject(dataIn);
       wireFormat.setInUnmarshallCache(index, object);
       return object;
     } else {
       short index = dataIn.readShort();
       return wireFormat.getFromUnmarshallCache(index);
     }
   } else {
     return wireFormat.looseUnmarshalNestedObject(dataIn);
   }
 }
 protected void looseMarshalCachedObject(
     OpenWireFormat wireFormat, DataStructure o, DataOutput dataOut) throws IOException {
   if (wireFormat.isCacheEnabled()) {
     Short index = wireFormat.getMarshallCacheIndex(o);
     dataOut.writeBoolean(index == null);
     if (index == null) {
       index = wireFormat.addToMarshallCache(o);
       dataOut.writeShort(index.shortValue());
       wireFormat.looseMarshalNestedObject(o, dataOut);
     } else {
       dataOut.writeShort(index.shortValue());
     }
   } else {
     wireFormat.looseMarshalNestedObject(o, dataOut);
   }
 }
 protected int tightMarshalCachedObject1(
     OpenWireFormat wireFormat, DataStructure o, BooleanStream bs) throws IOException {
   if (wireFormat.isCacheEnabled()) {
     Short index = wireFormat.getMarshallCacheIndex(o);
     bs.writeBoolean(index == null);
     if (index == null) {
       int rc = wireFormat.tightMarshalNestedObject1(o, bs);
       wireFormat.addToMarshallCache(o);
       return 2 + rc;
     } else {
       return 2;
     }
   } else {
     return wireFormat.tightMarshalNestedObject1(o, bs);
   }
 }
 public void physicalSend(Command command) throws IOException {
   try {
     ByteSequence bytes = wireFormat.marshal(command);
     ActiveMQBuffer buffer = OpenWireUtil.toActiveMQBuffer(bytes);
     synchronized (sendLock) {
       getTransportConnection().write(buffer, false, false);
     }
   } catch (IOException e) {
     throw e;
   } catch (Throwable t) {
     ActiveMQServerLogger.LOGGER.error("error sending", t);
   }
 }
 protected Throwable looseUnmarsalThrowable(OpenWireFormat wireFormat, DataInput dataIn)
     throws IOException {
   if (dataIn.readBoolean()) {
     String clazz = looseUnmarshalString(dataIn);
     String message = looseUnmarshalString(dataIn);
     Throwable o = createThrowable(clazz, message);
     if (wireFormat.isStackTraceEnabled()) {
       if (STACK_TRACE_ELEMENT_CONSTRUCTOR != null) {
         StackTraceElement ss[] = new StackTraceElement[dataIn.readShort()];
         for (int i = 0; i < ss.length; i++) {
           try {
             ss[i] =
                 (StackTraceElement)
                     STACK_TRACE_ELEMENT_CONSTRUCTOR.newInstance(
                         new Object[] {
                           looseUnmarshalString(dataIn),
                           looseUnmarshalString(dataIn),
                           looseUnmarshalString(dataIn),
                           Integer.valueOf(dataIn.readInt())
                         });
           } catch (IOException e) {
             throw e;
           } catch (Throwable e) {
           }
         }
         o.setStackTrace(ss);
       } else {
         short size = dataIn.readShort();
         for (int i = 0; i < size; i++) {
           looseUnmarshalString(dataIn);
           looseUnmarshalString(dataIn);
           looseUnmarshalString(dataIn);
           dataIn.readInt();
         }
       }
       o.initCause(looseUnmarsalThrowable(wireFormat, dataIn));
     }
     return o;
   } else {
     return null;
   }
 }
 protected void looseMarshalThrowable(OpenWireFormat wireFormat, Throwable o, DataOutput dataOut)
     throws IOException {
   dataOut.writeBoolean(o != null);
   if (o != null) {
     looseMarshalString(o.getClass().getName(), dataOut);
     looseMarshalString(o.getMessage(), dataOut);
     if (wireFormat.isStackTraceEnabled()) {
       StackTraceElement[] stackTrace = o.getStackTrace();
       dataOut.writeShort(stackTrace.length);
       for (int i = 0; i < stackTrace.length; i++) {
         StackTraceElement element = stackTrace[i];
         looseMarshalString(element.getClassName(), dataOut);
         looseMarshalString(element.getMethodName(), dataOut);
         looseMarshalString(element.getFileName(), dataOut);
         dataOut.writeInt(element.getLineNumber());
       }
       looseMarshalThrowable(wireFormat, o.getCause(), dataOut);
     }
   }
 }
 protected void tightMarshalThrowable2(
     OpenWireFormat wireFormat, Throwable o, DataOutput dataOut, BooleanStream bs)
     throws IOException {
   if (bs.readBoolean()) {
     tightMarshalString2(o.getClass().getName(), dataOut, bs);
     tightMarshalString2(o.getMessage(), dataOut, bs);
     if (wireFormat.isStackTraceEnabled()) {
       StackTraceElement[] stackTrace = o.getStackTrace();
       dataOut.writeShort(stackTrace.length);
       for (int i = 0; i < stackTrace.length; i++) {
         StackTraceElement element = stackTrace[i];
         tightMarshalString2(element.getClassName(), dataOut, bs);
         tightMarshalString2(element.getMethodName(), dataOut, bs);
         tightMarshalString2(element.getFileName(), dataOut, bs);
         dataOut.writeInt(element.getLineNumber());
       }
       tightMarshalThrowable2(wireFormat, o.getCause(), dataOut, bs);
     }
   }
 }
  @Override
  public void bufferReceived(Object connectionID, ActiveMQBuffer buffer) {
    try {
      dataReceived = true;

      Command command = (Command) wireFormat.unmarshal(buffer);

      boolean responseRequired = command.isResponseRequired();
      int commandId = command.getCommandId();
      // the connection handles pings, negotiations directly.
      // and delegate all other commands to manager.
      if (command.getClass() == KeepAliveInfo.class) {
        KeepAliveInfo info = (KeepAliveInfo) command;
        info.setResponseRequired(false);
        // if we don't respond to KeepAlive commands then the client will think the server is dead
        // and timeout
        // for some reason KeepAliveInfo.isResponseRequired() is always false
        protocolManager.sendReply(this, info);
      } else if (command.getClass() == WireFormatInfo.class) {
        // amq here starts a read/write monitor thread (detect ttl?)
        negotiate((WireFormatInfo) command);
      } else if (command.getClass() == ConnectionInfo.class
          || command.getClass() == ConsumerInfo.class
          || command.getClass() == RemoveInfo.class
          || command.getClass() == SessionInfo.class
          || command.getClass() == ProducerInfo.class
          || ActiveMQMessage.class.isAssignableFrom(command.getClass())
          || command.getClass() == MessageAck.class
          || command.getClass() == TransactionInfo.class
          || command.getClass() == DestinationInfo.class
          || command.getClass() == ShutdownInfo.class
          || command.getClass() == RemoveSubscriptionInfo.class) {
        Response response = null;

        if (pendingStop) {
          response = new ExceptionResponse(this.stopError);
        } else {
          response = ((Command) command).visit(this);

          if (response instanceof ExceptionResponse) {
            if (!responseRequired) {
              Throwable cause = ((ExceptionResponse) response).getException();
              serviceException(cause);
              response = null;
            }
          }
        }

        if (responseRequired) {
          if (response == null) {
            response = new Response();
          }
        }

        // The context may have been flagged so that the response is not
        // sent.
        if (context != null) {
          if (context.isDontSendReponse()) {
            context.setDontSendReponse(false);
            response = null;
          }
        }

        if (response != null && !protocolManager.isStopping()) {
          response.setCorrelationId(commandId);
          dispatchSync(response);
        }

      } else {
        // note!!! wait for negotiation (e.g. use a countdown latch)
        // before handling any other commands
        this.protocolManager.handleCommand(this, command);
      }
    } catch (IOException e) {
      ActiveMQServerLogger.LOGGER.error("error decoding", e);
    } catch (Throwable t) {
      ActiveMQServerLogger.LOGGER.error("error decoding", t);
    }
  }
  @Override
  public Response processAddConnection(ConnectionInfo info) throws Exception {
    WireFormatInfo wireFormatInfo = wireFormat.getPreferedWireFormatInfo();
    // Older clients should have been defaulting this field to true.. but
    // they were not.
    if (wireFormatInfo != null && wireFormatInfo.getVersion() <= 2) {
      info.setClientMaster(true);
    }

    state = new ConnectionState(info);

    context = new AMQConnectionContext();

    state.reset(info);

    this.faultTolerantConnection = info.isFaultTolerant();
    // Setup the context.
    String clientId = info.getClientId();
    context.setBroker(protocolManager);
    context.setClientId(clientId);
    context.setClientMaster(info.isClientMaster());
    context.setConnection(this);
    context.setConnectionId(info.getConnectionId());
    // for now we pass the manager as the connector and see what happens
    // it should be related to activemq's Acceptor
    context.setConnector(this.acceptorUsed);
    context.setMessageAuthorizationPolicy(getMessageAuthorizationPolicy());
    context.setNetworkConnection(networkConnection);
    context.setFaultTolerant(faultTolerantConnection);
    context.setTransactions(new ConcurrentHashMap<TransactionId, AMQTransaction>());
    context.setUserName(info.getUserName());
    context.setWireFormatInfo(wireFormatInfo);
    context.setReconnect(info.isFailoverReconnect());
    this.manageable = info.isManageable();
    context.setConnectionState(state);
    if (info.getClientIp() == null) {
      info.setClientIp(getRemoteAddress());
    }

    try {
      protocolManager.addConnection(context, info);
    } catch (Exception e) {
      if (e instanceof SecurityException) {
        // close this down - in case the peer of this transport doesn't play
        // nice
        delayedStop(2000, "Failed with SecurityException: " + e.getLocalizedMessage(), e);
      }
      Response resp = new ExceptionResponse(e);
      return resp;
    }
    if (info.isManageable()) {
      // send ConnectionCommand
      ConnectionControl command = this.acceptorUsed.getConnectionControl();
      command.setFaultTolerant(protocolManager.isFaultTolerantConfiguration());
      if (info.isFailoverReconnect()) {
        command.setRebalanceConnection(false);
      }
      dispatchAsync(command);
    }
    return null;
  }
 protected DataStructure tightUnmarsalNestedObject(
     OpenWireFormat wireFormat, DataInput dataIn, BooleanStream bs) throws IOException {
   return wireFormat.tightUnmarshalNestedObject(dataIn, bs);
 }
 protected int tightMarshalNestedObject1(
     OpenWireFormat wireFormat, DataStructure o, BooleanStream bs) throws IOException {
   return wireFormat.tightMarshalNestedObject1(o, bs);
 }
 protected void looseMarshalNestedObject(
     OpenWireFormat wireFormat, DataStructure o, DataOutput dataOut) throws IOException {
   wireFormat.looseMarshalNestedObject(o, dataOut);
 }
 protected DataStructure looseUnmarsalNestedObject(OpenWireFormat wireFormat, DataInput dataIn)
     throws IOException {
   return wireFormat.looseUnmarshalNestedObject(dataIn);
 }
 protected void tightMarshalNestedObject2(
     OpenWireFormat wireFormat, DataStructure o, DataOutput dataOut, BooleanStream bs)
     throws IOException {
   wireFormat.tightMarshalNestedObject2(o, dataOut, bs);
 }
 // throw a WireFormatInfo to the peer
 public void init() {
   WireFormatInfo info = wireFormat.getPreferedWireFormatInfo();
   protocolManager.send(this, info);
 }