i have in some spring application , in application-lcl.properties a line with :
key1=value1
I want to use the value of key1 in another xml like this :
<appender name="ELASTIC" class="com.internetitem.logback.elasticsearch.ElasticsearchAppender">
<url>${key1}</url>
${key1} doesn't work. Do you know how to do it ? (the .xml already exists )
Thanks
Its a 2 step process
Load properties file into java.util.java.util.Properties class object.
Use Properties.storeToXML() method to write the content as XML
String inPropertiesFile = "application.properties";
String outXmlFile = "applicationProperties.xml";
InputStream is = new FileInputStream(inPropertiesFile); //Input file
OutputStream os = new FileOutputStream(outXmlFile); //Output file
Properties props = new Properties();
props.load(is);
props.storeToXML(os, "application.properties","UTF-8");
in the xml , put
<springProperty name="value1" source="key1"/>
and then use it by calling
<url>${value1}</url>
Related
I have a config properties file in a java project created in eclipse.
Below are the contents in the properties file.
adminApp= testAdminDemo
customerApp = testCustDemo
appHostIP = 172.22.XX.XX
adminAppURL = http://appHostIP:9049/adminApp
customerAppURL = http://appHostIP:9049/customerApp
When the appURL parameter is read from a Java class,
Properties prop = new Properties();
FileInputStream f = new FileInputStream(System.getProperty("user.dir") + "\\config.properties");
prop.load(f);
String url = prop.getProperty("appURL");
System.out.println("URL: " + url);
the output is the same value mentioned for parameter 'url' in config.parameters file:
http://appHostIP:9049/adminApp
Actually, I expected the output to be like:
http://172.22.XX.XX:9049/testAdminDemo
Is there anything wrong in this approach?
I don't want to read host ip and app name in the java class file and then form a string. Instead, the required URl should get formed in the properties file as need to deal with different apps - admin, customer, etc.
You cannot perform concatination in the property file, You have to handle the concatination in code where you are using it like below.
appHostIP = 172.22.XX.XX
adminAppURL = :9049/adminApp
String url = "http://" + "prop.getProperty("appHostIP")+ prop.getProperty("appURL");
In my use case, I have a scenario where I want to populate some of the configs from the typesafe config file itself and some of those, I want to set programmatically. For example, for below myconfig.conf file
env=staging
topic=${env}_${event}
In above, snippet, topic should be resolved as staging_someevent event where someevent is set in one of the method which resolves this value based on argument in the job using args.
I am able to resolve one of those but not both. Below code resolves ${event}
String event = "someevent";
File file = new File("myconfig.conf");
InputStream inputStream = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(inputStream);
String configText = ConfigFactory.parseReader(reader)
.resolveWith(ConfigFactory.parseString("event=" + event))
.root
.render(ConfigRenderOptions.concise().setJson(false));
System.out.println(configText);
Similarly, below code resolves ${env} but how can I resolve both?
String configText = ConfigFactory.parseReader(reader)
.resolve()
.root
.render(ConfigRenderOptions.concise().setJson(false));
System.out.println(configText);
Hi I am trying to put < sign in java properties file, so that it will show message to user as ' < symbol is not accepted ' but I am not able to put it in the file.
I tried /</, \<\, and \\<... but it did not work.
The Properties text format has no problem with either <s or >s. The exact specification of the file format is laid out in Properties.load() and makes no mention of either character. This is also very easy to verify:
public static void main(String[] args) throws IOException {
Properties prop = new Properties();
prop.put("<key>", "<value>");
System.out.println("Properties Contents: " + prop);
StringWriter writer = new StringWriter();
prop.store(writer, "<comment>");
System.out.println("\nProperties File Format:\n" + writer);
prop = new Properties();
prop.load(new StringReader(writer.toString()));
System.out.println("Properties Contents: " + prop);
}
First we construct a Properties object and add a <key>:<value> pair. Then we write the object to a Writer and print the serialized contents, then we load those contents back into a new Properties object and can see there was no data loss. This program outputs:
Properties Contents: {<key>=<value>}
Properties File Format:
#<comment>
#Sat Jul 23 23:50:50 EDT 2016
<key>=<value>
Properties Contents: {<key>=<value>}
You should be able to load a properties file with such symbols without issue.
If you're using the XML file format (via loadFromXML() and storeToXML()) you need to escape < and > just like you would in any XML document. If you are trying to read/write XML it would have been very helpful to mention that in your question.
# Compulsory Dimension to create port
xOffset=-3
yOffset=50
How to get these xOffset and YOffset in java file.I tried with inputstream but not getting.These variable should get loaded in java file.
You can use Properties class from Java library
Properties prop = new Properties();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propFileName);
prop.load(inputStream);
The you can get the values as
prop.getProperty("propertyname");
Try the following code:
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("your_config.properties");
prop.load(input);
System.out.println(prop.getProperty("xOffset"));
System.out.println(prop.getProperty("yOffset"));
} catch (IOException e) {
// ...
}
As above explained Create a Function to read the property File During or Before Selenium Driver Constructor . So you can use them in test ( Help in Desire Capability Impl).
Store the values in Public Static final ( If you don not want to change them in Selenium and use as Default Property input)
As the values are read by Java Propertie file or .config file before selenium Driver so you can use them in Driver constructor or if you don't want you can use those properties stored as Static anywhere in the project. These values act as GLOBAL param.
How can I load data from an xml file into solr using the solrj API?
Thanks Pascal. I miss worded my question, I'm actually using groovy. But in any event your approach does work, but this was my solution:
CommonsHttpSolrServer server = SolrServerSingleton.getInstance().getServer();
def dataDir = System.getProperty("user.dir");
File xmlFile = new File(dataDir+"/book.xml");
def xml = xmlFile.getText();
DirectXmlRequest xmlreq = new DirectXmlRequest( "/update", xml);
server.request(xmlreq);
server.commit();
The first arg to DirectXmlRequest is a url path, it must be "/update" and that the variable xml is a string containing the XML. For example
<add>
<doc>
<field name="title">blah</field>
</doc>
</add>
With Java 6, you can use Xpath to fetch what you need from your xml file. Then, you populate a SolrInputDocument from what you extracted from the xml. When that document contains everything you need, you submit it to Solr using the add method of SolrServer.
SolrClient client = new HttpSolrClient("http://localhost:8983/solr/jiva/");
String dataDir = System.getProperty("user.dir");
File xmlFile = new File(dataDir + "/Alovera-Juice.xml");
if (xmlFile.exists()) {
InputStream is = new FileInputStream(xmlFile);
String str = IOUtils.toString(is);
DirectXmlRequest dxr = new DirectXmlRequest("/update", str);
client.request(dxr);
client.commit();
}