Пример #1
0
  // @Override
  public void testTransformerConfig() {
    // first of all test generic transformer configuration
    super.testTransformerConfig();

    Transformer t = muleContext.getRegistry().lookupTransformer("TestCompressionTransformer");
    assertNotNull(t);
    assertTrue(t instanceof TestCompressionTransformer);

    // This will only work with the MuleXml Builder other implementations
    // will have to set this proerty manually or mimic Mules behaviour
    assertEquals(
        "this was set from the manager properties!",
        ((TestCompressionTransformer) t).getBeanProperty1());
    assertEquals(12, ((TestCompressionTransformer) t).getBeanProperty2());

    assertEquals(t.getReturnClass(), java.lang.String.class);

    t = muleContext.getRegistry().lookupTransformer("TestTransformer");
    assertNotNull(t);
    assertEquals(t.getReturnClass(), byte[].class);
  }
Пример #2
0
  public void write(OutputStream out, Object o, String encoding) throws MuleException {
    if (outboundTransformer == null) {
      throw new IllegalArgumentException(
          CoreMessages.objectIsNull("outboundTransformer").getMessage());
    }
    try {
      Class returnClass = outboundTransformer.getReturnClass();
      if (returnClass == null) {
        logger.warn(
            "No return class was set on transformer: "
                + outboundTransformer
                + ". Attempting to work out how to treat the result transformation");

        Object result = outboundTransformer.transform(o);

        byte[] bytes;
        if (result instanceof byte[]) {
          bytes = (byte[]) result;
        } else {
          bytes = result.toString().getBytes(encoding);
        }

        out.write(bytes);
      } else if (returnClass.equals(byte[].class)) {
        byte[] b = (byte[]) outboundTransformer.transform(o);
        out.write(b);
      } else if (returnClass.equals(String.class)) {
        String s = (String) outboundTransformer.transform(o);
        out.write(s.getBytes(encoding));
      } else {
        throw new TransformerException(CoreMessages.transformFailedFrom(o.getClass()));
      }
    } catch (IOException e) {
      throw new TransformerException(CoreMessages.transformFailedFrom(o.getClass()), e);
    }
  }