View Javadoc

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          super();
26          muleContext = context;
27      }
28  
29      public MuleMessage create(Object transportMessage, String encoding) throws Exception
30      {
31          return create(transportMessage, null, encoding);
32      }
33      
34      public MuleMessage create(Object transportMessage, MuleMessage previousMessage, String encoding) 
35          throws Exception
36      {
37          if (transportMessage == null)
38          {
39              return new DefaultMuleMessage(NullPayload.getInstance(), muleContext);
40          }
41  
42          if (!isTransportMessageTypeSupported(transportMessage))
43          {
44              throw new MessageTypeNotSupportedException(transportMessage, getClass());
45          }
46  
47          Object payload = extractPayload(transportMessage, encoding);
48          DefaultMuleMessage message;
49          if (previousMessage != null)
50          {
51              message = new DefaultMuleMessage(payload, previousMessage, muleContext);
52          }
53          else
54          {
55              message = new DefaultMuleMessage(payload, muleContext);
56          }
57  
58          message.setEncoding(encoding);
59          addProperties(message, transportMessage);
60          addAttachments(message, transportMessage);
61          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      }
72  
73      protected void addAttachments(DefaultMuleMessage message, Object transportMessage) throws Exception
74      {
75          // Template method
76      }
77  
78      private boolean isTransportMessageTypeSupported(Object transportMessage)
79      {
80          Class<?> transportMessageType = transportMessage.getClass();
81          boolean match = false;
82          for (Class<?> type : getSupportedTransportMessageTypes())
83          {
84              if (type.isAssignableFrom(transportMessageType))
85              {
86                  match = true;
87                  break;
88              }
89          }
90          return match;
91      }
92  }