• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading a properties file from a Web service

 
sanjeewa sanjeewa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have deployed a webservice in apache-tomcat-5.5.23 using Axis2. (I have placed my .aar file in apache-tomcat-5.5.23\Axis2.war\WEB-INF\services). I need to read a properties file from my web service to read some text dynamically. Could sombody help me to know
1. Where should I place my properties file and
2. How do I specify the path for the properties file to read it in my web service java code.
thanks in advance
Best regards
sanjeewa
 
Doug Slattery
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sanjeewa,

I'm not 100% sure, but I believe they should go in the axis2 webapp class path
(i.e. ...\tomcat\webapps\axis2\WEB-INF\classes) by default. I haven't needed to put them elsewhere, so I don't know how to configure it (web.xml?). If you put your files there, you shouldn't need to specify the path.

Try that and let me know if it works. I'd be interested in knowing 100% myself .

Aloha,
Doug

-- Nothing is impossible if I'mPossible
 
sanjeewa sanjeewa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Doug
I tried the location you have specified, (and some other locations as well) but wasn't successful. Once i could solve this, I'll let you know the solution. If any body else could help me on this please....
Thanks
Best regards
Sanjeewa
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried? Are you using standard file I/O or are you accessing it as a resource through the class loader?
 
sanjeewa sanjeewa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just I'm using standard file I/O (Sorry for the delayed response)
 
Faraz Masood
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i guess the problem is with the way you are accessing the properties file

try reading it as getResourceAsStream , and put the file ONLY in child directory of your source code or atleast at the same level ..

for example :

dir1/dira/dir2/mysource.java
dir1/dira/dir2/myfile.properties OR
dir1/dira/dir2/dirx/../dirb/myfile.properties
 
sanjeewa sanjeewa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Faraz Masood,
It works fine great help. Thanks

I put the properties file in the class root and read it like bellow.

String m_FilePath = "/Constants.properties";
InputStreamReader is = new InputStreamReader(this.getClass().getResourceAsStream(m_FilePath));
BufferedReader in = new BufferedReader(is);

~Sanjeewa
 
Rahul Juneja
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this way it doesn't solve the purpose because it is inside the war. and it will require to be build when you change the properties.
 
Paolo Milli
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if it's still useful (after more then one year) but I resolve in the follow way:

In order to have the properties file outside the package, I put the file in axis2/WEB-INF/classes

In java code (web service) I have:

InputStreamReader fMainProp = new InputStreamReader(this.getClass().getResourceAsStream("myfile.properties"));
Properties mainProp new Properties();
mainProp.load(fMainProp)

It works.
 
Horst Wayne
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a related problem this week and my research often brought me to this thread so I thought I might post the solution I finally found here:

I am using Axis2 version 1.6.1 and the Axis2 standalone server. I have a JAX-WS annotated web service class, packaged with maven and put as a jar into the repository/servicejars folder.

Now I wanted to read in some database parameters from an external property file (so that I didn't have to redeploy the service when I wanted to change a user name for instance), but I couln't find the solution for a few days.

I tried several different ways to access the file and many possible locations, but always got the same NullPointerException.

Finally, on another forum I found the hint to use a FileWriter to check where the output went, and to my surprise it went straight to the axis root.

So I tried Paolos Code but put the file into the axis root folder and it worked! Then I put it to AXISROOT/conf/database/db.properties and used .getResourceAsStream("/conf/database/db.properties")); -- finally I had my solution.


Just thought I'd share this to help someone else who might have the same problem. ;)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic