• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read/Write file everytime ?  RSS feed

 
Nick Sher
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have been doing java development for a while but I am not good at the design side. Here is what I am looking for: I have a web application and the UI properties are saved in an XML file. The user can edit the properties from UI and the XML is updated. Then these properties are used application wide. As of now I am reading the XML file everytime a property has to read. I am not happy with this and there is a need for an efficient alternative.

How can I avoid reading the file everytime and just touch the file only when the properties are updated and read it once and use all the time?

Please help.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a cache that will live in the application context. You will populate the cache with the content of the file at startup. When a user modifies and saves some properties, write the changes and update the cache. When you want to access a property, read the cache, not the file.
 
Sandeep Sanaboyina
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Load all the properties into a collection (Eg: hashmap or hashtable) at start up.

Access this collection through out the application. That should do the trick.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As web app startup, you could read the XML document into an org.w3c.dom.Document object that you keep in memory. After making any changes to it you'd write it to disk. But there wouldn't be a need to ever read it again.

As an alternative to DOM, some people prefer to use APIs like JDOM or XOM that have a more Java-ish feel to it, but that is mostly a matter of preference, not functionality.
 
Nick Sher
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the replies.

I am familiar with loading the properties during startup (I use a singleton in which these properties can be loaded along with other properties that I initialize). How do I update these properties? For example, if UIConfig is doing save and get operations on all properties and AppConfig is the singleton which i can use application wide, do I need a updateConfig method in AppConfig that is called from 'set' method in UIConfig? In this case, the updateConfig method will be public and available to all the classes in the application, how do I hide this method from other classes?

Or is there an alternative?

Thanks.
 
Nick Sher
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I spent some time on resolving this issue but want to make sure what I am doing is correct. So please help me here.

The web application has Actions <--> Services <--> Implementations. The user comes to the UI, changes some settings (pagination size, show/hide properties), and these settings are saved in an XML file. The XML file is read/written by a 'UIConfiguration' class and settings are stored as a bean. My singleton 'ConfigService' instantiates this UIConfiguration. The code is like this.


As you can see how I update/retrieve the properties here.

Things that I think that I am not doing right are
:
1. I have ConfigService.getInstance() in the contextlistener.
2. Also, everywhere in the application I do ConfigService.getInstance() whereever I need a UI property.
3. I do ConfigService.getInstance().updateUIConfig(configurations) in my UIConfiguration class when I update the properties.

Is this a right way to do this? Is there any better/clean way of doing this. I also use Spring to inject services into my action classes. Can that help to better the behavior anyhow?

Please help me here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!