How access property into Mule Java Transformer - mule

I have a properties file shared on several apps. To access this properties into one app, I use this tag:
<context:property-placeholder location="classpath:br/com/empresa/configuracao/mule-apps.properties"/>
On several Mule components, like database attributes connections, I use the following expression to access the properties, p.e.: ${db.user}. It works!
But on Java Transformer, how I access the properties?
I tried the following instructions, but returned null:
System.getProperty("db.user");
message.getInboundProperty("db.user");
message.getOutboundProperty("db.user");
message.getInvocationProperty("db.user");
Is there a way to access properties into Mule Java Transformer?

This question has been answered for components here: How to get property from context property placeholder tag inside custom java component The exact same logic applies to transformers.
Use property injection:
<custom-transformer class="org.myCompany.CustomTransformer">
<property name="dbUser" value="${db.user}" />
</custom-transformer>
Don't forget to add setDbUser on your custom component!

You could use the old way of retrieving a property:
#Value("${db.user")
private String dbUser;

Related

Best way to load properties file during runtime in mule

I would like to know what would be the best way to load the properties file in mule for the following scenario.
I have a properties file where in I set all the email related fields set in it like templates, to and from etc.
All these need to be set to a specific object along with other changes to that object so I'm planning to use a Java transformer and now I need to load all those values from that properties file and send to transformer. So what would be a best approach to work in above scenario.
Load properties in Java transformer using core java load properties
Load properties using spring context and send it to transformer and access using inbound properties
Kindly let me know if there is any other better approach other than above
First, you should be able to load the property file using spring context as shown below:
<context:property-placeholder location="somename.properties" />
Then, you can set specific property value as flow variable as shown below:
<set-variable variableName="fromAddress" value="${xyz.fromAddress}" />
Finally, you can access this flow variable in your processor class as shown below:
String fromAddress = muleEvent.getFlowVariable("fromAddress");
I will suggest to use Java load properties if these properties are only used in one Transformer. There is one more benefit out of Java load properties is that after modifying your properties file you do not need to restart the application.

How to access the invocation property?

I am trying to access the _clienName from the invocation properties. I tried the below one, but doesnt help. any thoughts ? Is it really possible to access invocation property in a mule flow ?
#[message.invocationProperties['_clientName']]
Message properties:
INVOCATION scoped properties:
__httpEvent=HttpEvent #188...9{apiName=.....95574017,something=99034}
_clientId=680.....81d97344
_clientName=abcd-app
INBOUND scoped properties:
Invocation Properties can be accessed via flowVars: #[flowVars['_clientName']] or #[flowVars._clientName]
Post Mule EE 3.4 there has been a naming change on all the invocation property as FlowVariables . Even now the invocation properties will work if you try using #[flowVars.name] . In the Message properties transformer you have option to set only as invocation properties but it can be accessed as flowVars in rest of the flow.
In mule invocation scoped properties are nothing but flowVars. So you can access them by using the following mule expression
#[flowVars.'_clientName']
in the above expression no need to wrap the _clientName in square brackets, just quote them as I did.
FYI
Session scoped properties are nothing but sessionVars. So you can access them by using #[sessionVars.'_clientName'].
Though you have not asked about session scoped properties, I have answered it because by reading your question I came to know that you are asking the question by seeing the logs in the console caused by the Logger Component of Mule.
The logger, not only logs Invocation scoped properties but also session scoped properties as well. So I guess you might get a question in your mind about what are Session scoped properties, so answered it.

Setting global-property via java code

I need to set the global property grammatically. Functionally it should do exactly what the following statement should be doing
<global-property name="host-name" value="localhost" doc:name="Global Property"/>
I believe I should be able to access this property using ${host-name}. In this specific scenarion I don't want to load the property from properties file.
If you just want to resolve placeholders like ${host-name} you can extend the Spring PropertyPlaceholderConfigurer class with a custom implementation of method resolvePlaceholder, and add it as a Spring bean like this:
<spring:beans>
<spring:bean id="myConfigurer" class="my.test.MyConfigurer"/>
</spring:beans>
Your custom resolvePlaceholder is then called to resolve any unresolved properties.
It is also fairly simple to set actually set properties in the Mule context registry by getting the context from a message (message.getMuleContext()) or by implementing a MuleContextAware bean, but in those cases the property will not yet be available at the time when the property placeholders are resolved.
You would set the property like this:
context.getRegistry().registerObject("myKey", "myVal")
and access it later like this:
context.getRegistry().get("myKey")

How mule invoke the component?

After go through some guides about Mule, I have a question about the component, as it is said in mule documentation,
A simple POJO (Plain Old Java Object) component that will be invoked
by Mule when a message is received. The class or object instance to be
used can be specified using a child object factory element, or via the
'class' attribute. If the 'class' attribute is used, an object
factory cannot be configured as well. Using the 'class' attribute is
equivilant to using the propotype object factory ('prototype-object'
child element).
in this documentation just said the component will be invoked by Mule when a message is received, but a problem is Mule how to know how many methods the component have, and which method should be invoked? or there must one method in the component and no matter the method name? and also mule how to deal with the parameters the method have?
Alternatively, you can use:
<invoke object-ref="..." method="..." methodArguments="..." />
which is way more convenient than configuring entry point resolvers.
yes, I got the answer, the answer is using mule entry point

pass few properties to a custom action

What is a standart way of passing properties to a custom action without parsing them? I mean if I write "X1=X1value X2=X2value", then in my custom action X1 will be equal to "X1value X2=X2value", and X2 won't exist as a separate property. So what is the properties string format?
I don't know that there is a "standard" for serializing and serializng CustomActionData. There are a couple libraries out there though. If you happen to be using C# DTF custom actions there is a CustomActionData class that can serialize and deserialize the property collection for you. Otherwise you pretty much come up with your own pattern like:
/PROPERTYA=VALUEA /PROPERTYB=VALUEB
or
PROPERTYA=VALUEA;PROPERTYB=VALUEB
Or even an XML fragment like
<Properties>
<Property Id="PROPERTYA">VALUEA</Property>
<Property Id="PROPERTYB">VALUEB</Property>
</Properties>
The point is to serialize and deserialize so that it can be available to your deferred CA.