This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Websphere and the fly likes properties file in websphere Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Products » Websphere
Bookmark "properties file in websphere" Watch "properties file in websphere" New topic
Author

properties file in websphere

verduka fox
Ranch Hand

Joined: Jan 18, 2001
Posts: 178
I have used properties files in the past for specifying values that may change in java code. I would like to add a properties file for a JavaBean to reference. My questions are related to file placement of this properties file. I'm using WSAD 4.0.3 on Windows 2000. Where do I put the properties file in WSAD so that my code will compile? Next, where does this properties file go when I deploy my project to WAS 4.0.3 on AIX?
I would really like to use a properties file so that I can easily switch between our development database and our production database. If anyone could help me out, I'd really appreciate it!
Thanks.
verduka fox
Ranch Hand

Joined: Jan 18, 2001
Posts: 178
Can anyone address this issue please? I've searched on JavaRanch and google, and haven't been able to find a solution.
Kiran Kumar
Ranch Hand

Joined: May 18, 2001
Posts: 107
hi
could u pl specify whether u r using the properties file from a servlet in a war file or from a EJB in a jar file.
if servlet u can specify the path in the init parameter and read it within ur servlet. or u can load it using
InputStream is = getClass().getResourceAsStream("/rollout.properties");
Properties props = new Properties();
try
{
props.load(is);
}
catch (Exception e){}

if u want to load the properties file with in ur EJB then u can make use of the Environment variables.
hope this helps.


Thanks<br /> <br />Kiran <br /> <br />SCEA, SCJP 1.4,<br /> <br />"First they ignore u, then they laugh at u, then they fight u, <br />then u will win<br /> <br />Mahatma Gandhi"
pjoisha
Ranch Hand

Joined: Mar 06, 2001
Posts: 123
The easiest solution would be to use a ResourceBundle (specifically PropertiesResourceBundle) ..... place your properties file say sampleConfig.properties under ejbModule (for EJB Projects) or source directory for a Web project. Now in ur class file you need to have something like this ...
ResourceBundle bundle = ResourceBundle.getBundle("sampleConfig.properties");
....
String value1 = (String)bundle.getObject("Key1");
.....
Hope this helps.
PJ
[ July 30, 2002: Message edited by: Prashanth Joisha ]
Michael Zalewski
Ranch Hand

Joined: Apr 23, 2002
Posts: 168
For WSAD, I think you just attach the .properties file to your project as a resource.
For WAS, the .properties file goes into your 'Document Root', which is a directory you specify per web application. You find the setting for this in the properties for the web application.
Seema
Greenhorn

Joined: Aug 05, 2002
Posts: 15
I fully agreed with this solution .Only thing I want to add is that while using ResourceBundle the baseclass(Property file) must be fully qualified (for example, myPackage.sampleConfig, not just sampleConfig).
Regards
Seema Verma
Originally posted by Prashanth Joisha:
The easiest solution would be to use a ResourceBundle (specifically PropertiesResourceBundle) ..... place your properties file say sampleConfig.properties under ejbModule (for EJB Projects) or source directory for a Web project. Now in ur class file you need to have something like this ...
ResourceBundle bundle = ResourceBundle.getBundle("sampleConfig.properties");
....
String value1 = (String)bundle.getObject("Key1");
.....
Hope this helps.
PJ
[ July 30, 2002: Message edited by: Prashanth Joisha ]
pjoisha
Ranch Hand

Joined: Mar 06, 2001
Posts: 123
from a coding perspective - I don't think so.
PJ
[ August 06, 2002: Message edited by: Prashanth Joisha ]
Se Ravin
Greenhorn

Joined: Feb 18, 2002
Posts: 26
I am putting .properties file in WEB-INF/classes
and using ResourceBundle to extract the values,it works fine ,Is there any thumb rule that specifies that property files should be here or there?


We are and We are Not
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

The problem with this is that the properties will be unavailable on the EJB layer, so you have to either replicate the properties (evil) or find somewhere else to put it.
Of course it isn't so much of a problem if you don't have EJBs or they don't have to share properties.
Dave
Elizabeth Reynolds
Greenhorn

Joined: Mar 31, 2002
Posts: 24
In WSAD, you have more flexibility in where you can place your file. For example, I can place the file under the web application directory and reference it. The problem is when you deploy outside of WSAD. My recommendation is to put it into your source directory. It will then show up in your WEB-INF/class directory.
Steve Granton
Ranch Hand

Joined: Jan 13, 2002
Posts: 200
Folks,
You can always set the working directory of the application server and then pick up the properties file relative to the working directory. This allows EJBs and Web apps to access the same properties files.
I hope helps.
Cheers,
Steve
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

This is the way we currently do it, but it only really works if everything runs on the same machine (or if all machines share the same file system, which is just as bad) - it isn't compatable with a distributed application.
We're currently in the process of evaluating our use of properties (on Websphere) to come up with 'something better', but asn efficient solution to the distributed thing is a bit of a poser.
Steve Granton
Ranch Hand

Joined: Jan 13, 2002
Posts: 200
Hi,
If you are looking for a single repository for properties in a distributed environment you should probably be looking towards either a directory service such as LDAP or using a database to store the information.
The problem with only having one place for all the properties for all the servers is that you introduce a further single point of failure! If the database/directory service fails then all your servers are likely to suffer unless they cache the properties at startup - though they could still experience problems if you need to restart the servers.
You need to weigh up whether you require redundancy and failover or this single repository.
Cheers,
Steve
verduka fox
Ranch Hand

Joined: Jan 18, 2001
Posts: 178
Hi everyone,
I've tried some of your responses. Here's our scenario: we're using JSP and servlets on WAS 403 running on AIX, with Apache 1.3.20 Web Server. We're using only one app server. The JSP and servlets access JavaBeans. We're not using EJB's. The JavaBeans then need to create a connection with the database. My purpose of using the properties file is for an easy way to switch between our production and development databases. We have different database names and usernames and passwords for prod and dev. Therefore, I would like an easy way to change these values without having to modify the constants in the JavaBean. That said, here is how I'm trying to use the properties file:
I have created a properties file called test.properties with the following data:

I then have this code in my JavaBean:

I have tried specifying the full path to the properties file; however this doesn't work either. The message in the log is:

I have the properties file in /usr/apps/WebSphere/AppServer/installedApps/test-dev.ear/test.war/WEB-INF/classes/test directory. The test directory at the end is also where the class file for my JavaBean resides.
Can someone please tell me how I should be referring to the properties file? I'm not sure if the problem is how I'm referring to it or where I'm putting the file. The properties file has full permissions (777).
Any direction on this issue is greatly appreciated!!
[ August 12, 2002: Message edited by: verduka fox ]
pjoisha
Ranch Hand

Joined: Mar 06, 2001
Posts: 123
Try putting your properties file in the classes dir ..... ie WEB-INF\classes dir
PJ
verduka fox
Ranch Hand

Joined: Jan 18, 2001
Posts: 178
Ok, I got it working. I had to change the following line:
bundle = (PropertyResourceBundle)PropertyResourceBundle.getBundle("test.properties");
to:
bundle = (PropertyResourceBundle)PropertyResourceBundle.getBundle("com.mycompany.myproject.test");
Now it seems to work. One last question: when I change values in the properties file, should I have to stop and start the app server for these changes to be recognized? I didn't think I would need to, but from my testing it seems as though this is required. Can anyone comment on this?
verduka fox
Ranch Hand

Joined: Jan 18, 2001
Posts: 178
One more thing: my manager asked me to check if using the properties file creates any security holes, ie is anyone able to access this file through the browser? This would be crucial to know: if this is the case, we cannot use the properties file. I have the properties file in my WEB-INF/classes/... folder. Please respond.
[ August 12, 2002: Message edited by: verduka fox ]
verduka fox
Ranch Hand

Joined: Jan 18, 2001
Posts: 178
Can anyone comment on my two final issues with using the properties file please:
1) When I change values in the properties file, should I have to stop and start the app server for these changes to be recognized? I didn't think I would need to, but from my testing it seems as though this is required. Should this be the case?
2) Does using the properties file create any security holes, ie is anyone able to access this file through the browser? If this is the case, we cannot use the properties file as the intruder would then find the database name, username and password. I have the properties file in my WEB-INF/classes/... folder. Please let me know if this is a problem.
Your expertise in this area is really appreciated!
verduka fox
Ranch Hand

Joined: Jan 18, 2001
Posts: 178
I'm sorry to ask again, but I still haven't found the answer to my questions:
Can anyone comment on my two final issues with using the properties file please:
1) When I change values in the properties file, should I have to stop and start the app server for these changes to be recognized? I didn't think I would need to, but from my testing it seems as though this is required. Should this be the case?
2) Does using the properties file create any security holes, ie is anyone able to access this file through the browser? If this is the case, we cannot use the properties file as the intruder would then find the database name, username and password. I have the properties file in my WEB-INF/classes/... folder. Please let me know if this is a problem.
Your expertise in this area is really appreciated!
Thembile Mzantsi
Ranch Hand

Joined: Aug 06, 2002
Posts: 58
As for [2], to hide your properties you could add a jar file, say properties.jar that you put in adminserver.bat or setupCmdLine.bat. This will be found by your classloaders. At worst you can put this jar in /lib/app (assuming 4.x and above)
As for [1] how do you obtain your properties? Do you use ResourceBundle.java or do you use getSystemResourceAsStream? If you use the former, then you have to bounce your adminserver (90% sure). If you use the latter, I don't know, I would guess that it should be read from the classpath each time.
-t
Ron Ditch
Ranch Hand

Joined: May 16, 2002
Posts: 33
Just place the properties file in your class path, and use a class loader to gain access to it. Every time that you change your properties file you will have to restart your web container.
Also, if you read Sun's java spec, you will see that files in the WEB-INF directory are not accessible.
BTW - the WSAD 4.1 version is a little less buggy than 4.0.3.
I've found that certain key stroke combinations in 4.0.3 brings it down.
I typically create a resource servlet that bootstraps at load time of the servlet container. The resource servlet then gives access to the properties fields. I usually define an init parameter to my resource servlet in the web.xml file. This init parameter is the name of the properties file that I want to gain access to.
Then if you place the file in the same location in your sources directory in WSAD as your servlet, you can use the following code snippet.
String dataSource = getInitParameter("dataSource");
URL configURL = getServletContext.getResource(dataSource);
Properties props = new Properties();
props.load(configURL.openStream());
If you want to pass the properties to an ejb container, make sure your method signature to the ejb contains an environment variable. This environment variable should be a hashtable (or HashMap) containing all your needed properties by the ejb layer.
[ August 24, 2002: Message edited by: Ron Ditch ]
Max Tomlinson
Ranch Hand

Joined: Jul 17, 2001
Posts: 365
I think I am facign what is a familiar situation: I recently deployed Log$J in our project which takes a Property file as an argument. Under WSAD 4.03 I put the property file in the Application Developer directory and this works. When we try to deploy on AIX, however (WAS) we get a ‘Logger (the name of the wrapper class which wraps Log4J) violates loader constraint’.
The properties file is in the applications root directory along with the supporting jars.
Any ideas?
thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: properties file in websphere