private String toDescription(Transformer<?, ?> transformer) {
   return transformer.getClass().getName()
       + "("
       + transformer.getFrom()
       + ", "
       + transformer.getTo()
       + ")";
 }
Exemple #2
0
  @Test
  public void test_transform_anno_types_defined() {
    org.switchyard.transform.Transformer transformer =
        TransformerUtil.newTransformer(
            TestTransformer.class, QName.valueOf("X"), QName.valueOf("Y"));

    Assert.assertTrue(!(transformer instanceof TestTransformer));
    Assert.assertEquals("Y", transformer.transform("X"));
  }
Exemple #3
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);
  }
Exemple #4
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);
  }
  /**
   * Register a set of transformers in the transform registry associated with this deployment.
   *
   * @param transforms The transforms model.
   * @param failOnError false to eat duplicate exceptions
   * @throws DuplicateTransformerException an existing transformer has already been registered for
   *     the from and to types
   */
  public void registerTransformers(TransformsModel transforms, boolean failOnError)
      throws DuplicateTransformerException {
    if (transforms == null) {
      return;
    }

    try {
      for (TransformModel transformModel : transforms.getTransforms()) {
        Collection<Transformer<?, ?>> transformers = newTransformers(transformModel);
        transformerLoop:
        for (Transformer<?, ?> transformer : transformers) {
          if (_transformerRegistry.hasTransformer(transformer.getFrom(), transformer.getTo())) {
            Transformer<?, ?> registeredTransformer =
                _transformerRegistry.getTransformer(transformer.getFrom(), transformer.getTo());
            boolean test = false;
            testLoop:
            for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
              if (ste.getClassName().startsWith("org.switchyard.test.")) {
                test = true;
                break testLoop;
              }
            }
            String msg =
                "Failed to register Transformer '"
                    + toDescription(transformer)
                    + "'.  A Transformer for these types is already registered: '"
                    + toDescription(registeredTransformer)
                    + "'.";
            if (test) {
              _log.trace(msg);
            } else if (failOnError) {
              throw new DuplicateTransformerException(msg);
            } else {
              _log.debug(msg);
            }
            continue transformerLoop;
          }
          _log.debug(
              "Adding transformer =>"
                  + " From: "
                  + transformer.getFrom()
                  + ", To:"
                  + transformer.getTo());
          _transformerRegistry.addTransformer(transformer);
          _transformers.add(transformer);
        }
      }
    } catch (DuplicateTransformerException e) {
      throw e;
    } catch (RuntimeException e) {
      // If there was an exception for any reason... remove all Transformer instance that have
      // already been registered with the domain...
      unregisterTransformers();
      throw e;
    }
  }
Exemple #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);
  }
 @Override
 public BaseTransformerRegistry addTransformer(Transformer<?, ?> transformer) {
   _fallbackTransformers.clear();
   _transformers.put(new NameKey(transformer.getFrom(), transformer.getTo()), transformer);
   return this;
 }
 @Override
 public boolean removeTransformer(Transformer<?, ?> transformer) {
   _fallbackTransformers.clear();
   return _transformers.remove(new NameKey(transformer.getFrom(), transformer.getTo())) != null;
 }