cancel
Showing results for 
Search instead for 
Did you mean: 

Need to Put Some data in Properties File and Access it

archana_prasad
Explorer
0 Kudos

HI Friends,

I want to store the data (servername,User id ) in Archana.properties file from WebDYnpro Application

Pls let e know where to place the properties file and also let me know how to access the properties file from WD application and Modify it.

I have tried placing in: src/mimes, Configuration etc...but i am unale to access it.

Also If i access the properties file and update it, do i again need to deploy the WD application.

Kindly let me know if there are any better methods to store key value pairs from the application.

Regards,

Archana

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

The better approch will be to store the .properties file into a known folder manually created in the server in which your application is deployed.

Below is the code snippet to read from the property file which is placed in the server,


Properties properties = new Properties();
File propFile = new File("File path");   
if(propFile.exists()){
try {
properties.load(new FileInputStream(propFile));
if(!properties.isEmpty()){
if(properties.containsKey("userId"){
String userId = properties.getProperty("userId");
wdComponentAPI.getMessageManager().reportSuccess("UserId: "+userId);
}
else{
wdComponentAPI.getMessageManager().reportException("File does not have userId");
}
}
else{
wdComponentAPI.getMessageManager().reportException("File is Empty");
}
}
catch (Exception e) {
wdComponentAPI.getMessageManager().reportException(e.toString());
}
}
else{
wdComponentAPI.getMessageManager().reportException("File not Found");
}

Below is the code snippet to write into the property file which is placed in the server,


Properties properties = new Properties();
File propFile = new File("File Path");   
FileOutputStream stream = null;
if(propFile.exists()){
try {
properties.load(new FileInputStream(propFile));
if(!properties.isEmpty()){
if(properties.containsKey("Key"){
properties.remove("Key");
properties.put("Key", "Newvalue");
}
else{
properties.put("Key", "Newvalue");
}
stream = new FileOutputStream(propFile);
properties.store(stream, null);	
wdComponentAPI.getMessageManager().reportSuccess("File updated Successfully");
}
else{
wdComponentAPI.getMessageManager().reportException("File is Empty");
}
}
catch (Exception e) {
wdComponentAPI.getMessageManager().reportException(e.toString());
}
}
else{
wdComponentAPI.getMessageManager().reportException("File not Found");
}

Regards,

Vishweshwara P.K.M.

archana_prasad
Explorer
0 Kudos

HI,

The better approch will be to store the .properties file into a known folder manually created in the server where the Application is deployed?

That means where?

Itried placing my .prpoerties file at several places in my Project(Not server)- Mimes, Configuration, Packages.

I tried the same kind of code u specifed.

But the file.exists returns false, though there is a file in the location.

When i am trying with javaproject, it works but in webdynpro project, its says file not found

Below is the code:

-


package com.sap.demo.casestudy.wd;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Properties;

public class ReadWriteArc {

/**

  • @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

WriteProperty("Key3", "Value3");

String value=ReaddProperty("key2");

}

public static void WriteProperty(String key,String value) {

try {

Properties pro = new Properties();

File f = new File("C:/Users/i067753/workspace/720/workspaceSetUp.jdi/LocalDevelopment/DCs/demo.sap.com/casestudy/_comp/src/configuration/Archana.Properties");

//File f = new File("C:/Users/i067753/workspace/720/workspaceSetUp.jdi/LocalDevelopment/DCs/demo.sap.com/casestudy/_comp/src/mimes/Components/com.sap.demo.casestudy.wd.comp.webdynproappln.WebDynproAppln/Archana.Properties");

//File f = new File("C:/Users/i067753/workspace/720/workspaceSetUp.jdi/LocalDevelopment/DCs/demo.sap.com/casestudy/_comp/src/packages/com/sap/demo/casestudy/wd/Archana.Properties");

if (!f.exists()) {

System.out.println("File not found!");

} else {

FileInputStream in = new FileInputStream(f);

pro.load(in);

pro.setProperty(key, value);

pro.store(new FileOutputStream("Properties"), null);

System.out.println("Operation completly successfuly!");

}

}

catch (IOException e) {

System.out.println(e.getMessage());

}

}

public static String ReaddProperty(String key) {

try {

String valuemessage=null;

Properties pro = new Properties();

File f = new File("C:/Users/i067753/workspace/720/workspaceSetUp.jdi/LocalDevelopment/DCs/demo.sap.com/casestudy/_comp/src/configuration/Archana.Properties");

//File f = new File("C:/Users/i067753/workspace/720/workspaceSetUp.jdi/LocalDevelopment/DCs/demo.sap.com/casestudy/_comp/src/mimes/Components/com.sap.demo.casestudy.wd.comp.webdynproappln.WebDynproAppln/Archana.Properties");

//File f = new File("C:/Users/i067753/workspace/720/workspaceSetUp.jdi/LocalDevelopment/DCs/demo.sap.com/casestudy/_comp/src/packages/com/sap/demo/casestudy/wd/Archana.Properties");

if (!f.exists()) {

System.out.println("File not found!");

} else {

FileInputStream in = new FileInputStream(f);

pro.load(in);

valuemessage = pro.getProperty(key);

System.out.println("Read completly successfuly! and the value is "+valuemessage);

}

return valuemessage;

}

catch (IOException e) {

System.out.println(e.getMessage());

}

return null;

}

}

-


And from WD Application i called the class as:

ReadWriteArc.WriteProperty(engname, reserveEngine1);

Pls suggest if am making any mistake here

U say Server, how can i do that?

Regards,

Archana:)

Former Member
0 Kudos

Hi,

The issue with your code is that, you have given the file path as your local system path i.e. c:/Users/i067753/workspace/720...,

but the application will be running on the server and hence it will not find the given file and hence returns false.

So what i suggested was to create a folder in the server and place the property file there.

The other approch will be to place the property file under mimes/components/<folder named after component Name>, and use the below code to create the file instance,


String filePath = WDURLGenerator.getResourcePath(wdComponentAPI.getDeployableObjectPart(), "Archana.properties");
File file = new File(filePath);

Followed by the code i had given earlier, in the web dynpro application itself. No need to create a java file for this.

The WDURLGenerator.getResourcePath() method used will throw WDAliasResolvingException exception, so handle it in catch block.

Regards,

Vishweshwara P.K.M.

archana_prasad
Explorer
0 Kudos

HI Vishwesh,

Thanks for the Reply.

I have tried

String filePath;

try {

Properties pro = new Properties();

filePath = WDURLGenerator.getResourcePath(wdComponentAPI.getDeployableObjectPart(), "Archana.properties");

File file = new File(filePath);

if (!file.exists()) {

System.out.println("File not found!");

} else {

FileInputStream in;

try {

in = new FileInputStream(file);

try {

pro.load(in);

pro.setProperty(engname, reserveEngine1);

pro.store(new FileOutputStream("Properties"), null);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("Operation completly successfuly!");

}

} catch (WDAliasResolvingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Now the file is identified and above code runs without an exception.

But changes are not seen in Archana.properties file neither in

local: C:\Users\i067753\workspace\720\workspaceSetUp.jdi\LocalDevelopment\DCs\demo.sap.com\casestudy\_comp\src\mimes\Components\com.sap.demo.casestudy.wd.comp.webdynproappln.WebDynproAppln\Archana.Properties

nor in server: D:\usr\sap\C72\J00\j2ee\cluster\apps\demo.sap.com\casestudy\servlet_jsp\webdynpro\resources\demo.sap.com\casestudy\root\Components\com.sap.demo.casestudy.wd.comp.webdynproappln.WebDynproAppln

Any mistake that i have made ?

Kindy help me here.

Thanks n Regards,

Archana

Former Member
0 Kudos

Hi Archana,

How are you concluding that the file in the server is not getting updated?

The code mentioned here will always update the mentioned property file in the server. But the issue is that the initial property file placed in mimes/components/<component name> of the application is in your local system and will not be updated. So every time you deploy the application the property file in the server will be updated with the initial values.

Do the below test,

In your code after file.exists() check is made and prop file is loaded with file, write the below code


if(properties.containsKey(engname)){
wdComponentAPI.getMessageManager().reportSuccess(properties.getProperty(engname));
}
else{
//Continue to set value and store
}

After deploymnet the above code should write the engine name into prop file in the first execution and should Display the new value in the second execution. If not displayed then we can conclude that the file is not updated.

Regrads,

Vishweshwara P.K.M.

archana_prasad
Explorer
0 Kudos

HI Vishwa.

Yes....Your code worked soi have rewarded points to you

The problem was when i was storing the file, i gave wrong path...

Now All is well .

Thanks for the reply.

Regards,

Archana

junwu
Active Contributor
0 Kudos

i don't think that is a nice solution.

you will be screwed up when your server has multiply instance.

if you upgrade or migrate your server, it probably cause issue too.

you should follow standard sap way of handling properties file.

Answers (1)

Answers (1)

junwu
Active Contributor
0 Kudos

// for nw7	
  IWDDeployableObject deployableObject = wdComponentAPI.getDeployableObjectPart().getDeployableObject();
	  String configName = "test"; //This should match with the name specified in the src->configuration->test.properties
	  WDConfiguration.getConfigurationByName(deployableObject, configName)
	  
	  //For 7.1 instead of the above code use
String propName="conf.properties";
	  IWDWebModule webModule = wdComponentAPI.getDeployableObjectPart().getWebModule();
	IWDConfiguration config=  WDConfiguration.getConfigurationByName(webModule, propName);
config.getStringEntry("yourkey");

you can update the content of property file through NWA.

lokesh_kamana
Active Contributor
0 Kudos

Step1:-

Below is my properties file with name "test.properties".

Sample Data:-


key1 = lokesh

key2 = prem

key3 = surya

Step2:-

Place the properties file in the below specified path.


src/configuration/Components/yourComponentName(Folder of your component)

Step3:-

If your using Netweaver 7.0


try {
IWDConfiguration config = WDConfiguration.getConfigurationByName(wdComponentAPI.getDeployableObjectPart().getDeployableObject(),"test");
String value1 = config.getStringEntry("key2");
wdComponentAPI.getMessageManager().reportSuccess("Value from properties file"+value1);
} catch (WDConfigurationNotFoundException e) {
wdComponentAPI.getMessageManager().reportException("Exception"+e.getLocalizedMessage());
} catch (WDInvalidConfigParameterException e) {
wdComponentAPI.getMessageManager().reportException("Exception"+e.getLocalizedMessage());
} 

If your using Netweaver CE


try {
IWDConfiguration config = WDConfiguration.getConfigurationByName(wdComponentAPI.getDeployableObjectPart(),"test");
String value1 = config.getStringEntry("key2");
wdComponentAPI.getMessageManager().reportSuccess("Value from properties file"+value1);
} catch (WDConfigurationNotFoundException e) {
wdComponentAPI.getMessageManager().reportException("Exception"+e.getLocalizedMessage());
} catch (WDInvalidConfigParameterException e) {
wdComponentAPI.getMessageManager().reportException("Exception"+e.getLocalizedMessage());
} 

If you want to do it withur coding pasted above try using FileUpload UI Element and you can do that.

Thanks & Regards,

Lokesh Kamana