Coverage Report - org.mule.transformers.simple.MessagePropertiesTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
MessagePropertiesTransformer
0%
0/43
0%
0/14
2.333
 
 1  
 /*
 2  
  * $Id: MessagePropertiesTransformer.java 7976 2007-08-21 14:26:13Z 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.transformers.simple;
 12  
 
 13  
 import org.mule.transformers.AbstractEventAwareTransformer;
 14  
 import org.mule.umo.UMOEventContext;
 15  
 import org.mule.umo.UMOMessage;
 16  
 import org.mule.umo.transformer.TransformerException;
 17  
 
 18  
 import java.text.MessageFormat;
 19  
 import java.util.ArrayList;
 20  
 import java.util.HashMap;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 import java.util.Set;
 25  
 
 26  
 /**
 27  
  * A configurable message transformer that allows users to add, overwrite and delete
 28  
  * properties on the current message. Users can set a {@link List} of
 29  
  * 'deleteProperties' names to remove from the message and can also set a {@link Map}
 30  
  * of 'addProperties' that will be added to the message and possibly overwrite
 31  
  * existing properties with the same name.
 32  
  * <p>
 33  
  * If {@link #overwrite} is set to <code>false</code>, and a property exists on
 34  
  * the message (even if the value is <code>null</code>, it will be left intact.
 35  
  * The transformer then acts as a more gentle 'enricher'. The default setting is
 36  
  * <code>true</code>.
 37  
  */
 38  
 public class MessagePropertiesTransformer extends AbstractEventAwareTransformer
 39  
 {
 40  0
     private List deleteProperties = null;
 41  0
     private Map addProperties = null;
 42  0
     private boolean overwrite = true;
 43  
 
 44  
     public MessagePropertiesTransformer()
 45  0
     {
 46  0
         registerSourceType(Object.class);
 47  0
         setReturnClass(Object.class);
 48  0
     }
 49  
 
 50  
     // @Override
 51  
     public Object clone() throws CloneNotSupportedException
 52  
     {
 53  0
         MessagePropertiesTransformer clone = (MessagePropertiesTransformer) super.clone();
 54  
 
 55  0
         if (deleteProperties != null)
 56  
         {
 57  0
             clone.setDeleteProperties(new ArrayList(deleteProperties));
 58  
         }
 59  
 
 60  0
         if (addProperties != null)
 61  
         {
 62  0
             clone.setAddProperties(new HashMap(addProperties));
 63  
         }
 64  
 
 65  0
         return clone;
 66  
     }
 67  
 
 68  
     public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException
 69  
     {
 70  0
         final UMOMessage message = context.getMessage();
 71  
 
 72  0
         if (deleteProperties != null && deleteProperties.size() > 0)
 73  
         {
 74  0
             for (Iterator iterator = deleteProperties.iterator(); iterator.hasNext();)
 75  
             {
 76  0
                 message.removeProperty(iterator.next().toString());
 77  
             }
 78  
         }
 79  
 
 80  0
         if (addProperties != null && addProperties.size() > 0)
 81  
         {
 82  0
             final Set propertyNames = message.getPropertyNames();
 83  0
             for (Iterator iterator = addProperties.entrySet().iterator(); iterator.hasNext();)
 84  
             {
 85  0
                 Map.Entry entry = (Map.Entry) iterator.next();
 86  0
                 if (entry.getKey() == null)
 87  
                 {
 88  0
                     logger.error("Setting Null property keys is not supported, this entry is being ignored");
 89  
                 }
 90  
                 else
 91  
                 {
 92  0
                     final String key = entry.getKey().toString();
 93  
 
 94  0
                     final Object value = entry.getValue();
 95  0
                     if (overwrite)
 96  
                     {
 97  0
                         if (logger.isDebugEnabled())
 98  
                         {
 99  0
                             if (!propertyNames.contains(key))
 100  
                             {
 101  0
                                 logger.debug("Overwriting message property " + key);
 102  
                             }
 103  
                         }
 104  0
                         message.setProperty(key, value);
 105  
                     }
 106  
                     else
 107  
                     {
 108  0
                         if (propertyNames.contains(key))
 109  
                         {
 110  0
                             if (logger.isDebugEnabled())
 111  
                             {
 112  0
                                 logger.debug(MessageFormat.format(
 113  
                                         "Message already contains the property and overwrite is false, skipping: key={0}, value={1}",
 114  
                                         new Object[] {key, value}));
 115  
                             }
 116  
                         }
 117  
                     }
 118  
                 }
 119  
             }
 120  
         }
 121  
 
 122  0
         return src;
 123  
     }
 124  
 
 125  
     public List getDeleteProperties()
 126  
     {
 127  0
         return deleteProperties;
 128  
     }
 129  
 
 130  
     public void setDeleteProperties(List deleteProperties)
 131  
     {
 132  0
         this.deleteProperties = deleteProperties;
 133  0
     }
 134  
 
 135  
     public Map getAddProperties()
 136  
     {
 137  0
         return addProperties;
 138  
     }
 139  
 
 140  
     public void setAddProperties(Map addProperties)
 141  
     {
 142  0
         this.addProperties = addProperties;
 143  0
     }
 144  
 
 145  
     public boolean isOverwrite()
 146  
     {
 147  0
         return overwrite;
 148  
     }
 149  
 
 150  
     public void setOverwrite(final boolean overwrite)
 151  
     {
 152  0
         this.overwrite = overwrite;
 153  0
     }
 154  
 }