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