Home > SAP software/management Tips > SAP ABAP/Java developer tips > Just say no to hard-coded data
SAP Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

SAP ABAP/JAVA DEVELOPER TIPS

Just say no to hard-coded data


04.29.2002
Rating: -3.20- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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 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.


Rate this Tip
To rate tips, you must be a member of SearchSAP.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
SAP ABAP/Java developer tips
How to do additional dialog processing after SAP COMMIT WORK statement
How to find a piece of SAP ABAP code without debugging
How to read an SAP transaction in an ABAP code
How to provide an SAP R/3 4.5B application server with a Web service interface
How to find owners and transports of deleted ABAP programs
Fixing a common OPEN_FORM and START_FORM error in SAPscript
Select Text fields: Case-insensitive
Is this the quickest way to find a BADI?
Easily debug error messages in SAP processes
Accessing private attributes in ABAP Objects

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

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.



NetWeaver SAP White Papers
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
SearchSAP.com is a search service provided by TechTarget and is completely
independent of and not affiliated with SAP AG.
  TechTarget - The IT Media ROI Experts