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