要讓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");}
?