Mule Tutorial Series: Mapping Data Structure hierarchy & externalizing configuration properties

The CSV file formats in the previous blog posts namely post1 and post2 mapped to a single POJO object. This blog post demonstrates the ability to map CSV fields to a nested POJO structure.

The mule flow is graphically depicted below:

Complex Data Structure Example Mule Flow
Complex Data Structure Example Mule Flow

The contents of the CSV file are shown below:

Clark Kent,Orlando,Florida,US
Bruce Wayne,Lynchburg,Virginia,US
Santa Claus,Mumbai,Maharashtra,India

The CSV format is structured as name, city, state and country.

The File endpoint is configured as below. In the General tab add the following configuration.

Path: ${file.input.location}
Move to Directory: ${file.output.location}

Till now, all configuration properties have been hardcoded in the Mule flows. Mule flows support the ability to externalize the property values. To achieve this create properties file for e.g. app.properties

The contents of the app.properties file is shown below

file.input.location=C:\\MuleStudio\\File\\Config-In
file.output.location=C:\\MuleStudio\\File\\Config-Out

Note that the properties key name match the names defined in the flow. Note the properties file needs to be located in the application classpath. So the properties file is located in the src/main/java. Addition of the properties to the Mule flow is explained later.

The DataMapper component’s configuration depicted graphically is shown below

Complex Data Structure, Data Mapper graphical configuration
Complex Data Structure, Data Mapper graphical configuration

The POJO structure consists of PersonProfile class having two attributes; one is the name string and the second is the AddressProfile class.

The last component of the flow is Java. It is configured by adding the following property in general tab.

Class Name: com.vinraj.integration.ds.PersonProfilePrinter

The source code of PersonProfile, AddressProfile and PersonProfilePrinter classes is shown below:

package com.vinraj.integration.ds;

public class PersonProfile {
	
	private String name = null;
	
	private AddressProfile address = null;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public AddressProfile getAddress() {
		return address;
	}

	public void setAddress(AddressProfile address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "PersonProfile [name=" + name + ", address=" + address + "]";
	}	
}
package com.vinraj.integration.ds;

public class AddressProfile {

	private String city = null;
	private String state = null;
	private String country = null;

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	@Override
	public String toString() {
		return "AddressProfile [city=" + city + ", state=" + state
				+ ", country=" + country + "]";
	}
}
package com.vinraj.integration.ds;

import java.util.List;

public class PersonProfilePrinter {
	
	public void processProfile(List<PersonProfile> profiles) {
		System.out.println(profiles);
	}
}

Running the flow as Mule application generates the following output in the console.

INFO  2013-12-27 15:28:38,350 [[integration].connector.file.mule.default.receiver.47] 
org.mule.transport.file.FileMessageReceiver: Lock obtained on file: C:\MuleStudio\File\Config-In\data.csv

[PersonProfile [name=Clark Kent, address=AddressProfile [city=Orlando, state=Florida, country=US]], 
PersonProfile [name=Bruce Wayne, address=AddressProfile [city=Lynchburg, state=Virginia, country=US]], 
PersonProfile [name=Santa Claus, address=AddressProfile [city=Mumbai, state=Maharashtra, country=India ]]]

The source code of the flow’s XML is shown below:

<mule>
	<spring:beans>
	    <context:property-placeholder
	    	location="classpath:app.properties" />
	  </spring:beans>
    <data-mapper:config name="csv_to_pojo_2" 
	transformationGraphPath="csv_to_pojo_2.grf" doc:name="csv_to_pojo_2"/>
    <flow name="config_extFlow1" doc:name="config_extFlow1">
        <file:inbound-endpoint 		path="${file.input.location}"
		moveToDirectory="${file.output.location}" 
		responseTimeout="10000" doc:name="File"/>
        <data-mapper:transform config-ref="csv_to_pojo_2" doc:name="CSV To Pojo"/>
        <component class="com.vinraj.integration.ds.PersonProfilePrinter" 
		doc:name="Java"/>
    </flow>
</mule>

Note: the XML namespaces added within the mule element tag has been removed for code legibility.

Note the addition of the spring:beans element highlighted in the flow xml. The location attribute of the property-placeholder element is used to specify the properties file name whose values are used in the flow.

Advertisements

One thought on “Mule Tutorial Series: Mapping Data Structure hierarchy & externalizing configuration properties

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s