Reputation: 29
i am working on a dynamic web project(DWP) which has a property file. have created a jar that should read the contents of the property file in the DWP and the changes in the property file and incorporate the changes in the DWP.I am not able to read the property file from the jar. I want to know how a jar in a DWP read a property file of the DWP
here's the code opf the jar
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class fileReader extends Thread
{
publicstatic long oldTMS;
long currentTMS;
File file;
public static long limit;
//static String Path = "./configuration/RequestLimit.properties";
static String Path="configuration/RequestLimit.properties";
public static Properties properties;
public fileReader() throws IOException, InterruptedException {
System.out.println("Reading the property file for the first time");
Thread thread = new Thread();
setDaemon(true);
FileInputStream in = new FileInputStream(Path);
System.out.println("fd:"+in.getFD());
properties = new Properties();
properties.load(in);
String requestLimit = properties.getProperty("RequestLimit");
System.out
.println("request limit as read from the property file is ----"
+ requestLimit);
limit = Long.parseLong(properties.getProperty("RequestLimit"));
System.out.println("request limit as per the reading is ---" + limit);
start();
// waits for the thread to die
join();
}
public void run() {
System.out.println(" thread starts");
if (Thread.currentThread().isDaemon()) {
while (true) {
try {
Thread.sleep(3000);
System.out.println("entered in infinite loop");
file = new File(Path);
System.out.println("file name is --------" + file);
currentTMS = file.lastModified();
SimpleDateFormat dateFormat = new SimpleDateFormat(
"E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("file initial change time stamp:::::"
+ dateFormat.format(currentTMS));
try {
System.out
.println("lets see if the file has changed or not !!");
FileInputStream d = new FileInputStream(file);
if (oldTMS != currentTMS) {
System.out.println("checking for old and new time stamp!");
oldTMS = currentTMS;
System.out.println("file modified last at-----------"
+ oldTMS);
// reload property
try {
System.out.println("Reloading property file");
properties.load(d);
System.out.println("After Reloading property file properties : "
+ properties
.getProperty("RequestLimit"));
limit = Long.parseLong(properties
.getProperty("RequestLimit"));
System.out.println("request limit as per the reading is ---"
+ limit);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
catch (InterruptedException e2)
{
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
}
}
using jar in the DWP like this
package com.ING;
import java.io.IOException;
import com.change.fileReader;
public class deamonCaller
{
public long callDeamon() throws IOException, InterruptedException
{
fileReader fr = new fileReader();
Long requestLimit =fileReader.limit;
return requestLimit;
}
}
Upvotes: 1
Views: 396
Reputation: 29
well for those who may get stuck like me. it was a serendipity for me . Was explainig the prob to a senior when an idea clicked my head , implemented and it worked .. anyways heres a solution.
place the Propertyfile in web content directly,no need to put the file in a folder in the dynamic web project. In the Servelet of the Dynamic Web proj(DWP) use the following lines of code :
String pathPropertyFile = request.getSession().getServletContext()
.getRealPath("")
+ "\\RequestLimit.properties";
System.out.println("path of the property file is"+pathforFileReader);
pass this PATH obtained as "pathPropertyFile" to the class of the jar that has the code for reading the property file. i have used a Bean class , set the path in the beann and then feth the path from the bean and pass it to the method of the JAR class that reads the property file.
public class deamonCaller {
public long callDeamon(Tao tao) throws IOException, InterruptedException {
// obtain the path from the bean Tao
String Path = tao.getPath();
System.out.println("path in deamon:" + Path);
// call the jar method while passing PATH as a parameter
fileReader fr = new fileReader(Path);
Long requestLimit = fileReader.limit;
return requestLimit;
}
}
CODE of The FILE READER
package com.change;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class fileReader extends Thread {
static long oldTMS;
long currentTMS;
File file;
public static long limit;
//static String Path = "./configuration/RequestLimit.properties";
public static String pathName;
public static Properties properties;
public fileReader(String Path) throws IOException, InterruptedException {
pathName=Path;
System.out.println("Reading the property file for the first time");
Thread thread = new Thread();
setDaemon(true);
FileInputStream in = new FileInputStream(pathName);
System.out.println("fd:"+in.getFD());
properties = new Properties();
properties.load(in);
String requestLimit = properties.getProperty("RequestLimit");
System.out
.println("request limit as read from the property file is ----"
+ requestLimit);
limit = Long.parseLong(properties.getProperty("RequestLimit"));
System.out.println("request limit as per the reading is ---" + limit);
start();
// waits for the thread to die
join();
}
public void run() {
System.out.println(" thread starts");
if (Thread.currentThread().isDaemon()) {
while (true) {
try {
Thread.sleep(3000);
System.out.println("entered in infinite loop");
file = new File(pathName);
System.out.println("file name is --------" + file);
currentTMS = file.lastModified();
SimpleDateFormat dateFormat = new SimpleDateFormat(
"E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("file initial change time stamp:::::"
+ dateFormat.format(currentTMS));
try {
System.out
.println("lets see if the file has changed or not !!");
FileInputStream d = new FileInputStream(file);
if (oldTMS != currentTMS) {
System.out
.println("checking for old and new time stamp!");
oldTMS = currentTMS;
System.out
.println("file modified last at-----------"
+ oldTMS);
// reload property
try {
System.out.println("Reloading property file");
properties.load(d);
System.out
.println("After Reloading property file properties : "
+ properties
.getProperty("RequestLimit"));
limit = Long.parseLong(properties
.getProperty("RequestLimit"));
System.out
.println("request limit as per the reading is ---"
+ limit);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
}
}
Upvotes: 0
Reputation: 2098
Please try with below code. I hope, it will help you alot.
static String Path="configuration/RequestLimit.properties";
loadProperties(Path);
private void loadProperties(String propertiesName) {
if (properties != null) {
return;
}
// Properties properties = null;
InputStream inputStream = null;
inputStream = this.getClass().getResourceAsStream(propertiesName);
if (inputStream == null) {
throw new Exception(propertiesName + "something");
}
properties = new Properties();
try {
properties.load(inputStream);
} catch (IOException e) {
throw new Exception(e);
}
}
Upvotes: 1
Reputation: 10561
Provide the path as a -Dproperty=..... to java nad then read it from there instead of hardcode the value.
Upvotes: 0