26
Fri, Apr
1 New Articles

TechTip: Replace Data Areas with Java Properties Files

General
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Quit storing your applications' property values in data areas, and make your programs easier to understand.

 

One of the programming practices I've never cared for is the use of data areas to store property values for an application. While a data area is a useful tool for the programmer who designed the system, it isn't user-friendly for those of us who need to work on it later. Usually, the data area contains a random mass of data strung together in an incoherent fashion, and without good documentation it is difficult to determine what the data is used for. Consider the example data area in Figure 1. Why is there a "Y" in position 40? What's it used for? Is the number that starts in position 33 a 7-digit number, or is it a 4-digit number followed by a 3-digit number? Again, without some sort of external documentation of the data area, it's impossible to tell just by looking at the contents of the data area what the data is being used for.

 

Data area . . . . . . . :   PRODTEST                          

   Library . . . . . . . :     CGIBIN                          

 Type  . . . . . . . . . :   *CHAR                             

 Length  . . . . . . . . :   250                                

 Text  . . . . . . . . . :   Example Data Area                 

                                                               

            Value                                              

 Offset      *...+....1....+....2....+....3....+....4....+....5

     0      'My Property value  Juarez, MX   0000018YNYYYN03302'

 

Figure 1: Example Data Area PRODTEST

 

In addition, if maintenance of the data area is required, either the IT department will have to update it using the CHGDTAARA command or a custom display will need to be developed along with a corresponding program to update the data.

 

Java properties files, on the other hand, carry none of these burdens. A properties file is simply a plain-text file that can be created in Notepad (or a similar text editor) and then saved to the IFS with a .properties extension. Because they can be viewed or edited with any text editor, there is no need to do any special programming in order to maintain them. Also, because properties files can contain comments, the purpose of the values in the file can be clearly documented. Additionally, since data is stored in the format of key name equals key value, it's a lot easier to determine what the data is being used for because the key names can be as descriptive as you want them to be.

 

In order to use properties files from iSeries applications, I chose to create a prototype called RetrieveDataFromPropFile and store it in a service program called IFS_TOOLS. The prototype takes two pieces of data as input: the name and path to the properties file and the key value to retrieve. The prototype returns either the value of the key or blanks if the key wasn't found. The prototype will also return blanks if the properties file isn't found.

 

H Option(*NoDebugIO:*SrcStmt)

H Thread(*Serialize) NoMain 

/copy Source,IFS_ToolsC

/copy QSYSINC/QRPGLESRC,JNI

*<doc>*************************************************** @Program                                  

*    RetrieveDataFromPropFile               

* @Description                            

*    Retrieve Data From Properties File        

* @Parameters              

*    PropFileVal - The Name Of The Properties File    

* @Parameters                                     

*    PropFileVal - The Name Of The Properties File

*    KeyVal - The Key Value To Retrieve           

* @Returns                                        

*    Value Assigned To This Key     

****************************************************</doc>

                                        

P RetrieveDataFromPropFile...               

P                 b                   export 

                                               

D RetrieveDataFromPropFile...                                   

D                 pi          5000A                            

D  PropFileVal                1024A   Const Varying            

D  KeyVal                     1024A   Const Varying     

D PropConfig      S                   like(PropertiesConfiguration)

D PropFile        S            100A             

D PropFileStr     S                   like(jString)        

D PropString      S                   like(jString)           

                                             

 /free                                                      

   Monitor;                                            

     PropFileStr = new_String(%Trim(PropFileVal));            

     If PropConfig = *Null;                          

       PropConfig = new_propertiesConfiguartion(PropFileStr);

     EndIf;                                                         

     PropString = PropertiesConfiguartion_getString(PropConfig :    

                  new_String(%Trim(KeyVal)));                       

     return Convert_String_To_Alpha(PropString);                    

   On-Error;                                                        

     return *Blanks;                                                 

   EndMon;                                                          

/end-free                    

P                 e      

P Convert_String_To_Alpha...                            

P                 B                    Export            

D Convert_String_To_Alpha...                            

D                 PI          1024A                     

D  String                              like(jString)    

                                                        

D String_Long     S             10I 0                   

D Text            S           1024A                     

                                                        

 /Free                                                     

  String_Long = String_length(String);                  

  String_getBytes(String : 0 : String_Long :            

                  Text : 0);                            

  Return Text;                                          

 /End-Free                                              

P                 E   

Figure 2: Source Listing for IFS_TOOLS Service Program

 

As you can see, the code to access a properties file is quite simple. The prototype simply creates a properties file object using a Java constructor and then uses the getString method to retrieve the desired key value. Since the result is returned as a Java string object, it should be converted into a format that is meaningful to an RPG programmer. The conversion is performed using the getBytes method of the Java String class in the Convert_String_To_Alpha prototype. This converts the Java string into an RPG character string.

 

The necessary Java objects have been prototyped in a separate copy file (see Figure 3) in order to promote code reuse. Because the PropertiesConfiguration Java class is not native to the iSeries, several jar files will need to be installed on your system from the Apache Commons project in order for the code to be enabled for execution. (I will explain what jars are needed and where to get them later.) Although this article uses only a single class (PropertiesConfiguration) and a single method (getString) from the configuration package, the Apache Commons collection contains a multitude of other Java classes that can be accessed from the iSeries as well. Some of the more commonly used components include Digester (used to manipulate XML), Logging, and FileUpload. Considering the ease with which these classes can be integrated into RPG applications, it's well worth the effort to check them out.

 

D new_String      PR                  like(jString)         

D                                     EXTPROC(*JAVA         

D                                     :'java.lang.String'   

D                                     :*CONSTRUCTOR) 

D create_from                 1024A   VARYING const                   

D String_length...                                           

D                 PR            10I 0                        

D                                     EXTPROC(*JAVA          

D                                     :'java.lang.String'    

D                                     :'length')             

D String_getBytes...                                     

D                 PR                  EXTPROC(*JAVA      

D                                     :'java.lang.String'

D                                     :'getBytes')       

D  SrcBegin                     10I 0 Value              

D  SrcEnd                       10I 0 Value              

D  Dest                       1024A                      

D  DestBegin                    10I 0 Value              

D PropertiesConfiguration...                                    

D                 S               O   CLASS(*JAVA               

D                                     :'org.apache.commons.-    

D                                     configuration.-           

D                                     PropertiesConfiguration') 

D new_propertiesConfiguartion...                                     

D                 PR                  like(PropertiesConfiguration) 

D                                     EXTPROC(*JAVA                 

D                                     :'org.apache.commons.-        

D                                     configuration.-               

D                                     PropertiesConfiguration'      

D                                     :*CONSTRUCTOR)                

D PropFileName                        like(jString)                  

D PropertiesConfiguartion_getString...                        

D                 PR                  like(jString)           

D                                     EXTPROC(*JAVA           

D                                     :'org.apache.commons.-  

D                                     configuration.-         

D                                     PropertiesConfiguration'

D                                     :'getString')           

D KeyValue                            like(jString) Const   

D RetrieveDataFromPropFile...                           

D                 pr          5000A                     

D  PropFileVal                1024A   Const Varying     

D  KeyVal                     1024A   Const Varying       

D Convert_String_To_Alpha...                           

D                 PR          1024A                    

D  String                              like(jString)   

 

Figure 3: Source Listing for IFS_ToolsC Copy Member

 

To create the service program, use the following commands:

 

CRTRPGMOD MODULE(MyLib/IFS_TOOLS)

CRTSRVPGM SRVPGM(MyLib/IFS_TOOLS) MODULE(MyLib/IFS_TOOLS) 

          SRCFILE(MyLib/SOURCE) SRCMBR(IFS_TOOLSB)        

 

When creating service programs, I always create a separate binding source file to make it easier to change the service program later if I need to. If you don't have a great deal of experience with service programs, you may think this is an unnecessary waste of time, but trust me; it isn't. Using binding source will save you many headaches down the road.

 

STRPGMEXP PGMLVL(*CURRENT) SIGNATURE('IFS_TOOLS  v1.01')     

  EXPORT SYMBOL("RETRIEVEDATAFROMPROPFILE")                  

  EXPORT SYMBOL("CONVERT_STRING_TO_ALPHA")                   

ENDPGMEXP                                                     

 

Figure 4: Binding source member IFS_TOOLSB

 

Now that you've created the service program, the next step is to create a properties file and save it to a directory in the IFS.

 

# Comments start with the # sign

# This properties file is used for...

# Sample property PROP_ID_1

PROP_ID_1=My property value

# Text values that contain commas (,) must be escaped with a  # backslash ()

# Properties can have multiple values separated by commas. I did not

# implement this functionality so only one value will be returned.

# If commas are not escaped the prototype will return everything until

# the first comma is encountered.

# System Identifier Text

SYSTEM_ID_TXT=Juarez, MX

# Initial system selected in drop-down box

SYSTEM_ID_SELECTED=Juarez

# Number of items in drop-down box

SYSTEM_ID_NUM=18

#Note: Numeric values are returned as text and must be converted

 

Figure 5: Sample Properties File /mydir/test.properties

 

In order to access the properties file using the new service program IFS_TOOLS, use the sample program in Figure 6.

D PropFile        S           1024A    

D SystemID      S             100A

/Copy Source,IFS_ToolsC

PropFile = ‘/mydir/test.properties';

SystemID = RetrieveDataFromPropFile(PropFile :           

                                   ‘SYSTEM_ID_TXT');

*InLr = *On;

// SystemID will equal Juarez, MX

Figure 6: Sample RPG Program PROPTEST for Retrieving a Property from a Properties File

 

To create the sample program, use the following commands:

 

CRTRPGMOD MODULE(MyLib/PROPTEST)

CRTPGM PGM(MyLib/PROPTEST) BNDSRVPGM(MyLib/IFS_TOOLS)

 

Before running the sample program, you must install the required jar files:

  • Commons-configuration-1.1.jar
  • Commons-collections-3.1.jar
  • Commons-lang-2.0.jar
  • Commons-logging.jar

These can be easily found online by doing a quick Google search for Apache Commons or by navigating to http://commons.apache.org/. The easiest way to install them on your iSeries after you have downloaded them is to simply save them to the folder /QIBM/UserData/Java400/ext. Any jar files stored in this folder will automatically be found when the JVM starts, so there is no need to add them to your classpath. If you save them to any other IFS folder, you will need to explicitly add them to your classpath using the ADDENVVAR command before running the sample program.

 

While the code to implement the use of properties files is simple, the concept is powerful. One of the best uses I have found for properties files is multiple language support. I create a properties file and then enter values for everything that will be displayed on Web pages, displayed on reports, etc. Once I have the English version complete, I send it off to be translated into Spanish, Chinese, etc. By using the prototype described in this article, switching from one language to another is a breeze. I simply pull the properties from whichever language file is required per the user's request.

 

In summary, using a Java properties file is a great way to make an application more user-friendly because the properties can easily be updated, documented, and shared by many applications on a wide variety of platforms. By utilizing properties files, you can be assured that whatever application needs the data can access the data using only pure Java methods, thus eliminating the need to store the properties on multiple systems. Since the properties files can be (and should be) well-documented, developers no longer have to waste time figuring out what the data means before getting to work on fixing problems. These factors make replacing data areas with properties files a sound business decision and one that is well worth the investment.

DAVID MAYLE
Dave Mayle is a senior developer for Eaton Corporation, a global leader in electrical systems and components for power quality, distribution, and control. Dave has worked on the iSeries/System i since 1993 and is currently focused on developing Java, Web, and ILE applications. Dave has written numerous technical articles on the iSeries and is a three-time Speaker of Merit award winner at COMMON. Dave is a graduate of Bucknell University and can be reached at This email address is being protected from spambots. You need JavaScript enabled to view it.
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: