Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

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:


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@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:


         
      
1
2
3
4
public enum Property {
 
    FIRST_NAME, LAST_NAME, ADDRESS
}

Then the following are valid:


         
      
1
<enums:config myProperty="FIRST_NAME"/>

         
      
1
2
3
4
5
6
7
8
<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>

         
      
1
2
3
<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:


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

In this topic: