sap-基于jco2.x的简单封装
原文:http://blog.csdn.net/fangkailove/archive/2010/08/18/5820365.aspx
公司引入一套基于 java 的 bpm系统,同时需要和 sap 系统集成,为了方便调用 bapi ,将 jco 做了简单的封装:
- package com.hs.commUtil;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.util.Properties;
- import com.sap.mw.jco.IFunctionTemplate;
- import com.sap.mw.jco.IMetaData;
- import com.sap.mw.jco.JCO;
- import com.sap.mw.jco.JCO.Client;
- import com.sap.mw.jco.JCO.Function;
- import com.sap.mw.jco.JCO.Repository;
- import com.sap.mw.jco.JCO.Structure;
- import com.sap.mw.jco.JCO.Table;
- public class SapRfc
- {
- /**
- * 获取一个SAP的连接,并处于连接状态
- * @return
- */
- public static Client createJcoConn()
- {
- Properties prop = new Properties();
- try{
- String currpath = new File("").getAbsolutePath()+"\\plugs\\SAP_HOST.jcoDestination";
- InputStream in = new FileInputStream(new File(currpath));
- if(in != null){
- prop.load(in);
- String ashost = prop.getProperty("jco.client.ashost");
- String clientnr = prop.getProperty("jco.client.client");
- String sysnr = prop.getProperty("jco.client.sysnr");
- String user = prop.getProperty("jco.client.user");
- String pwd = prop.getProperty("jco.client.passwd");
- String lang = prop.getProperty("jco.client.lang");
- //String poolCpt = prop.getProperty("jco.destination.pool_capacity");
- //String pealLmt = prop.getProperty("jco.destination.peak_limit");
- Client myConnection = JCO.createClient(clientnr,
- user,
- pwd,
- lang,
- ashost,
- sysnr);
- myConnection.connect();
- return myConnection;
- }
- }catch(Exception e){
- e.printStackTrace(System.err);
- }
- return null;
- }
- /***
- * 关闭指定的 SAP 连接
- * @param myConnection SAP 连接
- */
- public static void closeJcoConn(Client myConnection)
- {
- if(myConnection != null && myConnection.isAlive())
- {
- myConnection.disconnect();
- }
- }
- /***
- * 获取SAP的对像仓库
- * @param myConnection SAP连接
- * @return
- */
- public static Repository getRepository(Client myConnection)
- {
- Repository repository = new Repository("", myConnection);
- return repository;
- }
- /***
- * 获取SAP的RFC函数(BAPI)
- * @param funName
- * @param myConnection
- * @return
- */
- public static Function getFunction(String funName,Client myConnection)
- {
- Repository myRepository = getRepository(myConnection);
- return getFunction(funName, myRepository);
- }
- /**
- * 获取SAP的RFC函数(BAPI)
- * @param funName
- * @param myRepository
- * @return
- */
- public static Function getFunction(String funName,Repository myRepository)
- {
- IFunctionTemplate template = myRepository.getFunctionTemplate(funName.toUpperCase());
- if (template != null)
- {
- Function fun = template.getFunction();
- return fun;
- }
- return null;
- }
- /**
- * 获取SAP的结构
- * @param structureName
- * @param myRepository
- * @return
- */
- public static Structure getGeneralStructure(String structureName,Repository myRepository)
- {
- IMetaData structureDefinition = myRepository.getStructureDefinition(structureName.toUpperCase());
- if (structureDefinition!=null)
- {
- Structure myStructure = JCO.createStructure(structureDefinition);
- return myStructure;
- }
- return null;
- }
- /***
- * 获取SAP的结构
- * @param structureName
- * @param myConnection
- * @return
- */
- public static Structure getGeneralStructure(String structureName,Client myConnection)
- {
- Repository myRepository = getRepository(myConnection);
- return getGeneralStructure(structureName,myRepository);
- }
- /***
- * 获取SAP的表
- * @param tableName
- * @param myRepository
- * @return
- */
- public static Table getGeneralTable(String tableName,Repository myRepository)
- {
- IMetaData tableDefinition = myRepository.getTableDefinition(tableName.toUpperCase());
- if (tableDefinition!=null)
- {
- Table myTable = JCO.createTable(tableDefinition);
- return myTable;
- }
- return null;
- }
- /***
- * 获取SAP的表
- * @param tableName
- * @param myConnection
- * @return
- */
- public static Table getGeneralTable(String tableName,Client myConnection)
- {
- Repository myRepository = getRepository(myConnection);
- return getGeneralTable(tableName,myRepository);
- }
- /***
- * 提交在这个连接上的操作
- * @param myConnection
- */
- public static void commit(Client myConnection)
- {
- Function fun = getFunction("BAPI_TRANSACTION_COMMIT", myConnection);
- if (fun!=null)
- {
- myConnection.execute(fun);
- }
- }
- /***
- * 回滚在这个连接上的操作
- * @param myConnection
- */
- public static void rollback(Client myConnection)
- {
- Function fun = getFunction("BAPI_TRANSACTION_ROLLBACK", myConnection);
- if (fun!=null)
- {
- myConnection.execute(fun);
- }
- }
- }