Contact Us 1-800-596-4880

Handling Enums

The DevKit provides an easy way for handling enumerated types. Methods annotated with @Processor or @Source can receive enums as parameters and these can be passed from Mule as strings. It is also possible to use enumerated types along with java.lang.Collection and java.lang.Map.

Let’s suppose the following extension:

@Module(name = "enums")
public class EnumModule {

    private Map<Property, String> properties;

    @Configurable
    private Property myProperty;

    @Processor
    public void setProperties(Map<Property, String> properties) {
        this.properties = properties;
    }

    @Processor
    public String getPropertyValue(Property property) {
        return properties.get(property);
    }

And this enum class:

public enum Property {

    FIRST_NAME, LAST_NAME, ADDRESS
}

Then the following are valid:

<enums:config myProperty="FIRST_NAME"/>
<flow name="setProperties">
    <enums:set-properties>
        <enums:properties>
            <enums:property key="FIRST_NAME">Federico</enums:property>
            <enums:property key="LAST_NAME">Recio</enums:property>
        </enums:properties>
    </enums:set-properties>
</flow>
<flow name="getPropertyValue">
    <enums:get-property-value property="LAST_NAME"/>
</flow>

How It Works

Under the covers, the DevKit generated a Transformer for all the enumerated type that appear as a parameters of @Processor and @Source methods, or instance fields annotated with @Configurable. So when a string is entered in the xml for an enumerated type, the enum constant of the enum type with the specified name is passed to the method or assigned to the field.

Also, the schema file generated adds restrictions as to what values can enums attributes take in the xml so that only matching enumerated values can be used. For example, since the possible values for the Property enum are FIRST_NAME, LAST_NAME and ADDRESS then the following is not valid and the validation of the schema will fail:

<flow name="getPropertyValue">
    <enums:get-property-value property="LAST_NAME"/>
</flow>