Coverage Report - org.mule.transformer.simple.ObjectToOutputHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectToOutputHandler
0%
0/20
0%
0/8
0
ObjectToOutputHandler$1
0%
0/3
N/A
0
ObjectToOutputHandler$2
0%
0/3
N/A
0
ObjectToOutputHandler$3
0%
0/6
N/A
0
ObjectToOutputHandler$4
0%
0/3
N/A
0
 
 1  
 /*
 2  
  * $Id: ObjectToOutputHandler.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.MuleEvent;
 14  
 import org.mule.api.transformer.DiscoverableTransformer;
 15  
 import org.mule.api.transformer.TransformerException;
 16  
 import org.mule.api.transport.OutputHandler;
 17  
 import org.mule.config.i18n.MessageFactory;
 18  
 import org.mule.transformer.AbstractTransformer;
 19  
 import org.mule.transformer.types.DataTypeFactory;
 20  
 import org.mule.util.IOUtils;
 21  
 
 22  
 import java.io.IOException;
 23  
 import java.io.InputStream;
 24  
 import java.io.OutputStream;
 25  
 import java.io.Serializable;
 26  
 
 27  
 import org.apache.commons.lang.SerializationUtils;
 28  
 
 29  
 /** <code>ObjectToOutputHandler</code> converts a byte array into a String. */
 30  
 public class ObjectToOutputHandler extends AbstractTransformer implements DiscoverableTransformer
 31  
 {
 32  
 
 33  
     /** Give core transformers a slighty higher priority */
 34  0
     private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING + 1;
 35  
 
 36  
     public ObjectToOutputHandler()
 37  0
     {
 38  0
         registerSourceType(DataTypeFactory.BYTE_ARRAY);
 39  0
         registerSourceType(DataTypeFactory.STRING);
 40  0
         registerSourceType(DataTypeFactory.INPUT_STREAM);
 41  0
         registerSourceType(DataTypeFactory.create(Serializable.class));
 42  0
         setReturnDataType(DataTypeFactory.create(OutputHandler.class));
 43  0
     }
 44  
 
 45  
     @Override
 46  
     public Object doTransform(final Object src, final String encoding) throws TransformerException
 47  
     {
 48  0
         if (src instanceof String)
 49  
         {
 50  0
             return new OutputHandler()
 51  0
             {
 52  
                 public void write(MuleEvent event, OutputStream out) throws IOException
 53  
                 {
 54  0
                     out.write(((String) src).getBytes(encoding));
 55  0
                 }
 56  
             };
 57  
         }
 58  0
         else if (src instanceof byte[])
 59  
         {
 60  0
             return new OutputHandler()
 61  0
             {
 62  
                 public void write(MuleEvent event, OutputStream out) throws IOException
 63  
                 {
 64  0
                     out.write((byte[]) src);
 65  0
                 }
 66  
             };
 67  
         }
 68  0
         else if (src instanceof InputStream)
 69  
         {
 70  0
             return new OutputHandler()
 71  0
             {
 72  
                 public void write(MuleEvent event, OutputStream out) throws IOException
 73  
                 {
 74  0
                     InputStream is = (InputStream) src;
 75  
                     try
 76  
                     {
 77  0
                         IOUtils.copyLarge(is, out);
 78  
                     }
 79  
                     finally
 80  
                     {
 81  0
                         is.close();
 82  0
                     }
 83  0
                 }
 84  
             };
 85  
         }
 86  0
         else if (src instanceof Serializable)
 87  
         {
 88  0
             return new OutputHandler()
 89  0
             {
 90  
                 public void write(MuleEvent event, OutputStream out) throws IOException
 91  
                 {
 92  0
                     SerializationUtils.serialize((Serializable) src, out);
 93  0
                 }
 94  
             };
 95  
         }
 96  
         else
 97  
         {
 98  0
             throw new TransformerException(MessageFactory
 99  
                     .createStaticMessage("Unable to convert " + src.getClass() + " to OutputHandler."));
 100  
         }
 101  
     }
 102  
 
 103  
     public int getPriorityWeighting()
 104  
     {
 105  0
         return priorityWeighting;
 106  
     }
 107  
 
 108  
     public void setPriorityWeighting(int priorityWeighting)
 109  
     {
 110  0
         this.priorityWeighting = priorityWeighting;
 111  0
     }
 112  
 }