I have a project A that I want to use from a project B, and I would like to pass some configuration parameters into the project A using a XML configuration file.
In other words, suppose I am implementing something like my own spring-data-mongo (for instance) and I want to configure my Mongo instance from a configuration file in which I have defined as follows:
<mongo: mongo host = "$ {mongo.hostname}" port = "$ {mongo.port}">
<mongo: options connections-per-host = "8"
threads-allowed-to-block-for-connection-multiplier = "4"
connect-timeout = "15000"
max-wait-time = "1500"
auto-connect-retry = "true"
socket-keep-alive = "true"
socket-timeout = "60000"
slave-ok = "true"
write-number = "1"
write-timeout = "0"
write-fsync = "false" />
How do I achieve something like this?
Related
I'm using JIB (not super relevant) and I want to pass in variables from command line in my deployment script.
I append using -PinputTag=${DOCKER_TAG} -PbuildEnv=nonprod in my gradle command, which is cool. But when it's missing, I want that ternary to kick in.
I'm getting the error:
Could not get unknown property 'inputTag' for project ':webserver' of type org.gradle.api.Project.
def inputTag = inputTag ?: 'latest'
def buildEnv = buildEnv ?: 'nonprod'
jib {
container {
mainClass = 'com.example.hi'
}
to {
image = 'image/cool-image'
tags = ['latest', inputTag]
}
container {
creationTime = 'USE_CURRENT_TIMESTAMP'
ports = ['8080']
jvmFlags = ['-Dspring.profiles.active=' + buildEnv]
}
}
Found Solution
def inputTag = project.hasProperty('inputTag') ? project.property('inputTag') : 'latest'
def buildEnv = project.hasProperty('buildEnv') ? project.property('buildEnv') : 'nonprod'
This seems to be working, is this the best way?
How about this?
image = 'image/cool-image:' + (project.findProperty('inputTag') ?: 'latest')
Note jib.to.tags are additional tags. jib.to.image = 'image/cool-image' already implies image/cool-image:latest, so no need to duplicate latest in jib.to.tags.
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<employee name = "abc" empId = "100" location = "goa" dept = "IT" />
<facilities transport="Y" mealcard = "" driver = "Y"/>
<reportees manager = "XXX" lead = "XXX" VP = "XXX"/>
<employee name = "XYZ" empId = "101" location = "mombai" dept = "IT" />
<facilities transport="Y" mealcard = "" driver = "Y"/>
<reportees manager = "XXX" lead = "XXX" VP = "XXX"/>
<employee name = "PQA" empId = "102" location = "delhi" dept = "IT" />
<facilities transport="Y" mealcard = "" driver = "Y"/>
<reportees manager = "XXX" lead = "XXX" VP = "XXX"/>
above is my XML. I've to validate the same. I'm using DBUnit to compare the same XML with other XML (other XML has the same content but generated from different source). While creating dataset I'm getting "org.dbunit.database.AmbiguousTableNameException".
I've SQL query for every XML tag. E.g. For employee tag I'm querying on empID. for facilities I'm querying on mealCard etc etc. I'm looping through the XML tags. first iteration goes fine without error. I'm using QueryDataSet.addTable(table_name ,query) to add tables i.e xml tags. But when next iteration comes and I'm trying to add employee tag again I'm getting the above stated error.
Configure the schema name so dbUnit knows which to use. See Why am I getting an "AmbiguousTableNameException"?
I resolved this issue. I'll tell you how. In dataset for repeating tags I just added random numbers e.g. in my above XML employee is repeating tag hence I added employee_123 for the first instance in dataset for next instance I added employee_098 this is how I manage to get rid of AmbigiousTableNameException. Later I used RegEx to remove the "_[randomNumber]" appended to employee tag. The code is running successful.
#DBUnit(qualifiedTableNames = true)
this setting helped me
I'm currently working with Cucumber and Java. I would like to retrieve that path of a file from ITestResult.
I'm currently retrieving the parameters with:
Object[] test = testResult.getParameters();
However the only thing I can access would seem the be the first objects name and nothing else.
test = {Object[1]#1492}
0 = {CucumberFeatureWrapper#1493} "Links at EDM Documents View,"
cucumberFeature = {CucumberFeature#1516}
path = "test/01-automation.feature"
feature = {Feature#1518}
cucumberBackground = null
currentStepContainer = {CucumberScenario#1519}
cucumberTagStatements = {ArrayList#1520} size = 1
i18n = {I18n#1521}
currentScenarioOutline = null
I cannot see anyway of retrieving path = "test/01-automation.feature" under cucumber feature.
Have you tried something like ((CucumberFeatureWrapper)test[0]).getCucumberFeature().getPath()?
I want to prohibit resolving of a.b. I want to substitute param from another config. Like this:
val d = ConfigFactory.load(ConfigFactory.parseString(
"""
|param = x
|a.b = ${param}
""".stripMargin))
val a = ConfigFactory.parseString("param = 1")
val result = a.withFallback(d).resolve()
In this case param gets value 1, but a.b remains x
I've tried to set ConfigResolveOptions.defaults().setAllowUnresolved(true) when loading config d, but that doesn't work.
How can I overcome this?
The problem is that Config.load is resolving the substitution immediately. If you take that out it resolves like you want it to:
val p = ConfigFactory.parseString(
"""
|param = x
|a.b = ${param}
""".stripMargin)
val a = ConfigFactory.parseString("param = 1")
val result = a.withFallback(p).resolve()
println(result.getString("a.b"))
This prints 1.
You don't need to use Config.load unless you want to use reference.conf, etc. If you do want to use Config.load then you should do it after you have composed all the configs together using withFallback.
For example, this also prints 1:
val p = ConfigFactory.parseString(
"""
|param = x
|a.b = ${param}
""".stripMargin)
val d = ConfigFactory.load(p)
val a = ConfigFactory.parseString("param = 1")
val result = a.withFallback(p)
val loaded = ConfigFactory.load(result)
println(loaded.getString("a.b"))
Or, say you have an application.conf with include that you want to use with ConfigFactory.load() (per your comment).
If application.conf looks like
include "foo"
and foo.conf looks like
a.b = ${param}
then this prints 1 also:
val a = ConfigFactory.parseString("param = 1")
val app = ConfigFactory.load("application", ConfigParseOptions.defaults,
ConfigResolveOptions.defaults.setAllowUnresolved(true))
val result = a.withFallback(app).resolve
println(result.getString("a.b"))
In general, if you want A to override B to override C then you should use A.withFallback(B).withFallback(C).
I struggled a bit with the same thing: trying to use "fallbacks" to override values, when it was designed for layering/merging configs
Assuming I understand your use case, I recommend instead using file includes.
In my application.conf I have the default value
a.b = "placeholder"
And at the bottom I have the following include
# Local overrides - for development use
include "local.conf"
And finally in local.conf
param = 1
a.b = ${param}
The end result is that a.b will be overridden with 1
Found a workaround for my problem:
So If I have config file application.conf which uses include to include config files which contain substitution syntax and files which contain declaration of the config values which are going to be substituted.
val a = ConfigFactory.parseString(s"""param = 1""")
val z = ConfigFactory.parseResources("application.conf") //this doesn't resolve substitutions
val result = a.withFallback(z).resolve().withFallback(ConfigFactory.load("application.conf"))
Below is a small example that I generally use in groovy, but I want something similar in java.
Configuration file
datastore{
oracle{
host="localhost"
port=1521
dbname="orcl"
}
db2{
host="localhost"
port=12807
dbname="sppd"
}
}
Groovy
public class Configuration {
public static final def cfg = new ConfigSlurper().parse(new File("configuration").toURL())
static main(args) {
println cfg.datastore.oracle.host
println cfg.datastore.db2.host
}
}
I'm guessing that you're wanting to access a property by its full path, e.g. datastore.oracle.host from Java like you can in Groovy. If so, do this:
ConfigObject config = new ConfigSlurper().parse(new File("configuration").toURL());
Map flattenedConfig = config.flatten();
String oracleHost = (String) flattenedConfig.get("datastore.oracle.host");
Better than Java properties because the type is maintained. From a Groovy User list post.
That is too much dynamic groovyness, but can be expressed in XML, properties, etc. You can try Common Configuration, which would express the same data:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<gui-definition>
<colors>
<background>#808080</background>
<text>#000000</text>
<header>#008000</header>
<link normal="#000080" visited="#800080"/>
<default>${colors.header}</default>
</colors>
</gui-definition>
And read with:
XMLConfiguration config = new XMLConfiguration("tables.xml");
String backColor = config.getString("colors.background");
String textColor = config.getString("colors.text");
String linkNormal = config.getString("colors.link[#normal]");
You can also give a try to PropertyConfiguration:
# Properties definining the GUI
colors.background = #FFFFFF
colors.foreground = #000080
window.width = 500
window.height = 300
Loaded with:
Configuration config = new PropertiesConfiguration("usergui.properties");
String backColor = config.getString("colors.background");