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");
Requires Free Membership to View
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

Join the conversationComment
Share
Comments
Results
Contribute to the conversation