Coverage Report - org.mule.transformer.simple.MessagePropertiesTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
MessagePropertiesTransformer
0%
0/64
0%
0/44
2.636
 
 1  
 /*
 2  
  * $Id: MessagePropertiesTransformer.java 11536 2008-04-08 18:00:40Z tcarlson $
 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.MuleMessage;
 14  
 import org.mule.api.transformer.TransformerException;
 15  
 import org.mule.transformer.AbstractMessageAwareTransformer;
 16  
 
 17  
 import java.text.MessageFormat;
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashMap;
 20  
 import java.util.Iterator;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 import java.util.Set;
 24  
 
 25  
 /**
 26  
  * A configurable message transformer that allows users to add, overwrite and delete
 27  
  * properties on the current message. Users can set a {@link List} of
 28  
  * 'deleteProperties' names to remove from the message and can also set a {@link Map}
 29  
  * of 'addProperties' that will be added to the message and possibly overwrite
 30  
  * existing properties with the same name. <p/> If {@link #overwrite} is set to
 31  
  * <code>false</code>, and a property exists on the message (even if the value is
 32  
  * <code>null</code>, it will be left intact. The transformer then acts as a more
 33  
  * gentle 'enricher'. The default setting is <code>true</code>.
 34  
  */
 35  
 public class MessagePropertiesTransformer extends AbstractMessageAwareTransformer
 36  
 {
 37  0
     private List deleteProperties = null;
 38  0
     private Map addProperties = null;
 39  
     /** the properties map containing rename mappings for message properties */
 40  
     private Map renameProperties;
 41  0
     private boolean overwrite = true;
 42  
 
 43  
     public MessagePropertiesTransformer()
 44  0
     {
 45  0
         registerSourceType(Object.class);
 46  0
         setReturnClass(Object.class);
 47  0
     }
 48  
 
 49  
     // @Override
 50  
     public Object clone() throws CloneNotSupportedException
 51  
     {
 52  0
         MessagePropertiesTransformer clone = (MessagePropertiesTransformer)super.clone();
 53  
 
 54  0
         if (deleteProperties != null)
 55  
         {
 56  0
             clone.setDeleteProperties(new ArrayList(deleteProperties));
 57  
         }
 58  
 
 59  0
         if (addProperties != null)
 60  
         {
 61  0
             clone.setAddProperties(new HashMap(addProperties));
 62  
         }
 63  
 
 64  0
         if (renameProperties != null)
 65  
         {
 66  0
             clone.setRenameProperties(new HashMap(renameProperties));
 67  
         }
 68  0
         return clone;
 69  
     }
 70  
 
 71  
     public Object transform(MuleMessage message, String outputEncoding) throws TransformerException
 72  
     {
 73  0
         if (deleteProperties != null && deleteProperties.size() > 0)
 74  
         {
 75  0
             for (Iterator iterator = deleteProperties.iterator(); iterator.hasNext();)
 76  
             {
 77  0
                 message.removeProperty(iterator.next().toString());
 78  
             }
 79  
         }
 80  
 
 81  0
         if (addProperties != null && addProperties.size() > 0)
 82  
         {
 83  0
             final Set propertyNames = message.getPropertyNames();
 84  0
             for (Iterator iterator = addProperties.entrySet().iterator(); iterator.hasNext();)
 85  
             {
 86  0
                 Map.Entry entry = (Map.Entry)iterator.next();
 87  0
                 if (entry.getKey() == null)
 88  
                 {
 89  0
                     logger.error("Setting Null property keys is not supported, this entry is being ignored");
 90  
                 }
 91  
                 else
 92  
                 {
 93  0
                     final String key = entry.getKey().toString();
 94  
 
 95  0
                     final Object value = entry.getValue();
 96  0
                     if (overwrite)
 97  
                     {
 98  0
                         if (logger.isDebugEnabled())
 99  
                         {
 100  0
                             if (!propertyNames.contains(key))
 101  
                             {
 102  0
                                 logger.debug("Overwriting message property " + key);
 103  
                             }
 104  
                         }
 105  0
                         message.setProperty(key, value);
 106  
                     }
 107  
                     else
 108  
                     {
 109  0
                         if (propertyNames.contains(key))
 110  
                         {
 111  0
                             if (logger.isDebugEnabled())
 112  
                             {
 113  0
                                 logger.debug(MessageFormat.format(
 114  
                                     "Message already contains the property and overwrite is false, skipping: key={0}, value={1}",
 115  
                                     new Object[]{key, value}));
 116  
                             }
 117  
                         }
 118  
                     }
 119  
                 }
 120  0
             }
 121  
         }
 122  
 
 123  
         /* perform renaming transformation */
 124  0
         if (this.renameProperties != null && this.renameProperties.size() > 0)
 125  
         {
 126  0
             final Set propertyNames = message.getPropertyNames();
 127  0
             for (Iterator iterator = this.renameProperties.entrySet().iterator(); iterator.hasNext();)
 128  
             {
 129  0
                 Map.Entry entry = (Map.Entry)iterator.next();
 130  
 
 131  0
                 if (entry.getKey() == null)
 132  
                 {
 133  0
                     logger.error("Setting Null property keys is not supported, this entry is being ignored");
 134  
                 }
 135  
                 else
 136  
                 {
 137  0
                     final String key = entry.getKey().toString();
 138  0
                     final String value = (String)entry.getValue();
 139  
 
 140  0
                     if (value == null)
 141  
                     {
 142  0
                         logger.error("Setting Null property values for renameProperties is not supported, this entry is being ignored");
 143  
                     }
 144  
                     else
 145  
                     {
 146  
 
 147  
                         /* log transformation */
 148  0
                         if (logger.isDebugEnabled() && !propertyNames.contains(key))
 149  
                         {
 150  0
                             logger.debug("renaming message property " + key + " to " + value);
 151  
                         }
 152  
 
 153  
                         /*
 154  
                          * store current value of the property. then remove key and
 155  
                          * store value under new key
 156  
                          */
 157  0
                         Object propValue = message.getProperty(key);
 158  0
                         message.removeProperty(key);
 159  0
                         message.setProperty(value, propValue);
 160  
                     }
 161  
                 }
 162  0
             }
 163  
         }
 164  0
         return message;
 165  
     }
 166  
 
 167  
     public List getDeleteProperties()
 168  
     {
 169  0
         return deleteProperties;
 170  
     }
 171  
 
 172  
     public void setDeleteProperties(List deleteProperties)
 173  
     {
 174  0
         this.deleteProperties = deleteProperties;
 175  0
     }
 176  
 
 177  
     public Map getAddProperties()
 178  
     {
 179  0
         return addProperties;
 180  
     }
 181  
 
 182  
     public void setAddProperties(Map addProperties)
 183  
     {
 184  0
         this.addProperties = addProperties;
 185  0
     }
 186  
 
 187  
     /**
 188  
      * @return the renameProperties
 189  
      */
 190  
     public Map getRenameProperties()
 191  
     {
 192  0
         return this.renameProperties;
 193  
     }
 194  
 
 195  
     /**
 196  
      * @param renameProperties the renameProperties to set
 197  
      */
 198  
     public void setRenameProperties(Map renameProperties)
 199  
     {
 200  0
         this.renameProperties = renameProperties;
 201  0
     }
 202  
 
 203  
     public boolean isOverwrite()
 204  
     {
 205  0
         return overwrite;
 206  
     }
 207  
 
 208  
     public void setOverwrite(final boolean overwrite)
 209  
     {
 210  0
         this.overwrite = overwrite;
 211  0
     }
 212  
 }