View Javadoc

1   /*
2    * $Id: ObjectToByteArray.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transformers.simple;
12  
13  import org.mule.umo.UMOEventContext;
14  import org.mule.umo.transformer.TransformerException;
15  
16  /**
17   * <code>ObjectToByteArray</code> converts serilaizable object to a byte array but
18   * treats <code>java.lang.String</code> differently by converting to bytes using
19   * the <code>String.getBytrs()</code> method.
20   */
21  public class ObjectToByteArray extends SerializableToByteArray
22  {
23  
24      public ObjectToByteArray()
25      {
26          this.registerSourceType(Object.class);
27      }
28  
29      // @Override
30      public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException
31      {
32          if (src instanceof String)
33          {
34              try
35              {
36                  return src.toString().getBytes(encoding);
37              }
38              catch (Exception e)
39              {
40                  throw new TransformerException(this, e);
41              }
42          }
43          else
44          {
45              return super.transform(src, encoding, context);
46          }
47      }
48  
49  }