Coverage Report - org.mule.transport.MessagePropertiesContext
 
Classes in this File Line Coverage Branch Coverage Complexity
MessagePropertiesContext
42%
43/103
47%
16/34
1.63
 
 1  
 /*
 2  
  * $Id: MessagePropertiesContext.java 12370 2008-07-17 13:11:17Z 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  
 package org.mule.transport;
 11  
 
 12  
 import org.mule.api.transport.PropertyScope;
 13  
 import org.mule.util.MapUtils;
 14  
 import org.mule.util.ObjectUtils;
 15  
 
 16  
 import java.io.Serializable;
 17  
 import java.util.Collections;
 18  
 import java.util.HashMap;
 19  
 import java.util.Iterator;
 20  
 import java.util.Map;
 21  
 import java.util.Set;
 22  
 import java.util.TreeMap;
 23  
 import java.util.TreeSet;
 24  
 
 25  
 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
 26  
 
 27  
 /** TODO */
 28  
 public class MessagePropertiesContext implements Serializable
 29  
 {
 30  
     protected Map scopedMap;
 31  
     protected Set keySet;
 32  
 
 33  
     //TODO RM*: Map these to a RegistryMapView, currently in another branch :(
 34  
     //Treat Application properites as a special call
 35  1032
     Map applicationProperties = new ConcurrentHashMap(0);
 36  
 
 37  1032
     private PropertyScope defaultScope = PropertyScope.OUTBOUND;
 38  
 
 39  
     /**
 40  
      * if a property is not available in any ther scope, should we check the registry.
 41  
      * Note there will be performance implementations is this is enabled
 42  
      */
 43  1032
     private boolean fallbackToRegistry = false;
 44  
 
 45  
     public MessagePropertiesContext()
 46  1032
     {
 47  1032
         keySet = new TreeSet();
 48  1032
         scopedMap = new TreeMap(new PropertyScope.ScopeComparator());
 49  
 
 50  1032
         scopedMap.put(PropertyScope.INVOCATION, new HashMap(6));
 51  1032
         scopedMap.put(PropertyScope.INBOUND, new HashMap(6));
 52  1032
         scopedMap.put(PropertyScope.OUTBOUND, new HashMap(6));
 53  1032
         scopedMap.put(PropertyScope.SESSION, new HashMap(6));
 54  
 
 55  1032
     }
 56  
 
 57  
     public MessagePropertiesContext(PropertyScope defaultScope)
 58  
     {
 59  0
         this();
 60  
         //We can't set a read only scope as default
 61  0
         checkScopeForWriteAccess(defaultScope);
 62  0
         this.defaultScope = defaultScope;
 63  0
     }
 64  
 
 65  
     protected Map getScopedProperties(PropertyScope scope)
 66  
     {
 67  1024
         Map map = (Map) scopedMap.get(scope);
 68  1024
         if (map == null)
 69  
         {
 70  0
             throw new IllegalArgumentException("Scope not registered: " + scope);
 71  
         }
 72  1024
         return map;
 73  
     }
 74  
 
 75  
     void registerInvocationProperties(Map properties)
 76  
     {
 77  0
         if (properties != null)
 78  
         {
 79  0
             getScopedProperties(PropertyScope.INVOCATION).putAll(properties);
 80  0
             keySet.addAll(properties.keySet());
 81  
         }
 82  0
     }
 83  
 
 84  
     public PropertyScope getDefaultScope()
 85  
     {
 86  708
         return defaultScope;
 87  
     }
 88  
 
 89  
     void addInboundProperties(Map properties)
 90  
     {
 91  0
         if (properties != null)
 92  
         {
 93  0
             getScopedProperties(PropertyScope.INBOUND).putAll(properties);
 94  0
             keySet.addAll(properties.keySet());
 95  
         }
 96  0
     }
 97  
 
 98  
     void registerSessionProperties(Map properties)
 99  
     {
 100  0
         if (properties != null)
 101  
         {
 102  0
             getScopedProperties(PropertyScope.SESSION).putAll(properties);
 103  0
             keySet.addAll(properties.keySet());
 104  
         }
 105  0
     }
 106  
 
 107  
 
 108  
     public Object getProperty(String key)
 109  
     {
 110  856
         Object value = null;
 111  856
         for (Iterator iterator = scopedMap.values().iterator(); iterator.hasNext();)
 112  
         {
 113  2848
             Map props = (Map) iterator.next();
 114  2848
             value = props.get(key);
 115  2848
             if (value != null)
 116  
             {
 117  576
                 break;
 118  
             }
 119  2272
         }
 120  856
         if (value == null && fallbackToRegistry)
 121  
         {
 122  0
             value = applicationProperties.get(key);
 123  
         }
 124  856
         return value;
 125  
     }
 126  
 
 127  
     public Object getProperty(String key, PropertyScope scope)
 128  
     {
 129  0
         if (PropertyScope.APPLICATION.equals(scope))
 130  
         {
 131  0
             return applicationProperties.get(key);
 132  
         }
 133  
 
 134  0
         Map props = getScopedProperties(scope);
 135  0
         return props.get(key);
 136  
     }
 137  
 
 138  
     public void clearProperties()
 139  
     {
 140  0
         Map props = getScopedProperties(PropertyScope.INVOCATION);
 141  0
         keySet.removeAll(props.keySet());
 142  0
         props.clear();
 143  0
         props = getScopedProperties(PropertyScope.OUTBOUND);
 144  0
         keySet.removeAll(props.keySet());
 145  0
         props.clear();
 146  0
         props = getScopedProperties(PropertyScope.SESSION);
 147  0
         keySet.removeAll(props.keySet());
 148  0
         props.clear();
 149  
         //inbound are read Only
 150  0
     }
 151  
 
 152  
     public void clearProperties(PropertyScope scope)
 153  
     {
 154  0
         checkScopeForWriteAccess(scope);
 155  0
         Map props = getScopedProperties(scope);
 156  0
         keySet.removeAll(props.keySet());
 157  0
         props.clear();
 158  0
     }
 159  
 
 160  
     /**
 161  
      * Removes a property on this message
 162  
      *
 163  
      * @param key the property key to remove
 164  
      * @return the removed property value or null if the property did not exist
 165  
      */
 166  
     public Object removeProperty(String key)
 167  
     {
 168  94
         Object value = getScopedProperties(PropertyScope.INVOCATION).remove(key);
 169  94
         if (value == null)
 170  
         {
 171  94
             value = getScopedProperties(PropertyScope.OUTBOUND).remove(key);
 172  
         }
 173  94
         if (value == null)
 174  
         {
 175  74
             value = getScopedProperties(PropertyScope.SESSION).remove(key);
 176  
         }
 177  94
         if (value != null)
 178  
         {
 179  20
             keySet.remove(key);
 180  
         }
 181  94
         return value;
 182  
     }
 183  
 
 184  
     /**
 185  
      * Set a property on the message
 186  
      *
 187  
      * @param key   the key on which to associate the value
 188  
      * @param value the property value
 189  
      */
 190  
     public void setProperty(String key, Object value)
 191  
     {
 192  638
         getScopedProperties(defaultScope).put(key, value);
 193  638
         keySet.add(key);
 194  638
     }
 195  
 
 196  
     /**
 197  
      * Set a property on the message
 198  
      *
 199  
      * @param key   the key on which to associate the value
 200  
      * @param value the property value
 201  
      * @param scope the scope to se the property on
 202  
      * @see org.mule.api.transport.PropertyScope
 203  
      */
 204  
     public void setProperty(String key, Object value, PropertyScope scope)
 205  
     {
 206  124
         checkScopeForWriteAccess(scope);
 207  124
         getScopedProperties(scope).put(key, value);
 208  124
         keySet.add(key);
 209  124
     }
 210  
 
 211  
     /** @return all property keys on this message */
 212  
     public Set getPropertyNames()
 213  
     {
 214  932
         return Collections.unmodifiableSet(keySet);
 215  
     }
 216  
 
 217  
     /** @return all property keys on this message for the given scope */
 218  
     public Set getPropertyNames(PropertyScope scope)
 219  
     {
 220  0
         return Collections.unmodifiableSet(getScopedProperties(scope).keySet());
 221  
     }
 222  
 
 223  
     protected void checkScopeForWriteAccess(PropertyScope scope)
 224  
     {
 225  124
         if (scope == null || PropertyScope.INBOUND.equals(scope) || PropertyScope.APPLICATION.equals(scope))
 226  
         {
 227  0
             throw new IllegalArgumentException("Scope is invalid for writing properties: " + scope);
 228  
         }
 229  124
     }
 230  
 
 231  
 
 232  
     public Object getProperty(String key, Object defaultValue)
 233  
     {
 234  0
         Object value = getProperty(key);
 235  0
         if (value == null)
 236  
         {
 237  0
             value = defaultValue;
 238  
         }
 239  0
         return value;
 240  
     }
 241  
 
 242  
     public byte getByteProperty(String name, byte defaultValue)
 243  
     {
 244  0
         return ObjectUtils.getByte(getProperty(name), defaultValue);
 245  
     }
 246  
 
 247  
     public short getShortProperty(String name, short defaultValue)
 248  
     {
 249  0
         return ObjectUtils.getShort(getProperty(name), defaultValue);
 250  
     }
 251  
 
 252  
     public int getIntProperty(String name, int defaultValue)
 253  
     {
 254  96
         return ObjectUtils.getInt(getProperty(name), defaultValue);
 255  
     }
 256  
 
 257  
     public long getLongProperty(String name, long defaultValue)
 258  
     {
 259  0
         return ObjectUtils.getLong(getProperty(name), defaultValue);
 260  
     }
 261  
 
 262  
     public float getFloatProperty(String name, float defaultValue)
 263  
     {
 264  0
         return ObjectUtils.getFloat(getProperty(name), defaultValue);
 265  
     }
 266  
 
 267  
     public double getDoubleProperty(String name, double defaultValue)
 268  
     {
 269  0
         return ObjectUtils.getDouble(getProperty(name), defaultValue);
 270  
     }
 271  
 
 272  
     public boolean getBooleanProperty(String name, boolean defaultValue)
 273  
     {
 274  0
         return ObjectUtils.getBoolean(getProperty(name), defaultValue);
 275  
     }
 276  
 
 277  
     public String getStringProperty(String name, String defaultValue)
 278  
     {
 279  0
         return ObjectUtils.getString(getProperty(name), defaultValue);
 280  
     }
 281  
 
 282  
     public String toString()
 283  
     {
 284  0
         StringBuffer buf = new StringBuffer(128);
 285  0
         buf.append("Properites{");
 286  0
         for (Iterator iterator = scopedMap.entrySet().iterator(); iterator.hasNext();)
 287  
         {
 288  0
             Map.Entry entry = (Map.Entry) iterator.next();
 289  0
             buf.append(entry.getKey()).append(":");
 290  0
             buf.append(MapUtils.toString((Map) entry.getValue(), false));
 291  0
             buf.append(", ");
 292  0
         }
 293  0
         buf.append("}");
 294  0
         return buf.toString();
 295  
     }
 296  
 }