Search code examples
groovymulemule-esbmule-el

Mule 3 retrieving the placeholder value dynamically


I have a use-case that I need to retrieve the value from my properties file but that key should be derived dynamically from my query params.

How to handle this in MEL or Groovy? I am aware it is possible in DW.

Http request
https://localhost:9898/getStore?search=customer.weststore.name

And my placeholders are -

file.properties
customer.weststore.name=TESTING
customer.eaststore.name=IRERRER

So the way I need to access something like this

<set-variable variableName="westDetail" value="#[message.inboundProperites['customer.weststore.name']" doc:name="Variable"/>
<logger message="${westDetail}" level="INFO" /> --> Failed as no placeholder available

When I tried the above it's failing due to no placeholder as "westDetail" available whereas I need the fetch that particular key from the properties file.

This is something related to this article - https://help.mulesoft.com/s/question/0D52T00004mXTQUSA4/dynamically-read-property-values-from-a-properties-file-in-mule but the only solution provided with DW not MEL or Groovy.

How anyone advises, Is it possible?


Solution

  • I understand that the problem is that you want to query the properties by a key that is obtained at execution.

    You are doing it incorrectly. ${} is for evaluating the value of a property, which is done at initialization time of the application. You missed that get the actual value in the set-variable.

    #[] is for executing a MEL expression, which happens at execution time. flowVars.westDetail is a MEL expression that returns the value of flow variable westDetail. You can't use a MEL expression to evaluate the property placeholder ${}, because they are evaluated at different times.

    A solution is to use a Spring bean to store the properties, instead of a configuration properties placeholder. Then you can assign it to a flow variable and access it like a map.

    Example:

    <spring:beans>
      <spring:bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <spring:property name="location" value="classpath:items.properties"/>      
      </spring:bean>    
    </spring:beans>
    
    <flow name="myFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <set-variable value="#[app.registry.myProperties]" variableName="props"></set-variable>
        <logger message="a=#[flowVars.props['a']]" level="INFO"/>
    </flow>
    

    items.properties:

    a=1
    b=2
    c=3
    

    Output:

    a=1