Wednesday, July 13, 2011

Mule - Expression Filter

Expression filter evaluates a range of expressions. The below example is based on jxpath evaluator.

mule-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2"
    xmlns:file="http://www.mulesource.org/schema/mule/file/2.2"
    xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2" xmlns:smtp="http://www.mulesource.org/schema/mule/smtp/2.2"
    xmlns:spring="www.springframework.org/schema/beans"
    xsi:schemaLocation="
       http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
       http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd
       http://www.mulesource.org/schema/mule/file/2.2 http://www.mulesource.org/schema/mule/file/2.2/mule-file.xsd
       http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd
       http://www.mulesource.org/schema/mule/smtp/2.2 http://www.mulesource.org/schema/mule/smtp/2.2/mule-smtp.xsd
       www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <stdio:connector name="stdioInConnector"
        outputMessage="Data : " promptMessage="Enter your name : " />

    <custom-transformer
        class="org.mybusiness.transformers.StringToNameTransformer" name="stringToNameTransformer" />

    <model>

        <service name="StdioService">
            <inbound>
                <stdio:inbound-endpoint system="IN"
                    connector-ref="stdioInConnector" />
            </inbound>
            <outbound>
                <pass-through-router>
                    <vm:outbound-endpoint path="processInputIN" />
                </pass-through-router>
            </outbound>
        </service>

        <service name="ProcessInput">
            <inbound>
                <vm:inbound-endpoint path="processInputIN">
                    <transformers>
                        <transformer ref="stringToNameTransformer" />
                    </transformers>
                </vm:inbound-endpoint>
            </inbound>
            <component class="org.mybusiness.components.ProcessInputComponent" />
            <outbound>
                <filtering-router>
                    <vm:outbound-endpoint path="computeLengthIN" />
                    <and-filter>
                        <expression-filter expression="firstName='vijay'"
                            evaluator="jxpath" />
                        <expression-filter expression="lastName='san'"
                            evaluator="jxpath" />
                    </and-filter>
                </filtering-router>
                <logging-catch-all-strategy />
            </outbound>
        </service>

        <service name="ComputeLength">
            <inbound>
                <vm:inbound-endpoint path="computeLengthIN" />
            </inbound>
            <component
                class="org.mybusiness.components.ComputeLengthComponent" />
        </service>
    </model>
</mule>


Component Classes

package org.mybusiness.components;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mybusiness.dtos.Name;

public class ProcessInputComponent {
Log LOG = LogFactory.getLog(ProcessInputComponent.class);

public Name display(Name name) throws Exception {
LOG.info("Input Message : " + name.getFirstName() + " "
+ name.getLastName());
if (name.getFirstName().trim().length() == 0) {
throw new Exception("Invalid Input");
}
return name;
}
}

package org.mybusiness.components;

import org.mybusiness.dtos.Name;

public class ComputeLengthComponent {

public String compute(Name input) {

System.out.println("First Name Length : " + input.getFirstName().length());
return ("First Name Length" + input.getFirstName().length());
}
}

Transformers

package org.mybusiness.transformers;

import java.util.StringTokenizer;

import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;
import org.mybusiness.dtos.Name;

public class StringToNameTransformer extends AbstractTransformer {

@Override
public boolean isSourceTypeSupported(Class class1) {
return String.class.equals(class1);
}

@Override
protected Object doTransform(Object src, String arg1)
throws TransformerException {

String input = (String) src;
Name name = new Name();

StringTokenizer st = new StringTokenizer(input);
name.setFirstName(st.nextToken());

if (st.hasMoreTokens()) {
name.setLastName(st.nextToken());
}

         return name;

}
}

Data Object

package org.mybusiness.dtos;

public class Name {

private String firstName;
private String lastName;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}
}

No comments:

Post a Comment