Java 如何在不创建*.JcoDestination文件的情况下使用JCo连接

Java 如何在不创建*.JcoDestination文件的情况下使用JCo连接,java,jakarta-ee,sap-erp,jco,bapi,Java,Jakarta Ee,Sap Erp,Jco,Bapi,我正在尝试使用JCo连接到SAP ECC 6.0。我正在学习教程。然而,有一张便条说: For this example the destination configuration is stored in a file that is called by the program. In practice you should avoid this for security reasons. 这是合理的,也是可以理解的。但是,没有解释如何设置安全的目标提供程序。 我在这里找到了一个解决方案,它

我正在尝试使用JCo连接到SAP ECC 6.0。我正在学习教程。然而,有一张便条说:

For this example the destination configuration is stored in a file that is called by the program. In practice you should avoid this for security reasons.
这是合理的,也是可以理解的。但是,没有解释如何设置安全的目标提供程序。 我在这里找到了一个解决方案,它创建了
DestinationDataProvider
的自定义实现,并在本地机器上运行。但是,当我在门户上部署它时,我得到一个错误,说已经有注册的
DestinationDataProvider
。 所以我的问题是: 如何在SAP Java EE应用程序中存储目标数据?

下面是我的代码,以进一步说明我正在尝试做什么

public static void main(String... args) throws JCoException {
        CustomDestinationProviderMap provider = new CustomDestinationProviderMap();
        com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(provider);

        Properties connectProperties = new Properties();
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "host.sap.my.domain.com");
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00");
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "100");
        connectProperties.setProperty(DestinationDataProvider.JCO_USER, "user");
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "password");
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "en");

        provider.addDestination(DESTINATION_NAME1, connectProperties);
        connect();
    }

    public static void connect() throws JCoException {
        String FUNCTION_NAME = "BAPI_EMPLOYEE_GETDATA";
        JCoDestination destination = JCoDestinationManager.getDestination(DESTINATION_NAME1);
        JCoContext.begin(destination);

        JCoFunction function = destination.getRepository().getFunction(FUNCTION_NAME);
        if (function == null) {
            throw new RuntimeException(FUNCTION_NAME + " not found in SAP.");
        }

        //function.getImportParameterList().setValue("EMPLOYEE_ID", "48");
        function.getImportParameterList().setValue("FSTNAME_M", "ANAKIN");
        function.getImportParameterList().setValue("LASTNAME_M", "SKYWALKER");

        try {
            function.execute(destination);
        } catch (AbapException e) {
            System.out.println(e.toString());
            return;
        }

        JCoTable table = function.getTableParameterList().getTable("PERSONAL_DATA");
        for (int i = 0; i < table.getNumRows(); i++) {
            table.setRow(i);
            System.out.println(table.getString("PERNO") + '\t' + table.getString("FIRSTNAME") + '\t' + table.getString("LAST_NAME")
            +'\t' + table.getString("BIRTHDATE")+'\t' + table.getString("GENDER"));
        }

        JCoContext.end(destination);
    }
publicstaticvoidmain(String…args)抛出JCoException{
CustomDestinationProviderMap provider=新的CustomDestinationProviderMap();
com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(提供者);
Properties connectProperties=新属性();
setProperties(DestinationDataProvider.JCO_ASHOST,“host.sap.my.domain.com”);
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,“00”);
connectProperties.setProperty(DestinationDataProvider.JCO_客户端,“100”);
setProperties(DestinationDataProvider.JCO_USER,“用户”);
setProperty(DestinationDataProvider.JCO_PASSWD,“密码”);
setProperty(DestinationDataProvider.JCO_LANG,“en”);
addDestination(DESTINATION_NAME1,connectProperties);
connect();
}
公共静态void connect()引发JCoException{
字符串函数\u NAME=“BAPI\u EMPLOYEE\u GETDATA”;
JCoDestination destination=JCoDestinationManager.getDestination(destination_NAME1);
JCoContext.begin(目的地);
JCoFunction function=destination.getRepository().getFunction(函数名称);
if(函数==null){
抛出新的运行时异常(函数_NAME+“在SAP中找不到”);
}
//function.getImportParameterList().setValue(“员工ID”,“48”);
function.getImportParameterList().setValue(“FSTNAME_M”,“ANAKIN”);
function.getImportParameterList().setValue(“LASTNAME_M”,“天行者”);
试一试{
执行(目的地);
}捕获(AbapException e){
System.out.println(例如toString());
返回;
}
JCoTable table=function.getTableParameterList().getTable(“个人数据”);
对于(int i=0;i
好的,我准备好了,想和大家分享一下我的研究成果。 您需要在门户中添加自己的目的地。要实现这一点,您需要转到NetWeaver管理员,位于:
host:port/nwa
。所以它将类似于
saportal.your.domain.com:50000/nwa

然后转到
Configuration
->
Infrastructure
->
Destinations
并将目标添加到那里。您可以将大多数字段(如消息服务器)留空。重要的部分是目的地名称,因为在我的例子中,它是如何检索它以及应该设置为RFC目的地的目的地类型。尝试ping新创建的目的地以检查其是否正常运行


最后,您应该能够通过简单地调用来获取目的地:
JCoDestination destination=JCoDestinationManager.getDestination(destination_NAME)将其添加到门户环境并从那里进行管理

看看JCo连接器下载的JCo示例中的CustomDestinationDataProvider。重要的部分是:

static class MyDestinationDataProvider implements DestinationDataProvider
...
com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(new MyDestinationDataProvider());
然后你可以简单地做:

instance = JCoDestinationManager.getDestination(DESTINATION_NAME);
顺便说一句,您可能还想签出,因为它们提供了很好的存储配置的方法。

另请参阅