Part 4: Specifying parameters with a custom properties file

The JCo API offers eight distinct JCO.createClient() methods to establish a connection to the SAP application server. This handy step-by-step guide reviews these mechanisms and offers some recommendations on the best way to pass system parameters to an SAP JCo connection.

Using a Properties file provides a good way to centralize system parameters in a single, maintainable format. However, there are some limitations to the properties file, including the fact that the fully qualified filesystem path name must be passed in order to ensure that the JVM can find the file.

In addition, using the createClient(Properties) method requires you to use the naming standard set forth in the JCo API for all name/value pairs. Although a minor inconvenience, the inability to create new name attributes may frustrate developers who adhere to an existing naming convention. Both these challenges can be overcome by creating a custom properties file that is read as a ResourceBundle within your Java application.

The ResourceBundle class allows you to specify a standard properties file (for this example I am using the "sap.properties" created previously), then selectively retrieve values within your application. The distinct advantage to using a ResourceBundle is that the properties file must simply exist somewhere in your applications CLASSPATH in order for the application to find it. The idea of using the CLASSPATH becomes important when you want to package and deploy your application within a Java application server using a JAR, WAR, or EAR format.

For this example, I have combined the ResourceBundle with the String array technique detailed previously:

import com.sap.mw.jco.*;
import java.util.ResourceBundle;
...
ResourceBundle sapBundle = ResourceBundle.getBundle("sap");
    
String[][] sapParams = { {"client", sapBundle.getString("jco.client.client")}, 
                         {"user", sapBundle.getString("jco.client.user")}, 
                         {"passwd", sapBundle.getString("jco.client.passwd")},
                         {"lang", sapBundle.getString("jco.client.lang")}, 
                         {"ashost", sapBundle.getString("jco.client.ashost")}, 
                         {"sysnr", sapBundle.getString("jco.client.sysnr")}
                         {"trace", sapBundle.getString("jco.client.trace")}};  

JCO.Client connection = JCO.createClient(sapParams);
...
connection.disconnect();
Notice that I did not need to specify the ".properties" extension. The ResourceBundle.getBundle() method assumes (and requires) that your properties file be tagged with the ".properties" extension, and thus, you do not need to specify it.

In this instance, I chose the String array method because I needed to pass the RFC trace parameter. You could also use the ResourceBundle technique with the basic connection method by passing each parameter as a String to createClient(). The ResourceBundle technique also lets you set system parameters dynamically rather than reading them from the properties file. Simply specify the parameter directly through your application, within the String array.

In order for your application to work with a ResourceBundle you must ensure that the CLASSPATH variable contains a pointer to the directory in which the properties file is located. From a Windows console, that command would look like this:

set CLASSPATH=%CLASSPATH%;<pathname to properties file>

GETTING CONNECTED WITH JCO

 Home: Introduction
 Part 1: Specifying parameters as single String values
 Part 2: Specifying parameters using a String array
 Part 3: Specifying parameters using an external properties file
  Part 4: Specifying parameters with a custom properties file
 Review: Conclusion and more resources
This was first published in July 2005

Dig deeper on SAP Java and J2EE

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

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:

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close