JCO 自定義DestinationDataProvider
要讓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:
public class CustomDestinationDataProvider implements DestinationDataProvider {
private Map providers = new HashMap();
public Properties getDestinationProperties(String destName) {
if (destName == null)
throw new NullPointerException("請指定目的名稱");
if (providers.size() == 0)
throw new IllegalStateException("請加入一個目的連線引數屬性給提供者");
return providers.get(destName);
}
// 沒有實現事件處理
@Override
public boolean supportEvents() {
return false;
}
@Override
public void setDestinationDataEventListener(DestinationDataEventListener listener) {
throw new UnsupportedOperationException();
}
public void addDestinationProperties(String destName, Properties provider) {
providers.put(destName, provider);
}
}
測試連線:
public class TestCustomDestinationDataProvider {
public static void main(String[] args) {
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "hostname");
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00");
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "400");
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "username");
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "password");
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "en");
CustomDestinationDataProvider provider = new CustomDestinationDataProvider();
provider.addDestinationProperties("SAP_CONN", connectProperties);
Environment.registerDestinationDataProvider(provider);
try {
JCoDestination dest = JCoDestinationManager.getDestination("SAP_CONN");
dest.ping();
System.out.println("連線成功");
} catch (JCoException ex) {
System.out.println(ex);
System.out.println("連線失敗");
}
}
}
此介面只有簡單的三個方法:
interface DestinationDataProvider {
Properties getDestinationProperties(java.lang.String destinationName);
void setDestinationDataEventListener(DestinationDataEventListener eventListener);
boolean supportsEvents();
}
getDestinationProperties 當Java程式獲取到SAP的連線時,jco會從這裡讀取連線屬性,你可以程式設計動態的設定這些屬性
setDestinationDataEventListener 設定一個連線事件監聽器,實現一個監聽器,當JCO連線SAP以獲得通知
supportsEvents 返回是否被實現的DestinationDataProvider有事件監聽器
實現一個自定義Provider:
public class CustomDestinationDataProvider implements DestinationDataProvider {
private Map
public Properties getDestinationProperties(String destName) {
if (destName == null)
throw new NullPointerException("請指定目的名稱");
if (providers.size() == 0)
throw new IllegalStateException("請加入一個目的連線引數屬性給提供者");
return providers.get(destName);
}
// 沒有實現事件處理
@Override
public boolean supportEvents() {
return false;
}
@Override
public void setDestinationDataEventListener(DestinationDataEventListener listener) {
throw new UnsupportedOperationException();
}
public void addDestinationProperties(String destName, Properties provider) {
providers.put(destName, provider);
}
}
測試連線:
public class TestCustomDestinationDataProvider {
public static void main(String[] args) {
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "hostname");
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00");
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "400");
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "username");
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "password");
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "en");
CustomDestinationDataProvider provider = new CustomDestinationDataProvider();
provider.addDestinationProperties("SAP_CONN", connectProperties);
Environment.registerDestinationDataProvider(provider);
try {
JCoDestination dest = JCoDestinationManager.getDestination("SAP_CONN");
dest.ping();
System.out.println("連線成功");
} catch (JCoException ex) {
System.out.println(ex);
System.out.println("連線失敗");
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29917/viewspace-619496/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- JCO:為了生存運動!
- android自定義view(自定義數字鍵盤)AndroidView
- netty自定義Decoder用於自定義協議Netty協議
- 自定義VIEWView
- 自定義SnackBar
- 自定義_ajax
- 自定義 Anaconda
- 自定義ClassLoader
- 自定義QTimeEditQT
- 自定義 tabBartabBar
- tailwind自定義AI
- 自定義 GitGit
- 自定義元件元件
- 自定義表格
- 自定義LinkedList
- 自定義useState
- vue自定義全域性元件(或自定義外掛)Vue元件
- SAP Java Connector 錯誤 - JCO_ERROR_COMMUNICATIONJavaError
- 4. 自定義控制元件(4) --- 自定義屬性控制元件
- Android 自定義View:深入理解自定義屬性(七)AndroidView
- MySQL自定義排序MySql排序
- JavaScript自定義事件JavaScript事件
- Maven 自定義archeTypeMaven
- SpringBoot 自定義 starterSpring Boot
- 自定義的PopupWindow
- 自定義元件——TitleView元件View
- 自定義目錄
- 自定義 Artisan 命令
- 自定義型別型別
- Gradle 自定義 pluginGradlePlugin
- 自定義Spring ShellSpring
- 自定義JSON名JSON
- phpstudy自定義版本PHP
- Swift 自定義 UIDatePickerSwiftUI
- 自定義ORM框架ORM框架
- Python自定義排序Python排序
- 自定義 Word 模板
- 自定義圓環
- SpringBoot自定義StarterSpring Boot