Coverage Report - org.mule.providers.streaming.AbstractStreamingTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractStreamingTransformer
0%
0/45
0%
0/16
1.562
 
 1  
 /*
 2  
  * $Id: AbstractStreamingTransformer.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 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.providers.streaming;
 12  
 
 13  
 import org.mule.MuleManager;
 14  
 import org.mule.umo.endpoint.UMOImmutableEndpoint;
 15  
 import org.mule.umo.lifecycle.InitialisationException;
 16  
 import org.mule.umo.transformer.TransformerException;
 17  
 import org.mule.umo.transformer.UMOStreamingTransformer;
 18  
 import org.mule.util.ClassUtils;
 19  
 
 20  
 import java.io.InputStream;
 21  
 import java.io.OutputStream;
 22  
 
 23  
 import org.apache.commons.beanutils.BeanUtils;
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 
 27  
 /**
 28  
  * TODO
 29  
  */
 30  
 public abstract class AbstractStreamingTransformer implements UMOStreamingTransformer
 31  
 {
 32  
     /**
 33  
      * logger used by this class
 34  
      */
 35  0
     protected final Log logger = LogFactory.getLog(getClass());
 36  
 
 37  
     /**
 38  
      * The name that identifies this transformer. If none is set the class name of
 39  
      * the transformer is used
 40  
      */
 41  0
     protected String name = null;
 42  
 
 43  
     /**
 44  
      * The endpoint that this transformer instance is configured on
 45  
      */
 46  0
     protected UMOImmutableEndpoint endpoint = null;
 47  
 
 48  
     /**
 49  
      * This is the following transformer in the chain of transformers.
 50  
      */
 51  
     protected UMOStreamingTransformer nextTransformer;
 52  
 
 53  
     /**
 54  
      * Determines whether the transformer will throw an exception if the message
 55  
      * passed is is not supported or the return tye is incorrect
 56  
      */
 57  0
     private boolean ignoreBadInput = false;
 58  
 
 59  
     /**
 60  
      * default constructor required for discovery
 61  
      */
 62  
     public AbstractStreamingTransformer()
 63  0
     {
 64  0
         name = generateTransformerName();
 65  0
     }
 66  
 
 67  
     /**
 68  
      * @return transformer name
 69  
      */
 70  
     public String getName()
 71  
     {
 72  0
         if (name == null)
 73  
         {
 74  0
             setName(ClassUtils.getSimpleName(getClass()));
 75  
         }
 76  0
         return name;
 77  
     }
 78  
 
 79  
     /**
 80  
      * @param string
 81  
      */
 82  
     public void setName(String string)
 83  
     {
 84  0
         logger.debug("Setting transformer name to: " + name);
 85  0
         name = string;
 86  0
     }
 87  
 
 88  
     /**
 89  
      * Transforms the object.
 90  
      * 
 91  
      * @param src The source object to transform.
 92  
      * @return The transformed object
 93  
      */
 94  
     public final Object transform(InputStream src, OutputStream dest, String encoding)
 95  
         throws TransformerException
 96  
     {
 97  0
         if (encoding == null && endpoint != null)
 98  
         {
 99  0
             encoding = endpoint.getEncoding();
 100  
         }
 101  
 
 102  
         // last resort
 103  0
         if (encoding == null)
 104  
         {
 105  0
             encoding = MuleManager.getConfiguration().getEncoding();
 106  
         }
 107  
 
 108  0
         if (logger.isDebugEnabled())
 109  
         {
 110  0
             logger.debug("Applying transformer " + getName() + " (" + getClass().getName() + ")");
 111  0
             logger.debug("Object before transform");
 112  
         }
 113  
 
 114  
         // TODO we should have a pipeline of transformers that we just 'execute'
 115  
         // Object result = doTransform(src, dest, encoding);
 116  
         // if (result == null)
 117  
         // {
 118  
         // result = NullPayload.getInstance();
 119  
         // }
 120  
         //
 121  
         // if (logger.isDebugEnabled())
 122  
         // {
 123  
         // logger.debug("Object after transform");
 124  
         // }
 125  
         //
 126  
         // if (nextTransformer != null)
 127  
         // {
 128  
         // logger.debug("Following transformer in the chain is " +
 129  
         // nextTransformer.getName() + " ("
 130  
         // + nextTransformer.getClass().getName() + ")");
 131  
         // result = nextTransformer.transform(result);
 132  
         // }
 133  
         //
 134  
         // return result;
 135  0
         return null;
 136  
     }
 137  
 
 138  
     public UMOImmutableEndpoint getEndpoint()
 139  
     {
 140  0
         return endpoint;
 141  
     }
 142  
 
 143  
     /*
 144  
      * (non-Javadoc)
 145  
      * 
 146  
      * @see org.mule.umo.transformer.UMOTransformer#setConnector(org.mule.umo.provider.UMOConnector)
 147  
      */
 148  
     public void setEndpoint(UMOImmutableEndpoint endpoint)
 149  
     {
 150  0
         this.endpoint = endpoint;
 151  0
         UMOStreamingTransformer trans = nextTransformer;
 152  0
         while (trans != null && endpoint != null)
 153  
         {
 154  0
             trans.setEndpoint(endpoint);
 155  0
             trans = trans.getNextTransformer();
 156  
         }
 157  0
     }
 158  
 
 159  
     protected abstract Object doTransform(InputStream src, OutputStream dest, String encoding)
 160  
         throws TransformerException;
 161  
 
 162  
     /*
 163  
      * (non-Javadoc)
 164  
      * 
 165  
      * @see org.mule.umo.transformer.UMOTransformer#getNextTransformer()
 166  
      */
 167  
     public UMOStreamingTransformer getNextTransformer()
 168  
     {
 169  0
         return nextTransformer;
 170  
     }
 171  
 
 172  
     /*
 173  
      * (non-Javadoc)
 174  
      * 
 175  
      * @see org.mule.umo.transformer.UMOTransformer#setNextTransformer(org.mule.umo.transformer.UMOTransformer)
 176  
      */
 177  
     public void setNextTransformer(UMOStreamingTransformer nextTransformer)
 178  
     {
 179  0
         this.nextTransformer = nextTransformer;
 180  0
     }
 181  
 
 182  
     /*
 183  
      * (non-Javadoc)
 184  
      * 
 185  
      * @see java.lang.Object#clone()
 186  
      */
 187  
     public Object clone() throws CloneNotSupportedException
 188  
     {
 189  
         try
 190  
         {
 191  
             // TODO see AbstractTransformer.clone() for why this is not enough
 192  0
             return BeanUtils.cloneBean(this);
 193  
         }
 194  0
         catch (Exception e)
 195  
         {
 196  0
             throw (CloneNotSupportedException) new CloneNotSupportedException("Failed to clone transformer: "
 197  
                             + e.getMessage()).initCause(e);
 198  
         }
 199  
     }
 200  
 
 201  
     /**
 202  
      * Template method were deriving classes can do any initialisation after the
 203  
      * properties have been set on this transformer
 204  
      * 
 205  
      * @throws org.mule.umo.lifecycle.InitialisationException
 206  
      */
 207  
     public void initialise() throws InitialisationException
 208  
     {
 209  
         // nothing to do
 210  0
     }
 211  
 
 212  
     protected String generateTransformerName()
 213  
     {
 214  0
         String name = getClass().getName();
 215  0
         int i = name.lastIndexOf(".");
 216  0
         if (i > -1)
 217  
         {
 218  0
             name = name.substring(i + 1);
 219  
         }
 220  0
         return name;
 221  
     }
 222  
 
 223  
     public boolean isIgnoreBadInput()
 224  
     {
 225  0
         return ignoreBadInput;
 226  
     }
 227  
 
 228  
     public void setIgnoreBadInput(boolean ignoreBadInput)
 229  
     {
 230  0
         this.ignoreBadInput = ignoreBadInput;
 231  0
     }
 232  
 
 233  
     public String toString()
 234  
     {
 235  0
         return "StreamingTransformer{" + "name='" + name + "'" + ", ignoreBadInput=" + ignoreBadInput + "}";
 236  
     }
 237  
 
 238  
     public boolean isAcceptNull()
 239  
     {
 240  0
         return false;
 241  
     }
 242  
 
 243  
 }