简体   繁体   中英

Mule MEL validation

My property file values are:

companies = test
companies1 =
companies2 = 'another'

now if I have a MEL validation like below doesn't work.

<choice  doc:name = "PageNum-1">
    <when expression = "#[ ${companies} != '' ]">
        <logger message = "===== Nothing to execute in process 1 ===" level="INFO" doc:name = "Logger"/>    
    </when>
    <otherwise>
        <logger message = "===== Nothing to execute in process 0 ===" level="INFO" doc:name = "Logger"/>    
    </otherwise>
</choice>

Error:

Exception stack is:
1. [Error: illegal use of operator: !=]
[Near : {... !='' ....}]
             ^
[Line: 1, Column: 1] (org.mule.mvel2.CompileException)

EDIT:

As a workaround solution, I have added a flow variable and verifying the data in expression as

<set-variable variableName = "company1" value = "${companies1}" doc:name = "company1"/>
<when expression = "#[flowVars.company1.isEmpty()]">

But, is there any simpler way to do with only by using $ as ${commpany1}

Your problem is related with how the properties are replaced. They are replaced in a one-by-one fashion, so if you dont surround with quoes the property value companies, just like you did in companies2 it won't work.

Also, there is no need to use #[] when inside an expression attribute. The following did work for me:

<context:property-placeholder location="myprop.properties" />

<flow name="test-http">
    <http:inbound-endpoint host="127.0.0.1" port="8081"
        doc:name="HTTP" />
    <choice doc:name="PageNum-1">
        <when expression="${companies}!=''">
            <logger message="===== Nothing to execute in process 1 ==="
                level="INFO" doc:name="Logger" />
        </when>
        <otherwise>
            <logger message="===== Nothing to execute in process 0 ==="
                level="INFO" doc:name="Logger" />
        </otherwise>
    </choice>
</flow>

If you want MEL to handle the property value as a string you should either add the quotes in the expression or in the properties file not on both locations or a mix as you've done. So to test if the property ${companies} is empty in the file you can use.

<when expression="'${companies}' != empty" >

After properties has been resolved this will become

<when expression="'' != empty" >

when the properties file contains

 companies=

and it will become

<when expression="'Acme Inc' != empty" >

when the properties file contains

 companies=Acme Inc

However if your properties file contains

 companies='Snake Oil Ltd'

the result will be

<when expression="''Snake Oil Ltd'' != empty" >

Which will generate an error so in that case you should not have the quotes in the properties file.

If your require the property to contain a single quotes one option would have be to use single quotes for the expression attribute and double to surround the string value.

    <when expression='"${companies}" != empty' >

This is fully compliant XML and work as expected in runtime, however the Anypoint Studio editor will flag this as an error (even if it is not) so because of this bug in Anypoint Studio I would advise you to use the workaround you found yourself and load the property into a flow variable and then use it in the expression.

<set-variable variableName="company1" value="${companies1}" doc:name="company1"/>
<when expression="#[flowVars.company1.isEmpty()]">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM