- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package jcoapp;
- import com.sap.mw.jco.*;
- import com.sap.conn.jco.*;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Properties;
- /**
- *
- * @author luolai
- */
- import com.sap.conn.jco.ext.DestinationDataProvider;
- import com.sap.mw.jco.JCO;
- import com.sap.mw.jco.JCO.Table;
- import java.util.Vector;
- public class JCOClientDemo {
- static String ABAP_AS = "ABAP_AS_WITHOUT_POOL";
- static String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
- static String DESTINATION_NAME1 = "ABAP_AS_WITHOUT_POOL";
- static String DESTINATION_NAME2 = "ABAP_AS_WITH_POOL";
- // private String JCO_ASHOST = "";
- // private String JCO_SYSNR = "";
- // private String JCO_CLIENT = "";
- // private String JCO_USER = "";
- // private String JCO_PASSWD = "";
- // private String JCO_LANG = "";
- LogInfo loginfo = new LogInfo();
- public JCOClientDemo(String JCO_ASHOST, String JCO_SYSNR, String JCO_CLIENT, String JCO_USER, String JCO_PASSWD,
- String JCO_LANG) {
- Properties connectProperties = new Properties();
- connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, JCO_ASHOST);
- connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, JCO_SYSNR);
- // connectProperties.setProperty(DestinationDataProvider.JCO_R3NAME, "C01");
- connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, JCO_CLIENT);
- connectProperties.setProperty(DestinationDataProvider.JCO_USER, JCO_USER);
- connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, JCO_PASSWD);
- connectProperties.setProperty(DestinationDataProvider.JCO_LANG, JCO_LANG);
- // createDataFile(ABAP_AS, "jcoDestination", connectProperties);
- connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");
- connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "5");
- createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
- }
- static void createDataFile(String name, String suffix, Properties properties) {
- File cfg = new File(name + "." + suffix);
- if (cfg.exists()) {
- cfg.delete();
- }
- if (!cfg.exists()) {
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(cfg, false);
- properties.store(fos, "for tests only !");
- fos.close();
- } catch (Exception e) {
- throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
- } finally {
- try {
- fos.close();
- } catch (IOException e) {
- }
- }
- }
- }
- public static String callFuction(String strF, String echo) throws JCoException {
- JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
- JCoFunction function = destination.getRepository().getFunction(strF);
- if (function == null) {
- throw new RuntimeException(strF + " not found in SAP.");
- }
- try {
- function.execute(destination);
- } catch (AbapException e) {
- System.out.println(e.toString());
- return "Error";
- }
- //System.out.println(" Echo: " + function.getExportParameterList().toXML());
- //System.out.println(" Echo: " + function.getExportParameterList().getString(echo));
- return function.getExportParameterList().getString(echo);
- }
- public static void call(String thread) throws JCoException {
- JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
- JCoFunction function = destination.getRepository().getFunction("STFC_CONNECTION");
- if (function == null) {
- throw new RuntimeException("STFC_CONNECTION not found in SAP.");
- }
- function.getImportParameterList().setValue("REQUTEXT", "Hello SAP");
- try {
- function.execute(destination);
- } catch (AbapException e) {
- System.out.println(e.toString());
- return;
- }
- System.out.println("thread:" + thread);
- System.out.println("STFC_CONNECTION finished:");
- System.out.println(" Echo: " + function.getExportParameterList().getString("ECHOTEXT"));
- System.out.println(" Response: " + function.getExportParameterList().getString("RESPTEXT"));
- System.out.println();
- }
- public static void step1Connect() throws JCoException {
- JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
- System.out.println("Attributes:");
- System.out.println(destination.getAttributes());
- System.out.println();
- }
- public static void step2ConnectUsingPool() throws JCoException {
- JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
- destination.ping();
- System.out.println("Attributes:");
- System.out.println(destination.getAttributes());
- System.out.println();
- }
- public static void step3SimpleCall() throws JCoException {
- JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
- JCoFunction function = destination.getRepository().getFunction("STFC_CONNECTION");
- if (function == null) {
- throw new RuntimeException("STFC_CONNECTION not found in SAP.");
- }
- function.getImportParameterList().setValue("REQUTEXT", "Hello SAP");
- try {
- function.execute(destination);
- } catch (AbapException e) {
- System.out.println(e.toString());
- return;
- }
- System.out.println("STFC_CONNECTION finished:");
- System.out.println(" Echo: " + function.getExportParameterList().getString("ECHOTEXT"));
- System.out.println(" Response: " + function.getExportParameterList().getString("RESPTEXT"));
- System.out.println();
- }
- public static void step3WorkWithStructure() throws JCoException {
- JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
- JCoFunction function = destination.getRepository().getFunction("RFC_SYSTEM_INFO");
- if (function == null) {
- throw new RuntimeException("RFC_SYSTEM_INFO not found in SAP.");
- }
- try {
- function.execute(destination);
- } catch (AbapException e) {
- System.out.println(e.toString());
- return;
- }
- JCoStructure exportStructure = function.getExportParameterList().getStructure("RFCSI_EXPORT");
- System.out.println("System info for " + destination.getAttributes().getSystemID() + ":\n");
- for (int i = 0; i < exportStructure.getMetaData().getFieldCount(); i++) {
- System.out.println(exportStructure.getMetaData().getName(i) + ":\t" + exportStructure.getString(i));
- }
- System.out.println();
- //JCo still supports the JCoFields, but direct access via getXX is more efficient as field iterator
- System.out.println("The same using field iterator: \nSystem info for " + destination.getAttributes().getSystemID() + ":\n");
- for (JCoField field : exportStructure) {
- System.out.println(field.getName() + ":\t" + field.getString());
- }
- System.out.println();
- }
- public static void step4WorkWithTable() throws JCoException {
- JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME2);
- JCoFunction function = destination.getRepository().getFunction("BAPI_COMPANYCODE_GETLIST");
- if (function == null) {
- throw new RuntimeException("BAPI_COMPANYCODE_GETLIST not found in SAP.");
- }
- try {
- function.execute(destination);
- } catch (AbapException e) {
- System.out.println(e.toString());
- return;
- }
- JCoStructure returnStructure = function.getExportParameterList().getStructure("RETURN");
- if (!(returnStructure.getString("TYPE").equals("") || returnStructure.getString("TYPE").equals("S"))) {
- throw new RuntimeException(returnStructure.getString("MESSAGE"));
- }
- System.out.println("COMP_CODE" + '\t' + "COMP_NAME");
- JCoTable codes = function.getTableParameterList().getTable("COMPANYCODE_LIST");
- for (int i = 0; i < codes.getNumRows(); i++) {
- codes.setRow(i);
- System.out.println(codes.getString("COMP_CODE") + '\t' + codes.getString("COMP_NAME"));
- }
- codes.firstRow();
- System.out.println("COMP_CODE" + '\t' + "COUNTRY" + '\t' + "CITY");
- for (int i = 0; i < codes.getNumRows(); i++, codes.nextRow()) {
- function = destination.getRepository().getFunction("BAPI_COMPANYCODE_GETDETAIL");
- if (function == null) {
- throw new RuntimeException("BAPI_COMPANYCODE_GETDETAIL not found in SAP.");
- }
- function.getImportParameterList().setValue("COMPANYCODEID", codes.getString("COMP_CODE"));
- function.getExportParameterList().setActive("COMPANYCODE_ADDRESS", false);
- try {
- function.execute(destination);
- } catch (AbapException e) {
- System.out.println(e.toString());
- return;
- }
- returnStructure = function.getExportParameterList().getStructure("RETURN");
- if (!(returnStructure.getString("TYPE").equals("")
- || returnStructure.getString("TYPE").equals("S")
- || returnStructure.getString("TYPE").equals("W"))) {
- throw new RuntimeException(returnStructure.getString("MESSAGE"));
- }
- JCoStructure detail = function.getExportParameterList().getStructure("COMPANYCODE_DETAIL");
- System.out.println(detail.getString("COMP_CODE") + '\t'
- + detail.getString("COUNTRY") + '\t'
- + detail.getString("CITY"));
- }//for
- }
- public static void main(String[] args) {
- // Print the version of the underlying JCO library
- System.out.println("\nVersion of the JCO-library:\n" + "---------------------------\n" + JCO.getMiddlewareVersion());
- try {
- System.out.println(JCOClientDemo.callFuction("ZWMF_GET_FUNCTION", "ITAB"));
- //JCOClientDemo.call("");
- //JCOClientDemo.step4WorkWithTable();
- } catch (JCoException e) {
- e.printStackTrace();
- }
- }
- }
java调用SAP RFC函数
最新推荐文章于 2024-09-22 16:06:13 发布