Coverage Report - org.mule.transport.AbstractMuleMessageFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractMuleMessageFactory
0%
0/25
0%
0/10
0
 
 1  
 /*
 2  
  * $Id: AbstractMuleMessageFactory.java 19191 2010-08-25 21:05:23Z tcarlson $
 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.transport;
 12  
 
 13  
 import org.mule.DefaultMuleMessage;
 14  
 import org.mule.api.MuleContext;
 15  
 import org.mule.api.MuleMessage;
 16  
 import org.mule.api.transport.MessageTypeNotSupportedException;
 17  
 import org.mule.api.transport.MuleMessageFactory;
 18  
 
 19  
 public abstract class AbstractMuleMessageFactory implements MuleMessageFactory
 20  
 {
 21  
     protected MuleContext muleContext;
 22  
 
 23  
     public AbstractMuleMessageFactory(MuleContext context)
 24  
     {
 25  0
         super();
 26  0
         muleContext = context;
 27  0
     }
 28  
 
 29  
     public MuleMessage create(Object transportMessage, String encoding) throws Exception
 30  
     {
 31  0
         return create(transportMessage, null, encoding);
 32  
     }
 33  
     
 34  
     public MuleMessage create(Object transportMessage, MuleMessage previousMessage, String encoding) 
 35  
         throws Exception
 36  
     {
 37  0
         if (transportMessage == null)
 38  
         {
 39  0
             return new DefaultMuleMessage(NullPayload.getInstance(), muleContext);
 40  
         }
 41  
 
 42  0
         if (!isTransportMessageTypeSupported(transportMessage))
 43  
         {
 44  0
             throw new MessageTypeNotSupportedException(transportMessage, getClass());
 45  
         }
 46  
 
 47  0
         Object payload = extractPayload(transportMessage, encoding);
 48  
         DefaultMuleMessage message;
 49  0
         if (previousMessage != null)
 50  
         {
 51  0
             message = new DefaultMuleMessage(payload, previousMessage, muleContext);
 52  
         }
 53  
         else
 54  
         {
 55  0
             message = new DefaultMuleMessage(payload, muleContext);
 56  
         }
 57  
 
 58  0
         message.setEncoding(encoding);
 59  0
         addProperties(message, transportMessage);
 60  0
         addAttachments(message, transportMessage);
 61  0
         return message;
 62  
     }
 63  
 
 64  
     protected abstract Class<?>[] getSupportedTransportMessageTypes();
 65  
 
 66  
     protected abstract Object extractPayload(Object transportMessage, String encoding) throws Exception;
 67  
 
 68  
     protected void addProperties(DefaultMuleMessage message, Object transportMessage) throws Exception
 69  
     {
 70  
         // Template method
 71  0
     }
 72  
 
 73  
     protected void addAttachments(DefaultMuleMessage message, Object transportMessage) throws Exception
 74  
     {
 75  
         // Template method
 76  0
     }
 77  
 
 78  
     private boolean isTransportMessageTypeSupported(Object transportMessage)
 79  
     {
 80  0
         Class<?> transportMessageType = transportMessage.getClass();
 81  0
         boolean match = false;
 82  0
         for (Class<?> type : getSupportedTransportMessageTypes())
 83  
         {
 84  0
             if (type.isAssignableFrom(transportMessageType))
 85  
             {
 86  0
                 match = true;
 87  0
                 break;
 88  
             }
 89  
         }
 90  0
         return match;
 91  
     }
 92  
 }