Пример #1
0
  @Test
  public void test_transform_interface_impl() {
    org.switchyard.transform.Transformer transformer =
        TransformerUtil.newTransformer(
            TestTransformer.class,
            JavaService.toMessageType(A.class),
            JavaService.toMessageType(B.class));

    Assert.assertTrue(transformer instanceof TestTransformer);
    Assert.assertTrue(transformer.transform(new A()) instanceof B);
  }
Пример #2
0
  @Test
  public void test_transform_anno_no_types_defined() {
    org.switchyard.transform.Transformer transformer =
        TransformerUtil.newTransformer(
            TestTransformer.class,
            JavaService.toMessageType(B.class),
            JavaService.toMessageType(A.class));

    Assert.assertTrue(!(transformer instanceof TestTransformer));
    Assert.assertTrue(transformer.transform(new B()) instanceof A);
  }
Пример #3
0
 @Override
 public int compare(T t1, T t2) {
   if (t1._javaType == t2._javaType) {
     return 0;
   } else if (t1._javaType.isAssignableFrom(t2._javaType)) {
     return 1;
   } else if (t2._javaType.isAssignableFrom(t1._javaType)) {
     return -1;
   } else {
     // Unrelated types.  This means there are branches in the inheritance options,
     // which means there are multiple possibilities, therefore it's not uniquely resolvable.
     throw new MultipleFallbacks(
         "Multiple possible fallback types '"
             + JavaService.toMessageType(t1._javaType)
             + "' and '"
             + JavaService.toMessageType(t2._javaType)
             + "'.");
   }
 }
Пример #4
0
  @Test
  public void test_listTransformations() {
    List<TransformerTypes> transformTypes =
        TransformerUtil.listTransformations(TestTransformer.class);

    Assert.assertEquals(5, transformTypes.size());
    Assert.assertEquals(QName.valueOf("X"), transformTypes.get(0).getFrom());
    Assert.assertEquals(QName.valueOf("Y"), transformTypes.get(0).getTo());
    Assert.assertEquals(QName.valueOf("Z"), transformTypes.get(1).getFrom());
    Assert.assertEquals(JavaService.toMessageType(A.class), transformTypes.get(1).getTo());
    Assert.assertEquals(JavaService.toMessageType(A.class), transformTypes.get(2).getFrom());
    Assert.assertEquals(JavaService.toMessageType(B.class), transformTypes.get(2).getTo());
    Assert.assertEquals(JavaService.toMessageType(B.class), transformTypes.get(3).getFrom());
    Assert.assertEquals(QName.valueOf("Z"), transformTypes.get(3).getTo());
    Assert.assertEquals(JavaService.toMessageType(B.class), transformTypes.get(4).getFrom());
    Assert.assertEquals(JavaService.toMessageType(A.class), transformTypes.get(4).getTo());
  }
Пример #5
0
 public TestTransformer() {
   super(JavaService.toMessageType(A.class), JavaService.toMessageType(B.class));
 }
Пример #6
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);
  }
Пример #7
0
  /**
   * Send a message hash to the specified operation.
   *
   * @param operationName The operation name.
   * @param rubyHash The message hash.
   * @return The response object if an IN_OUT operation was invoked, otherwise null.
   * @throws Throwable An exception occurred while invoking the target operation.
   */
  public Object send(String operationName, RubyHash rubyHash) throws Throwable {
    if (operationName == null) {
      throw new IllegalArgumentException("null 'operationName' argument.");
    }
    if (rubyHash == null) {
      throw new IllegalArgumentException("null 'rubyHash' argument.");
    }

    ServiceOperation operation = _serviceReference.getInterface().getOperation(operationName);

    if (operation == null) {
      throw new IllegalArgumentException(
          "Unknown operation name '"
              + operationName
              + "' on Service '"
              + _serviceReference.getName()
              + "'.");
    }

    // Clone the RubyHash to convert it to a normal Map based graph.  This makes it possible
    // to more safely transport the payload data out of the ruby app via a SwitchYard Exchange...
    Map<String, Object> payload = deepClone(rubyHash);

    // Create the exchange contract...
    BaseExchangeContract exchangeContract = new BaseExchangeContract(operation);

    // Set the input type...
    exchangeContract
        .getInvokerInvocationMetaData()
        .setInputType(JavaService.toMessageType(payload.getClass()));

    if (operation.getExchangePattern() == ExchangePattern.IN_OUT) {
      final BlockingQueue<Exchange> responseQueue = new ArrayBlockingQueue<Exchange>(1);

      AtomicReference<ExchangeHandler> responseExchangeHandler =
          new AtomicReference<ExchangeHandler>(
              new ExchangeHandler() {
                public void handleMessage(Exchange exchange) throws HandlerException {
                  responseQueue.offer(exchange);
                }

                public void handleFault(Exchange exchange) {
                  responseQueue.offer(exchange);
                }
              });

      Exchange exchange =
          _serviceReference.createExchange(exchangeContract, responseExchangeHandler.get());
      Message message = exchange.createMessage().setContent(payload);

      exchange.send(message);
      Exchange exchangeOut = null;
      try {
        exchangeOut = responseQueue.take();
      } catch (InterruptedException e) {
        throw new SwitchYardException(
            "Operation '"
                + operationName
                + "' on Service '"
                + _serviceReference.getName()
                + "' interrupted.",
            e);
      }

      if (exchangeOut.getState() == ExchangeState.OK) {
        return exchangeOut.getMessage().getContent();
      } else {
        Object failureObj = exchangeOut.getMessage().getContent();
        if (failureObj instanceof Throwable) {
          if (failureObj instanceof InvocationTargetException) {
            throw ((Throwable) failureObj).getCause();
          } else {
            throw (Throwable) failureObj;
          }
        } else {
          throw new SwitchYardException(
              "Service invocation failure.  Service '"
                  + _serviceReference.getName()
                  + "', operation '"
                  + operationName
                  + "'.  Non Throwable failure message payload: "
                  + failureObj);
        }
      }
    } else {
      Exchange exchange = _serviceReference.createExchange(exchangeContract);
      Message message = exchange.createMessage().setContent(payload);

      exchange.send(message);
    }

    return null;
  }