Mule Expression component not able to fetch value from properties file - mule-component

I want to fetch values dynamically from properties so I have implemented one poc. In that poc I have declared one object with value in mule expression component. After that I am fetching the value key from properties file. It is showing exceptions while testing the application.
Exception MSG: Root Exception stack trace: unresolvable property or identifier: $
EX-1:
flowVars.deptCode=21432143;
property3=${flowVars.deptCode};
EX-2:
property3=${21432143};
In the above two examples ex-2 has worked fine and ex-1 has failed .
Please let me know if anyone have clarity on that.
Thanks,
Praveen

Mule is using Spring Properties which can be kept in a seperate properties file and then retrieved/used in your application via ${propertyName}.
A property placeholder is used to define where you keep those properties.
Ex 1 is not possible because properties are not aware at all of any variables or properties inside of your Mule application.
Another issue is that those files will be loaded when the application is started.
If you change the value of a property a restart of your application is needed, so your approach isn't going to work.
More info in the docs here:
https://docs.mulesoft.com/mule-user-guide/v/3.8/configuring-properties

You can use dataweave script to dynamically read values from property file
#[dw("p(flowVars.deptCode)")]

Related

How to get Spring property with embedded property variable as a literal

I currently have a Spring application.properties with 2 properties defined as follows:
validator.url=http://location.com/${domain}/${family}
query.validator.url=${validator.url}
Currently my application resolves query.validator.url to be ${validator.url}
is there anyway that I can have it resolve to the same value as validator.url which is http://location.com/${domain}/${family}
Note: ${domain} and ${family} don't resolve, they are handled in code.
Try the following:
validator.url=http://location.com/#{'$'}{domain}/#{'$'}{family}
query.validator.url=${validator.url}
Both validator.url and query.validator.url will resolve to http://location.com/${domain}/${family}
Alternatively, you can create a property for the $, which you can then use inside validator.url, e.g.:
var=$
validator.url=http://location.com/${var}{domain}/${var}{family}
There's an issue regarding this on Spring's JIRA

Manually force reading environment spring property

If we can use spring context in order to read properties which where already loaded from a property file, like this:
ctx.getEnvironment().getProperty("viva.val")
How can I get the above line getting an updated value once I manually cahnge the property file?
I have notice that the getEnvironment().getProperty(...) remember the old value, but I somehow wants to manually set a new value for the property, so the next time it gets called, it will be the new value, or even better, telling the context to read the property file again.
Is there any way to do that (Note, I'm not using singletone for the above code, and also not using #Value) without restart or spring cloud config.

Spring Boot - external property not found

My application is trying to externalize all project properties, some ones will be inside my app and another ones will be in a folder somewhere in Windows.
I set up Spring to execute this way: --spring.config.location=file:///C:\Temp\config\application.properties,classpath:application.properties
As you can see, if the same property exists in both sides, application property will be kept (priority order). I noticed for example some properties such as "server.port" can be found if exists outside folder (file://) but if I create one such "common.acronym-name" my project can not find its value.
Why "server.port" has a different behaviour that one create by me? Is there any configuration I need to tell Spring Boot to see this external property in my project?
#Value("${common.acronym-name:}") //Just find it in application classpath
private String acronymEnv;
Thanks!
To simulate this error, just create a class to handle the banner, for example:
#Component
public class ShowBanner {
#Value("${spring.main.show-banner:}")
private String showBanner;
#PostConstruct
public void init() {
System.out.println(showBanner);
}
}
In this code if you set at external property file the property "spring.main.show-banner=false" the banner still shows in console if it is set before server section. When the banner should not appear. Because the property returns empty.
If I move the property after server section the banner disappears, because return false value as expected.
Keep in mind my application.properties project is empty.
By the way, Even running via Eclipse or java console it happens:
java -jar sample-1.2.3.RELEASE.jar --spring.config.location=file:///C:\Temp\config\application.properties,application.properties
Why?
Believe or not, the order of properties makes difference for external properties be found.
If I set this below order where "spring.main.show_banner" is the first one at the top, my properties can not be found. For example:
spring.main.show-banner=false
server.port=9043
server.session-timeout=1800
server.ssl.key-store=file:///C:/Temp/config/localhost.jks
server.ssl.key-store-password=localhost
server.ssl.key-password=localhost
So, If change to below order, everything works fine:
server.port=9043
server.session-timeout=1800
server.ssl.key-store=file:///C:/Temp/config/localhost.jks
server.ssl.key-store-password=localhost
server.ssl.key-password=localhost
spring.main.show-banner=false
Is there any reason for it? Spring Boot needs to have the properties in right order? Seems the "server" section must the first one in the properties.
thanks.

OpenEJB - replace the "classpath.ear" by other name

The app I'm working have a custom ServiceLocator, so, I need to be able to specify the name I want for the app.
In other words, instead of bind the EJB's to:
java:global/classpath.ear/project-name/JNDI
I want to bind it to:
java:global/myapp/project-name/JNDI
Setting javax.ejb.embeddable.appName in jndi.properties, passing it to new InitialContext(Properties) or as a System property doesn't seem to take any effect.
BTW: I'm using OpenEJB embedded for tests.
Thanks in advance.

Extended Properties for Spring Framework

Looking for a solution that will provide us more functionality within Spring properties such as:
nested structures
maps/lists
properties referencing other properties. Example:
city.name=Toronto
city.address=#{city.name}, 123 Ave SW
I tried EProperties (Google) and Commons Configurations (Apache) but doesn't seem to integrate very well with the Spring Framework.
Also, we're using Velocity to access properties using #springMessage("city.address"), so it needs to work for that.
Does anyone know how I can achieve the above by extending the default Properties capability?
With newest versions of Spring you can use the PropertySource mechanism. You register all your PropertySource and the order in which they are searched and then you don't have to do anything, except perhaps add this to your XML:
<context:property-placeholder />
As long as you declare only one of these without specifying local property files (the "old way"), you will be able to reference property A as the value of property B, even if they are not in the same property source.
For nested structures this may help if you don't like the properties readability:
https://stackoverflow.com/a/13470704/82609
For parsing problems you can easily handle lists and other stuff like that manually very easily:
Reading a List from properties file and load with spring annotation #Value

Resources