要让JAVA程序能访问SAP系统,一般通过SAP JCO接口进行通讯,在获取到SAP的连接时需求提供一些连接参数,这些参数在最新的 JCO 3.0 中需要被保存到一个带有扩展名.jcoDestination的文件中,这个文件同时被保存在应用程序的安装目录中。因为这只中一个纯文本文件,所有的连接参数并没有被加密,这样对于公用程序可能有安全问题。要使用登陆连接更加安全可以实现自定义的 DestinationDataProvider 实现:
此接口只有简单的三个方法:
interface DestinationDataProvider {Properties getDestinationProperties(java.lang.String destinationName);void setDestinationDataEventListener(DestinationDataEventListener eventListener);boolean supportsEvents(); }
getDestinationProperties 当Java程序获取到SAP的连接时,jco会从这里读取连接属性,你可以编程动态的设定这些属性
setDestinationDataEventListener 设置一个连接事件监听器,实现一个监听器,当JCO连接SAP以获得通知
supportsEvents 返回是否被实现的DestinationDataProvider有事件监听器
实现一个自定义Provider:
static class MyDestinationDataProvider implements DestinationDataProvider{private DestinationDataEventListener eL;private Properties ABAP_AS_properties; public Properties getDestinationProperties(String destinationName){if(destinationName.equals("ABAP_AS") && ABAP_AS_properties!=null)return ABAP_AS_properties;return null;//alternatively throw runtime exception//throw new RuntimeException("Destination " + destinationName + " is not available"); }public void setDestinationDataEventListener(DestinationDataEventListener eventListener){this.eL = eventListener;}public boolean supportsEvents(){return true;}void changePropertiesForABAP_AS(Properties properties){if(properties==null){eL.deleted("ABAP_AS");ABAP_AS_properties = null;}else {if(ABAP_AS_properties!=null && !ABAP_AS_properties.equals(properties))eL.updated("ABAP_AS");ABAP_AS_properties = properties;}}}
测试连接:
public static void main(String[] args) throws Exception{Properties connectProperties = new Properties();connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "binmain");connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "53");connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "000");connectProperties.setProperty(DestinationDataProvider.JCO_USER, "JCOTEST");connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "JCOTEST");connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "en");MyDestinationDataProvider myProvider = new MyDestinationDataProvider();com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider);myProvider.changePropertiesForABAP_AS(connectProperties);JCoDestination ABAP_AS = JCoDestinationManager.getDestination("ABAP_AS");ABAP_AS.ping();System.out.println("ABAP_AS destination is ok");}