View Javadoc

1   /*
2    * $Id: SerializableToByteArray.java 21584 2011-03-18 12:17:31Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transformer.simple;
12  
13  import org.mule.api.transformer.DiscoverableTransformer;
14  import org.mule.api.transformer.TransformerException;
15  import org.mule.transformer.AbstractTransformer;
16  import org.mule.transformer.types.DataTypeFactory;
17  import org.mule.util.SerializationUtils;
18  
19  import java.io.Serializable;
20  
21  /**
22   * <code>SerializableToByteArray</code> converts a serializable object or a String
23   * to a byte array. If <code>MuleMessage</code> is configured as a source type on this
24   * transformer by calling <code>setAcceptMuleMessage(true)</code> then the MuleMessage
25   * will be serialised. This is useful for transports such as TCP where the message
26   * headers would normally be lost.
27   */
28  public class SerializableToByteArray extends AbstractTransformer implements DiscoverableTransformer
29  {
30      private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING;
31  
32      public SerializableToByteArray()
33      {
34          this.registerSourceType(DataTypeFactory.create(Serializable.class));
35          this.setReturnDataType(DataTypeFactory.BYTE_ARRAY);
36      }
37  
38      public boolean isAcceptMuleMessage()
39      {
40          return this.isSourceDataTypeSupported(DataTypeFactory.MULE_MESSAGE, true);
41      }
42  
43      public void setAcceptMuleMessage(boolean value)
44      {
45          if (value)
46          {
47              this.registerSourceType(DataTypeFactory.MULE_MESSAGE);
48          }
49          else
50          {
51              this.unregisterSourceType(DataTypeFactory.MULE_MESSAGE);
52          }
53      }
54  
55      @Override
56      public Object doTransform(Object src, String outputEncoding) throws TransformerException
57      {
58          /*
59           * If the MuleMessage source type has been registered then we can assume that
60           * the whole message is to be serialised, not just the payload. This can be
61           * useful for protocols such as tcp where the protocol does not support
62           * headers and the whole message needs to be serialized.
63           */
64  
65          try
66          {
67              return SerializationUtils.serialize((Serializable) src);
68          }
69          catch (Exception e)
70          {
71              throw new TransformerException(this, e);
72          }
73      }
74  
75      public int getPriorityWeighting()
76      {
77          return priorityWeighting;
78      }
79  
80      public void setPriorityWeighting(int priorityWeighting)
81      {
82          this.priorityWeighting = priorityWeighting;
83      }
84  }