You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "krn1231@gmail.com" <kr...@gmail.com> on 2011/11/11 14:20:35 UTC

How to load a custom Properties file from webservice implementation method

Inside my Webservice implementation inside Apache CXF 
view plaincopy to clipboardprint?
@WebService public class STWeb implements STWebService {  
// Inside this class , i need to read a properties file located in WEB-INF
/classes/ folder  
}  

Please tell me how to read the properties file , located in WEB-INF
/classes/ folder from this webservice implementation method 

I tried using , the following options 

1. InputStream is = this.getClass().getResourceAsStream("WEB-INF
/classes/STBwebservice.properties"); 

2.
view plaincopy to clipboardprint?
 Properties properties = new Properties();  
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("WEB-INF
/classes/STBwebservice.properties"));  


Please remember that this is a pure java file (i mean no JEE ) and please
suggest if theer is abeter approach for this

--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-load-a-custom-Properties-file-from-webservice-implementation-method-tp4984336p4984336.html
Sent from the cxf-user mailing list archive at Nabble.com.

RE: How to load a custom Properties file from webservice implementation method

Posted by Sven Zethelius <sv...@expedia.com>.
I think something you said may be a bit misleading.  If you are in "a pure java file (i mean no JEE )", I'd assume you are loading your app from a java main method, which would mean that WEB-INF/classes wouldn't be an expected directory.  WEB-INF/classes implies you are in a tomcat (or similar) container, which means you get a different classloader search path.

You'd want this.getClass().getClassloader().getResourceAsStream(...) if you want to load something relative to the "root" of the classloader.  this.getClass().getResourceAsStream(...) is relative to the class of this.

Example:
// this = org.example.foo.MyClass
this.getClass().getResourceAsStream("a/b/c"); // looks for classpath:/org/example/foo/a/b/c
this.getClass().getClassLoader().getResourceAsStream("a/b/c"); // looks for classpath:/a/b/c

When you throw a tomcat classloader into the mix, WEB-INF/classes should NOT appear in the requested path.
this.getClass().getResourceAsStream("WEB-INF/classes/foo"); // looks for classpath:/org/example/foo/WEB-INF/classes/foo, where classpath: includes WEB-INF/classes already, so in fact, it would be looking for <WEBAPP ROOT>/WEB-INF/classes/org/example/foo/WEB-INF/classes/foo


-----Original Message-----
From: krn1231@gmail.com [mailto:krn1231@gmail.com] 
Sent: Friday, November 11, 2011 5:21 AM
To: users@cxf.apache.org
Subject: How to load a custom Properties file from webservice implementation method

Inside my Webservice implementation inside Apache CXF 
view plaincopy to clipboardprint?
@WebService public class STWeb implements STWebService {  
// Inside this class , i need to read a properties file located in WEB-INF
/classes/ folder  
}  

Please tell me how to read the properties file , located in WEB-INF
/classes/ folder from this webservice implementation method 

I tried using , the following options 

1. InputStream is = this.getClass().getResourceAsStream("WEB-INF
/classes/STBwebservice.properties"); 

2.
view plaincopy to clipboardprint?
 Properties properties = new Properties();  
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("WEB-INF
/classes/STBwebservice.properties"));  


Please remember that this is a pure java file (i mean no JEE ) and please
suggest if theer is abeter approach for this

--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-load-a-custom-Properties-file-from-webservice-implementation-method-tp4984336p4984336.html
Sent from the cxf-user mailing list archive at Nabble.com.