Coverage Report - org.mule.transport.DefaultMessageAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultMessageAdapter
53%
25/47
44%
8/18
2.75
 
 1  
 /*
 2  
  * $Id: DefaultMessageAdapter.java 10489 2008-01-23 17:53:38Z 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.transport;
 12  
 
 13  
 import org.mule.api.MuleRuntimeException;
 14  
 import org.mule.api.ThreadSafeAccess;
 15  
 import org.mule.api.transport.MessageAdapter;
 16  
 import org.mule.api.transport.MutableMessageAdapter;
 17  
 import org.mule.config.i18n.CoreMessages;
 18  
 
 19  
 import java.util.Iterator;
 20  
 import java.util.Map;
 21  
 
 22  
 import javax.activation.DataHandler;
 23  
 
 24  
 /**
 25  
  * <code>DefaultMessageAdapter</code> can be used to wrap an arbitary object where
 26  
  * no special 'apapting' is needed. The adapter allows for a set of properties to be
 27  
  * associated with an object.
 28  
  */
 29  
 
 30  
 public final class DefaultMessageAdapter extends AbstractMessageAdapter implements MutableMessageAdapter
 31  
 {
 32  
     /**
 33  
      * Serial version
 34  
      */
 35  
     private static final long serialVersionUID = 1908152148142575505L;
 36  
 
 37  
     /**
 38  
      * The message object wrapped by this adapter
 39  
      */
 40  
     protected Object message;
 41  
 
 42  
     
 43  
     protected DefaultMessageAdapter()
 44  
     {
 45  0
         super();
 46  0
     }
 47  
 
 48  
     /**
 49  
      * Creates a default message adapter with properties and attachments
 50  
      *
 51  
      * @param message the message to wrap. If this is null and NullPayload object
 52  
      *            will be used
 53  
      * @see NullPayload
 54  
      */
 55  
     public DefaultMessageAdapter(Object message)
 56  714
     {
 57  714
         if (message == null)
 58  
         {
 59  12
             this.message = NullPayload.getInstance();
 60  
         }
 61  
         else
 62  
         {
 63  702
             this.message = message;
 64  
         }
 65  714
     }
 66  
 
 67  
     public DefaultMessageAdapter(Object message, MessageAdapter previous)
 68  
     {
 69  318
         super(previous);
 70  318
         if (previous != null)
 71  
         {
 72  318
             if (message == null)
 73  
             {
 74  0
                 this.message = NullPayload.getInstance();
 75  
             }
 76  
             else
 77  
             {
 78  318
                 this.message = message;
 79  
             }
 80  
             // MULE-1564
 81  
             // this is an iterator over a concurrent map and so is weakly consistent (not fail-safe)
 82  
             // that means we don't get errors here, but may see changed values.
 83  
             // so we can make this safe to null values (although not predictable) by simply checking values
 84  318
             for (Iterator iterator = previous.getAttachmentNames().iterator(); iterator.hasNext();)
 85  
             {
 86  0
                 String name = (String) iterator.next();
 87  
                 try
 88  
                 {
 89  0
                     DataHandler dh = previous.getAttachment(name);
 90  0
                     if (null == dh)
 91  
                     {
 92  0
                         logger.warn("Detected concurrent access to attachment " + name + " for " + previous);
 93  
 //                        new Throwable().printStackTrace();
 94  
                     }
 95  
                     else
 96  
                     {
 97  0
                         addAttachment(name, dh);
 98  
                     }
 99  
                 }
 100  0
                 catch (Exception e)
 101  
                 {
 102  0
                     throw new MuleRuntimeException(CoreMessages.failedToReadPayload(), e);
 103  0
                 }
 104  0
             }
 105  318
             for (Iterator iterator = previous.getPropertyNames().iterator(); iterator.hasNext();)
 106  
             {
 107  102
                 String name = (String) iterator.next();
 108  
                 try
 109  
                 {
 110  102
                     Object value = previous.getProperty(name);
 111  102
                     if (null == value)
 112  
                     {
 113  0
                         logger.warn("Detected concurrent access to property " + name + " for " + previous);
 114  
 //                        new Throwable().printStackTrace();
 115  
                     }
 116  
                     else
 117  
                     {
 118  102
                         setProperty(name, value);
 119  
                     }
 120  
                 }
 121  0
                 catch (Exception e)
 122  
                 {
 123  0
                     throw new MuleRuntimeException(CoreMessages.failedToReadPayload(), e);
 124  102
                 }
 125  102
             }
 126  
         }
 127  
         else
 128  
         {
 129  0
             throw new IllegalArgumentException("previousAdapter may not be null");
 130  
         }
 131  318
     }
 132  
 
 133  
     /**
 134  
      * Creates a default message adapter with properties and attachments
 135  
      *
 136  
      * @param message the message to wrap. If this is null and NullPayload object
 137  
      *            will be used
 138  
      * @param properties a map properties to set on the adapter. Can be null.
 139  
      * @param attachments a map attaches (DataHandler objects) to set on the adapter.
 140  
      *            Can be null.
 141  
      * @see NullPayload
 142  
      * @see javax.activation.DataHandler
 143  
      */
 144  
     public DefaultMessageAdapter(Object message, Map properties, Map attachments)
 145  
     {
 146  0
         this(message);
 147  0
         if (properties != null)
 148  
         {
 149  0
             this.properties.addInboundProperties(properties);
 150  
         }
 151  0
         if (attachments != null)
 152  
         {
 153  0
             this.attachments.putAll(attachments);
 154  
         }
 155  0
     }
 156  
 
 157  
     /** {@inheritDoc} */
 158  
     public Object getPayload()
 159  
     {
 160  2104
         return message;
 161  
     }
 162  
 
 163  
     /** {@inheritDoc} */
 164  
     public void setPayload(Object payload)
 165  
     {
 166  172
         synchronized(message)
 167  
         {
 168  172
             message = payload;
 169  172
         }
 170  172
     }
 171  
 
 172  
     /** {@inheritDoc} */
 173  
     public String getUniqueId()
 174  
     {
 175  434
         return id;
 176  
     }
 177  
 
 178  
     ////////////////////////// ThreadSafeAccess impl ////////////////////
 179  
 
 180  
     /** {@inheritDoc} */
 181  
     public ThreadSafeAccess newThreadCopy()
 182  
     {
 183  280
         return new DefaultMessageAdapter(getPayload(), this);
 184  
     }
 185  
 
 186  
 }