View Javadoc

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