How to get Env specific yaml file inside java class in Mule 4 - spring

Hi I Have enviornment specific yaml files in my Mule Application . I need to read these file in my Java Class . My Yaml File name will be formed as
properties/${mule.env}/config-${mule.env}.yaml
So for each env it will load speicfic file. I need to get the respective env file in my java class . How i can do that . I tried like below in Java Class but its coming as null
#Value("${rixml.VersionID}")
private String version;

You can form the YAML filepath in your mule flow and then pass it as an argument to your java class.In your java class you can read the YAML file(from the file path passed to it), parse it and extract the value that you want.

Related

spring boot read property value from file content (not property file)

Is there a way to inject the value of a property from file content?
In my case i want to read a public certificate:
#ConstructorBinding
#ConfigurationProperties(prefix = "certificate")
#Value
public class Certificate {
String publicKey;
}
The certificate is in a file with content like
-----BEGIN CERTIFICATE-----
MIIC3DCCAcSgAwIBAgIGAYYWvEf6MA0GCSqGSIb3DQEBCwUAMC8xLTArBgNVBAMM
JDhjOGVmNjQxLTEwMGEtNDUxMi1iOTFhLWM3Mzc5NDcwMTdjMzAeFw0yMzAyMDMx
...
4/eJiZvtUhlPTZAeBCbmwHhLFufMRrYtOje/JLDcXFUhF4Ypb6BITbbWijJ7oMqP
1Amyt3eKiVhFdIVk1U4gp19wda4oeKP+5gaPTvAlYrN+EWdC1lUDRBipcM5zioFk
CwELjzRA2Dzg059g93NN7Q==
-----END CERTIFICATE-----
Currently i have 2 ways to load this as property:
load it in env variable with shell CERTIFICATE_PUBLIC_KEY="$(cat ./certs/device-cert.pem)" - need to run before
change the file to a property file beginning with certificate.publicKey=
and adding "\n" at every line end and adding it as additional property source
Is there a way to load the file content directly into a property on start?
At the moment i don't want to loose the Spring Boot Property feature - because it is really flexible.
If not possible i can of course just load the file and use its content.
It is possible with Spring. You can add the following option to your application properties file:
spring:
config:
import: configtree:/specify_the_path_where_your_file_is_located/
Then you should put your public key file to that location and give a name to this file according to your desired configuration properties:
certificate.publicKey
And you're done here! During application startup the content of that file will be injected to that property and will be both accesible from your configuration properties or from Environment bean

How to autowire 2 symfony services, which use the same interface and addtional arguments

I have two services, which both use the same interface and one is injected in the other.
With this configuration in the service.yaml everything worked well:
# fix autowiring for 2 services using the same interface
App\Domain\ListService: ~
App\Domain\SapService\SapListService: ~
App\Domain\ListService $sapListService: '#App\Domain\SapService\SapListService'
App\Domain\ListServiceInterface: '#App\Domain\ListService'
following the official documentation found here.
Now one of my services needs the information in which environment the class is currently running.
In a simple service configuration I would write it like this:
App\Service\FooService:
arguments:
$env: '%env(APP_ENV)%'
But how do I add the environment information in my more complex situation?
I tried this:
App\Domain\ListService: ~
App\Domain\SapService\SapListService: ~
App\Domain\ListService $sapListService: '#App\Domain\SapService\SapListService'
arguments:
$env: '%env(APP_ENV)%'
App\Domain\ListServiceInterface: '#App\Domain\ListService'
which throws this error:
The file "/var/www/src/../config/services.yaml" does not contain valid YAML: Unable to parse at line 52 (near " arguments:").
What is the proper formatting to parse the environment information into my service?
I tried manual wiring like this:
public function __construct(
ListServiceInterface $sapListService,
#[Autowire('%env(APP_ENV)%')]
string $env
) {
$this->sapListService = $sapListService;
$this->env = $env;
}
which gives me the error:
In DefinitionErrorExceptionPass.php line 54:
Cannot autowire service "App\Domain\ListService": argument "$env" of method "__construct()" is type-hinted "string", you should configure its
value explicitly.
Looks like the Autowire annotation is only available with symfony 6
You should use the namespace in the redis config to separate the different environments and not use the APP_ENV var to create the keys to store.
I guess the best solution is to prefix the cache key with the environment via the cache configuration in the cache.yaml file.
The solution is described here

How to copy/access file in Kubernetes

Working on Spring boot microservice and we have file in the folder file/Service.wsdl
And we are accessing the file using
#Value("file:file/Service.wsdl")
String WSDL_LOCATION;
In Kubernetes, we are getting "file/directory not found"...How to solve this.
You can try this #Value("${file:file/Service.wsdl}")
String WSDL_LOCATION;
Or else you can create config map of using this file and use that path .

Loading multiple config file from spring cloud config server

Hey I got to know how to check properties in config server i.e. http://{Config server URL}:{Port}/{ServiceID}/{Profile}
Right now I am facing a problem where I've to read multiple properties file from cloud config server. Ex: Right now I've to read properties file(huge file) from a single property file i.e. employee.properties
I don't want huge list of properties file in single folder. like
employee-dev.properties
employee-dit.properties
employee-sit.properties
employee-uat.properties
employee-preprod.properties
employee-prod.properties
customers-dev.properties
customers-dit.properties
customers-sit.properties
customers-uat.properties
customers-preprod.properties
customers-prod.properties
.
.
.
and soo on.
My requirement is I need to maitain a list of properties file and config server will provide me these values. like :
**{env}/employee.properties**
**{env}/customers.properties**
Here env could be any environment like dit, sit, prod etc.
Hope I'll get answer here.
If you don't want to have all properties files in an only folder, so you need to add this configuration on config server properties file.
1. Match all options
spring.cloud.config.server.native.searchLocations = [classpath:/, classpath:/config, classpath:/config/{application}, classpath:/config/{application}/{profile}]
2. Match only /application-name/application-name-profile.properties
spring.cloud.config.server.native.searchLocations = classpath:config/{application}
Then you'll have matched based on application name folder for example.
resources/config/application-name/application-name-properties.

Generate Swagger json at Build time

I'm working on a ASP.NET Core WebAPI project and I'm trying to find a way to generate swagger json at build time ?
As i'm working with 4 environments, i'd like to have 4 swagger.json with a different name of my choice, like:
swagger_{appName}dev{buildNumber}.json
swagger_{appName}demo{buildNumber}.json
swagger_{appName}int{buildNumber}.json
swagger_{appName}staging{buildNumber}.json
Is it also possible to edit the fields in the json ? I'd like to edit (depending of the environment) the following fields : host, schemes and basePath.
I'm using Swashbuckle.AspNetCore but it appears it doesn't have an option to do such task or am I wrong ?
Thanks in advance
You have the option of generating OpenApi json file(s) from the command line without deploying using the Swashbuckle.AspNetCore.Cli nuget package.
The command will look something like this and can be added as a "post build" script
dotnet <nugetpackages>\Swashbuckle.AspNetCore.Cli\bin\$(Configuration)\netcoreapp<ver>\dotnet-swagger.dll tofile --host http://localhost --output swagger.json <bin>\<AssemblyName>.dll v1
more details can be found here https://github.com/domaindrivendev/Swashbuckle.AspNetCore#swashbuckleaspnetcorecli
I'm not sure about generating separate files for each environment offhand
The simple way is using PowerShell task to update json file and save with another name.
Simple workflow:
Read the file through Get-Content script
Using ConvertFrom-Json to converts content to object
Set the property value to update object
Using ConvertTo-Json to convert an object to a JSON-formatted string
Using Set-Content to write content to a file
For detail code, you can refer to this thread: how do I update json file using powershell.
The build number value is stored in the predefined variable: Build.BuildNumber (PowerShell: $env:BUILD_BUILDNUMBER)

Resources