Search code examples
wso2wso2-micro-integrator

Is there a way to access properties inside the class mediator from outside the class mediator?


I am trying to access the property named logProperty which is inside the class mediator outside of it. I need to log the value of the logProperty from that log mediator. But it prints null. Why is that? Isn't there a way to access class mediator properties globally?

<class name="com.example.LogMediator">
       <property name="logProperty" value="log"/>
</class>
<log level="full">
       <property expression="$ctx:logProperty" name="After Class Mediator"/>
</log>

This is my latest code,

LogMediator

private String logProperty;

public boolean mediate(MessageContext context) {
     context.setProperty(logProperty, "result");
}

// getters and setters

Solution

  • You can't access it the way you are trying. As a workaround, you can set the value to the message context from within the class mediator.

    context.setProperty("logProperty",  logPropertyValue);
    

    Another option is to pass the value to the Class mEdiator through a property.

    <property value="log" name="logProperty"/>
    <class name="com.example.LogMediator">
           <property name="logProperty" expression="$ctx:logProperty"/>
    </class>
    <log level="full">
           <property expression="$ctx:logProperty" name="After Class Mediator"/>
    </log>