Coverage Report - org.mule.transformer.TransformerUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
TransformerUtils
34%
14/41
18%
4/22
2
TransformerUtils$1
100%
2/2
N/A
2
TransformerUtils$2
100%
2/2
N/A
2
TransformerUtils$3
100%
2/2
N/A
2
TransformerUtils$TransformerSource
N/A
N/A
2
 
 1  
 /*
 2  
  * $Id: TransformerUtils.java 11311 2008-03-10 20:15:57Z 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;
 12  
 
 13  
 import org.mule.RegistryContext;
 14  
 import org.mule.api.DefaultMuleException;
 15  
 import org.mule.api.MuleException;
 16  
 import org.mule.api.lifecycle.InitialisationException;
 17  
 import org.mule.api.transformer.Transformer;
 18  
 import org.mule.config.i18n.CoreMessages;
 19  
 import org.mule.transport.service.TransportFactoryException;
 20  
 import org.mule.transport.service.TransportServiceDescriptor;
 21  
 
 22  
 import java.util.Iterator;
 23  
 import java.util.LinkedList;
 24  
 import java.util.List;
 25  
 import java.util.StringTokenizer;
 26  
 
 27  
 import org.apache.commons.logging.Log;
 28  
 import org.apache.commons.logging.LogFactory;
 29  
 
 30  0
 public class TransformerUtils
 31  
 {
 32  
 
 33  
     public static final String COMMA = ",";
 34  
 
 35  2
     private static Log logger = LogFactory.getLog(AbstractTransformer.class);
 36  
 
 37  
     public static void initialiseAllTransformers(List transformers) throws InitialisationException
 38  
     {
 39  550
         if (transformers != null)
 40  
         {
 41  550
             Iterator transformer = transformers.iterator();
 42  1100
             while (transformer.hasNext())
 43  
             {
 44  550
                 ((Transformer) transformer.next()).initialise();
 45  
             }
 46  
         }
 47  550
     }
 48  
 
 49  
     public static String toString(List transformers)
 50  
     {
 51  0
         StringBuffer buffer = new StringBuffer();
 52  0
             Iterator transformer = transformers.iterator();
 53  0
             while (transformer.hasNext())
 54  
             {
 55  0
                 buffer.append(transformer.next().toString());
 56  0
                 if (transformer.hasNext())
 57  
                 {
 58  0
                     buffer.append(" -> ");
 59  
                 }
 60  
             }
 61  0
         return buffer.toString();
 62  
     }
 63  
 
 64  
     public static Transformer firstOrNull(List transformers)
 65  
     {
 66  0
         if (transformers != null && 0 != transformers.size())
 67  
         {
 68  0
             return (Transformer) transformers.get(0);
 69  
         }
 70  
         else
 71  
         {
 72  0
             return null;
 73  
         }
 74  
     }
 75  
 
 76  
     public static boolean isSourceTypeSupportedByFirst(List transformers, Class clazz)
 77  
     {
 78  0
         Transformer transformer = firstOrNull(transformers);
 79  0
         return null != transformer && transformer.isSourceTypeSupported(clazz);
 80  
     }
 81  
 
 82  0
     protected static interface TransformerSource
 83  
     {
 84  
         public List getTransformers() throws TransportFactoryException;
 85  
     }
 86  
 
 87  
     protected static List getTransformersFromSource(TransformerSource source)
 88  
     {
 89  
         try
 90  
         {
 91  550
             List transformers = source.getTransformers();
 92  550
             TransformerUtils.initialiseAllTransformers(transformers);
 93  550
             return transformers;
 94  
         }
 95  0
         catch (MuleException e)
 96  
         {
 97  0
             logger.debug(e.getMessage(), e);
 98  0
             return null;
 99  
         }
 100  
     }
 101  
 
 102  
     public static List getDefaultInboundTransformers(final TransportServiceDescriptor serviceDescriptor)
 103  
     {
 104  68
         return getTransformersFromSource(new TransformerSource()
 105  
         {
 106  68
             public List getTransformers() throws TransportFactoryException
 107  
             {
 108  68
                 return serviceDescriptor.createInboundTransformers();
 109  
             }
 110  
         });
 111  
     }
 112  
 
 113  
     public static List getDefaultResponseTransformers(final TransportServiceDescriptor serviceDescriptor)
 114  
     {
 115  68
         return getTransformersFromSource(new TransformerSource()
 116  
         {
 117  68
             public List getTransformers() throws TransportFactoryException
 118  
             {
 119  68
                 return serviceDescriptor.createResponseTransformers();
 120  
             }
 121  
         });
 122  
     }
 123  
 
 124  
     public static List getDefaultOutboundTransformers(final TransportServiceDescriptor serviceDescriptor)
 125  
     {
 126  414
         return getTransformersFromSource(new TransformerSource()
 127  
         {
 128  414
             public List getTransformers() throws TransportFactoryException
 129  
             {
 130  414
                 return serviceDescriptor.createOutboundTransformers();
 131  
             }
 132  
         });
 133  
     }
 134  
 
 135  
     /**
 136  
      * Builds a list of UMOTransformers.
 137  
      *
 138  
      * @param names - a list of transformers separated by commans
 139  
      * @return a list (possibly empty) of transformers or
 140  
      * {@link TransformerUtils#UNDEFINED} if the names list is null
 141  
      * @throws org.mule.api.DefaultMuleException
 142  
      */
 143  
     public static List getTransformers(String names) throws DefaultMuleException
 144  
     {
 145  968
         if (null != names)
 146  
         {
 147  0
             List transformers = new LinkedList();
 148  0
             StringTokenizer st = new StringTokenizer(names, COMMA);
 149  0
             while (st.hasMoreTokens())
 150  
             {
 151  0
                 String key = st.nextToken().trim();
 152  0
                 Transformer transformer = RegistryContext.getRegistry().lookupTransformer(key);
 153  
 
 154  0
                 if (transformer == null)
 155  
                 {
 156  0
                     throw new DefaultMuleException(CoreMessages.objectNotRegistered("Transformer", key));
 157  
                 }
 158  0
                 transformers.add(transformer);
 159  0
             }
 160  0
             return transformers;
 161  
         }
 162  
         else
 163  
         {
 164  968
             return null;
 165  
         }
 166  
     }
 167  
 
 168  
 }