Sunday, May 25, 2008

Properties and System Properties in Java

One of our visitors, Ran Vijay posted the following two questions:-

  • What are System Properties in Java?
  • What is the difference between loading a JDBC Driver using System Properties and loading it using Class.forName()?

Let's first discuss Properties in Java and then we'll move on to System Properties and finally to the second question, which is regarding loading of JDBC Drivers.

Properties in Java

In Java, Properties are actually configuration values which are managed as the traditional key-value pairs. Both the Key and the Value in every pair will be String values only. As the name suggests, the Key will be used to retrieve the corresponding Value and hence it must be unique in a properties file. These files are plain text files and by convention we give them ".properties" extension. To manage properties in Java, we need to create instance of java.util.Properties class
. This class contains all the methods required to manage and access the properties file. The properties files are used quite frequently in Java applications for storing and managing the configuration value which your application may require during various phases of its execution. For Example, you may have a property named "DBURL" and you can simply use the value associated with this key to fetch the Database URL you would like to use at run time. The advantage of using this approach is that if you want to change the DBURL then you don't need to touch the code now. The all you need is to go and change the Value associated with the "DBURL" Key in the properties file being accessed in your application. This will make your application far more flexible and maintainable. If a user (say a tester) doesn't know Java then it'll be difficult for him/her to change the URL in the Java code, but changing the value in a text-file will be easy for everyone and hence such an approach will enhance the usability of your application as well.

getProperty(String key) and getProperty(String key, String defaultValueifkeyNotFound) are the methods used to read the properties. Similarly, setProperty(String key, String value) is the method to add another Key-Value pair to the Properties object.

Caution: Access to a properties file requires approval from the current security manager of the running application. So, you may not like to put any such code inside an applet as the browser, your code will run into, may not have enough priviledges to access a file on the local disk. Moreover, it's hard to assume that the property you require in your applet will be there in the properties file (or even a file with that name will exist) on the local disk.

System Properties in Java

The way we use custom proprties file to maintain the configuration details of our application, the Java platform also uses a Properties object to maintain the configuration details of the current runtime environment. This object is called System Properties and it stores hell lot of Key-Value pairs for different properties the Java runtime environment uses to maintain the execution environment. Some of these properties store the Current User, Version of the Java Runtime Env, Path Separator character, OS Name, OS Version, User's Working Directory, User's Home Directory, JDBC Drivers, etc. Many of these System Properties are standard, but few additional platform dependent System Properties may also exist.

Reading/Writing to the System Properties

Reading is pretty simple. You just need to use 'System.getProperty()' method. This method has two variants. One, which requires only one String argument. Two, which requires two String arguments. For the one which requires only one argument, you need to give the name of the System Property you're trying to get the value of. This method returns 'null' if the property with that name doesn't exist. The second variant of this method requires you to specify the second String argument as the value you would like to be returned (instead of 'null') in case a property with the name given as the first argument doesn't exist.

Writing to the System Properties is not quite similar to what we use to write to a normal Properties onject. We use 'System.setProperties()' method, which requires a Propereties object. You need to set this object with all the required Key-value pairs. Beware, it replaces the entire set of system properties with those represented by the passed properties object. The changes made by this method will last only for the current run time environment and the Java run time system re-initializes the system properties each time it starts up. If you want to make the changes permanent then you need to write the value to some file before shutting down the application and load and set the System Properties each time the application starts up.

Caution: Read/Write access to the System Properties is also governed by the current Security Manager of the running application, so you may be denied the access in various situation - most common of them being 'accessing them in Applets'.

Difference between loading the JDBC Drivers through System Properties and through Class.forName()?

Well... no real difference in the functioning of the drivers. It's just the time of loading and instantiating the drivers that gets different in these two cases. In case of loading the drivers through System Properties, they get loaded and instantiated during the start up of the Java Runtime Environment itself while loading them through Class.forName() will cause the loading and instantiating of the driver during the execution of this Java statement. You may like to read the article 'Role of DriverManager Class and Driver Interface in Java' for more details.


No comments: