views:

101

answers:

5

I am developing a Java Desktop Application. This app needs a configuration to be started. For this, I want to supply a defaultConfig.properties or defaultConfig.xml file with the application so that If user doesn't select any configuration, then the application will start with the help of defaultConfig file.

But I am afraid of my application crash if the user accidentally edit the defaultConfig file. So Is there any mechanism through which I can check before the start of the application that whether the config file has changed or not.

How other applications (out in the market) deal with this type of situation in which their application depends on a configuration file?


If the user edited the config file accidentally or intentionally, then the application won't run in future unless he re-installs the application.

+2  A: 

I am not at all sure that this is a good approach but if you want to go ahead with this you can compute a hash of the configuration file (say md5) and recompute and compare every time the app starts. Come to think of it, if the user is forbidden to edit a file why expose it? Stick it in a jar file for example, far away from the user's eyes.

David Soroko
Unlike .exe files, Jar files can easily be uncompressed
Yatendra Goel
+1 ...............................
Yatendra Goel
A: 

If the default configuration is not supposed to be edited, perhaps you don't really want to store it in a file in the first place? Could you not store the default values of the configuration in the code directly?

Thomas
This solution can cause many problems down the line.
instanceofTom
@TomNeyland: yes, it can - but not necessarily so, depending on the actual case.
Thomas
+2  A: 

I agree with David in that using a MD5 hash is a good and simple way to accomplish what you want.

Basically you would use the MD5 hashing code provided by the JDK (or somewhere else) to generate a hash-code based on the default data in Config.xml, and save that hash-code to a file (or hardcode it into the function that does the checking). Then each time your application starts load the hash-code that you saved to the file, and then load the Config.xml file and again generate a hash-code from it, compare the saved hash-code to the one generated from the loaded config file, if they are the same then the data has not changed, if they are different, then the data has been modified.

However as others are suggesting if the file should not be editable by the user then you should consider storing the configuration in a manner that the user can not easily edit. The easiest thing I can think of would be to wrap the Output Stream that you are using to write the Config.xml file in a GZIP Output Stream. Not only will this make it difficult for the user to edit the configuration file, but it will also cause the Config.xml file to take up less space.

instanceofTom
A: 
  1. Remove write permissions for the file. This way the user gets a warning before trying to change the file.
  2. Add a hash or checksum and verify this before loading file
  3. For added security, you can replace the simple hash with a cryptographic signature.
A: 

From I have found online so far there seems to be different approaches code wise. none appear to be a 100 hundred percent fix, ex:

The DirectoryWatcher implements AbstractResourceWatcher to monitor a specified directory.

Code found here twit88.com develop-a-java-file-watcher

one problem encountered was If I copy a large file from a remote network source to the local directory being monitored, that file will still show up in the directory listing, but before the network copy has completed. If I try to do almost anything non trivial to the file at that moment like move it to another directory or open it for writing, an exception will be thrown because really the file is not yet completely there and the OS still has a write lock on it.

found on the same site, further below.

How the program works It accepts a ResourceListener class, which is FileListener. If a change is detected in the program a onAdd, onChange, or onDelete event will be thrown and passing the file to.

will keep searching for more solutions.

Justin Gregoire