Using Java RFC packages 

In order to use Java BAPI Proxy Classes, the application programmer needs to be familar with some aspects of the Java RFC package: com.sap.rfc and com.sap.rfc.exception . The application programmer needs to provide IRfcConnection objects for logging on to R/3 so that the proxy objects can communicate with their corresponding instances.

The following function shows sample code that attempts to log on to an R/3 system. Note the use of the classes in the com.sap.rfc and com.sap.rfc.exception , such as MiddlewareInfo , FactoryManager , JRfcBaseRuntimeException , userInfo , connectInfo , JRfcRfcConnectionException , etc.

//

// connect

//

public static boolean connect()

{

//cleanup previous connection

cleanUp();

try

{

// create a middlewareInfo object with rfcHost name

MiddlewareInfo mdInfo

= new MiddlewareInfo();

mdInfo.setMiddlewareType

(MiddlewareInfo.middlewareTypeOrbix);

mdInfo.setOrbServerName(rfcHost);

// let the global factory manager use the middlewareInfo

// to create all object factories by binding to the server

facMan = FactoryManager.getSingleInstance();

if(facMan != null)

facMan.setMiddlewareInfo(mdInfo);

}

catch (JRfcBaseRuntimeException je)

{

System.out.println("Bind to object failed.\n"

+ "Unexpected JRFC runtime exception:\n" + je.toString ());

return false;

}

connection = logon();

if( connection == null) return false;

return true;

}

//

// logon

//

// This function attempts to log on to an R/3 system. It assumes

// that the following static attributes are initialized:

//

// String rfcHost = null,

// client = null,

// user = null,

// password = null,

// r3Host = null,

// r3SysNo = null,

// language = null;

//

private static IRfcConnection logon()

{

UserInfo userInfo = new UserInfo();

ConnectInfo connectInfo = new ConnectInfo();

IRfcConnection connection = null;

userInfo.setClient(client);

userInfo.setUserName(user);

userInfo.setPassword(password);

userInfo.setLanguage(language);

connectInfo.setRfcMode(ConnectInfo.RFC_MODE_VERSION_3);

connectInfo.setDestination("xxx");

connectInfo.setHostName(r3Host);

connectInfo.setSystemNo((short)Integer.parseInt(r3SysNo));

connectInfo.setLoadBalancing(false);

connectInfo.setCheckAuthorization(true);

try

{

connection = facMan.getRfcConnectionFactory() .createRfcConnection(connectInfo, userInfo);

connection.open();

}

catch (JRfcRfcConnectionException re)

{

System.out.println("Unexpected RfcError while opening

connection.\n" + re.toString());

return null;

}

catch (JRfcBaseRuntimeException je)

{

System.out.println("Unexpected JRFC runtime exception:\n"

+ " while opening connection.\n"

+ je.toString());

return null;

}

return connection;

}