java調用SAP RFC函數

[java] view plaincopy
  1. /* 
  2.  * To change this template, choose Tools | Templates 
  3.  * and open the template in the editor. 
  4.  */  
  5. package jcoapp;  
  6.   
  7. import com.sap.mw.jco.*;  
  8. import com.sap.conn.jco.*;  
  9. import java.io.File;  
  10. import java.io.FileOutputStream;  
  11. import java.io.IOException;  
  12. import java.util.Properties;  
  13.   
  14. /** 
  15.  * 
  16.  * @author luolai 
  17.  */  
  18. import com.sap.conn.jco.ext.DestinationDataProvider;  
  19. import com.sap.mw.jco.JCO;  
  20. import com.sap.mw.jco.JCO.Table;  
  21. import java.util.Vector;  
  22.   
  23. public class JCOClientDemo {  
  24.   
  25.     static String ABAP_AS = "ABAP_AS_WITHOUT_POOL";  
  26.     static String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";  
  27.     static String DESTINATION_NAME1 = "ABAP_AS_WITHOUT_POOL";  
  28.     static String DESTINATION_NAME2 = "ABAP_AS_WITH_POOL";  
  29. //    private String JCO_ASHOST = "";  
  30. //    private String JCO_SYSNR = "";  
  31. //    private String JCO_CLIENT = "";  
  32. //    private String JCO_USER = "";  
  33. //    private String JCO_PASSWD = "";  
  34. //    private String JCO_LANG = "";  
  35.     LogInfo loginfo = new LogInfo();  
  36.   
  37.     public JCOClientDemo(String JCO_ASHOST, String JCO_SYSNR, String JCO_CLIENT, String JCO_USER, String JCO_PASSWD,  
  38.             String JCO_LANG) {  
  39.         Properties connectProperties = new Properties();  
  40.         connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, JCO_ASHOST);  
  41.         connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, JCO_SYSNR);  
  42. //      connectProperties.setProperty(DestinationDataProvider.JCO_R3NAME, "C01");  
  43.         connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, JCO_CLIENT);  
  44.         connectProperties.setProperty(DestinationDataProvider.JCO_USER, JCO_USER);  
  45.         connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, JCO_PASSWD);  
  46.         connectProperties.setProperty(DestinationDataProvider.JCO_LANG, JCO_LANG);  
  47. //        createDataFile(ABAP_AS, "jcoDestination", connectProperties);  
  48.   
  49.         connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");  
  50.         connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "5");  
  51.         createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);  
  52.     }  
  53.   
  54.     static void createDataFile(String name, String suffix, Properties properties) {  
  55.         File cfg = new File(name + "." + suffix);  
  56.         if (cfg.exists()) {  
  57.             cfg.delete();  
  58.         }  
  59.         if (!cfg.exists()) {  
  60.             FileOutputStream fos = null;  
  61.             try {  
  62.                 fos = new FileOutputStream(cfg, false);  
  63.                 properties.store(fos, "for tests only !");  
  64.                 fos.close();  
  65.             } catch (Exception e) {  
  66.                 throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);  
  67.             } finally {  
  68.                 try {  
  69.                     fos.close();  
  70.                 } catch (IOException e) {  
  71.                 }  
  72.             }  
  73.         }  
  74.     }  
  75.   
  76.     public static String callFuction(String strF, String echo) throws JCoException {  
  77.         JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);  
  78.         JCoFunction function = destination.getRepository().getFunction(strF);  
  79.         if (function == null) {  
  80.             throw new RuntimeException(strF + " not found in SAP.");  
  81.         }  
  82.         try {  
  83.             function.execute(destination);  
  84.         } catch (AbapException e) {  
  85.             System.out.println(e.toString());  
  86.             return "Error";  
  87.         }  
  88.   
  89.         //System.out.println(" Echo: " + function.getExportParameterList().toXML());  
  90.         //System.out.println(" Echo: " + function.getExportParameterList().getString(echo));  
  91.         return function.getExportParameterList().getString(echo);  
  92.     }  
  93.   
  94.     public static void call(String thread) throws JCoException {  
  95.         JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);  
  96.         JCoFunction function = destination.getRepository().getFunction("STFC_CONNECTION");  
  97.         if (function == null) {  
  98.             throw new RuntimeException("STFC_CONNECTION not found in SAP.");  
  99.         }  
  100.   
  101.         function.getImportParameterList().setValue("REQUTEXT""Hello SAP");  
  102.   
  103.         try {  
  104.             function.execute(destination);  
  105.         } catch (AbapException e) {  
  106.             System.out.println(e.toString());  
  107.             return;  
  108.         }  
  109.         System.out.println("thread:" + thread);  
  110.         System.out.println("STFC_CONNECTION finished:");  
  111.         System.out.println(" Echo: " + function.getExportParameterList().getString("ECHOTEXT"));  
  112.         System.out.println(" Response: " + function.getExportParameterList().getString("RESPTEXT"));  
  113.         System.out.println();  
  114.     }  
  115.   
  116.     public static void step1Connect() throws JCoException {  
  117.         JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);  
  118.         System.out.println("Attributes:");  
  119.         System.out.println(destination.getAttributes());  
  120.         System.out.println();  
  121.     }  
  122.   
  123.     public static void step2ConnectUsingPool() throws JCoException {  
  124.         JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);  
  125.         destination.ping();  
  126.         System.out.println("Attributes:");  
  127.         System.out.println(destination.getAttributes());  
  128.         System.out.println();  
  129.     }  
  130.   
  131.     public static void step3SimpleCall() throws JCoException {  
  132.         JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);  
  133.         JCoFunction function = destination.getRepository().getFunction("STFC_CONNECTION");  
  134.         if (function == null) {  
  135.             throw new RuntimeException("STFC_CONNECTION not found in SAP.");  
  136.         }  
  137.         function.getImportParameterList().setValue("REQUTEXT""Hello SAP");  
  138.         try {  
  139.             function.execute(destination);  
  140.         } catch (AbapException e) {  
  141.             System.out.println(e.toString());  
  142.             return;  
  143.         }  
  144.         System.out.println("STFC_CONNECTION finished:");  
  145.         System.out.println(" Echo: " + function.getExportParameterList().getString("ECHOTEXT"));  
  146.         System.out.println(" Response: " + function.getExportParameterList().getString("RESPTEXT"));  
  147.         System.out.println();  
  148.     }  
  149.   
  150.     public static void step3WorkWithStructure() throws JCoException {  
  151.         JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);  
  152.         JCoFunction function = destination.getRepository().getFunction("RFC_SYSTEM_INFO");  
  153.         if (function == null) {  
  154.             throw new RuntimeException("RFC_SYSTEM_INFO not found in SAP.");  
  155.         }  
  156.         try {  
  157.             function.execute(destination);  
  158.         } catch (AbapException e) {  
  159.             System.out.println(e.toString());  
  160.             return;  
  161.         }  
  162.         JCoStructure exportStructure = function.getExportParameterList().getStructure("RFCSI_EXPORT");  
  163.         System.out.println("System info for " + destination.getAttributes().getSystemID() + ":\n");  
  164.         for (int i = 0; i < exportStructure.getMetaData().getFieldCount(); i++) {  
  165.             System.out.println(exportStructure.getMetaData().getName(i) + ":\t" + exportStructure.getString(i));  
  166.         }  
  167.         System.out.println();  
  168. //JCo still supports the JCoFields, but direct access via getXX is more efficient as field iterator  
  169.         System.out.println("The same using field iterator: \nSystem info for " + destination.getAttributes().getSystemID() + ":\n");  
  170.         for (JCoField field : exportStructure) {  
  171.             System.out.println(field.getName() + ":\t" + field.getString());  
  172.         }  
  173.         System.out.println();  
  174.     }  
  175.   
  176.     public static void step4WorkWithTable() throws JCoException {  
  177.         JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME2);  
  178.         JCoFunction function = destination.getRepository().getFunction("BAPI_COMPANYCODE_GETLIST");  
  179.         if (function == null) {  
  180.             throw new RuntimeException("BAPI_COMPANYCODE_GETLIST not found in SAP.");  
  181.         }  
  182.         try {  
  183.             function.execute(destination);  
  184.         } catch (AbapException e) {  
  185.             System.out.println(e.toString());  
  186.             return;  
  187.         }  
  188.         JCoStructure returnStructure = function.getExportParameterList().getStructure("RETURN");  
  189.         if (!(returnStructure.getString("TYPE").equals("") || returnStructure.getString("TYPE").equals("S"))) {  
  190.             throw new RuntimeException(returnStructure.getString("MESSAGE"));  
  191.         }  
  192.         System.out.println("COMP_CODE" + '\t' + "COMP_NAME");  
  193.         JCoTable codes = function.getTableParameterList().getTable("COMPANYCODE_LIST");  
  194.         for (int i = 0; i < codes.getNumRows(); i++) {  
  195.             codes.setRow(i);  
  196.             System.out.println(codes.getString("COMP_CODE") + '\t' + codes.getString("COMP_NAME"));  
  197.         }  
  198.   
  199.         codes.firstRow();  
  200.         System.out.println("COMP_CODE" + '\t' + "COUNTRY" + '\t' + "CITY");  
  201.         for (int i = 0; i < codes.getNumRows(); i++, codes.nextRow()) {  
  202.             function = destination.getRepository().getFunction("BAPI_COMPANYCODE_GETDETAIL");  
  203.             if (function == null) {  
  204.                 throw new RuntimeException("BAPI_COMPANYCODE_GETDETAIL not found in SAP.");  
  205.             }  
  206.             function.getImportParameterList().setValue("COMPANYCODEID", codes.getString("COMP_CODE"));  
  207.             function.getExportParameterList().setActive("COMPANYCODE_ADDRESS"false);  
  208.             try {  
  209.                 function.execute(destination);  
  210.             } catch (AbapException e) {  
  211.                 System.out.println(e.toString());  
  212.                 return;  
  213.             }  
  214.             returnStructure = function.getExportParameterList().getStructure("RETURN");  
  215.             if (!(returnStructure.getString("TYPE").equals("")  
  216.                     || returnStructure.getString("TYPE").equals("S")  
  217.                     || returnStructure.getString("TYPE").equals("W"))) {  
  218.                 throw new RuntimeException(returnStructure.getString("MESSAGE"));  
  219.             }  
  220.             JCoStructure detail = function.getExportParameterList().getStructure("COMPANYCODE_DETAIL");  
  221.             System.out.println(detail.getString("COMP_CODE") + '\t'  
  222.                     + detail.getString("COUNTRY") + '\t'  
  223.                     + detail.getString("CITY"));  
  224.         }//for  
  225.     }  
  226.   
  227.     public static void main(String[] args) {  
  228.         // Print the version of the underlying JCO library  
  229.         System.out.println("\nVersion of the JCO-library:\n" + "---------------------------\n" + JCO.getMiddlewareVersion());  
  230.         try {  
  231.             System.out.println(JCOClientDemo.callFuction("ZWMF_GET_FUNCTION""ITAB"));  
  232.             //JCOClientDemo.call("");  
  233.             //JCOClientDemo.step4WorkWithTable();  
  234.         } catch (JCoException e) {  
  235.             e.printStackTrace();  
  236.         }  
  237.     }  
  238. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章