retrieve property values from message broker bar file with java api - java

I'm trying to read the property values from a bar file created by message broker.
I want to do this via java. The api is here: http://publib.boulder.ibm.com/infocenter/wmbhelp/v7r0m0/index.jsp?topic=%2Fcom.ibm.etools.mft.doc%2Fbe43410_.htm
However, I can only figure out how to get the names of the properties NOT THEIR VALUES by using the deployment descriptor. I can see how to override the value that a property has, but once again, not how to retrieve the value. Another words I can see only how to write to the property not read from it. I want to do both! Call me greedy ;)
If I use the command line based utility: http://publib.boulder.ibm.com/infocenter/wmbhelp/v7r0m0/index.jsp?topic=%2Fcom.ibm.etools.mft.doc%2Faf03900_.htm
I can get the property values no problem.
But I want to get them via java if at all possible.
Thanks in advance for any help on this!

The problem was I was misunderstanding how the deployment descriptor worked. I thought that when the java API referred to overridden properties it meant ones that were over ridden in my java code. But it actually meant all the properties that had a value in the bar file.
That being said getting the values is not strait forward. You have to get all the identifiers and then pass them to getOverride();
BarFile b = BarFile.loadBarFile("C:\\BarParamTest\\myBar.bar");
DeploymentDescriptor d = b.getDeploymentDescriptor();
Enumeration<String> properties = d.getPropertyIdentifiers();
while(properties.hasMoreElements())
{
String p = properties.nextElement();
System.out.println(p + " = " + d.getOverride(p));
}
or use the following to only list properties that have values
Enumeration<String> properties = d.getOverriddenPropertyIdentifiers();

For some reason settings are not written to file, if they are not overriden or not changed.(the reason is the lack of necessity to keep the property's default value:) ) so the way to get the properties is to know their default values. But I would recommend you to use com.ibm.mq.jar library if you're able to connect to broker to read properties using method
java.util.Properties MessageFlowProxy.Node.getProperties()
from already deployed .bar.

Related

getProperty() method in java.util not able to recognize keys value variables in ${.} inside springboot's application.properties

I have a springboot application where the application.properties look like this -
keyA=valueA
keyB=valueB
keyC=${keyA}
So I know that using the #Value annotation, I can correctly get the value of keyC as "valueA". However, my program is currently taking the use of a read only file where the getProperty() method returns the value of keyC as "${keyA}" instead of "valueA".
Sample Code
read-only
public String getPropertyValue(String prop){
FileReader reader = new FileReader("app.properties");
Properties p = new Properties();
p.load(reader);
System.out.println(p.getProperty(prop));
}
getPropertyValue("keyC");
Output
${keyA}
I will not be able to modify the read only file. I want to know if there's any suitable alternative to add a variable like ${keyA} in application.properties so that it can be recognized by the getPropertyValue method. Any help would be appreciated, thanks.
You have to post-process the Properties yourself, replacing all values of which key name matches the pattern "^${(.*)}$" by the corresponding value of the key being the group 1 of the pattern. (Be careful to possible buggy cross-references of values in the .properties... that may lead to infinite loop...)

Can we parameterize the request file name to the Read method in Karate?

As I'm trying to automate the API testing process, have to pass the XML file to Read method for example,
Given request read ( varXmlFile )
FYI: XML file is present in the same folder where the feature file exists.
Doing this, its throwing an exception like this
com.intuit.karate.exception.KarateException: called: D:\workspace\APIAutomationDemo\target\test-classes\com\org\features\rci_api_testing.feature, scenario: Get Membership Details, line: 15
javascript evaluation failed: read (varXmlFile )
So Karate doesn't allow this way or can we have any other alternative ?
Suggestion please.
Thanks
Please ensure the variable is set:
* def varXmlFile = 'some-xml-file.xml'
Given request read(varXmlFile)
Or just use normally:
Given request read('some-xml-file.xml')
The problem got solved as in the variable varXmlFile holds the file name along with single quote like this 'SampleXmlRequest.xml'.
So I removed the single quote while returning from the method.

Read contents of a file containing key value pairs without normal parsing

My scenario is to read a file from a file endpoint which contains only key value paris like a property file and take a few data from it based on the key .
Any idea how to do them other that using a custom bean or java component.
I would like to know if this is possible any way in Mule or Camel.
Thanks in advance.
If you want to use a Camel route, to pickup files, then something like this
from("file:inbox")
.convertBodyTo(Properties.class)
.log("The foo value is {${body[foo]}")
.log("The bar value is {${body[bar]}")
What we then need is a type converter from java.io.File -> java.util.Properties. Which we could add to camel-core out of the box.
I logged a ticket to add that type converter out of the box in Camel: https://issues.apache.org/jira/browse/CAMEL-7312
I think to this problem explained, the very easy solution is to use java.util.Properties class. Load the file using Properties class which maintains key value pair only.

Commons configuration library to add elements

I am using the apache commons configuration library to read a configuration xml and it works nicely. However, I am not able to modify the value of the elements or add new ones.
To read the xml I use the following code:
XMLConfiguration config = new XMLConfiguration(dnsXmlPath);
boolean enabled = config.getBoolean("enabled", true));
int size = config.getInt("size");
To write I am trying to use:
config.setProperty("newProperty", "valueNewProperty");
config.save();
If I call config.getString("newProperty"), I obtain "valueNewProperty", but the xml has not been changed.
Obviously it is not the right way or I am missing something, because it does not work.
Could anybody tell me how to do this?
Thanks in advance.
You're modifying xml structure in memory
The parsed document will be stored keeping its structure. The class also tries to preserve as much information from the loaded XML document as possible, including comments and processing instructions. These will be contained in documents created by the save() methods, too.
Like other file based configuration classes this class maintains the name and path to the loaded configuration file. These properties can be altered using several setter methods, but they are not modified by save() and load() methods. If XML documents contain relative paths to other documents (e.g. to a DTD), these references are resolved based on the path set for this configuration.
You need to use XMLConfiguration.html#save(java.io.Writer) method
For example, after you've done all your modifications save it:
config.save(new PrintWriter(new File(dnsXmlPath)));
EDIT
As mentioned in comment, calling config.load() before calling setProperty() method fixes the issue.
I solved it with the following lines. I was missing the config.load().
XMLConfiguration config = new XMLConfiguration(dnsXmlPath);
config.load();
config.setProperty("newProperty", "valueNewProperty");
config.save();
It is true though that you can used the next line instead of config.save() and works the same.
config.save(new PrintWriter(new File(dnsXmlPath)));

Java : Getting a value from a properties file

I'm using the <liferay-ui:message key="username" /> to get some data from my property file in my portlet.
Is there a Java code equivalent for this tag ?
Thank you.
Actually the question title does not go with question content. To read from portlet.properties you have to do as what Jonny said. But on seeing the content of the question, I assume that what you want is the java code equivalent of the tag output that you have mentioned.
liferay-ui:message DOES NOT read the value from portlet.properties file so PortletProps will not work if that is what you are expecting as it is meant to read value only from portlet.properties and not Language.properties.
You should use methods of LanguageUtil class to get the value.
Yes, it's PortletProps.get(String key).
Hope this helps!
~~ EDIT ~~
The above as Sandeep has pointed out isn't the equivalent of what liferay-ui:message does, but it is the method to retrieve values from a portlet.properties file.
As Sandeep has said you should use LanguageUtil to replicate the functionality in Java code.
If you need merely read property from property file you can:
Properties p = new Properties();
p.load(new FileInputStream("file_with.properties"));
String message = p.getProperty("username");

Categories