DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Stack in Data Structures
  • IDE Changing as Fast as Cloud Native
  • DevOps in Legacy Systems
  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices

Trending

  • Stack in Data Structures
  • IDE Changing as Fast as Cloud Native
  • DevOps in Legacy Systems
  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices

How to Generate Property Key at Run-Time

In this article, Abhay Singh explains how to generate a property key at run-time and accessing the corresponding value using DataWeave.

Abhay Singh user avatar by
Abhay Singh
·
Dec. 23, 16 · Tutorial
Like (1)
Save
Tweet
Share
4.70K Views

Join the DZone community and get the full member experience.

Join For Free

we are very much aware how important of a role property files play in mule esb in making integrations and applications dynamic, and even in making it reusable.

generally, a value in the property file can be accessed by nesting the key within curly brackets, which is preceded by the $ symbol (like this: ${key} .).

with the idea of making integrations and applications as dynamic and fluid as possible, one can generate values from property files at compile-time or run-time.

one way of generating property values at runtime is by using dataweave. dataweave supports accessing property values in property files using property keys.

scenario

in this scenario, we will be creating a file whose properties are in a property file. these properties are the name of the file, the content of the file, and the path where the file will be placed.

to begin with, we have an http listener that accepts interfacenumber as the input parameter. based on the interface number, the application will form property keys in the message properties transformer, which will be assigned to a unique flow variable. this flow variable, when accessed inside dataweave, will return the desired property value specific to that particular interface.

the applications endpoint is a file connector, wherein configurations such as file name/pattern and path are generated dynamically depending on the interface.

mule flow

image title

the configuration xml of the flow is given below:

<http:listener-config name="http_listener_configuration" host="${http.host}" port="${http.port}" doc:name="http listener configuration"/>
<flow name="runtimeproperties_flow">
<http:listener config-ref="http_listener_configuration" path="${http.path}" doc:name="http"/>
        <message-properties-transformer scope="invocation" doc:name="message properties">
            <add-message-property key="filename" value="#['interface.' + message.inboundproperties['http.query.params']['interfacenumber'] + '.filename']"/>
            <add-message-property key="filecontent" value="#['interface.' + message.inboundproperties['http.query.params']['interfacenumber'] + '.filecontent']"/>
            <add-message-property key="filepath" value="#['interface.' + message.inboundproperties['http.query.params']['interfacenumber'] + '.filepath']"/>
        </message-properties-transformer>
        <set-payload doc:name="set payload" value="#[dw('p(flowvars.filecontent)')]"/>
        <file:outbound-endpoint responsetimeout="10000" doc:name="file" outputpattern="#[dw('p(flowvars.filename)')]" path="#[dw('p(flowvars.filepath)')]"/>

</flow>

the structure of the property file is given below:

####################### http properties  #######################
http.host=0.0.0.0
http.port=8081
http.path=/test

################### interface 001 properties ###################
interface.001.filename=interface001.txt
interface.001.filecontent=this is test data for interface 001
interface.001.filepath=c:\users/xyz/desktop/test/interface 001

################### interface 002 properties ###################
interface.002.filename=interface002.txt
interface.002.filecontent=this is test data for interface 002
interface.002.filepath=c:\users/xyz/desktop/test/interface 002

from the configuration xml, one can figure that we are creating a key structure similar to that in the property file and store it in a flow variable.

these flow variables are then accessed using the dataweave function inside any component as per the configuration.

hence, we have managed to dynamically access property values at compile time solely from the input received and successfully generated the desired result.

Property (programming)

Opinions expressed by DZone contributors are their own.

Trending

  • Stack in Data Structures
  • IDE Changing as Fast as Cloud Native
  • DevOps in Legacy Systems
  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: