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?
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.
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.
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?
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.
Do not threaten THIS beaver! Not even with this tiny ad:
a bit of art, as a gift, that will fit in a stocking