Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamically read/add value to the parameter of conf file with Properties

I have a message like below in my conf file.

text.message = Richard has to go to School in 01/06/2012 / 1days.

All highlighted field will be variable.

I want to read this text.me string and insert the value from my java using Properties.

I know how to read the whole string using Prop, but don't know how to read like above String which will be like.

text.message = #name# has to go to #place# in #date# / #days#.

  1. how can I read the above string from the conf using Properties and insert data dynamically?

  2. It can be either date or days in the string. How I can turn on and off between those parameters?

Thanks ahead.

like image 819
kitokid Avatar asked Jun 01 '12 03:06

kitokid


People also ask

How do I use a value file in a parameter set?

Using a value file in a parameter set allows you to set values of a parameter dynamically. For Example: Job A updates the value file. Job B uses a parameter set that points to that value file.

What is the importance of parameters in a File Adapter?

These parameters are very important for fulfilling the business requirements. E.g. For File Receiver Adapter, the most common requirement is to create the file with dynamic file name and also sometime in a directory which needs to be decided at runtime.

How do I update the value file to reflect different environments?

When moving jobs from development to test or production you can update the value file to reflect different values for the different environments without having to recompile the job. To create a new Parameter Set, select File, New and select "Create new Parameter Set"

Are properties files just simple key=value pairs?

And although properties files are simple key=value pairs, there are some corner cases that make it challenging. For example, when setting a key/value pair, the key may be commented out with a hash mark in front of the line.


2 Answers

You can use the MessageFormat API for this.

Kickoff example:

text.message = {0} has to go to {1} in {2,date,dd/MM/yyyy} / {3}

with

String message = properties.getProperty("text.message");
String formattedMessage = MessageFormat.format(message, "Richard", "School", new Date(), "1days");
System.out.println(formattedMessage); // Richard has to go to School in 31/05/2012 / 1days
like image 78
BalusC Avatar answered Oct 24 '22 19:10

BalusC


You can use the MessageFormat class, which replaces dynamic placeholders in a string with the desired values.

For example, the following code...

String pattern = "{0} has to go to {1} in {2,date} / {3,number,integer} days.";
String result = MessageFormat.format(pattern, "Richard", "school", new Date(), 5);
System.out.println(result);

...will produce the following output:

Richard has to go to school in 31-May-2012 / 5 days.

You can simply get the pattern from your Properties object, then apply the MessageFormat translation.

like image 45
Mansoor Siddiqui Avatar answered Oct 24 '22 19:10

Mansoor Siddiqui