Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questions HashMap - global Settings -Servlets

 
Miltos Tereres
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
Thats a nice forum...
I am developing a web application.
Firstly i want to ask you if that i am doing is good practice.
I store at a xml file the description of some user rights...
like that...

...
<category name="customers" display-name="whatever">
<right name="add" display-name="whatever"></right>
<right name="manage" display-name="whatever"></right>
<right name="remove" display-name="whatever"></right>
<right name="edit" display-name="whatever"></right>
</category>
...

At context initialised event i load these stuff into a class that has some useffull methods to manage these.like getcategories etc...
I use the singleton pattern in order to load the settings only once... Its like that... RightsManager.getinstance(filepath)...
after that i just call RightsManager.getinstance() .
the filepath is stored in web.xml...

Is this a good or bad practice?

Also i have a LinkedHashMap within that class. But LinkedHashMap isnt thread safe..Should i do anything for that or its ok ?

thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Parhs Parhs", please check your private messages for an important administrative matter.

Also, please be sure to post English only. The non-English portion of your post has been elided.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than using a singleton, I'd place the map in the application context.

If all access to it (once it is loaded) is read-only, you have no contention worries.
 
Miltos Tereres
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr Bear Bibeault we arent all English speakers. I cant understand which is the problem..anyways.
My first though was to save all tha stuff at application context..But i cant figure how to access application object within my classes.I know within servlet i can access the application context but within my classes?
Suppose i have a class that has these functions
class myClass
{
getCategories()
getRightsForCategory(bla)
etc...
}
How would this access the application context:S?
The only way i can figure is myClass to hold a reference to application object...

Is there a better way to achieve this?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can simply pass data to your other classes. If you find yourself needing the data in a whoel lot of laces, you might have uncovered some structuring issues.

A singleton can be employed, but I usually find that such "global" variables usually cause more problems than they solve.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check your private messages again.
 
Miltos Tereres
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply...
I just want to know how you would do it!

You proposed me to do something like that ?
class RightsParser
{
RightsParser(LinkedHashMap) //constructor

getCategories()
getRightsForCategory(bla)
etc...
}

myservler(mplampla)
{
RightsParser rights=new RightsParser(application.getAtrribute("rights"));
rights.getCategories();
etc.....?
}


or just keep the TreeHashMap inside the RightsParserClass and not pass it from the application to the class every time ?


Any other solution is welcomed..
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another tip: Be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic