How do you compare strings in a mule expression when one string is loaded via a property file?

GarySharpe picture GarySharpe · Apr 6, 2015 · Viewed 8.1k times · Source

I have the following mule expression in a choice component:

 <choice doc:name="Choice">
        <when expression="'localhost' == ${environment}">
           ... Do something
        </when>

The environment property is loaded via a property file, and appears to resolve correctly. However, I get the following error:

[Error: unresolvable property or identifier: localhost]
[Near : {... 'localhost' == localhost ....}]

I've also tried wrapping the expression in #[], and reversing the order of the variables in the comparison <when expression="${environment} == 'localhost'">, but I get the same error.

Answer

Ryan Hoegg picture Ryan Hoegg · Apr 7, 2015

The spring property is interpolated before the XML is parsed, leading to the following XML configuration when the environment property is equal to "localhost":

<choice doc:name="Choice">
    <when expression="'localhost' == localhost">
       ... Do something
    </when>

Mule will look for a flow variable or session variable named "localhost", which is probably not what you want.

Try this:

<choice doc:name="Choice">
    <when expression="'localhost' == '${environment}'">
       ... Do something
    </when>