how can load properties file for one time and use in entire application

Hi folks, I dont want to read properties file every time , and want to get property value in any servelet or jsp page.(means i want to read only property file only for once and want to get value in any where ) , how to do this.Message was edited by: RajeshwarReddyT
[293 byte] By [RajeshwarReddyTa] at [2007-11-27 5:13:22]
# 1
If it is a servlet or a JSP, save the loaded properties object as a session or maybe even application context item.Or, you could probably even make the Properties object an instance variable since Servlets (and AFAIK JSPs) are only instatiated once, then used in multiple threads.
masijade.a at 2007-7-12 10:34:53 > top of Java-index,Java Essentials,New To Java...
# 2
Do u mean cache it ?
New_Kida at 2007-7-12 10:34:53 > top of Java-index,Java Essentials,New To Java...
# 3
it is not a servlet it is a ordinary class , then how to do it?
RajeshwarReddyTa at 2007-7-12 10:34:53 > top of Java-index,Java Essentials,New To Java...
# 4
How about a Singleton properties wrapper class? When you initialise it, you read all properties and you can access them whenever needed...
Peetzorea at 2007-7-12 10:34:53 > top of Java-index,Java Essentials,New To Java...
# 5
May use following codejava.util.ResourceBundle rb = java.util.ResourceBundle.getBundle("<property file name>",new Locale("en","US"));
m_k_ma at 2007-7-12 10:34:53 > top of Java-index,Java Essentials,New To Java...
# 6
can u tell me how to do it
RajeshwarReddyTa at 2007-7-12 10:34:53 > top of Java-index,Java Essentials,New To Java...
# 7
> it is not a servlet it is a ordinary class , then how> to do it?Create a Singleton class that loads the property file once and stores it inside a static hashmap.@see Singleton pattern.
karma-9a at 2007-7-12 10:34:53 > top of Java-index,Java Essentials,New To Java...
# 8
i will make it class singleton , store it in static HashMap but how to access it in other ordinary class.....
RajeshwarReddyTa at 2007-7-12 10:34:53 > top of Java-index,Java Essentials,New To Java...
# 9

> i will make it class singleton , store it in static

> HashMap but how to access it in other ordinary

> class.....

? By importing the Singleton class and calling its static method . Something like:

// get the static hashmap of the Singleton "PropertyManager"

PropertyManager.getProperties(),

Do you know how to code a Singleton?

karma-9a at 2007-7-12 10:34:53 > top of Java-index,Java Essentials,New To Java...
# 10
means we have to read file every time Know but i dont want to be happen that ?
RajeshwarReddyTa at 2007-7-12 10:34:53 > top of Java-index,Java Essentials,New To Java...
# 11
Just read the properties file only once and store all values in datastructure. Use that datastructure whereever you want.
AnanSmritia at 2007-7-12 10:34:53 > top of Java-index,Java Essentials,New To Java...
# 12

> means we have to read file every time Know but i dont

> want to be happen that ?

? No you don't . You read the file once. You store it in the hashmap. Then you hand that hashmap to whatever class needing the data.

getProperties() returns the hashmap, doesn't read the file.

Maybe I should have called the method getMap or something.

Message was edited by:

karma-9

karma-9a at 2007-7-12 10:34:53 > top of Java-index,Java Essentials,New To Java...
# 13
can u write step by step code including accessing HashMap in other class pls
RajeshwarReddyTa at 2007-7-12 10:34:53 > top of Java-index,Java Essentials,New To Java...
# 14

> can u write step by step code including accessing

> HashMap in other class pls

No, I won't.

I do not spoon-feed people. Maybe you'll have better luck with others.

Read the Singleton pattern, and come back when you have written some code and have specific problem(s).

karma-9a at 2007-7-12 10:34:53 > top of Java-index,Java Essentials,New To Java...
# 15
check this URL http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html
AnanSmritia at 2007-7-21 21:22:18 > top of Java-index,Java Essentials,New To Java...
# 16

I think that the resources are already cached, at least the ones from ResourceBundle.

The specification of ResourceBundle says:

"...Resource bundle instances created by the getBundle factory methods

are cached by default, and the factory methods return the same resource

bundle instance multiple times if it has been cached...."

[]

S_i_m_ua at 2007-7-21 21:22:18 > top of Java-index,Java Essentials,New To Java...
# 17

> it is not a servlet it is a ordinary class , then how

> to do it?

I'm sorry, but then why did your original post say:

I dont want to read properties file every time , and want to get property value in any servelet or jsp page.

Also, the singleton is your solution. And no, I am not going to post you the code either. There are plenty of Properties examples and Singleton examples (poosibly even Singleton Properties examples) available, Google for some.

masijade.a at 2007-7-21 21:22:18 > top of Java-index,Java Essentials,New To Java...
# 18

my code is as folllows:

public class ClassicSingleton {

private static HashMap managers = new HashMap();

private static ResourceBundle instance = null;

protected ClassicSingleton() {

// Exists only to defeat instantiation.

}

public static readProperty getInstance() {

if(instance == null) {

instance = new ResourceBundle ();

/* logic to store property file in Static HashMap that is in variable managers*/

}

return instance;

}

public getHashMap(){

return managers;

}

//now i will call getHashMap() method in other classes like ....

}

class A{

void call(){

ClassicSingleton s=new ClassicSingleton ();

s.getHashMap();//calling HashMap

}

}

RajeshwarReddyTa at 2007-7-21 21:22:18 > top of Java-index,Java Essentials,New To Java...
# 19
If those are the things you're having problems with, then I think you need to back up a step and start here: http://java.sun.com/docs/books/tutorial/java/index.html
masijade.a at 2007-7-21 21:22:18 > top of Java-index,Java Essentials,New To Java...
# 20
Why all that...as I posted above, the ResourceBundle already caches the resources.[]
S_i_m_ua at 2007-7-21 21:22:18 > top of Java-index,Java Essentials,New To Java...
# 21

I prefer this style

public class ClassicSingleton {

private static HashMap managers = new HashMap();

private static ResourceBundle instance = null;

private ClassicSingleton() {

// Exists only to defeat instantiation.

}

public static readProperty getInstance() {

if(instance == null) {

instance = new ResourceBundle ();

/* logic to store property file in Static HashMap that is in variable managers*/

}

return instance;

}

public static getHashMap(){

return managers;

}

//now i will call getHashMap() method in other classes like ....

}

class A{

void call(){

ClassicSingleton.getHashMap();//calling HashMap

}

}

Any objections?

AnanSmritia at 2007-7-21 21:22:18 > top of Java-index,Java Essentials,New To Java...
# 22
Typopublic static HashMap getHashMap(){return managers;}
AnanSmritia at 2007-7-21 21:22:18 > top of Java-index,Java Essentials,New To Java...