class AutoConvertMessageTranslator extends MessageTypeTranslatorImp {

  public Message translate(AdaptrisMessage msg) throws JMSException {
    Message result = null;
    MessageTypeTranslator mt = null;

    boolean convertingBackToOriginal = false;
    try {
      if(convertBackToConsumedType() && (!StringUtils.isEmpty(msg.getMetadataValue(CONSUMED_MESSAGE_TYPE_KEY)))) {
        SupportedMessageType messageType = null;
        try {
          messageType = SupportedMessageType.valueOf(msg.getMetadataValue(CONSUMED_MESSAGE_TYPE_KEY));
          mt = messageType.create(this);
          convertingBackToOriginal = true;
        } catch (IllegalArgumentException ex) {
          log.warn("Cannot convert to type: {}", msg.getMetadataValue(CONSUMED_MESSAGE_TYPE_KEY));
          for (SupportedMessageType mti : SupportedMessageType.values()) {
            if (mti.name().equalsIgnoreCase(getJmsOutputType())) {
              mt = mti.create(this);
              break;
            }
          }
        }
      } else {
        for (SupportedMessageType mti : SupportedMessageType.values()) {
          if (mti.name().equalsIgnoreCase(getJmsOutputType())) {
            mt = mti.create(this);
            break;
          }
        }
      }
      if (mt != null) {
        if((convertingBackToOriginal) && (removeOriginalMessageTypeKey()))
          msg.removeMessageHeader(CONSUMED_MESSAGE_TYPE_KEY);
        start(mt);
        result = mt.translate(msg);
      }
      else {
        mt = SupportedMessageType.createFallback(this);
        start(mt);
        result = mt.translate(msg);
      }
    }
    finally {
      stop(mt);
    }
    return result;
  }

  public  AutoConvertMessageTranslator();

}