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!
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:
- System.getProperty(String key): http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getProperty(java.lang.String)
- List of all supported O.S names, versions and architectures: http://lopica.sourceforge.net/os.htm
Good job Muneeb, keep blogging!
ReplyDeleteThanks @Afnan :)
Deletegood post....really informative..:)
ReplyDeleteThanks @Abdullah! :)
DeleteAnother idea will be to create config files specific to each environment and deploy the config specific to the environment. This way you can use more than one environments and definitely use the same OS
ReplyDeleteThanks for the suggestion @Stinger.
DeletePerhaps i'll create a separate post out of it!
Thanks
Awsome Muneeb. Keep up the good work
ReplyDeleteThanks @Mahtab! :)
DeleteIts an awesome work, a suggestion is "If you can enlist all the classes, methods and parameters that are being used/provided in your lib" in the readme.txt or in a separate document as in Javadoc, It would be even more great. :)
ReplyDeleteA good Work, keep it up.
Thanks @Idris :)
DeleteCan you suggest a sample to write such a readme.txt?
If i have local linux too the, this method will fail, I think there should be one config parameters in your web configuration, like is_local = true, if true then use local file otherwise use global.
ReplyDeleteThis method would only fail when both (local and server) the machines have same O.S name, version and architecture!
Delete