/**
  * Send a message to an address. The message is either an {@link IEnvelope} which contains an
  * {@link IInternalMessage} or a {@link Map} with an "address" parameter. This makes it easier to
  * support both dynamic Javascript messages and more type safe Java messages.
  *
  * <p>Send attaches a "replyTo" address and a correlationId to them message so that it returns the
  * the {@link IBpmScriptEngine}
  */
 @SuppressWarnings("unchecked")
 public void send(
     final String fromPid,
     final String fromBranch,
     final String fromVersion,
     final String queueId,
     Object message)
     throws BpmScriptException {
   String correlationId = correlationIdFormatter.format(fromPid, fromBranch, fromVersion, queueId);
   if (message instanceof IEnvelope) {
     IEnvelope envelope = (IEnvelope) message;
     InvocationMessage invocationMessage = ((InvocationMessage) envelope.getMessage());
     invocationMessage.setReplyTo(replyTo);
     invocationMessage.setCorrelationId(correlationId);
     String address = (String) envelope.getAddress();
     sender.send(address, invocationMessage);
   } else {
     Map<String, Object> map = (Map<String, Object>) message;
     map.put("correlationId", correlationId);
     map.put("replyTo", replyTo);
     String address = (String) map.get("address");
     MapInvocationMessage result = new MapInvocationMessage(map);
     if (address == null) {
       log.warn("address for message " + result.toString() + " is null");
     }
     sender.send(address, result);
   }
 }
 /**
  * Reply to a message. The message an {@link IInternalMessage} or a {@link Map} with a "content"
  * key with the return Object.
  */
 @SuppressWarnings("unchecked")
 public void reply(String pid, Object in, Object out) throws BpmScriptException {
   IInternalMessage messageToSend = null;
   String correlationId = ((IInternalMessage) in).getCorrelationId();
   if (out instanceof ExceptionMessage) {
     ExceptionMessage exceptionMessage = (ExceptionMessage) out;
     exceptionMessage.setCorrelationId(correlationId);
     messageToSend = exceptionMessage;
   } else if (out instanceof ResponseMessage) {
     ResponseMessage responseMessage = (ResponseMessage) out;
     responseMessage.setCorrelationId(correlationId);
     messageToSend = responseMessage;
   } else {
     Map<String, Object> map = (Map<String, Object>) out;
     Object content = map.get("content");
     if (content != null && content instanceof Throwable) {
       messageToSend = new ExceptionMessage(correlationId, (Throwable) content);
     } else {
       messageToSend = new MapResponseMessage(map);
     }
     map.put("correlationId", correlationId);
   }
   if (in instanceof IInvocationMessage) {
     final IInvocationMessage inMessage = (IInvocationMessage) in;
     sender.send(inMessage.getReplyTo(), messageToSend);
   } else {
     sender.send(errorAddress, messageToSend);
   }
 }
  /** Send a message to an address */
  public void send(String address, IInternalMessage internalMessage) {
    if (log.isDebugEnabled()) {
      log.debug("bus received message " + internalMessage + " for " + address);
    }
    if (address == null) {
      log.warn("address is null for message " + internalMessage);
    }
    if (internalMessage == null) {
      log.warn("message is null for address " + address);
    }

    int indexOfColon = address.indexOf(":");
    if (indexOfColon > 0) {
      String namespace = address.substring(0, indexOfColon);
      if (namespace != name) {
        messageSender.send(address, internalMessage);
      } else {
        auditSender.send(address, internalMessage);
        queue.add(new Object[] {address.substring(indexOfColon + 1), internalMessage});
      }
    } else {
      auditSender.send(name + ":" + address, internalMessage);
      queue.add(new Object[] {address, internalMessage});
    }
  }
 /**
  * Send a message to an address. The message is either an {@link IEnvelope} which contains an
  * {@link IInternalMessage} or a {@link Map} with an "address" parameter. This makes it easier to
  * support both dynamic Javascript messages and more type safe Java messages.
  */
 @SuppressWarnings("unchecked")
 public void sendOneWay(Object message) throws BpmScriptException {
   if (message instanceof IEnvelope) {
     IEnvelope envelope = (IEnvelope) message;
     sender.send((String) envelope.getAddress(), (IInternalMessage) envelope.getMessage());
   } else {
     Map<String, Object> map = (Map<String, Object>) message;
     String address = (String) map.get("address");
     MapInvocationMessage result = new MapInvocationMessage(map);
     if (address == null) {
       log.warn("address for message " + result.toString() + " is null");
     }
     sender.send(address, result);
   }
 }
 /**
  * Sends a timeout message address to the {@link TimeoutAdapter}
  *
  * <p>Send attaches a "replyTo" address and a correlationId to them message so that it returns the
  * the {@link IBpmScriptEngine}
  */
 public void sendTimeout(
     String fromPid, String fromBranch, String fromVersion, String queueId, long duration)
     throws BpmScriptException {
   InvocationMessage message = new InvocationMessage();
   message.setArgs(duration);
   message.setReplyTo(replyTo);
   message.setCorrelationId(
       correlationIdFormatter.format(fromPid, fromBranch, fromVersion, queueId));
   sender.send(timeoutAddress, message);
 }