The JCo utility cookbook
25 Aug 2005
The following utilities represent some useful tips and techniques for working with RFC/BAPI interfaces when developing with SAP's Java Connector, JCo. These truly are development utilities in the sense that their intent is to provide generic support for almost any programming effort that utilizes JCo to interface with SAP.
Although each one is fully functional "out of the box", I fully expect that you, as the developer, will tailor each to your own individual needs. In actuality, these tools are the ones that I am always searching for through my own development library every time I start a new JCo/SAP project. This checklist is meant to be an easy-to-use reference whenever you need one of these functions and can't quite remember which project/CD/source control system that utility is stored in.
Part 1: Writing an RFC structure to XML
25 Aug 2005
This utility takes any RFC/BAPI name as input and automatically generates an XML representation of the various parameters/structures/tables that make up the external interface to this function. This tool is very useful for generating a structured view of the RFC/BAPI that can readily be viewed in XML-capable browsers like Internet Explorer.
For example, viewing an XML document in Internet Explorer displays the nested interface structures that can be used as a drilldown to determine specific table, structure and field names. I have found this utility especially useful when, during the course of JCo development, I need to look up a specific field name and don't want to go through the hassle of logging onto SAP to do so.
This utility can also be used to generate XML structures that can be imported into other XML-based mapping tools. You can use this structure to pass data as an XML document to your JCo application. Of course, your JCo application is still responsible for parsing the XML and making the appropriate JCo calls.
import
com.sap.mw.JCo.
*
;
public
class
Rfc2Xml
{
static
final
String[][] sapParams
=
{
{
"
client
"
,
"
000
"
}
,
{
"
user
"
,
"
myUsername
"
}
,
{
"
passwd
"
,
"
myPassword
"
}
,
{
"
lang
"
,
"
en
"
}
,
{
"
ashost
"
,
"
mySapHost
"
}
,
{
"
sysnr
"
,
"
00
"
}
}
;
static
final
String filePath
=
"
c:/dev/interface
"
;
static
final
String interfaceName
=
"
RFC_SYSTEM_INFO
"
;
public
static
void
main(String[] args)
{
try
{
JCo.Client connection
=
JCo.createClient(sapParams);
IRepository repository
=
new
JCo.Repository(
"
saprep
"
, connection);
JCo.Function function
=
repository.getFunctionTemplate(
interfaceName).getFunction();
connection.disconnect();
function.writeHTML(filePath
+
"
.html
"
);
function.writeXML(filePath
+
"
.xml
"
);
System.out.println(
"
Output written to
"
+
filePath
+
"
.html
"
);
System.out.println(
"
Output written to
"
+
filePath
+
"
.xml
"
);
}
catch
(Exception ex)
{
ex.printStackTrace();
}
}
}
Part 2: Generating metadata for an RFC structure
25 Aug 2005
Metadata, what the heck do you mean by metadata? In this context, metadata simply refers to the structural definition of a specific RFC. This includes the parameters, structures, and tables required to call the RFC as well as the fields included within each.
Often, I find it useful to be able to automatically generate an object-based structure for an RFC interface. This means that, unlike an XML or plain text view, the object structure of the RFC can easily be used to create other formats or views of the structure. By modifying this utility you could (for example) output a Java bean interface, an MQ message format, or even an XML document type definition for any RFC/BAPI interface in SAP. In order to keep things simple, the included source code generates and displays a plain text view of any RFC you care to specify.
Note that the output formatting in the formatRfc() method is pretty clumsy and would normally be replaced with something more elegant. The intent was to create human-readable output of the RFC interface in a plaintext file.
import
java.io.FileWriter;
import
com.sap.mw.JCo.
*
;
public
class
RetrieveRFC
{
static
final
String[][] sapParams
=
{
{
"
client
"
,
"
000
"
}
,
{
"
user
"
,
"
myUsername
"
}
,
{
"
passwd
"
,
"
myPassword
"
}
,
{
"
lang
"
,
"
en
"
}
,
{
"
ashost
"
,
"
mySapHost
"
}
,
{
"
sysnr
"
,
"
00
"
}
}
;
static
final
String interfaceName
=
"
RFC_SYSTEM_INFO
"
;
static
final
String filePath
=
"
c:/dev/interface.rfc
"
;
public
static
void
main(String[] args)
{
try
{
JCo.Client connection
=
JCo.createClient(sapParams);
connection.connect();
IRepository repository
=
new
JCo.Repository(
"
SAPRep
"
, connection);
JCo.Function interfaceFunction
=
repository.getFunctionTemplate(
"
RFC_GET_FUNCTION_INTERFACE
"
).getFunction();
interfaceFunction.getImportParameterList().setValue(interfaceName,
"
FUNCNAME
"
);
connection.execute(interfaceFunction);
JCo.Function structFunction
=
repository.getFunctionTemplate(
"
RFC_GET_STRUCTURE_DEFINITION
"
).getFunction();
JCo.Table interfaceTab
=
interfaceFunction.getTableParameterList().getTable(
"
PARAMS
"
);
JCo.Table structTab;
FileWriter out
=
new
FileWriter(filePath,
false
);
out.write(
"
Type
"
+
"
/t/t/t
"
);
out.write(
"
Name
"
+
"
/t/t/t/t/t
"
);
out.write(
"
Description
"
+
"
/t/t/t/t
"
);
out.write(
"
/n
"
);
while
(interfaceTab.nextRow())
{
structFunction.getImportParameterList().setValue((String)interfaceTab.getValue(
"
TABNAME
"
),
"
TABNAME
"
);
connection.execute(structFunction);
structTab
=
structFunction.getTableParameterList().getTable(
"
FIELDS
"
);
formatRFC(interfaceTab, structTab, out);
}
out.close();
}
catch
(Exception ex)
{ex.printStackTrace();}
System.out.println(
"
Output written to
"
+
filePath);
}
private
static
void
formatRFC(JCo.Table interfaceTab, JCo.Table structTab, FileWriter out)
{
try
{
if
(
"
I
"
.equals((String)interfaceTab.getValue(
"
PARAMCLASS
"
)))
{
out.write(
"
Parameter
"
+
"
/t/t
"
+
(String)interfaceTab.getValue(
"
PARAMETER
"
));
if
(((String)interfaceTab.getValue(
"
PARAMETER
"
)).length()
<
18
)
out.write(
"
/t/t/t
"
);
else
out.write(
"
/t/t
"
);
out.write((String)interfaceTab.getValue(
"
PARAMTEXT
"
)
+
"
/t/t/t
"
);
out.write(
"
/n
"
);
}
if
(
"
T
"
.equals((String)interfaceTab.getValue(
"
PARAMCLASS
"
))
||
"
E
"
.equals((String)interfaceTab.getValue(
"
PARAMCLASS
"
)))
{
if
(
"
T
"
.equals((String)interfaceTab.getValue(
"
PARAMCLASS
"
)))
out.write(
"
Table/t/t/t
"
+
(String)interfaceTab.getValue(
"
PARAMETER
"
));
else
out.write(
"
Structure/t/t
"
+
(String)interfaceTab.getValue(
"
PARAMETER
"
));
if
(((String)interfaceTab.getValue(
"
PARAMETER
"
)).length()
<
18
)
out.write(
"
/t/t/t
"
);
else
out.write(
"
/t/t
"
);
out.write((String)interfaceTab.getValue(
"
PARAMTEXT
"
)
+
"
/n
"
);
while
(structTab.nextRow())
{
out.write(
"
Field/t/t/t
"
+
(String) structTab.getValue(
"
FIELDNAME
"
)
+
"
/n
"
);
}
}
out.write(
"
/n
"
);
}
catch
(Exception ex)
{ex.printStackTrace();}
}
}
The key to this utility is the use of two specific RFCs that allow the application to retrieve both function module and structure definitions. The first step is to pass the name of the RFC/BAPI to RFC_GET_FUNCTION_INTERFACE. This retrieves a list of all parameters, structures, and tables used for this interface. The next step is to call RFC_GET_STRUCTURE_DEFINITION for each table and structure in the RFC/BAPI. This will return a list of the fields and proper fieldnames that comprise a given structure or table. Once the structure definitions have been retrieved, the application then writes them out in the specified (in this case, plain text) format.
Part 3: Load testing SAP with JCo
25 Aug 2005
This utility provides a very simple load testing application. Load testing SAP using RFC connections gives you a good idea of how SAP would perform under a specific load by your application. The code itself is a multi-threaded utility that allows you to specify the number of threads (connections) that you want to test SAP with. After creation, each thread creates a connection to SAP then enters an infinite loop, with each loop executing a single RFC call to SAP.
import
com.sap.mw.JCo.
*
;
public
class
SAPLoadTest
extends
Thread
{
static
final
String[][] sapParams
=
{
{
"
client
"
,
"
000
"
}
,
{
"
user
"
,
"
myUsername
"
}
,
{
"
passwd
"
,
"
myPassword
"
}
,
{
"
lang
"
,
"
en
"
}
,
{
"
ashost
"
,
"
mySapHost
"
}
,
{
"
sysnr
"
,
"
00
"
}
}
;
static
final
int
maxConnections
=
100
;
//
Number of threads
static
int
count
=
0
;
static
int
maxThread
=
0
;
static
JCo.Function function;
public
SAPLoadTest()
{
super
(
""
+
++
count);
start();
}
public
void
run()
{
JCo.Client threadConn
=
null
;
try
{
threadConn
=
JCo.createClient(sapParams);
threadConn.connect();
}
catch
(Exception pEx)
{
pEx.printStackTrace();
}
for
(
int
i
=
0
;
true
; i
++
)
{
try
{
threadConn.execute(function);
if
(maxThread
<
new
Integer(getName()).intValue())
{
maxThread
=
new
Integer(getName()).intValue();
System.out.println(maxThread);
}
}
catch
(Exception ex)
{
ex.printStackTrace();
}
}
}
public
static
void
main(String[] args)
{
JCo.Client connection
=
JCo.createClient(sapParams);
connection.connect();
IRepository repository
=
new
JCo.Repository(
"
SAPRep
"
, connection);
IFunctionTemplate functionTempl
=
repository
.getFunctionTemplate(
"
RFC_SYSTEM_INFO
"
);
function
=
functionTempl.getFunction();
connection.disconnect();
for
(
int
i
=
0
; i
<
maxConnections; i
++
)
{
new
SAPLoadTest();
}
}
}
Bear in mind, any create/update RFC/BAPI will continue to modify the system until the application is manually terminated.
Part 4: Creating a serialized RFC interface
25 Aug 2005
Throughout the course of a development effort there are always times in which I want to test my JCo application but do not have live (online) access to an SAP system. I may be working from home, on a different customer site, or just experiencing a planned SAP outage, but not having SAP to run JCo makes the application pretty useless.
The quickest way to remedy this is to build the ability to use serialized RFC interfaces into your application. In Java, you can create serialized Java objects, whereby the full state of the object is written to the filesystem and can be re-read and re-used whenever necessary. In this case, I create and save a serialized RFC from a live SAP system, then reuse the serialized interface whenever I need to test the application.
The included application demonstrates how to create the serialized interface, then disconnect from SAP and use the serialized JCo function object as if it was STILL connected to an online SAP system.
import
java.io.
*
;
import
com.sap.mw.JCo.
*
;
public
class
SerializeRFC
{
static
final
String[][] sapParams
=
{
{
"
client
"
,
"
000
"
}
,
{
"
user
"
,
"
myUsername
"
}
,
{
"
passwd
"
,
"
myPassword
"
}
,
{
"
lang
"
,
"
EN
"
}
,
{
"
ashost
"
,
"
mySapHost
"
}
,
{
"
sysnr
"
,
"
00
"
}
}
;
static
final
String filePath
=
"
c:/dev/JCofunction.ser
"
;
static
final
String interfaceName
=
"
RFC_SYSTEM_INFO
"
;
public
static
void
getInterface()
{
File fileOut
=
new
File(filePath);
try
{
System.out.println(
"
Serializing online RFC function..
"
);
JCo.Client connection
=
JCo.createClient(sapParams);
IRepository repository
=
new
JCo.Repository(
"
saprep
"
, connection);
JCo.Function function
=
repository.getFunctionTemplate(interfaceName).getFunction();
connection.execute(function);
connection.disconnect();
ObjectOutputStream functionOut
=
new
ObjectOutputStream(
new
FileOutputStream(fileOut));
functionOut.writeObject(function);
functionOut.close();
}
catch
(Exception ex)
{
ex.printStackTrace();
}
}
private
static
JCo.Function retrieveInterface()
{
//
Retrieve serialized RFC interface
ObjectInputStream functionIn
=
null
;
JCo.Function function
=
null
;
try
{
File fileIn
=
new
File(filePath);
System.out.println(
"
Retrieving offline RFC function
"
);
functionIn
=
new
ObjectInputStream(
new
FileInputStream(fileIn));
function
=
(JCo.Function) functionIn.readObject();
}
catch
(Exception ex)
{
ex.printStackTrace();
}
return
function;
}
public
static
void
executeTestCase()
{
//
Fake test case
JCo.Function function
=
retrieveInterface();
System.out.println(
"
Executing test case for RFC System application
"
);
JCo.Structure expStruct
=
function.getExportParameterList().getStructure(
"
RFCSI_EXPORT
"
);
if
(expStruct.getValue(
"
RFCHOST
"
).equals(
"
mySapHost
"
))
System.out.println(
"
Test successful
"
);
else
System.out.println(
"
Test failed
"
);
}
public
static
void
main(String[] args)
{
SerializeRFC.getInterface();
SerializeRFC.executeTestCase();
}
}
Conclusion and more resources
25 Aug 2005
Ultimately, the message is: "Please plagiarize this code!" Hopefully, it will be a good reference for you any time you need to kick-start a JCo/SAP development effort. These simple utilities could form the basis of your development toolkit or can be used to supplement an existing toolkit. Over time, you will likely discover your own useful JCo tools and techniques and add these to your development toolkit. Feel free to e-mail me with any useful modifications or new JCo utilities. I am always looking for new recipes to add to my own JCo cookbook!