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.expression;
8   
9   import org.mule.RequestContext;
10  import org.mule.api.MuleContext;
11  import org.mule.api.MuleEventContext;
12  import org.mule.api.MuleMessage;
13  import org.mule.api.MuleRuntimeException;
14  import org.mule.api.context.MuleContextAware;
15  import org.mule.api.expression.ExpressionEvaluator;
16  import org.mule.api.service.Service;
17  import org.mule.config.i18n.CoreMessages;
18  
19  /**
20   * This property extractor exposes mule context information as expressions. This can be context information about
21   * the server itself such as the server id or about the current request such as the current service name.
22   * <ul>
23   * <li>serviceName - returns the name of the service currently processing the event.</li>
24   * <li>modelName - returns the name of the model that hosts the current service</li>
25   * <li>inboundEndpoint - returns the URI string of the endpoint that received the current messgae.</li>
26   * <li>serverId - the Mule instance server Id.</li>
27   * <li>clusterId - the Mule instance cluster Id.</li>
28   * <li>domainId - the Mule instance domain Id.</li>
29   * <li>workingDir - Mule's working directory.</li>
30   * <li>homeDir - Mule's home directory</li>
31   * </ul>
32   */
33  public class MuleContextExpressionEvaluator implements ExpressionEvaluator, MuleContextAware
34  {
35      public static final String NAME = "context";
36  
37      protected MuleContext muleContext;
38  
39      public void setMuleContext(MuleContext context)
40      {
41          this.muleContext = context;
42      }
43  
44      /**
45       * Extracts a single property from the message
46       *
47       * @param expression the property expression or expression
48       * @param message    the message to extract from
49       * @return the result of the extraction or null if the property was not found
50       */
51      public Object evaluate(String expression, MuleMessage message)
52      {
53          if (expression.equals("serviceName"))
54          {
55              return getEventContext().getFlowConstruct().getName();
56          }
57          else if (expression.equals("modelName"))
58          {
59              if (getEventContext().getFlowConstruct() instanceof Service)
60              {
61                  return ((Service) getEventContext().getFlowConstruct()).getModel().getName();
62              }
63              else
64              {
65                  throw new UnsupportedOperationException("The 'modelName' function can only be used with Service");
66              }
67          }
68          else if (expression.equals("inboundEndpoint"))
69          {
70              return getEventContext().getEndpointURI();
71          }
72          else if (expression.equals("serverId"))
73          {
74              return getMuleContext().getConfiguration().getId();
75          }
76          else if (expression.equals("clusterId"))
77          {
78              return getMuleContext().getConfiguration().getClusterId();
79          }
80          else if (expression.equals("domainId"))
81          {
82              return getMuleContext().getConfiguration().getDomainId();
83          }
84          else if (expression.equals("workingDir"))
85          {
86              return getMuleContext().getConfiguration().getWorkingDirectory();
87          }
88          else if (expression.equals("homeDir"))
89          {
90              return getMuleContext().getConfiguration().getMuleHomeDirectory();
91          }
92          else
93          {
94              throw new IllegalArgumentException(expression);
95          }
96      }
97  
98      protected MuleContext getMuleContext()
99      {
100         return muleContext;
101     }
102 
103     protected MuleEventContext getEventContext()
104     {
105         if(RequestContext.getEventContext()==null)
106         {
107              throw new MuleRuntimeException(CoreMessages.objectIsNull("MuleEventContext"));
108         }
109         else
110         {
111             return RequestContext.getEventContext();
112         }
113     }
114 
115     /**
116      * Gts the name of the object
117      *
118      * @return the name of the object
119      */
120     public String getName()
121     {
122         return NAME;
123     }
124 
125     /**
126      * Sets the name of the object
127      *
128      * @param name the name of the object
129      */
130     public void setName(String name)
131     {
132         throw new UnsupportedOperationException();
133     }
134 }