?
Solved

how to load properties file only once in enterprise application

Posted on 2012-03-19
8
Medium Priority
?
2,062 Views
Last Modified: 2012-08-14
This is the class which loads properties file which returns static instance;
**********************************************************************

public class LoadProperties {
	private static Properties properties = new Properties();

	
	public static Properties getProperties() {
		return properties;
	}

	
	public static void setProperties(
			Properties properties) {
		LoadProperties.properties = properties;
	}

}

Open in new window


This is Listener class mentioned in web.xml which can be read when server starts up;
**********************************************************************
public class LoadProperties implements ServletContextListener {

public void contextInitialized(ServletContextEvent arg0) {
		
		//Below properties placed in application server class path in server lib folder with compressed jar;
		InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("server.properties");
		
			
		LoadProperties.getProperties().load(inputStream);
		
				//value is printed successfully;
			System.out.println("LoadProperties.."+LoadProperties.getProperties().getProperty("type"));

		}

}

Open in new window



My requirement is How do we read this property(LoadProperties.getProperties().getProperty("type")) in EJB class without loading the properties file once again?because when we load this properties in WAR level we will get once instance of LoadProperties;I dont think same instance will not be propagated in EJB level as well.So Do i need to load same properties once again in EJB classes as well?

my EAR structure is like this;

APP.ear
lib--- jars -- server.properties related jar is placed in the application server class path not in this folder;
ejb.jar
web.war
0
Comment
Question by:chaitu chaitu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37740897
As far as I undersatnd, for servelets you coudl use ServletContext Attributes to store your applicaion wide properties, but there are no EJB context Attributes, so there is no way to do it in the same way.

This question is discussed in this rather old trail below. While the tone of the discussion is pessimistic, there are some suggestions there about using entity bean or JNDI to store such application -wise propertis:

http://www.velocityreviews.com/forums/t139068-get-and-set-attribute-for-ejb-context-storing-objects.html
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 37741064
you mean to say i need to again load this properties file in EJB beans as well;

if i do like this ,does this properties will be loaded?

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream"server.properties");
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37741118
I think you are correct - but you need to have server.properties in the classpath

I think there is a lot of  useful information on this subject  here (inclufding recommendation to put application-wide properties in ejb-jar.xml)
http://www.velocityreviews.com/forums/t692007-properties-file-in-a-jar-for-ejb-3-0-a.html
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 37741187
I think this is a good example of how to do it without  reading properties many time:

http://javaevangelist.blogspot.com/2008/08/how-to-implement-ejb-30-in-ejb-jarxml.html

and this is the full project from that page to check how it is implemented:
http://enlightenment.bluelotussoftware.com/examples/EJBEnvironmentResourceInjectionExample.zip
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 37741530
I will do one more excercise.

i will remove Listner class mentioned in web.xml;so i dont read this properties in Servlet;

instead of that i will read the properties in EJB then the LoadProperties instance will be loaded right.

then after that servlet doesn’t find a resource in the WAR, then it should delegate to its parent classloader.

will it work?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1400 total points
ID: 37741640
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 600 total points
ID: 37743474
If you need the same set of properties accessible as in the EJB   in all  servlets and JSP  then I think you can load them the way you show in contextInitialized() and then store in ServletContext attributes, which you can then access in all servlets of this application,

like it is shown in the code in the solution to this question:

http://stackoverflow.com/questions/7975162/bare-bones-servlet-application-where-to-store-config-settings-that-i-can-refere
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37746463
:)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question