View Javadoc

1   /*
2    * $Id: MuleContextExpressionEvaluator.java 11457 2008-03-20 23:21:50Z rossmason $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.util.expression;
11  
12  import org.mule.MuleServer;
13  import org.mule.RequestContext;
14  import org.mule.api.MuleContext;
15  import org.mule.api.MuleEventContext;
16  import org.mule.api.MuleRuntimeException;
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
34  {
35      public static final String NAME = "mule";
36  
37      /**
38       * Extracts a single property from the message
39       *
40       * @param expression the property expression or expression
41       * @param message    the message to extract from
42       * @return the result of the extraction or null if the property was not found
43       */
44      public Object evaluate(String expression, Object message)
45      {
46          if (expression.equals("serviceName"))
47          {
48              return getEventContext().getService().getName();
49          }
50          else if (expression.equals("modelName"))
51          {
52              return getEventContext().getService().getModel().getName();
53          }
54          else if (expression.equals("inboundEndpoint"))
55          {
56              return getEventContext().getEndpointURI();
57          }
58          else if (expression.equals("serverId"))
59          {
60              return getMuleContext().getConfiguration().getId();
61          }
62          else if (expression.equals("clusterId"))
63          {
64              return getMuleContext().getConfiguration().getClusterId();
65          }
66          else if (expression.equals("domainId"))
67          {
68              return getMuleContext().getConfiguration().getDomainId();
69          }
70          else if (expression.equals("workingDir"))
71          {
72              return getMuleContext().getConfiguration().getWorkingDirectory();
73          }
74          else if (expression.equals("homeDir"))
75          {
76              return getMuleContext().getConfiguration().getMuleHomeDirectory();
77          }
78          else
79          {
80              throw new IllegalArgumentException(expression);
81          }
82      }
83  
84      protected MuleContext getMuleContext()
85      {
86          if(MuleServer.getMuleContext()==null)
87          {
88               throw new MuleRuntimeException(CoreMessages.objectIsNull("MuleContext"));
89          }
90          else
91          {
92              return MuleServer.getMuleContext();
93          }
94      }
95  
96      protected MuleEventContext getEventContext()
97      {
98          if(RequestContext.getEventContext()==null)
99          {
100              throw new MuleRuntimeException(CoreMessages.objectIsNull("MuleEventContext"));
101         }
102         else
103         {
104             return RequestContext.getEventContext();
105         }
106     }
107 
108     /**
109      * Gts the name of the object
110      *
111      * @return the name of the object
112      */
113     public String getName()
114     {
115         return NAME;
116     }
117 
118     /**
119      * Sets the name of the object
120      *
121      * @param name the name of the object
122      */
123     public void setName(String name)
124     {
125         throw new UnsupportedOperationException("setName");
126     }
127 }