reuse parameters defined within config.parmeters file in java - java

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");

Related

How to resolve config from file and also few config programmatically?

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);

Get value from application-lcl.properties in an xml configuration Spring

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>

Copy SmbFile from one remote network share to another remote network share

I try to copy files from one network share to another.
When I run the code it says "failed to copy". But it is able to create the folder structure in the target folder.
That means this folder exists on target share after run:
\DiskStation\OpenKM Import Handled\20200728-132700\Strato\2013
But no file is created. You can see source and target folder in the first exception.
What am I doing wrong? In case it is important. The app runs on windows. The differrent shares are on a Synology Diskstation.
Any help is appreciated.
Thanks
I use jcifs.smb package for that.
The relevant lines are:
SmbFile targetFolder = getMoveToTargetFile(targetToMoveTo, newDocName, def);
SmbFile targetFile = new SmbFile(targetFolder, String.format("%s%s", targetToMoveTo, newDocName));
entry.copyTo(targetFile);
The execptions are:
jcifs.smb.SmbException: Failed to copy file from [smb://;OpenKM:xxxx#Diskstation/OpenKM Hot Folder/Strato/2013/DRP48646659.pdf] to [smb://;OpenKM:xxxx#Diskstation/OpenKM Import Handled/20200728-132700/Strato/2013/DRP4864665(2).pdf]
at jcifs.smb.SmbCopyUtil.copyFile(SmbCopyUtil.java:186)
at jcifs.smb.SmbFile.copyRecursive(SmbFile.java:1390)
at jcifs.smb.SmbFile.copyTo(SmbFile.java:1441)
at com.engst.test.java_project.App.handleFileEntry(App.java:272)
at com.engst.test.java_project.App.handleDirectoryEntry(App.java:181)
at com.engst.test.java_project.App.handleDirectoryEntry(App.java:194)
at com.engst.test.java_project.App.handleDirectoryEntry(App.java:194)
at com.engst.test.java_project.App.main(App.java:100)
Caused by: jcifs.smb.SmbException: The filename, directory name, or volume label syntax is incorrect.
at jcifs.smb.SmbTransportImpl.checkStatus2(SmbTransportImpl.java:1461)
at jcifs.smb.SmbTransportImpl.checkStatus(SmbTransportImpl.java:1572)
at jcifs.smb.SmbTransportImpl.sendrecv(SmbTransportImpl.java:1027)
at jcifs.smb.SmbTransportImpl.send(SmbTransportImpl.java:1543)
at jcifs.smb.SmbSessionImpl.send(SmbSessionImpl.java:409)
at jcifs.smb.SmbTreeImpl.send(SmbTreeImpl.java:472)
at jcifs.smb.SmbTreeConnection.send0(SmbTreeConnection.java:404)
at jcifs.smb.SmbTreeConnection.send(SmbTreeConnection.java:318)
at jcifs.smb.SmbTreeConnection.send(SmbTreeConnection.java:298)
at jcifs.smb.SmbTreeHandleImpl.send(SmbTreeHandleImpl.java:130)
at jcifs.smb.SmbTreeHandleImpl.send(SmbTreeHandleImpl.java:117)
at jcifs.smb.SmbFile.openUnshared(SmbFile.java:693)
at jcifs.smb.SmbFile.openUnshared(SmbFile.java:655)
at jcifs.smb.SmbCopyUtil.openCopyTargetFile(SmbCopyUtil.java:68)
at jcifs.smb.SmbCopyUtil.copyFile(SmbCopyUtil.java:124)
... 7 more
Refer this .
The filename, directory name or volume label syntax incorrect
Maybe directory or filename have invalid character such as white space.
You can try to use english character only.And if that works try challenge abother thing.
Thabk you.
I found now a solution that works for me. I am now getting SmbResourece for the networks shares and based on those I get SmbFile objects to do the copy operation.
I can also omit username and password in my connectString variables.
With that approach I can copy the files.
Feel free to let me know how I can make that smoother or more best practice like.
Thanks.
SmbFile srcFile = null;
try {
Properties prop = new Properties();
prop.put( "jcifs.smb.client.enableSMB2", "true");
prop.put( "jcifs.smb.client.disableSMB1", "false");
prop.put( "jcifs.traceResources", "true" );
Configuration config = new PropertyConfiguration(prop);
BaseContext bc = new BaseContext(config);
NtlmPasswordAuthenticator auth = new NtlmPasswordAuthenticator("", "OpenKM", "xxxx");
CIFSContext ct = bc.withCredentials(auth);
String connectString = "smb://192.168.10.10/OpenKM Hot Folder/";
connectString = connectString.replace('\\', '/');
SmbResource sr = ct.get(connectString);
srcFile = new SmbFile(sr, "Strato/2013/DRP46412852.pdf");
BaseContext bc2 = new BaseContext(config);
NtlmPasswordAuthenticator auth2 = new NtlmPasswordAuthenticator("", "OpenKM", "xxxx");
CIFSContext ct2 = bc2.withCredentials(auth2);
String connectString2 = "smb://192.168.10.10/OpenKM Import Handled/";
connectString2 = connectString2.replace('\\', '/');
SmbResource sr2 = ct2.get(connectString2);
SmbFile targetFile = new SmbFile(sr2, "20200728-223352/Strato/2013/paul(2).pdf");
srcFile.copyTo(targetFile);
} finally {
srcFile.close();
}

How to put '<' character in java properties file

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.

To get the content from .properties file into selenium framework

# 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.

Categories