Is there a way to set application.conf location in Scala? - scala

If i'm using Java, I can set custom application.properties location with something like this:
#PropertySources({
#PropertySource(value = "file:${APP_CONF_DIR}/application.properties", ignoreResourceNotFound = true)
})
Is there any way to do the same thing in Scala without using "-Dbla.bla=app.config" on application start

The behavior of Lightbend Config around loading application.conf is mostly from ConfigFactory.load(), which will look for application.conf or the file/resource given by Java properties (the -Dconfig.file and friends in your question).
If not interested in setting the alternative to application.conf through Java properties, one can load a different resource from the classpath with ConfigFactory.load("foo") to load foo.conf. Alternatively if one is loading a file, one can use ConfigFactory.load(ConfigFactory.parseFile(file)), where file is a java.io.File.

Related

How do you overwrite array elements in HOCONS?

I have this in a .conf file and I want to overwrite the value at index 0 in array field1
database {
master {
field1:["a","b","c"]
}
}
and I run the application via sbt like this:
sbt -Ddatabase.master.field1.0="11.111.11.111:3306" package
then I look inside the jar at the .conf file and nothing is changed.
This guide indicates to change each array element by index instead of the whole array (which I also tried but to no avail): https://salsa.debian.org/java-team/typesafe-config/blob/master/HOCON.md#array-and-object-concatenation
How do you overwrite array elements in HOCONS?
I think the problem is, that your hocon is part of what you try to pack, but the -D will give the params to the JVM of sbt. Why should the config of the sbt's JVM have any influence to the .jar you pack?
Edit
Adrian taught me, that this is actually possible. Still my solution below is what I would prefer. It is explicit and good to understand. Some params and the sbt call seems to me to not be nice and clean.
I guess you want to have an environment specific database config.
You could start the application with your config as you tried with sbt or put all configs for different systems in different hocons and load the hocons depending on the system you start, which you can define by a parameter for the program.
Look at the docs to see how to load additional files.

reference.conf vs application.conf files in Akka

I am new in Akka and I am trying to understand the difference between the reference.conf and application.conf files?
What is the proper way to use them? What kind of variables should I have in each file?
reference.conf is a file for library developers. All properties required by the library should be mentioned there and filled with reasonable default values when possible.
application.conf is a file for application developers. A developer can set values for properties used by libraries in case they are not the same as defaults.
Also he/she can declare his own properties and refer them from the code.
When ConfigFactory.load is called all reference.conf and application.conf files are merged into one configuration and application.conf has higher precedence.

Setting Cassandra password in reference.conf using Scala

How can an attribute be set programmatically in a reference.conf file?
For example, I am using something like this in Spring to set the attributes of the keystore:
System.setProperty("server.ssl.keyStore", "keystore.jks")
System.setProperty("server.ssl.keyStorePassword", "password123")
Same way, you can override configuration from the reference conf file using system properties, they have the highest precedence order as described here:
https://github.com/lightbend/config#standard-behavior
Please be aware you need to do it before the config is loaded by the class that uses it (via ConfigFactory.load()) and if any other class has already used ConfigFactory, then a call to ConfigFactory.invalidateCaches() will also be required, otherwise the cached value will be used.

How can I save a property using the Props object in scala Lift?

First of all, should I use Lift Props to store specific configurations of my App? Lift documentation explains that you can use this Props to configure the enviroment (production, testing, etc etc.) but never says if you can use them for other purposes.
If there's nothing wrong in using them, how can I store these properties (save them). All the functions that I can see in Props object are getters. E.g if I have a property myapp.myconf1
I would like to do something like:
Prop.save("myapp.myconf1","value1")
Is this possible using Props or should I use other libs like Typesafe config or java props?
Thanks in advance!
The Lift Props object isn't intended to be a read/write store. It's a repository for deployment specific config info (i.e. the settings that are different between your development/test/release environments). The file is stored within the classpath and if you are deploying as WAR file I don't believe there would be any way to change it even if an API existed.
If all you need is to store name value pairs then java props should work fine.

How to use Ostrich

I have a task to collect and report some runtime statistics for my application. Ostrich looks quite friendly in both API and feature set. But I can't find any documentation about the most of declared features. Especially it is difficult to configure stats reporting through the web interface without any understanding of configuration principles.
So my main question: is there any documentation besides the README?
If no, could someone give an example of the following features (all of them are from the top of README):
load & reload per-environment configuration (there is example on SO already, but what if I want to use a classpath resource? how to define an environment? how to reload configuration?)
report statistics into log files
Or (perfectly) give a good architectural overview of configuration in Ostrich so I can find some way to do configuration by myself.
Ostrich config files are just regular scala classes, so if you want to load a classpath resource, you probably should create the config instance in code instead.
Here is how I load default config if -f command-line arg is not specified:
val runtime = RuntimeEnvironment(this, args)
val server =
if (runtime.configFile.exists) {
runtime.loadRuntimeConfig[Server]()
} else {
(new RPCServerConfig)()(runtime)
}
Note that you have to
- create config instance
- then call it's apply() method
- then apply(runtime) on the result
You can easily extend this to load different configs depending by e.g. lift's Props.mode