SAP接口编程 之 JCo3.0系列(01):JCoDestination

字数2101 阅读103 评论0 喜欢0

JCo3.0是Java语言与ABAP语言双向通讯的中间件。与之前1.0/2.0相比,是重新设计的产品。API和架构设计与NCo3.0比较类似,前面也说过,NCo3.0的设计参考了JCo3.0。从本篇开始,系统介绍JCo3.0编程的技术要点。

JCo3.0安装

https://service.sap.com/connectors 可以下载JCo3.0,注意下载的时候根据操作系统JVM版本(32位还是64)选择不同的版本。安装就是解压,将文件解压到目标文件夹。以Windows系统为例,主要的文件包括:

sapjco3.dll
sapjco3.jar

SAP强烈推荐将这两个文件放在同一文件夹下。测试安装是否成功,可以在命令窗口下,进入安装文件夹,运行下面的命令:

java -jar sapjco3.jar

如果安装成功,应该显示如下界面:

jco3安装成功的显示界面

JCoDestination

JCoDestination代表后台SAP系统,程序员不用关心与SAP的连接,jco3.0运行时环境负责管理连接和释放连接。我们先以一个简单的例子看看jco3.0 JCoDestination类的一些要点。

我使用的编程环境是Eclipse,环境准备如下:

  • 新建一个Java项目,项目名为JCo3Demo。
  • 将sapjco3.jar加入到项目的build path中。注意前面所说的sapjco3.jar和sapjco3.dll要放在同一个文件夹下。
  • 在Eclipse Java项目文件夹下,新建一个文本文件,文件名命名为ECC.jocdestination, 文件的内容如下(SAP系统的连接参数的设置):

    #SAP Logon parameters!
    #Tue Dec 08 16:41:30 CST 2015
    jco.client.lang=EN
    jco.client.client=001
    jco.client.passwd=xxxxxx
    jco.client.user=STONE
    jco.client.sysnr=00
    jco.client.ashost=192.168.65.100

对照SAP GUI,不难理解:

SAP GUI

环境准备好了,先来一段最简单的代码:

package jco3.demo1;

import java.util.Properties;
import org.junit.Test;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException; public class JCoDestinationDemo
{
public JCoDestination getDestination() throws JCoException
{
/**
* Get instance of JCoDestination from file: ECC.jcodestination
* which should be located in the installation folder of project
*/ JCoDestination dest = JCoDestinationManager.getDestination("ECC");
return dest;
} @Test
public void pingDestination() throws JCoException
{
JCoDestination dest = this.getDestination();
dest.ping();
}
}

代码说明:

  • getDestination()方法中,JCoDestinationManager.getDestination("ECC")从ECC.jcodestination文件中获取连接参数,创建JCoDestination对象的实例。
    这里有一个重要的约定,JCoDestinationManager.getDestination("ECC")方法,会从Eclipse Java项目的根目录,查找ECC.jcodestination文件(文件路径和扩展名不能改变)是否存在,如果存在,从文件的内容中获取连接参数。这是DestinationDataProvider接口的一个默认实现,在开发和测试的时候还是很方便的,但如果在真实项目中使用,安全性和灵活性就不够。后面会介绍解决方法。

  • pingDestination()方法调用JcoDestination对象的ping()方法测试SAP系统的连接。

  • @Test: 使用junit进行测试

配置文件的生成

刚才我们手工编辑了ECC.jcodestination文件,对于这个配置文件,因为很多连接参数来自于DestinationDataProvider接口,如果想通过代码来创建配置文件,可以使用如下代码:

package jco3.demo2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import org.junit.Test;
import com.sap.conn.jco.ext.DestinationDataProvider; public class DestinationFile
{
private Properties setProperties()
{
// logon parameters and other properties
Properties connProps = new Properties();
connProps.setProperty(DestinationDataProvider.JCO_ASHOST, "192.168.65.100");
connProps.setProperty(DestinationDataProvider.JCO_SYSNR, "00");
connProps.setProperty(DestinationDataProvider.JCO_USER, "STONE");
connProps.setProperty(DestinationDataProvider.JCO_PASSWD, "xxxxxx");
connProps.setProperty(DestinationDataProvider.JCO_CLIENT, "001");
connProps.setProperty(DestinationDataProvider.JCO_LANG, "EN"); return connProps;
} private void doCreateFile(String fName, String suffix, Properties props) throws IOException
{
/**
* Write contents of properties into a text file
* which was named [fName+suffix.jcodestination]
*/ File cfg = new File(fName+"."+suffix);
if (!cfg.exists()){ // file not exists
// Create file output stream, not using append mode
FileOutputStream fOutputStream = new FileOutputStream(cfg, false); // store the properties in file output stream
// and also add comments
props.store(fOutputStream, "SAP logon parameters:"); fOutputStream.close();
}else{
throw new RuntimeException("File alreay exists.");
}
} @Test
public void createConfigFile() throws IOException
{
Properties props = this.setProperties();
String fileName = "SAP_AS"; // sap application server // jcodestination suffix is required by JCoDestinationManager
this.doCreateFile(fileName, "jcodestination", props);
}
}

代码说明:

  • setProperties()方法属性参照DestinationDataProvider类的常量设置Properties的实例。
  • doCreateFile()方法根据需求的文件名,扩展名在Eclipse项目的根文件夹下,创建一个文本文件,文件的内容就是Properties实例的内容。
  • createConfigFile()方法,调用上面的两个方法,创建配置文件。

更改配置文件名的路径和扩展名

我们看到,默认情况下,SAP对配置文件的路径和扩展名都不能改变,如果我们想把文件放在任意位置,扩展名也使用其他的扩展名,有没有办法?答案是有,方法是实现DestinationDataProvider接口,并改写(override)getDestinationProperties()方法,然后通过Environment.registerDestinationDataProvider()方法进行注册。

OK, 一起来看看代码,代码分为三个部分:

  • 第一部分: 创建FileDestinationDataProviderImp类,实现DestinationDataProvider接口
  • 第二部分: 创建FileDestinationDataProvider类,注册FileDestinationDataProviderImp的实例,并提供getDestination()方法供调用
  • 第三部分:调用FileDestinationDataProvider类的getDestination()方法

第一部分:DestinationDataProvider接口的实现:

package jco3.demo2;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import com.sap.conn.jco.ext.DestinationDataEventListener;
import com.sap.conn.jco.ext.DestinationDataProvider; public class FileDestinationDataProviderImp implements DestinationDataProvider
{
private File dir;
private String destName; // destination name
private String suffix; public void setDestinationFile(File dir, String destName, String suffix)
{
this.dir = dir;
this.destName = destName;
this.suffix = suffix;
} private Properties loadProperties(File dir, String destName, String suffix) throws IOException
{
Properties props = null; // create a file with name: fullName in destDirectory
File destFile = new File(dir, destName+"."+suffix); if (destFile.exists()){
FileInputStream fInputStream = new FileInputStream(destFile);
props = new Properties();
props.load(fInputStream);
fInputStream.close();
}else{
throw new RuntimeException("Destination file does not exist.");
} return props;
} @Override
public Properties getDestinationProperties(String destName)
{
Properties props = null; try {
props = this.loadProperties(this.dir, this.destName, this.suffix);
} catch (IOException e) {
e.printStackTrace();
} return props;
} @Override
public void setDestinationDataEventListener(DestinationDataEventListener listener)
{
throw new UnsupportedOperationException();
} @Override
public boolean supportsEvents()
{
return false;
}
}

第二部分: 创建FileDestinationDataProvider类,注册FileDestinationDataProviderImp的实例,并且提供getDestination()方法。

package jco3.demo2;

import java.io.File;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.Environment; public class FileDestinationDataProvider
{
public static JCoDestination getDestination() throws JCoException
{
File directory = new File("."); // current directory;
String fileName = "SAP_AS";
String suffix = "txt"; FileDestinationDataProviderImp destDataProvider = new FileDestinationDataProviderImp();
destDataProvider.setDestinationFile(directory, fileName, suffix);
Environment.registerDestinationDataProvider(destDataProvider); JCoDestination dest = JCoDestinationManager.getDestination(fileName); return dest;
}
}

我们看到,getDestination方法中,文件的路径,文件的扩展名,都是我们自己定义的。文件名作为JCoDestinationManager.getDestination方法的destination name。从这里也可可以看到,JCoDestinationManager.getDestination方法从哪里查找连接参数,是依赖于Environment注册的DestinationDataProvider实现

第三部分:测试代码FileDestinationDataProvidergetDestination方法:

package jco3.demo2;

import org.junit.Test;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoException; public class TestFileDestinationProvider
{
@Test
public void pingSAPDestination() throws JCoException
{
JCoDestination dest = FileDestinationDataProvider.getDestination();
dest.ping();
}
}

DestinationDataProvider另一种实现

记得nco3.0可以将登陆参数写在代码中吗,如果我们也想将连接参数直接写在代码中,怎么做呢?刚才说过了,关键就是实现DestinationDataProvider接口,并改写getDestinationProperties()方法。不多说,上代码。

第一部分:DestinationDataProvider接口实现

package jco3.demo3;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import com.sap.conn.jco.ext.DestinationDataEventListener;
import com.sap.conn.jco.ext.DestinationDataProvider; public class DestinationDataProviderImp implements DestinationDataProvider
{
/**
* DestinationDataProvider is an interface
* We define DestinationDataProviderImp class to implements this interface
* so that we can define the logon parameters more flexibly
* not just in xxx.jcodestionation file.
*
* The key point is that we override getDestinationProperties() method
* Afterwards, instance of DestinationDataProvider should be registered
* using Environment.registerDestinationDataProvider() method to take effect
*/ @SuppressWarnings("rawtypes")
private Map provider = new HashMap(); @SuppressWarnings("unchecked")
public void addDestinationProperties(String destName, Properties props)
{
provider.put(destName, props);
} @Override
public Properties getDestinationProperties(String destName)
{
if (destName == null){
throw new NullPointerException("Destinantion name is empty.");
} if (provider.size() == 0){
throw new IllegalStateException("Data provider is empty.");
} return (Properties) provider.get(destName);
} @Override
public void setDestinationDataEventListener(DestinationDataEventListener listener)
{
throw new UnsupportedOperationException();
} @Override
public boolean supportsEvents()
{
return false;
}
}

第二部分:创建DestinationProivder类,提供getDestination()方法,注册DestinationDataProviderImp类的实例:

package jco3.demo3;

import java.util.Properties;
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;
import com.sap.conn.jco.ext.Environment; public class DestinationProvider
{
private static Properties setProperties()
{
// logon parameters and other properties
Properties connProps = new Properties();
connProps.setProperty(DestinationDataProvider.JCO_ASHOST, "192.168.65.100");
connProps.setProperty(DestinationDataProvider.JCO_SYSNR, "00");
connProps.setProperty(DestinationDataProvider.JCO_USER, "STONE");
connProps.setProperty(DestinationDataProvider.JCO_PASSWD, "xxxxxx");
connProps.setProperty(DestinationDataProvider.JCO_CLIENT, "001");
connProps.setProperty(DestinationDataProvider.JCO_LANG, "EN"); return connProps;
} public static JCoDestination getDestination() throws JCoException
{
String destName = "SAP_AS"; Properties props = setProperties();
DestinationDataProviderImp destDataProvider = new DestinationDataProviderImp();
destDataProvider.addDestinationProperties(destName, props);
Environment.registerDestinationDataProvider(destDataProvider); JCoDestination dest = JCoDestinationManager.getDestination(destName);
return dest;
}
}

第三部分:测试DestinationProvidergetDestination()方法:

package jco3.demo3;

import org.junit.Test;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoException; public class TestDestionProvider
{
@Test
public void pingSAPDestination() throws JCoException
{
JCoDestination dest = DestinationProvider.getDestination();
dest.ping();
}
}

SAP接口编程 之 JCo3.0系列(01):JCoDestination的更多相关文章

  1. SAP接口编程 之 JCo3.0系列(02) : JCo Client Programming

    SAP接口编程 之 JCo3.0系列(02) : JCo Client Programming 字数545 阅读52 评论0 喜欢1 JCo3.0调用SAP函数的过程 大致可以总结为以下步骤: 连接至 ...

  2. SAP接口编程 之 JCo3.0系列(04) : 会话管理

    在SAP接口编程之 NCo3.0系列(06) : 会话管理 这篇文章中,对会话管理的相关知识点已经说得很详细了,请参考.现在用JCo3.0来实现. 1. JCoContext 如果SAP中多个函数需要 ...

  3. SAP接口编程 之 JCo3.0系列(03) : Table参数

    Table参数作为export parameter BAPI_COMPANYCODE_GETDETAIL是一个适合演示的函数,没有import paramter参数,调用后COMPANYCODE_GE ...

  4. SAP接口编程 之 JCo3.0系列(05) : Exception Handling

    JCO3.0的Exception,常用的Exception如下: JCoException 继承自java.lang.Exception,是JCo3中Exception的基类. JCoRuntimeE ...

  5. LXD 2.0 系列(四):资源控制

    LXD 提供了各种资源限制.其中一些与容器本身相关,如内存配额.CPU 限制和 I/O 优先级.而另外一些则与特定设备相关,如 I/O 带宽或磁盘用量限制.-- Stéphane Graber 本文导 ...

  6. java io系列01之 "目录"

    java io 系列目录如下: 01. java io系列01之  "目录" 02. java io系列02之 ByteArrayInputStream的简介,源码分析和示例(包括 ...

  7. Java 集合系列 01 总体框架

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  8. Java 之 I/O 系列 01 ——基础

    Java 之 I/O 系列 目录 Java 之 I/O 系列 01 ——基础 Java 之 I/O 系列 02 ——序列化(一) Java 之 I/O 系列 02 ——序列化(二) 整理<疯狂j ...

  9. Spring Boot 2.0系列文章(七):SpringApplication 深入探索

    关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...

随机推荐

  1. Linux下通过crontab及expect实现自动化处理

    版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 目标 为实现每天定时从其他服务器上复制文件到本地,需要使用crontab建立定时任务,并通过scp进行Linux之间的文件复制. ...

  2. JavaEE基础(六)

    1.面向对象(面向对象思想概述) A:面向过程思想概述 第一步 第二步 B:面向对象思想概述 找对象(第一步,第二步) C:举例 买煎饼果子 洗衣服 D:面向对象思想特点 a:是一种更符合我们思想习惯 ...

  3. android基础小结

    (注:此小结文档在全屏模式下观看效果最佳) 2016年3月1日,正式开始了我的android学习之路. 最最开始的,当然是学习怎样搭载环境了,然而苦逼的我在win10各种坑爹的指引下还是安装了一个星期 ...

  4. 【转】表删除时 Cannot delete or update a parent row: a foreign key constraint fails 异常处理

    转载地址:http://lijiejava.iteye.com/blog/790478 有两张表,结构如下: t_item:                          t_bid: id    ...

  5. ZOJ 3861 - Valid Pattern Lock

    3861 - Valid Pattern Lock Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & ...

  6. 手把手教你用C++ 写ACM自动刷题神器(冲入HDU首页)

    转载注明原地址:http://blog.csdn.net/nk_test/article/details/49497017 少年,作为苦练ACM,通宵刷题的你 是不是想着有一天能够荣登各大OJ榜首,俯 ...

  7. 表单校验组件ValidForm

    10.1使用入门 1.引入css 请查看下载文件中的style.css,把里面Validform必须部分复制到你的css中 (文件里这个注释 "/*==========以下部分是Validf ...

  8. Unity帧序列实时渲染脚本

    该脚本会创建一个新相机进行录制,支持包含所有相机内容,完美解决跳帧问题,可自定义分辨率等参数,脚本会输出品质为100的jpg序列,基本无损. 但缺点是帧率始终是每秒100帧,必须压制时限制帧数. 而用 ...

  9. ubuntu /etc/profile和/etc/environment的比较

    先将export LANG=zh_CN加入/etc/profile ,退出系统重新登录,登录提示显示英文. 将/etc/profile 中的export LANG=zh_CN删除,将LNAG=zh_C ...

  10. Hadoop Mac OSX 安装笔记

    本次测试安装的机器为Mac Book Pro, 系统为 OS X 10.9.4.Hadoop版本2.4.1. 使用Java版本为Oracle的JDK 1.6.0_65. 1. 下载安装 Hadoop2 ...