Coverage Report - org.mule.transformer.simple.SerializableToByteArray
 
Classes in this File Line Coverage Branch Coverage Complexity
SerializableToByteArray
0%
0/16
0%
0/2
0
 
 1  
 /*
 2  
  * $Id: SerializableToByteArray.java 19250 2010-08-30 16:53:14Z dirk.olmes $
 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  
 
 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>setAcceptMuleMessage(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  0
     private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING;
 32  
 
 33  
     public SerializableToByteArray()
 34  0
     {
 35  0
         this.registerSourceType(DataTypeFactory.create(Serializable.class));
 36  0
         this.setReturnDataType(DataTypeFactory.BYTE_ARRAY);
 37  0
     }
 38  
 
 39  
     public boolean isAcceptMuleMessage()
 40  
     {
 41  0
         return this.isSourceDataTypeSupported(DataTypeFactory.MULE_MESSAGE, true);
 42  
     }
 43  
 
 44  
     public void setAcceptMuleMessage(boolean value)
 45  
     {
 46  0
         if (value)
 47  
         {
 48  0
             this.registerSourceType(DataTypeFactory.MULE_MESSAGE);
 49  
         }
 50  
         else
 51  
         {
 52  0
             this.unregisterSourceType(DataTypeFactory.MULE_MESSAGE);
 53  
         }
 54  0
     }
 55  
 
 56  
     @Override
 57  
     public Object doTransform(Object src, String outputEncoding) 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  0
             return SerializationUtils.serialize((Serializable) src);
 69  
         }
 70  0
         catch (Exception e)
 71  
         {
 72  0
             throw new TransformerException(this, e);
 73  
         }
 74  
     }
 75  
 
 76  
     public int getPriorityWeighting()
 77  
     {
 78  0
         return priorityWeighting;
 79  
     }
 80  
 
 81  
     public void setPriorityWeighting(int priorityWeighting)
 82  
     {
 83  0
         this.priorityWeighting = priorityWeighting;
 84  0
     }
 85  
 }