0

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?

NGBeginner
  • 403
  • 1
  • 3
  • 14

1 Answers1

1

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
aled
  • 21,330
  • 3
  • 27
  • 34
  • Thanks but as I mentioned in my use case it dynamically passes the flowVars to read the property value, so eventually the value of the variable and the Key name should be the same. This is the actual question I am trying to explore - https://help.mulesoft.com/s/question/0D52T00004mXTQUSA4/dynamically-read-property-values-from-a-properties-file-in-mule – NGBeginner Aug 09 '20 at 20:00
  • The question should be updated to clarify that. Questions should be self contained, not depending on external links. – aled Aug 10 '20 at 02:21
  • Updated my edits and its self contained only but kind of similar use case. – NGBeginner Aug 10 '20 at 09:41