View Javadoc
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.routing.correlation;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.config.MuleProperties;
11  import org.mule.expression.MessageHeaderExpressionEvaluator;
12  
13  /**
14   * <code>CorrelationPropertiesExpressionEvaluator</code> is a default implementation used for
15   * getting the Correlation information from a message. This object is only used when
16   * getting a specific property to be set on the message. When reading the property
17   * the getProperty(...) or the direct property accessor will be used i.e.
18   * message.getCorrelationId() or
19   * message.getProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY)
20   */
21  public class CorrelationPropertiesExpressionEvaluator extends MessageHeaderExpressionEvaluator
22  {
23      @Override
24      public final Object evaluate(String name, MuleMessage message)
25      {
26          Object result;
27          if (message != null)
28          {
29              if (MuleProperties.MULE_CORRELATION_ID_PROPERTY.equals(name))
30              {
31                  result = getCorrelationId(message);
32              }
33              else if (MuleProperties.MULE_MESSAGE_ID_PROPERTY.equals(name))
34              {
35                  result = getMessageId(message);
36              }
37              else
38              {
39                  throw new IllegalArgumentException("Property name: " + name
40                                                     + " not recognised by the Correlation Property Extractor");
41              }
42              if (result == null)
43              {
44                  throw new IllegalArgumentException(
45                      "Property Extractor cannot return a null value. Extractor is: " + getClass().getName());
46              }
47          }
48          else
49          {
50              return super.evaluate(name, message);
51          }
52          return result;
53      }
54  
55      public String getMessageId(MuleMessage message)
56      {
57          return message.getUniqueId();
58      }
59  
60      public String getCorrelationId(MuleMessage message)
61      {
62          String id = message.getCorrelationId();
63          if (id == null)
64          {
65              id = message.getUniqueId();
66          }
67          return id;
68      }
69  }