Coverage Report - org.mule.expression.MuleContextExpressionEvaluator
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleContextExpressionEvaluator
0%
0/28
0%
0/20
4.667
 
 1  
 /*
 2  
  * $Id: MuleContextExpressionEvaluator.java 20321 2010-11-24 15:21:24Z dfeist $
 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  0
 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  0
         this.muleContext = context;
 45  0
     }
 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  0
         if (expression.equals("serviceName"))
 57  
         {
 58  0
             return getEventContext().getFlowConstruct().getName();
 59  
         }
 60  0
         else if (expression.equals("modelName"))
 61  
         {
 62  0
             if (getEventContext().getFlowConstruct() instanceof Service)
 63  
             {
 64  0
                 return ((Service) getEventContext().getFlowConstruct()).getModel().getName();
 65  
             }
 66  
             else
 67  
             {
 68  0
                 throw new UnsupportedOperationException("The 'modelName' function can only be used with Service");
 69  
             }
 70  
         }
 71  0
         else if (expression.equals("inboundEndpoint"))
 72  
         {
 73  0
             return getEventContext().getEndpointURI();
 74  
         }
 75  0
         else if (expression.equals("serverId"))
 76  
         {
 77  0
             return getMuleContext().getConfiguration().getId();
 78  
         }
 79  0
         else if (expression.equals("clusterId"))
 80  
         {
 81  0
             return getMuleContext().getConfiguration().getClusterId();
 82  
         }
 83  0
         else if (expression.equals("domainId"))
 84  
         {
 85  0
             return getMuleContext().getConfiguration().getDomainId();
 86  
         }
 87  0
         else if (expression.equals("workingDir"))
 88  
         {
 89  0
             return getMuleContext().getConfiguration().getWorkingDirectory();
 90  
         }
 91  0
         else if (expression.equals("homeDir"))
 92  
         {
 93  0
             return getMuleContext().getConfiguration().getMuleHomeDirectory();
 94  
         }
 95  
         else
 96  
         {
 97  0
             throw new IllegalArgumentException(expression);
 98  
         }
 99  
     }
 100  
 
 101  
     protected MuleContext getMuleContext()
 102  
     {
 103  0
         return muleContext;
 104  
     }
 105  
 
 106  
     protected MuleEventContext getEventContext()
 107  
     {
 108  0
         if(RequestContext.getEventContext()==null)
 109  
         {
 110  0
              throw new MuleRuntimeException(CoreMessages.objectIsNull("MuleEventContext"));
 111  
         }
 112  
         else
 113  
         {
 114  0
             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  0
         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  0
         throw new UnsupportedOperationException();
 136  
     }
 137  
 }