private String toDescription(Transformer<?, ?> transformer) {
   return transformer.getClass().getName()
       + "("
       + transformer.getFrom()
       + ", "
       + transformer.getTo()
       + ")";
 }
Exemplo n.º 2
0
  @Override
  public <T> T getContent(final Class<T> type) {
    if (type == null) {
      throw new IllegalArgumentException("null 'type' argument.");
    }
    if (_content == null) {
      return null;
    }
    if (type.isInstance(_content)) {
      return type.cast(_content);
    }
    if (_transformerRegistry == null) {
      throw new SwitchYardException(
          "Cannot convert from '"
              + _content.getClass().getName()
              + "' to '"
              + type.getName()
              + "'.  No TransformRegistry available.");
    }

    QName fromType = JavaService.toMessageType(_content.getClass());
    QName toType = JavaService.toMessageType(type);
    Transformer transformer = _transformerRegistry.getTransformer(fromType, toType);
    if (transformer == null) {
      throw new SwitchYardException(
          "Cannot convert from '"
              + _content.getClass().getName()
              + "' to '"
              + type.getName()
              + "'.  No registered Transformer available for transforming from '"
              + fromType
              + "' to '"
              + toType
              + "'.  A Transformer must be registered.");
    }

    Object transformedContent = transformer.transform(_content);
    if (transformedContent == null) {
      throw new SwitchYardException(
          "Error converting from '"
              + _content.getClass().getName()
              + "' to '"
              + type.getName()
              + "'.  Transformer '"
              + transformer.getClass().getName()
              + "' returned null.");
    }
    if (!type.isInstance(transformedContent)) {
      throw new SwitchYardException(
          "Error converting from '"
              + _content.getClass().getName()
              + "' to '"
              + type.getName()
              + "'.  Transformer '"
              + transformer.getClass().getName()
              + "' returned incompatible type '"
              + transformedContent.getClass().getName()
              + "'.");
    }

    return type.cast(transformedContent);
  }