View Javadoc

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