Application Configuration Properties on Payara (how to read them) - java

I would like to use Application Configuration Properties that you can configure using the user interface of payara (for example):
My question is: how can I access these values on java code side? Through annotation? Through some sort of context instance maybe? Is it even possible?
I naively tried with System.getProperty with no success.

I don't believe there is a way.
An alternative would be to use system properties along with variable substitution via ${} syntax or environment variable via ${ENV.XXX}

Related

read ENVIRONMENT_VARIABLE from application-env.yml in plain java

I have a simple java web service that uses javalin framework. I want to deploy it in multiple environments, so the env variables are different for each deployment.
I basically want to read
application-env.yml
keystore:
path: ${KEY_STORE_PATH}
password: ${KEY_STORE_PASSWORD}
from application-env.yml without using Spring.
And then use them in java.
Properties properties = PropertiesLoader.loadProperties();
System.out.println(properties.getProperty("keystore.password"));
// ${KEY_STORE_PASSWORD}
System.out.println(System.getenv("KEY_STORE_PASSWORD"));
// hunter2
How do I get hunter2 from properties?
EDIT:
i didnt get what I wanted, ended up doing this:
https://stackoverflow.com/a/73539823/2948875
You could use something like SnakeYAML, ie https://www.baeldung.com/java-snake-yaml

Setting spring.profiles.active from an environment variable in Spring Boot with Gradle

My company has a standard way to specifying the environment a web service should run in, specifically 'development' and 'production' by using an environment variable APP_ENV. Each of these environments have a separate config file: application-dev.yml and application-prod.yml.
In spring boot, we can use SPRING_PROFILES_ACTIVE or equivalents to do this, but what I really want to do is grab the VALUE from APP_ENV, and use that as the profile.
I've got some code
SpringApplication app = new SpringApplication(Application.class);
springApp.setAdditionalProfiles(System.getenv("APP_ENV"););
springApp.run(args);
Any tips on how to do this a bit cleaner? I'm getting
java.lang.IllegalStateException: Failed to load ApplicationContext errors on various gradle tasks that make this solution kind of flimsy.
you can use System.getProperty which will always return a String like below,
springApp.setAdditionalProfiles(System.getProperty("APP_ENV", "dev"));
In the above snippet, we use System.getProperty(“APP_ENV”) to extract the value of the property APP_ENV. We also are making use of the default value so if the property is not available in the system, getProperty returns dev.

Datasource details as System properties?

Is it possible to pass the properties of a datasource to quarkus by using System properties or environment variables for example instead of using the application properties file, pass
-Dquarkus.datasource.url=jdbc:mysql://localhost:3306/ib24_booking?useSSL=false
so that the values can be passed differently according to the environment I am in.
It should work as you said. You can find more details here https://quarkus.io/guides/datasource

How to use web.xml to define security realm / jaas domain

I've been tasked to have my (Java-GWT) web app determine the environment it's in based on the hostname of the server the app is running in. This would be easy enough to do if this check existed in the code (via InetAddress.getLocalHost().getHostName() although not preferred I know), but the request is that I add this configuration to the web.xml via the method mentioned in the title.
I don't know much about defining security realms or jaas domains but what I need is simple. I simply need to read in the hostname of the server and if the string patter prod exists, set a flag or value that the app code can access and decide against. Hopefully this makes sense.
If this strategy seems like a flawed one altogether, I'm open to suggestions on the best way to have my app determine which environment it's running in.
If I understand well, you want you application to know what environment it is running on. This has nothing to do with security (or at least I don't see it)
I would set the environment descriptive name as a property external to the application, for example as a custom JNDI resource, system property o environment property. Preferable the first in order to let the application declare its dependency, via a <env-entry>
https://docs.oracle.com/cd/E19857-01/820-1639/6nda10e5g/index.html#bhanr

Setting Runtime Property in Web Application

I am planning to use JGroups in a web application.
JGroups by default uses IPv6 for multicasting of messages. JGroups can only be configured to use IPv4 by setting a property like the following (see docs)
-Djava.net.preferIPv4Stack=true
This does not work if set in code. What are my options when running a war file in an application server for setting this property, specifically tomcat and glassfish?
It seems to me the following route might work. First, add a ServletContextListener to your web app and register it in web.xml before JGroups is loaded.
Now, inside the contextInitialized method, use System.setProperty("java.net.preferIPv4Stack", "true").
Disclaimer: not tested.
Unless you have a specific reason to put this in code, it can easily be configured for Tomcat by setting the environment variable CATALINA_OPTS (to -Djava.net.preferIPv4Stack=true). I think the proper place to do this is to create ${CATALINA_HOME}/bin/setenv.sh and place the setting in there.

Categories