Connecting to the SAP System

Team-Fly

The connection to an SAP system is handled through the IRfcConnection and IRfcConnectionFactory interfaces. (See Chapter 8, 'Objects, Classes, and Interfaces,' for a general discussion of interfaces in the Java language.) Basically, an interface is a collection of methods (beyond those that the class inherits from the class hierarchy) that can be included in a class. The classes that are affected by these interfaces are described in Table 11.2. You need to instantiate and populate the classes UserInfo and ConnectInfo before you can establish the connection to the SAP system.

Table 11.2 Classes Used with the IRfcConnection Interface

Class

Description

UserInfo

Contains the SAP user information used in collection, such as login ID, language, and password

ConnectInfo

Contains information about a connection to SAP R/3, such as system name and number

SystemInfo

Contains the information about the R/3 system that is contained in the SYST SAP information structure

ConnectionEvent

Supports the ConnectionListener interface

The following portion of code shows how to set up the UserInfo and ConnectInfo objects, and then connect to a SAP system.

// Instantiate classes UserInfo userInfo = new UserInfo(); ConnectInfo connectInfo = new ConnectInfo(); // Define variables within the newly created objects userInfo.setClient("000"); userInfo.setUserName("TESTUSER"); userInfo.setPassword("abc123"); userInfo.setLanguage("E"); 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); // Call up the factory manager to create a new object and assign // a reference to this object to the variable "connection". This object // is assumed to have the methods that are defined in the  // IRfcConnection interface, such as open(); IRfcConnection connection = facMan.getRfcConnectionFactory()                        .createRfcConnection(connectInfo, userInfo); connection.open();

In Chapter 5 we developed a simple Java program to ping an SAP system. That example does not have a user interface, and the program is executed from a DOS shell window. To keep the program as simple as possible, it was developed as a single class. We are now going to develop a more complex program-with windowing features and multiple classes-that will log in to an SAP system and return some of the system variables contained in the system info structure SYST.

The example contains three classes: ShowSyst, SystFrame, and Sap. The ShowSyst class is the main class; it calls the other classes. The ShowSyst class handles all screen I/O, and the Sap class handles all calls to the SAP system. Java allows only one class per file; therefore, we must link these three files to form the executable program. This example is set up as previous examples have been, as a console application from the Microsoft Java Compiler. After the project is set up, we add the three classes, as shown in Figure 11.1.

click to expand

Figure 11.1: Compiler screen for GetSyst

GetSyst.Java

 // The main class for the GetSyst program // import the necessary Java Packages import java.awt.*; import java.awt.event.*; public class GetSyst {     public static void main (String[] args)     {         // Instantiate the SystFrame Class         Frame ourFrame = new SystFrame();         ourFrame.setSize(340,300);         // Start up the show method of SystFrame         ourFrame.show();     } }

Sap.Java

// This class handles all communication to the // SAP system import com.sap.rfc.*; import com.sap.rfc.exception.*; public class Sap {     public String     rfcHost    = null,                    r3Host    = null,                     r3SysNo    = null,                    r3FieldFld     = null,                    r3InfoFld    = null,                    client    = null,                   user          = null,                    password    = null,                    language    = null,                    errmess    = null;     public String custNo=null, custName=null;     protected    FactoryManager facMan = null;     protected    IRfcConnection connection = null;     protected    MiddlewareInfo mdInfo = null;     public boolean connect()     {         errmess = new String("Connected to SAP System");         //cleanup previous connection         cleanUp();         try         {             if( null == mdInfo )             {                 // create a middlewareInfo object with rfcHost name                 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();             facMan.setMiddlewareInfo(mdInfo);         }         catch (JRfcBaseRuntimeException je)         {             errmess = new String(je.toString());             return false;         }         connection = logon();         if( connection == null ) return false;                  return true;     }     protected 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)         {             errmess = new String(re.toString());             System.out.println("Unexpected RfcError while opening connection.\n"                                 + re.toString());             return null;         }         catch (JRfcBaseRuntimeException je)        {             errmess = new String(je.toString());             System.out.println("Unexpected RfcError while opening connection.\n"                                 + je.toString());             return null;         }         return connection;     }          public boolean getSystInfo()     {         SystemInfo si;         ISimple    sparam;         try { si = connection.getSystemInfo();}         catch (JRfcRfcConnectionException je)         {             errmess = new String("GetSystemInfo Failed");             return false;         }         sparam = si.getItem(r3FieldFld);         r3InfoFld = sparam.getstring();         errmess = new String(sparam.getFieldName() + ":Call Done");         return true;     }     public void cleanUp()     {         try         {             if( null != connection )                 connection.close();             connection = null;         }         catch (JRfcBaseRuntimeException je)         {             errmess = new String(je.toString());             return;         }    } }

SystFrame.Java

// This class displays a window and handles all  // I/O from that window (like logging into SAP) import java.awt.*; import java.awt.event.*; import com.sap.rfc.*; import com.sap.rfc.exception.*; // Frame is part of the AWT and is a subclass of the Window class. It provides a  // window with a title bar, close boxes, and other platform-specific window // features. Our class "SystFrame" will be an extention of this class and thus // will inherit all of its features, along with the methods that we will code   // (again like logging into an SAP system). public class SystFrame extends Frame implements ActionListener {     private Sap ourSap = new Sap();   // New SAP object          private TextField rfcHostFld,     // Java Server Name                       r3HostFld,     // SAP System Name                       r3SysNoFld,    // SAP System Number                       clientFld,     // Client                       userFld,       // Login Name                       passwordFld,   // Password                       languageFld,   // Login Language                       r3FieldFld,    // Input Field for inquiry                       r3InfoFld;     // Output Field for inquiry     private Label      messageStr;    // Error Messages     private TextArea resultArea;     private final String connectStr = "Login to SAP System";     private Label rfcHostLbl  = new Label("Java Server Name:");     private Label r3HostLbl   = new Label("SAP Server host:");     private Label r3SysNoLbl  = new Label("System number:");     private Label clientLbl   = new Label("Client:");     private Label userLbl     = new Label("User:");     private Label passwordLbl = new Label("Password:");     private Label languageLbl = new Label("Language:");     private Label r3FieldLbl = new Label("Field Name:");     private Label r3InfoLbl   = new Label("Result:");     private Button connectBut;     private Button getInfoBut;     public SystFrame()     {         // Set the Title for the Frame         setTitle("GetSyst - Get SAP System variables");         // For this example we will use the GridLayout layout manager         // in the constructor we indicate the number of columns and rows         // that we will have         GridLayout gLayout = new GridLayout(12,5,2,5);         // Define the input fields         rfcHostFld        = new TextField("", 30);         r3HostFld        = new TextField("", 30);         r3SysNoFld        = new TextField("", 2);         clientFld        = new TextField("", 3);         userFld            = new TextField("", 12);         languageFld        = new TextField("E", 2);         r3FieldFld        = new TextField("",10);         r3InfoFld        = new TextField("",30);         messageStr        = new Label("");         passwordFld        = new TextField("", 8);         passwordFld.setEchoChar(‘*');         // Define the Buttons        connectBut = new Button("Connect");         getInfoBut = new Button("GetInfo");         // Define the initial status of input boxes and buttons         r3FieldFld.setEnabled(false);         r3InfoFld.setEnabled(false);         getInfoBut.setEnabled(false);         // add components to the frame, these are added in order,          // from left to right, top to bottom.         setLayout(gLayout);         add(rfcHostLbl);         add(rfcHostFld);         add(r3HostLbl);         add(r3HostFld);         add(r3SysNoLbl);         add(r3SysNoFld);         add(clientLbl);         add(clientFld);         add(userLbl);         add(userFld);         add(passwordLbl);         add(passwordFld);         add(languageLbl);         add(languageFld);         add(connectBut);         add(new Label(" "));         add(r3FieldLbl);         add(r3FieldFld);         add(getInfoBut);         add(new Label(" "));         add(r3InfoLbl);         add(r3InfoFld);         add(messageStr);         // subscribe to the buttons actionlistener events for the buttons         connectBut.addActionListener(this);         getInfoBut.addActionListener(this);         // subscribe to the window events, only handle close for          // right now.         addWindowListener         (             new WindowAdapter()             {                 public void windowClosing(WindowEvent e)                 {                     ourSap.cleanUp();                     System.exit(0);                 }             }         );     }     // This piece of code handles the actions via the actionlistener     public void actionPerformed(ActionEvent evt)     {         // Handle the Connection Button         if(evt.getActionCommand().equals("Connect"))         {             ourSap.rfcHost = rfcHostFld.getText();             ourSap.r3Host = r3HostFld.getText();             ourSap.r3SysNo = r3SysNoFld.getText();             ourSap.client = clientFld.getText();             ourSap.user = userFld.getText();             ourSap.password = passwordFld.getText();             ourSap.language = languageFld.getText();             if (ourSap.connect() == true)             {                 dispMessage(ourSap.errmess);                 connectBut.setEnabled(false);                 getInfoBut.setEnabled(true);                 r3FieldFld.setEnabled(true);             }             else             {                dispMessage(ourSap.errmess);             }         }         // Handle the GetInfo Button         if(evt.getActionCommand().equals("GetInfo"));         {             ourSap.r3FieldFld = r3FieldFld.getText();             ourSap.getSystInfo();             r3InfoFld.setText(ourSap.r3InfoFld);             dispMessage(ourSap.errmess);         }     }     // Display a message on the bottom of the screen     void dispMessage(String instr)     {         messageStr.setForeground(Color.red);         messageStr.setText(instr);     } }

This program enables you to query for fields in the RFCSI structure, along with CURRENT_RESOURCES and MAXIMAL_RESOURCES (the function module call RFC_SYSTEM_INFO handles these queries). An example of the Java screen appears in Figure 11.2.

Figure 11.2: A GetSyst example screen


Team-Fly


Java & BAPI Technology for SAP
Java & BAPI Technology for SAP
ISBN: 761523057
EAN: N/A
Year: 1998
Pages: 199

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net