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

How to use JCo connection without creating *.JcoDestination file

我正在尝试使用 JCo 连接到 SAP ECC 6.0。我正在关注 this 教程。但是,有一个注释说:

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.

这是合理且可以理解的。但是,没有说明如何设置安全目标提供程序。 我在这个 thread 中找到了解决方案,它创建了 DestinationDataProvider 的自定义实现并且可以在我的本地机器上运行。但是当我在 Portal 上部署它时,我收到一条错误消息,指出已经注册 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);
    }

好吧,所以我准备好了,我想我会分享我的研究。 您需要在 Portal 中添加自己的目的地。为此,您需要转到 NetWeaver Administrator,位于:host:port/nwa。所以它将类似于 sapportal.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);

顺便说一句。您可能还想查看 http://hibersap.org/,因为它们也提供了存储配置的好方法。