Pages

Monday, 25 March 2013

Using your O.S Information to perform tasks locally in Web Applications

Hello everyone. This is my first ever blog post. So if someone find a mistake by me or something missing, I apologize in advance for that. Kindly let me now and i'll try to avoid it!

So lets get Started!

Problem:

While developing a web application that is developed on a local machine and then deployed on a server machine, sometimes we need to test or log something in the local test environment that is not needed in the server environment. 
e.g: the database settings. Your database settings may be different in local & server environment.

for instance you local database settings may be: 
IP: localhost 
Port:3306 
Database Name: my_database
User Name: localdb
password: localpassword

and you remote database settings may be: 
IP: 192.168.15.1
Port:3306 
Database Name: my_database
User Name: remotedb
password: remotepassword

And it usually becomes hectic to modify these values each time you're going to deploy your code on server. (in my case i usually forgot to change these values!)

Solution:

In Java, this issue can be addressed by using your O.S System Information provided the same O.S and same version is not installed on both the machines (i.e: your local machine and the server machine).
To overcome this issue, i usually create a boolean flag to check if my code is being executed on the local machine or server machine!

The following below code can detect if the application is running in windows environment or some other environment using java's os.name property. This property can be accessed using System.getProperty(String propertyName) method can be accessed:

1
public static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().indexOf("win") >= 0;

and this boolean flag can be used in following way:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
String ip = null;
int port = 3306;
String db = "my_database";
String username = null;
String password = null;
if (IS_WINDOWS) { 
    System.out.println("Application is running in windows");
    ip = "localhost" ;
    username = "localdb";
    password = "localpassword";
} else {
    System.out.println("Application is running on server");
    ip = "192.168.15.1" ;
    username = "remotedb";
    password = "remotepassword";
}

The above code demonstrates the usage of property os.name. Other properties of O.S like version and architecture can also be used to achieve the goal. 
These properties can be get in following way:

1
2
3
String osName = System.getProperty("os.name");
String osVersion = System.getProperty("os.version");
String osArchitecture = System.getProperty("os.arch");

Here is a link where all supported O.S names, versions and architectures are enlisted!

References:

  1. System.getProperty(String key): http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getProperty(java.lang.String)
  2. List of all supported O.S names, versions and architectures: http://lopica.sourceforge.net/os.htm