package main.java.com.platform.wms.util;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;
public class SAPConn {
private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
static{
Properties connectProperties = new Properties();
//单链接
/* connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "127.0.0.1");//服务器
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "02"); //系统编号
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "666"); //SAP集团客户端号
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "RF_XXX"); //SAP用户名
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "*******"); //密码
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "zh"); //登录语言
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "20"); //最大连接数
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "40"); */ //最大连接线程
//组连接
connectProperties.setProperty(DestinationDataProvider.JCO_MSHOST, "127.0.0.1");//服务器
connectProperties.setProperty(DestinationDataProvider.JCO_MSSERV, "8888");//服务端口
connectProperties.setProperty(DestinationDataProvider.JCO_GROUP, "MC_XXX_ALL"); //组名称
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "888"); //SAP集团客户端号
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "RF_XXX_C101"); //SAP用户名
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "xxxxxx"); //密码
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "zh"); //登录语言
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "20"); //最大连接数
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "40"); //最大连接线程
connectProperties.setProperty(DestinationDataProvider.JCO_MAX_GET_TIME, "240"); //最大连接时间
createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
}
/**
* 创建SAP接口属性文件。
* @param name ABAP管道名称
* @param suffix 属性文件后缀
* @param properties 属性文件内容
*/
private static void createDataFile(String name, String suffix, Properties properties){
File cfg = new File(name+"."+suffix);
if(cfg.exists()){
cfg.deleteOnExit();
}
try{
FileOutputStream fos = new FileOutputStream(cfg, false);
properties.store(fos, "for tests only !");
fos.close();
}catch (Exception e){
log.error("Create Data file fault, error msg: " + e.toString());
throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
}
}
/**
* 获取SAP连接
* @return SAP连接对象
*/
public static JCoDestination connect(){
JCoDestination destination =null;
try {
destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
} catch (JCoException e) {
log.error("Connect SAP fault, error msg: " + e.toString());
}
return destination;
}
private static Logger log = Logger.getLogger(SAPConn.class); // 初始化日志对象
}