Time and time again, I see code that contains hardcoded variables, whether it's the host name of an R/3 system,...
a user ID, or even a password. The problem with hardcoding, especially in Java, is that any changes need to be made to the source code itself, recompiled, and added back to the application framework. Obviously, you don't want to give general users access to your source code just so they can personalize their login ID. Java gives us a very simple way around this problem using properties files. A properties file is a plain text (non-binary) document that contains name/value pairs. This file can be easily read into your Java application, extracting only those name/value pairs needed. A sample properties file looks something like this:
SAP.user=me SAP.password=yourpass SAP.host=SAPSERVER SAP.client=100 SD.salesorg=3000 SD.division=00 SD.distrchan=00 In this example, the names are represented by two terms separated by a period. The period is not necessary, nor are you limited to two terms. However, by using this style of notation, you can effectively separate the variables into different classes. In this properties file "SAP.*" represents generic SAP system data and "SD.*" represents sales and distribution data. The name/value pair is separated by an equals sign and anything following the equals sign will be attributed to the value before it within your application. The properties file must be named with a ".properties" file extension and placed in your Java classpath. As long as the file is in your classpath, you will be able to retrieve the file from within your application. For our example, lets call the properties file "sapdata.properties". Now lets take a look at the source code that will let us use the data within our application. ResourceBundle sapProperties = ResourceBundle.getBundle("sapdata"); We now have a ResourceBundle object that contains all of the name/value pairs in our properties file. Note that I did not need to add the ".properties" file extension, as this is assumed by the ResourceBundle.getBundle() method. Now lets retrieve a property. String sapUser = (String) sapProperties.getString("SAP.user"); We now have a String object that contains the value 'me' as extracted from the properties file. This object can now be used within our application wherever we need to use the SAP user ID as a variable. The properties file can be edited within a standard text editor, from UNIX emacs to Windows notepad. By providing your users with easy to edit properties files, you allow them to maintain certain parameters within your application without giving them access to your source code. Follow this practice as you begin to develop applications for SAP using such connectors as JCo and you will make it much simpler to support and maintain your Java applications.
Author Austin Sincock is product manager for ROBUSTA(tm), Gamma Enterprise Technologies Web sales solution for SAP.