Coverage Report - org.mule.message.ExceptionMessage
 
Classes in this File Line Coverage Branch Coverage Complexity
ExceptionMessage
0%
0/48
0%
0/4
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.message;
 8  
 
 9  
 import org.mule.api.MuleEvent;
 10  
 import org.mule.api.endpoint.EndpointURI;
 11  
 import org.mule.api.transport.PropertyScope;
 12  
 
 13  
 import java.io.IOException;
 14  
 import java.io.NotSerializableException;
 15  
 import java.io.ObjectInputStream;
 16  
 import java.io.ObjectOutputStream;
 17  
 import java.io.Serializable;
 18  
 import java.util.Date;
 19  
 import java.util.HashMap;
 20  
 import java.util.Iterator;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.apache.commons.logging.Log;
 24  
 import org.apache.commons.logging.LogFactory;
 25  
 
 26  
 /**
 27  
  * <code>ExceptionMessage</code> is used by the DefaultServiceExceptionStrategy
 28  
  * for wrapping an exception with a message to send via an endpointUri.
 29  
  */
 30  
 public class ExceptionMessage implements Serializable
 31  
 {
 32  
     /**
 33  
      * Serial version
 34  
      */
 35  
     private static final long serialVersionUID = -538516243574950621L;
 36  
 
 37  0
     private static final Log logger = LogFactory.getLog(ExceptionMessage.class);
 38  
 
 39  
     // This object uses custom serialization via the writeObject() method
 40  
     private transient Object payload;
 41  
     // This object uses custom serialization via the writeObject() method
 42  
     private transient Throwable exception;
 43  
 
 44  
     protected Map<String, Object> properties;
 45  
     private String componentName;
 46  
     private String endpointUri;
 47  
     private Date timeStamp;
 48  
 
 49  
     public ExceptionMessage(MuleEvent event,
 50  
                             Throwable exception,
 51  
                             String componentName,
 52  
                             EndpointURI endpointUri)
 53  0
     {
 54  0
         this.payload = event.getMessage().getPayload();
 55  0
         properties = new HashMap<String, Object>();
 56  0
         this.exception = exception;
 57  0
         timeStamp = new Date();
 58  0
         this.componentName = componentName;
 59  0
         if (endpointUri != null)
 60  
         {
 61  0
             this.endpointUri = endpointUri.toString();
 62  
         }
 63  
 
 64  0
         for (Iterator iterator = event.getMessage().getPropertyNames(PropertyScope.OUTBOUND).iterator(); iterator.hasNext();)
 65  
         {
 66  0
             String propertyKey = (String) iterator.next();
 67  0
             setProperty(propertyKey, event.getMessage().getProperty(propertyKey, PropertyScope.OUTBOUND));
 68  0
         }
 69  0
     }
 70  
 
 71  
     private void writeObject(ObjectOutputStream out) throws IOException
 72  
     {
 73  0
         out.defaultWriteObject();
 74  
         try
 75  
         {
 76  0
             out.writeObject(exception);
 77  
         }
 78  0
         catch (NotSerializableException e)
 79  
         {
 80  0
             logger.warn("Exception " + exception.getClass().getName() + " is not serializable and will be lost when sending ExceptionMessage over the wire: " + e.getMessage());
 81  0
         }
 82  
         try
 83  
         {
 84  0
             out.writeObject(payload);
 85  
         }
 86  0
         catch (NotSerializableException e)
 87  
         {
 88  0
             logger.warn("Payload " + payload.getClass().getName() + " is not serializable and will be lost when sending ExceptionMessage over the wire: " + e.getMessage());
 89  0
         }
 90  0
     }
 91  
 
 92  
     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
 93  
     {
 94  0
         in.defaultReadObject();
 95  
         try
 96  
         {
 97  0
             exception = (Throwable) in.readObject();
 98  
         }
 99  0
         catch (Exception e)
 100  
         {
 101  
             // ignore
 102  0
         }
 103  
         try
 104  
         {
 105  0
             payload = in.readObject();
 106  
         }
 107  0
         catch (Exception e)
 108  
         {
 109  
             // ignore
 110  0
         }
 111  0
     }
 112  
 
 113  
     public void setPayload(Object payload)
 114  
     {
 115  0
         this.payload = payload;
 116  0
     }
 117  
 
 118  
     /**
 119  
      * @return the current message
 120  
      */
 121  
     public Object getPayload()
 122  
     {
 123  0
         return payload;
 124  
     }
 125  
 
 126  
 
 127  
     /**
 128  
      * Adds a map of properties to associated with this message
 129  
      *
 130  
      * @param properties the properties add to this message
 131  
      */
 132  
     public void addProperties(Map<String, Object> properties)
 133  
     {
 134  0
         this.properties.putAll(properties);
 135  0
     }
 136  
 
 137  
     /**
 138  
      * Removes all properties on this message
 139  
      */
 140  
     public void clearProperties()
 141  
     {
 142  0
         properties.clear();
 143  0
     }
 144  
 
 145  
     /**
 146  
      * Returns a map of all properties on this message
 147  
      *
 148  
      * @return a map of all properties on this message
 149  
      */
 150  
     public Map getProperties()
 151  
     {
 152  0
         return properties;
 153  
     }
 154  
 
 155  
     public void setProperty(String key, Object value)
 156  
     {
 157  0
         properties.put(key, value);
 158  0
     }
 159  
 
 160  
     public Object getProperty(String key)
 161  
     {
 162  0
         return properties.get(key);
 163  
     }
 164  
 
 165  
     public String getComponentName()
 166  
     {
 167  0
         return componentName;
 168  
     }
 169  
 
 170  
     public String getEndpoint()
 171  
     {
 172  0
         return endpointUri;
 173  
     }
 174  
 
 175  
     public Date getTimeStamp()
 176  
     {
 177  0
         return timeStamp;
 178  
     }
 179  
 
 180  
     public Throwable getException()
 181  
     {
 182  0
         return exception;
 183  
     }
 184  
 
 185  
     @Override
 186  
     public String toString()
 187  
     {
 188  0
         return "ExceptionMessage{" + "payload=" + getPayload() + ", context=" + properties + "exception=" + exception
 189  
                 + ", componentName='" + componentName + "'" + ", endpointUri=" + endpointUri + ", timeStamp="
 190  
                 + timeStamp + "}";
 191  
     }
 192  
 }