Thursday 26 February 2015

snLibrary - Inducing External Values to avoid Hard Coding

snLibrary - Inducing External Values


One of the guiding principles that is part of the philosophy of 'snLibrary' is to maintain zero-hard-coding in application development.

I have many times noticed while web-application development, specific requirements of 'hard-coding' does come in which are hard to reject.  One such example would be the requirement to hard-code URLs or IP addresses or domain names of other servers or services that you need to put into the application code for URL re-direction or for providing hyperlinks. When such situation arises, what can we do?   The easiest method is either to 'hard-code'!! or save them up in a Java 'class' or 'interface' and implement them whenever needed.  But it would be at the cost that whenever there arises a request for change, you would need to modify the code and re-deploy in production.  Another method would be to set them up as context parameters in the web.xml and use it within the program.  Here again, whenever a change is required, the web-administrator would  need to actually extract the web.xml file out of the web-archive (.war) file and then modify and re-deploy the same.  This requires man-hours and technical expertise and careful handling. While in the tomcat environment this might be pretty easy, in JBoss or other environments and wherever exploding web archive file option is disabled, it would entail recompiling of the .war file.

Shall we avoid such extensive operations by any means?  Can snLibrary provide some solution?  This feature is available since version 22.2 dtd. 14-Feb-2015.

The following methodology can be used as a solution to the above problem.

Let us assume that we need to specify certain variables (or say constants - in this case) that needs to form prefix to few hyperlinks that your application would refer to.  Let us define the variable names as http.1, http.2, http.3, http.4, http.5, http.6.  (Needless to mention that the name of the variable/constant could be anything of your choice - say it could be alpha, beta, gama or say main.background-color, main.foreground-color etc.).  Another variable called 'defaultLanguage' is also mentioned for better understanding.

File:-c:/snConfig/snConfigMyApplication.ini
External Variable-http.1=http://10.34.133.216:8084/TimeAttendanceNew
External Variable-http.2=http://10.75.132.31
External Variable-http.3=http://10.34.132.119
External Variable-http.4=http://10.75.132.30:8080
External Variable-http.5=http://10.18.64.84
External Variable-http.6=http://10.75.132.15
External Variable-defaultLanguage=Hindi

 In the Java code, we could refer these variables/constants as follows:
File:- someJavaCode.java
import config.snConfigVars; 
// ...
// .. Some part of code goes here....
// ..
snConfigVars V = snConfigVars.getInstance();
String urlMypath = V.getExternalVariable("http.4", "");
// Thus 'urlMypath' would contain the value 'http://10.75.132.30:8080'

String lang = V.getExternalVariable("defaultLanguage", "English"); 
// Here, 'lang' would contain the value 'Hindi'.  In case, the variable was not provided in snConfigFile, it would have taken it as 'English' for default.

String bkg_color = V.getExternalVariable("main.background-color", "#FFFF");
// Thus the value of bkg_color would be #FFFF since no value has been specified in the snConfigFile.

Having found this very useful, I would rather also use it to create some other features - say create a web-page which is language sensitive.  For this, I could define as follows:-

File:-c:/snConfig/snConfigMyApplication.ini
External Variable-defaultLanguage=hi
#External Variable-mainpage.name.en=Name
#External Variable-mainpage.age.en=Age
#External Variable-mainpage.dob.en=Date of Birth
#External Variable-mainpage.addr.en=Address
External Variable-mainpage.name.hi=नाम
External Variable-mainpage.age.hi=आयु
External Variable-mainpage.dob.hi=जन्म तिथि
External Variable-mainpage.addr.hi=पता

And in the mainpage,jsp, the form could be something as follows.

File:-mainpage.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="config.snConfigVars" %>
<%
snConfigVars V = snConfigVars.getInstance();
String lang = V.getExternalVariable("defaultLanguage", "en"); 
%>
<html>
<body>
<form action="nextpage.jsp">
    <table border='0'>

    <tr><td> <%= V.getExternalVariable("mainpage.name." + lang, "Name")%> 
    </td><td> <input name="username" type="text" />
    </td></tr>

    <tr><td> <%= V.getExternalVariable("mainpage.age." + lang, "Age")%> 
    </td><td> <input name="age" type="text" />
    </td></tr>

    <tr><td> <%= V.getExternalVariable("mainpage.dob." + lang, "Date of Birth")%> 
    </td><td> <input name="d_o_b" type="text" />
    </td></tr>

    <tr><td> <%= V.getExternalVariable("mainpage.addr." + lang, "Address")%> 
    </td><td> <input name="addr" type="text" />
    </td></tr>

    </table>
     <input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>

Better still, if it was a case of Language properties, it could also be in an external properties file.  In that case, it could be mentioned in the snConfigFile as follows and we can move all the definitions of external variables into the external file.

File:-c:/snConfig/snConfigMyApplication.ini
External Variable Properties File=c:/snConfig/snExternalVariablesProperties.properties

File:-c:/snConfig/snExternalVariablesProperties.properties
defaultLanguage=hi

mainpage.name.en=Name
mainpage.name.hi=नाम
mainpage.name.sn=नाम: 

mainpage.age.en=Age
mainpage.age.hi=उम्र
mainpage.age.sn=आयु:

mainpage.dob.en=Date of Birth
mainpage.dob.hi=जनम दिन
mainpage.dob.sn=जन्म दिनाङ्कः

mainpage.addr.en=Address
mainpage.addr.hi=पता
mainpage.addr.sn=सङ्केत:



No comments:

Post a Comment