Tip

Just say no to hard-coded data

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:
<P>
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

    Requires Free Membership to View

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.


This was first published in April 2002

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.