View Javadoc

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